From 3dcf26f75069cfbe091d9634114219db00cb87ae Mon Sep 17 00:00:00 2001 From: chriskiehl Date: Sat, 7 Nov 2015 13:25:07 -0500 Subject: [PATCH] Issue 101: allow image customization --- gooey/gui/application.py | 6 +-- gooey/gui/image_repository.py | 51 ++++++++++++++---- gooey/gui/lang/i18n.py | 27 +++------- ...success_checkmark.png => success_icon.png} | Bin gooey/python_bindings/config_generator.py | 5 +- gooey/python_bindings/gooey_decorator.py | 6 ++- 6 files changed, 59 insertions(+), 36 deletions(-) rename gooey/images/{success_checkmark.png => success_icon.png} (100%) diff --git a/gooey/gui/application.py b/gooey/gui/application.py index 4508d88..209599e 100644 --- a/gooey/gui/application.py +++ b/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() diff --git a/gooey/gui/image_repository.py b/gooey/gui/image_repository.py index 56278ce..478e763 100644 --- a/gooey/gui/image_repository.py +++ b/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") diff --git a/gooey/gui/lang/i18n.py b/gooey/gui/lang/i18n.py index 47ad921..20a15db 100644 --- a/gooey/gui/lang/i18n.py +++ b/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) diff --git a/gooey/images/success_checkmark.png b/gooey/images/success_icon.png similarity index 100% rename from gooey/images/success_checkmark.png rename to gooey/images/success_icon.png diff --git a/gooey/python_bindings/config_generator.py b/gooey/python_bindings/config_generator.py index fc61514..dde6e67 100644 --- a/gooey/python_bindings/config_generator.py +++ b/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: diff --git a/gooey/python_bindings/gooey_decorator.py b/gooey/python_bindings/gooey_decorator.py index 122d668..11e91b1 100644 --- a/gooey/python_bindings/gooey_decorator.py +++ b/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