From f6aee1463b55b7c621894b2299fcb368d1214d04 Mon Sep 17 00:00:00 2001 From: Alexander Gordeyev Date: Fri, 23 Oct 2015 15:51:45 +0300 Subject: [PATCH] add cx_freeze support --- gooey/gui/image_repository.py | 4 ++-- gooey/gui/lang/i18n.py | 13 ++++--------- gooey/gui/util/freeze.py | 20 ++++++++++++++++++++ gooey/gui/util/quoting.py | 10 +++++++++- gooey/python_bindings/config_generator.py | 11 +++++------ 5 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 gooey/gui/util/freeze.py diff --git a/gooey/gui/image_repository.py b/gooey/gui/image_repository.py index e52603b..0ca0e29 100644 --- a/gooey/gui/image_repository.py +++ b/gooey/gui/image_repository.py @@ -1,10 +1,10 @@ import os +from gooey.gui.util.freeze import get_resource_path __author__ = 'Chris' -base_path = os.path.dirname(__file__) -image_dir = os.path.join(base_path, '../images') +image_dir = get_resource_path('images') alessandro_rei_checkmark = os.path.join(image_dir, "alessandro_rei_checkmark.png") computer = os.path.join(image_dir, "computer.png") diff --git a/gooey/gui/lang/i18n.py b/gooey/gui/lang/i18n.py index e977499..47ad921 100644 --- a/gooey/gui/lang/i18n.py +++ b/gooey/gui/lang/i18n.py @@ -11,12 +11,13 @@ 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 = os.path.join(os.path.dirname(__file__), '../../languages') +_DEFAULT_DIR = get_resource_path("languages") _DICTIONARY = None @@ -41,13 +42,7 @@ def load(filename): 'translation file is in the languages directory, ') def translate(key): - return _DICTIONARY[key] + return _DICTIONARY.get(key, key) def _(key): - return _DICTIONARY[key] - -if __name__ == '__main__': - pass - - - + return translate(key) diff --git a/gooey/gui/util/freeze.py b/gooey/gui/util/freeze.py new file mode 100644 index 0000000..e1c5303 --- /dev/null +++ b/gooey/gui/util/freeze.py @@ -0,0 +1,20 @@ +import sys +import os + + +def is_frozen(): + return getattr(sys, 'frozen', False) + + +def get_resource_path(*args): + if is_frozen(): + resource_dir = os.path.join(os.path.dirname(sys.executable), 'gooey') + if not os.path.isdir(resource_dir): + raise IOError(("cannot locate Gooey resources. It seems that the program " + "was frozen, but resource files were not copied to " + "directory of the executable file. Please copy " + "`languages` and `images` folders from gooey module " + "directory into `{}{}` directory.".format(resource_dir, os.sep))) + else: + resource_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', '..')) + return os.path.join(resource_dir, *args) diff --git a/gooey/gui/util/quoting.py b/gooey/gui/util/quoting.py index b1b59bf..f955f8f 100644 --- a/gooey/gui/util/quoting.py +++ b/gooey/gui/util/quoting.py @@ -1,4 +1,12 @@ -import re +import sys + + +if sys.platform.startswith("win"): + def quote(value): + return '"{}"'.format('{}'.format(value).replace('"', '""')) +else: # POSIX shell + def quote(value): + return "'{}'".format('{}'.format(value).replace("'", "'\\''")) def maybe_quote(string): diff --git a/gooey/python_bindings/config_generator.py b/gooey/python_bindings/config_generator.py index 18bf089..fc61514 100644 --- a/gooey/python_bindings/config_generator.py +++ b/gooey/python_bindings/config_generator.py @@ -1,18 +1,17 @@ import os import sys -import argparse_to_json from gooey.gui.windows import layouts -from gooey.python_bindings import source_parser +from gooey.python_bindings import argparse_to_json +from gooey.gui.util.quoting import quote def create_from_parser(parser, source_path, **kwargs): show_config = kwargs.get('show_config', False) - #If script has been frozen execute it straight - if hasattr(sys, 'frozen'): - run_cmd = source_path + if source_path.endswith(".py"): + run_cmd = '{} {}'.format(quote(sys.executable), quote(source_path)) else: - run_cmd = 'python -u {}'.format(source_path) + run_cmd = quote(source_path) build_spec = { 'language': kwargs.get('language', 'english'),