From dc6386c3a9dfc953e45b31272365229349b2ffd8 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 7 May 2017 21:58:49 +0200 Subject: [PATCH] Added python 3 compatibility --- gooey/gui/lang/i18n.py | 3 ++- gooey/gui/model.py | 10 +++++----- gooey/gui/presenter.py | 2 +- gooey/gui/processor.py | 14 ++++++++++---- gooey/gui/widgets/components.py | 8 ++++---- gooey/gui/widgets/widget_pack.py | 2 +- gooey/gui/windows/advanced_config.py | 7 +++++-- gooey/gui/windows/base_window.py | 2 ++ gooey/python_bindings/argparse_to_json.py | 14 +++++++------- gooey/python_bindings/gooey_decorator.py | 4 ++-- gooey/tests/test_argparse_to_json.py | 10 +++++----- 11 files changed, 44 insertions(+), 32 deletions(-) diff --git a/gooey/gui/lang/i18n.py b/gooey/gui/lang/i18n.py index 20a15db..d891a88 100644 --- a/gooey/gui/lang/i18n.py +++ b/gooey/gui/lang/i18n.py @@ -7,6 +7,7 @@ Provides Internationalization for all text within the program. ''' +import io import os import json @@ -19,7 +20,7 @@ def load(language_dir, filename): global _DICTIONARY try: json_file = filename + '.json' - with open(os.path.join(language_dir, json_file), 'rb') as f: + with io.open(os.path.join(language_dir, json_file), 'r', encoding='utf-8') as f: _DICTIONARY = json.load(f) except IOError: raise IOError('{0} Language file not found at location {1}. ' diff --git a/gooey/gui/model.py b/gooey/gui/model.py index 5b99ff1..5762777 100644 --- a/gooey/gui/model.py +++ b/gooey/gui/model.py @@ -158,7 +158,7 @@ class MyModel(object): self.stop_button_disabled = self.build_spec['disable_stop_button'] self.argument_groups = self.wrap(self.build_spec.get('widgets', {})) - self.active_group = iter(self.argument_groups).next() + self.active_group = next(iter(self.argument_groups)) self.num_required_cols = self.build_spec['num_required_cols'] self.num_optional_cols = self.build_spec['num_optional_cols'] @@ -207,7 +207,7 @@ class MyModel(object): return self.build_spec['manual_start'] def is_required_section_complete(self): - completed_values = filter(None, [arg.value for arg in self.required_args]) + completed_values = list(filter(None, [arg.value for arg in self.required_args])) return len(self.required_args) == len(completed_values) def build_command_line_string(self): @@ -216,7 +216,7 @@ class MyModel(object): position_args = [c.value for c in self.required_args if not c.commands] if position_args: position_args.insert(0, "--") - cmd_string = ' '.join(filter(None, chain(required_args, optional_args, position_args))) + cmd_string = ' '.join(list(filter(None, chain(required_args, optional_args, position_args)))) if self.layout_type == 'column': cmd_string = u'{} {}'.format(self.argument_groups[self.active_group].command, cmd_string) return u'{} --ignore-gooey {}'.format(self.build_spec['target'], cmd_string) @@ -228,11 +228,11 @@ class MyModel(object): required_args, optional_args = self.partition(widget_list, is_required) if self.build_spec['group_by_type']: optional_args = chain(*self.partition(optional_args, not_checkbox)) - return map(self.to_object, required_args), map(self.to_object, optional_args) + return list(map(self.to_object, required_args)), list(map(self.to_object, optional_args)) @staticmethod def partition(collection, condition): - return filter(condition, collection), filter(lambda x: not condition(x), collection) + return list(filter(condition, collection)), list(filter(lambda x: not condition(x), collection)) def to_object(self, data): details = data['data'] diff --git a/gooey/gui/presenter.py b/gooey/gui/presenter.py index dee3f39..1bf3161 100644 --- a/gooey/gui/presenter.py +++ b/gooey/gui/presenter.py @@ -58,7 +58,7 @@ class Presenter(object): self.view.enable_stop_button() if self.model.layout_type == layouts.COLUMN: - self.view.set_list_contents(self.model.argument_groups.keys()) + self.view.set_list_contents(list(self.model.argument_groups.keys())) if self.model.auto_start: self.model.update_state(States.RUNNNING) diff --git a/gooey/gui/processor.py b/gooey/gui/processor.py index 8513a78..d2bcdb6 100644 --- a/gooey/gui/processor.py +++ b/gooey/gui/processor.py @@ -38,10 +38,16 @@ class ProcessController(object): def run(self, command): env = os.environ.copy() env["GOOEY"] = "1" - self._process = subprocess.Popen( - command.encode(sys.getfilesystemencoding()), - bufsize=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE, - stderr=subprocess.STDOUT, shell=True, env=env) + try: + self._process = subprocess.Popen( + command.encode(sys.getfilesystemencoding()), + bufsize=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE, + stderr=subprocess.STDOUT, shell=True, env=env) + except: + self._process = subprocess.Popen( + command, + bufsize=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE, + stderr=subprocess.STDOUT, shell=True, env=env) Pool(1).apply_async(self._forward_stdout, (self._process,)) def _forward_stdout(self, process): diff --git a/gooey/gui/widgets/components.py b/gooey/gui/widgets/components.py index 31de615..251735b 100644 --- a/gooey/gui/widgets/components.py +++ b/gooey/gui/widgets/components.py @@ -54,7 +54,7 @@ class BaseGuiComponent(object): return self.panel def bind(self, *args, **kwargs): - print self.widget_pack.widget.Bind(*args, **kwargs) + print(self.widget_pack.widget.Bind(*args, **kwargs)) def get_title(self): return self.title.GetLabel() @@ -104,7 +104,7 @@ class BaseGuiComponent(object): def set_value(self, val): if val: - self.widget_pack.widget.SetValue(unicode(val)) + self.widget_pack.widget.SetValue(str(val)) def __repr__(self): return self.__class__.__name__ @@ -196,9 +196,9 @@ class RadioGroup(object): return self.panel def showz(self, evt): - print evt + print(evt) for i in self.radio_buttons: - print i.GetValue() + print(i.GetValue()) def onResize(self, evt): msg = self.help_msgs[0] diff --git a/gooey/gui/widgets/widget_pack.py b/gooey/gui/widgets/widget_pack.py index 3fff3bb..bf5fc94 100644 --- a/gooey/gui/widgets/widget_pack.py +++ b/gooey/gui/widgets/widget_pack.py @@ -184,7 +184,7 @@ class CounterPayload(WidgetPack): parent=parent, id=-1, value='', - choices=map(str, range(1, 11)), + choices=list(map(str, range(1, 11))), style=wx.CB_DROPDOWN ) return self.widget diff --git a/gooey/gui/windows/advanced_config.py b/gooey/gui/windows/advanced_config.py index 8d38884..ae74a2b 100644 --- a/gooey/gui/windows/advanced_config.py +++ b/gooey/gui/windows/advanced_config.py @@ -7,7 +7,10 @@ Managed the internal layout for configuration options import wx from wx.lib.scrolledpanel import ScrolledPanel -from itertools import chain, izip_longest +try: + from itertools import zip_longest +except ImportError: + from itertools import izip_longest as zip_longest from gooey.gui.util import wx_util from gooey.gui.lang import i18n @@ -69,7 +72,7 @@ class WidgetContainer(wx.Panel): def chunk(self, iterable, n, fillvalue=None): "itertools recipe: Collect data into fixed-length chunks or blocks" args = [iter(iterable)] * n - return izip_longest(fillvalue=fillvalue, *args) + return zip_longest(fillvalue=fillvalue, *args) def __iter__(self): return iter(self.widgets) diff --git a/gooey/gui/windows/base_window.py b/gooey/gui/windows/base_window.py index a20389f..377b205 100644 --- a/gooey/gui/windows/base_window.py +++ b/gooey/gui/windows/base_window.py @@ -191,6 +191,8 @@ class BaseWindow(wx.Frame): def UpdateProgressBar(self, value, disable_animation=False): pb = self.foot_panel.progress_bar + if value is None: + return if value < 0: pb.Pulse() else: diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index 8117947..878909d 100644 --- a/gooey/python_bindings/argparse_to_json.py +++ b/gooey/python_bindings/argparse_to_json.py @@ -64,7 +64,7 @@ def convert(parser): (choose_name(name, sub_parser), { 'command': name, 'contents': process(sub_parser, getattr(sub_parser, 'widgets', {})) - }) for name, sub_parser in get_subparser(actions).choices.iteritems()) + }) for name, sub_parser in get_subparser(actions).choices.items()) else: layout_type = 'standard' @@ -97,7 +97,7 @@ def process(parser, widget_dict): return list(categorize(required_actions, widget_dict, required=True)) + \ list(categorize(optional_actions, widget_dict)) + \ - map(build_radio_group, mutually_exclusive_groups) + list(map(build_radio_group, mutually_exclusive_groups)) def categorize(actions, widget_dict, required=False): _get_widget = partial(get_widget, widgets=widget_dict) @@ -111,7 +111,7 @@ def categorize(actions, widget_dict, required=False): elif is_counter(action): _json = as_json(action, _get_widget(action) or 'Counter', required) # pre-fill the 'counter' dropdown - _json['data']['choices'] = map(str, range(1, 11)) + _json['data']['choices'] = list(map(str, range(1, 11))) yield _json else: raise UnknownWidgetType(action) @@ -129,16 +129,16 @@ def is_required(action): return not isinstance(action, _SubParsersAction) and (action.required == True and action.nargs not in ['*', '?']) def has_required(actions): - return filter(None, filter(is_required, actions)) + return list(filter(None, list(filter(is_required, actions)))) def is_subparser(action): return isinstance(action,_SubParsersAction) def has_subparsers(actions): - return filter(is_subparser, actions) + return list(filter(is_subparser, actions)) def get_subparser(actions): - return filter(is_subparser, actions)[0] + return list(filter(is_subparser, actions))[0] def is_optional(action): ''' @@ -167,7 +167,7 @@ def is_standard(action): def is_flag(action): """ _actions which are either storeconst, store_bool, etc.. """ action_types = [_StoreTrueAction, _StoreFalseAction, _StoreConstAction] - return any(map(lambda Action: isinstance(action, Action), action_types)) + return any(list(map(lambda Action: isinstance(action, Action), action_types))) def is_counter(action): """ _actions which are of type _CountAction """ diff --git a/gooey/python_bindings/gooey_decorator.py b/gooey/python_bindings/gooey_decorator.py index 754fd5d..c6a04fe 100644 --- a/gooey/python_bindings/gooey_decorator.py +++ b/gooey/python_bindings/gooey_decorator.py @@ -52,7 +52,7 @@ def Gooey(f=None, if load_build_config: try: build_spec = json.load(open(load_build_config, "r")) - except Exception, e: + except Exception as e: print( 'Exception loading Build Config from {0}: {1}'.format(load_build_config, e)) sys.exit(1) @@ -61,7 +61,7 @@ def Gooey(f=None, if dump_build_config: config_path = os.path.join(os.getcwd(), 'gooey_config.json') - print 'Writing Build Config to: {}'.format(config_path) + print('Writing Build Config to: {}'.format(config_path)) with open(config_path, 'w') as f: f.write(json.dumps(build_spec, indent=2)) application.run(build_spec) diff --git a/gooey/tests/test_argparse_to_json.py b/gooey/tests/test_argparse_to_json.py index 41eb04e..69af826 100644 --- a/gooey/tests/test_argparse_to_json.py +++ b/gooey/tests/test_argparse_to_json.py @@ -27,7 +27,7 @@ def test_grouping_structure(complete_parser): # should now be a dict rather than a list assert isinstance(groupings, dict) # make sure our expected root keys are there - for name, group in groupings.iteritems(): + for name, group in groupings.items(): assert 'command' in group assert 'contents' in group # contents should be the old list of widget info @@ -60,7 +60,7 @@ def test_convert_std_parser(complete_parser): result = convert(complete_parser) # grab the first entry from the dict entry = result['widgets']['primary']['contents'][0] - print entry + print(entry) assert 'type' in entry assert 'required' in entry assert 'data' in entry @@ -85,14 +85,14 @@ def test_has_subparsers(subparser, complete_parser): def test_is_required(complete_parser): - required = filter(is_required, complete_parser._actions) + required = list(filter(is_required, complete_parser._actions)) assert len(required) == 4 for action in required: - print action.dest.startswith('req') + print(action.dest.startswith('req')) def test_is_optional(complete_parser): - optional = filter(is_optional, complete_parser._actions) + optional = list(filter(is_optional, complete_parser._actions)) assert len(optional) == 10 for action in optional: assert 'req' not in action.dest