Browse Source

Issue 101: allow image customization

pull/123/merge
chriskiehl 9 years ago
parent
commit
3dcf26f750
6 changed files with 59 additions and 36 deletions
  1. 6
      gooey/gui/application.py
  2. 51
      gooey/gui/image_repository.py
  3. 27
      gooey/gui/lang/i18n.py
  4. 0
      gooey/images/success_icon.png
  5. 5
      gooey/python_bindings/config_generator.py
  6. 6
      gooey/python_bindings/gooey_decorator.py

6
gooey/gui/application.py

@ -15,7 +15,7 @@ from gooey.gui.windows.base_window import BaseWindow
from gooey.gui.windows.advanced_config import ConfigPanel
from gooey.python_bindings import config_generator, source_parser
from gooey.gui import image_repository
def main():
parser = argparse.ArgumentParser(
@ -71,8 +71,8 @@ def do_run(args):
def run(build_spec):
app = wx.App(False)
i18n.load(build_spec['language'])
i18n.load(build_spec['language_dir'], build_spec['language'])
image_repository.patch_images(build_spec['image_dir'])
frame = BaseWindow(build_spec)
frame.Show(True)
app.MainLoop()

51
gooey/gui/image_repository.py

@ -1,19 +1,52 @@
'''
Default Gooey icons.
Collection of the image paths.
Most icons provided by kidcomic.net
The module is meant to act as a singleton, hence the globals() abuse.
Image credit: kidcomic.net
'''
from functools import partial
import os
from gooey.gui.util.freeze import get_resource_path
image_dir = get_resource_path('images')
_image_details = (
('program_icon', 'program_icon.ico'),
('success_icon', 'success_icon.png'),
('running_icon', 'running_icon.png'),
('loading_icon', 'loading_icon.gif'),
('config_icon', 'config_icon.png'),
('error_icon', 'error_icon.png')
)
def init(image_dir):
''' initalize the images from the default directory path '''
defaults = {variable_name: os.path.join(image_dir, filename)
for variable_name, filename in _image_details}
globals().update(defaults)
def patch_images(new_image_dir):
'''
Loads custom images from the user supplied directory
'''
pathto = partial(os.path.join, new_image_dir)
if new_image_dir != 'default':
if not os.path.isdir(new_image_dir):
raise IOError('Unable to find the user supplied directory {}'.format(new_image_dir))
new_images = ((varname, pathto(filename))
for varname, filename in _image_details
if os.path.exists(pathto(filename)))
# push the changes into module scope
globals().update(new_images)
default_dir = get_resource_path('images')
init(default_dir)
program_icon = os.path.join(image_dir, "program_icon.ico")
success_icon = os.path.join(image_dir, "success_checkmark.png")
running_icon = os.path.join(image_dir, "running_icon.png")
loading_icon = os.path.join(image_dir, "loading_icon.gif")
config_icon = os.path.join(image_dir, "config_icon.png")
error_icon = os.path.join(image_dir, "error_icon.png")

27
gooey/gui/lang/i18n.py

@ -10,36 +10,21 @@ Provides Internationalization for all text within the program.
import os
import json
from gooey.gui.lang import i18n_config
from gooey.gui.util.freeze import get_resource_path
__all__ = ['translate']
_LANG = i18n_config.LANG
_DEFAULT_DIR = get_resource_path("languages")
__all__ = ['load', '_']
_DICTIONARY = None
def get_path(language):
''' Returns the full path to the language file '''
filename = language.lower() + '.json'
lang_file_path = os.path.join(_DEFAULT_DIR, filename)
if not os.path.exists(lang_file_path):
raise IOError('Could not find {} language file'.format(language))
return lang_file_path
def load(filename):
def load(language_dir, filename):
''' Open and return the supplied json file '''
global _DICTIONARY
try:
json_file = filename + '.json'
with open(os.path.join(_DEFAULT_DIR, json_file), 'rb') as f:
with open(os.path.join(language_dir, json_file), 'rb') as f:
_DICTIONARY = json.load(f)
except IOError:
raise IOError('Language file not found. Make sure that your ',
'translation file is in the languages directory, ')
raise IOError('{0} Language file not found at location {1}. '
'Make sure that your translation file is in the '
'listed language directory'.format(filename.title(), language_dir))
def translate(key):
return _DICTIONARY.get(key, key)

gooey/images/success_checkmark.png → gooey/images/success_icon.png

5
gooey/python_bindings/config_generator.py

@ -25,7 +25,10 @@ def create_from_parser(parser, source_path, **kwargs):
'num_optional_cols': kwargs.get('optional_cols', 3),
'manual_start': False,
'layout_type': 'column',
'monospace_display': kwargs.get('monospace_display', False)
'monospace_display': kwargs.get('monospace_display', False),
'image_dir': kwargs.get('image_dir'),
'language_dir': kwargs.get('language_dir'),
}
if show_config:

6
gooey/python_bindings/gooey_decorator.py

@ -18,7 +18,7 @@ import sys
from gooey.gui import application
from argparse import ArgumentParser
from gooey.gui.util.freeze import get_resource_path
IGNORE_COMMAND = '--ignore-gooey'
@ -33,7 +33,9 @@ def Gooey(f=None,
optional_cols=2,
dump_build_config=False,
load_build_config=None,
monospace_display=False):
monospace_display=False,
image_dir='default',
language_dir=get_resource_path('languages')):
'''
Decorator for client code's main function.
Serializes argparse data to JSON for use with the Gooey front end

Loading…
Cancel
Save