From 44ea7c774b4e7e95a13a02a350390fbd4196d5ea Mon Sep 17 00:00:00 2001 From: "andrzej.grymkowski" Date: Wed, 13 May 2015 13:05:45 +0200 Subject: [PATCH 1/5] updated kivy pep8 and corrected some files --- Makefile | 2 +- plyer/platforms/linux/filechooser.py | 53 ++++++++++++++++++++-------- plyer/platforms/win/filechooser.py | 2 +- plyer/tools/pep8checker/pep8.py | 29 +++++++++++++-- plyer/tools/pep8checker/pep8kivy.py | 7 ++-- 5 files changed, 73 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index e641d0dd9..ab7ab9dcc 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ html: $(MAKE) -C docs html style: - $(PYTHON) $(CHECKSCRIPT) $(PLYER_DIR) + $(PYTHON) $(CHECKSCRIPT) . stylereport: $(PYTHON) $(CHECKSCRIPT) -html $(PLYER_DIR) diff --git a/plyer/platforms/linux/filechooser.py b/plyer/platforms/linux/filechooser.py index 8b44ad384..093102a7e 100644 --- a/plyer/platforms/linux/filechooser.py +++ b/plyer/platforms/linux/filechooser.py @@ -50,7 +50,7 @@ def _run_command(self, cmd): self._process = sp.Popen(cmd, stdout=sp.PIPE) while True: ret = self._process.poll() - if ret != None: + if ret is not None: if ret == self.successretcode: out = self._process.communicate()[0].strip() self.selection = self._split_output(out) @@ -88,7 +88,8 @@ class ZenityFileChooser(SubprocessFileChooser): successretcode = 0 def _gen_cmdline(self): - cmdline = [which(self.executable), "--file-selection", "--confirm-overwrite"] + cmdline = [which(self.executable), + "--file-selection", "--confirm-overwrite"] if self.multiple: cmdline += ["--multiple"] if self.mode == "save": @@ -98,16 +99,20 @@ def _gen_cmdline(self): if self.path: cmdline += ["--filename", self.path] if self.title: - cmdline += ["--name", self.title] + cmdline += ["--name", self.title] if self.icon: cmdline += ["--window-icon", self.icon] for f in self.filters: if type(f) == str: cmdline += ["--file-filter", f] else: - cmdline += ["--file-filter", "{name} | {flt}".format(name=f[0], flt=" ".join(f[1:]))] + cmdline += [ + "--file-filter", + "{name} | {flt}".format(name=f[0], flt=" ".join(f[1:])) + ] return cmdline + class KDialogFileChooser(SubprocessFileChooser): """A FileChooser implementation using KDialog (on GNU/Linux). @@ -132,11 +137,22 @@ def _gen_cmdline(self): filt += list(f[1:]) if self.mode == "dir": - cmdline += ["--getexistingdirectory", (self.path if self.path else os.path.expanduser("~"))] + cmdline += [ + "--getexistingdirectory", + (self.path if self.path else os.path.expanduser("~")) + ] elif self.mode == "save": - cmdline += ["--getopenfilename", (self.path if self.path else os.path.expanduser("~")), " ".join(filt)] + cmdline += [ + "--getopenfilename", + (self.path if self.path else os.path.expanduser("~")), + " ".join(filt) + ] else: - cmdline += ["--getopenfilename", (self.path if self.path else os.path.expanduser("~")), " ".join(filt)] + cmdline += [ + "--getopenfilename", + (self.path if self.path else os.path.expanduser("~")), + " ".join(filt) + ] if self.multiple: cmdline += ["--multiple", "--separate-output"] if self.title: @@ -145,6 +161,7 @@ def _gen_cmdline(self): cmdline += ["--icon", self.icon] return cmdline + class YADFileChooser(SubprocessFileChooser): """A NativeFileChooser implementation using YAD (on GNU/Linux). @@ -157,7 +174,8 @@ class YADFileChooser(SubprocessFileChooser): successretcode = 0 def _gen_cmdline(self): - cmdline = [which(self.executable), "--file-selection", "--confirm-overwrite", "--geometry", "800x600+150+150"] + cmdline = [which(self.executable), "--file-selection", + "--confirm-overwrite", "--geometry", "800x600+150+150"] if self.multiple: cmdline += ["--multiple", "--separator", self.separator] if self.mode == "save": @@ -169,19 +187,25 @@ def _gen_cmdline(self): if self.path: cmdline += ["--filename", self.path] if self.title: - cmdline += ["--name", self.title] + cmdline += ["--name", self.title] if self.icon: cmdline += ["--window-icon", self.icon] for f in self.filters: if type(f) == str: cmdline += ["--file-filter", f] else: - cmdline += ["--file-filter", "{name} | {flt}".format(name=f[0], flt=" ".join(f[1:]))] + cmdline += [ + "--file-filter", + "{name} | {flt}".format(name=f[0], flt=" ".join(f[1:])) + ] return cmdline -CHOOSERS = {"gnome": ZenityFileChooser, - "kde": KDialogFileChooser, - "yad": YADFileChooser} +CHOOSERS = { + "gnome": ZenityFileChooser, + "kde": KDialogFileChooser, + "yad": YADFileChooser +} + class LinuxFileChooser(FileChooser): """FileChooser implementation for GNu/Linux. Accepts one additional @@ -193,7 +217,8 @@ class LinuxFileChooser(FileChooser): """ desktop = None - if str(os.environ.get("XDG_CURRENT_DESKTOP")).lower() == "kde" and which("kdialog"): + if str(os.environ.get("XDG_CURRENT_DESKTOP")).lower() == "kde" \ + and which("kdialog"): desktop = "kde" elif which("yad"): desktop = "yad" diff --git a/plyer/platforms/win/filechooser.py b/plyer/platforms/win/filechooser.py index 80cd43189..8c478c741 100644 --- a/plyer/platforms/win/filechooser.py +++ b/plyer/platforms/win/filechooser.py @@ -46,7 +46,7 @@ def run(self): args = {} if self.path: - atgs["InitialDir"] = os.path.dirname(self.path) + args["InitialDir"] = os.path.dirname(self.path) args["File"] = os.path.splitext(os.path.dirname(self.path))[0] args["DefExt"] = os.path.splitext(os.path.dirname(self.path))[1] args["Title"] = self.title if self.title else "Pick a file..." diff --git a/plyer/tools/pep8checker/pep8.py b/plyer/tools/pep8checker/pep8.py index bc79acdc9..5daeb4339 100644 --- a/plyer/tools/pep8checker/pep8.py +++ b/plyer/tools/pep8checker/pep8.py @@ -106,9 +106,9 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number) from fnmatch import fnmatch try: from ConfigParser import RawConfigParser - from io import TextIOWrapper except ImportError: from configparser import RawConfigParser + from io import TextIOWrapper DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git' DEFAULT_IGNORE = 'E24' @@ -1328,6 +1328,26 @@ def check_all(self, expected=None, line_offset=0): pos = 'l.%s' % token[3][0] print(('l.%s\t%s\t%s\t%r' % (token[2][0], pos, tokenize.tok_name[token[0]], text))) + if token_type == tokenize.COMMENT or token_type == tokenize.STRING: + for sre in re.finditer(r"[:.;,] ?[A-Za-z]", text): + pos = sre.span()[0] + part = text[:pos] + line = token[2][0] + part.count('\n') + offset = 0 if part.count('\n') > 0 else token[2][1] + col = offset + pos - part.rfind('\n') + 1 + if sre.group(0)[0] == '.': + self.report_error(line, col, + 'E289 Too many spaces after period. Use only one.', + check=None) + elif sre.group(0)[0] == ',': + self.report_error(line, col, + 'E288 Too many spaces after comma. Use only one.', + check=None) + else: + self.report_error(line, col, + 'E287 Too many spaces after punctuation. ' + 'Use only one.', + check=None) if token_type == tokenize.OP: if text in '([{': parens += 1 @@ -1352,6 +1372,11 @@ def check_all(self, expected=None, line_offset=0): if COMMENT_WITH_NL: # The comment also ends a physical line self.tokens = [] + if self.blank_lines > 1: + self.report_error(token[2][0],0, + 'E389 File ends in multiple blank lines', + check=None) + return self.report.get_file_results() @@ -1479,7 +1504,7 @@ def error(self, line_number, offset, text, check): line = self.lines[line_number - 1] print((line.rstrip())) print((' ' * offset + '^')) - if self._show_pep8: + if self._show_pep8 and check is not None: print((check.__doc__.lstrip('\n').rstrip())) return code diff --git a/plyer/tools/pep8checker/pep8kivy.py b/plyer/tools/pep8checker/pep8kivy.py index 96e013581..245928e57 100644 --- a/plyer/tools/pep8checker/pep8kivy.py +++ b/plyer/tools/pep8checker/pep8kivy.py @@ -57,8 +57,11 @@ def check(fn): return checker.check_all() errors = 0 - exclude_dirs = ['/pep8', '/doc'] - exclude_files = [] + exclude_dirs = ['/lib', '/coverage', '/pep8', '/doc'] + exclude_files = ['kivy/gesture.py', 'osx/build.py', 'win32/build.py', + 'kivy/tools/stub-gl-debug.py', + 'kivy/modules/webdebugger.py', + 'kivy/modules/_webdebugger.py'] for target in targets: if isdir(target): if htmlmode: From 7646faad7ea365e90030718ac220bd4df9d8de50 Mon Sep 17 00:00:00 2001 From: "andrzej.grymkowski" Date: Thu, 14 May 2015 10:50:08 +0200 Subject: [PATCH 2/5] improved styles in files --- examples/accelerometer/basic/main.py | 10 ++- examples/compass/main.py | 20 +++--- examples/gps/main.py | 6 +- examples/sms/main.py | 2 + examples/text2speech/main.py | 5 +- plyer/platforms/android/camera.py | 8 ++- plyer/platforms/android/irblaster.py | 1 + plyer/platforms/ios/gps.py | 2 +- plyer/platforms/win/filechooser.py | 102 +++++++++++++++------------ 9 files changed, 91 insertions(+), 65 deletions(-) diff --git a/examples/accelerometer/basic/main.py b/examples/accelerometer/basic/main.py index c0fff2054..f4813fdb3 100644 --- a/examples/accelerometer/basic/main.py +++ b/examples/accelerometer/basic/main.py @@ -10,6 +10,7 @@ from plyer import accelerometer + class AccelerometerTest(BoxLayout): def __init__(self): super(AccelerometerTest, self).__init__() @@ -30,17 +31,20 @@ def do_toggle(self): self.sensorEnabled = False self.ids.toggle_button.text = "Start Accelerometer" except NotImplementedError: - import traceback; traceback.print_exc() - self.ids.accel_status.text = "Accelerometer is not implemented for your platform" + import traceback + traceback.print_exc() + status = "Accelerometer is not implemented for your platform" + self.ids.accel_status.text = status def get_acceleration(self, dt): val = accelerometer.acceleration[:3] - if(not val == (None, None, None)): + if not val == (None, None, None): self.ids.x_label.text = "X: " + str(val[0]) self.ids.y_label.text = "Y: " + str(val[1]) self.ids.z_label.text = "Z: " + str(val[2]) + class AccelerometerTestApp(App): def build(self): return AccelerometerTest() diff --git a/examples/compass/main.py b/examples/compass/main.py index 48fbf3a1a..0bcbf02db 100644 --- a/examples/compass/main.py +++ b/examples/compass/main.py @@ -2,17 +2,16 @@ Compass example. ''' -from kivy.lang import Builder from kivy.app import App -from kivy.properties import ObjectProperty from kivy.uix.boxlayout import BoxLayout from kivy.clock import Clock from plyer import compass -class compassTest(BoxLayout): + +class CompassTest(BoxLayout): def __init__(self): - super(compassTest, self).__init__() + super(CompassTest, self).__init__() self.sensorEnabled = False def do_toggle(self): @@ -30,8 +29,10 @@ def do_toggle(self): self.sensorEnabled = False self.ids.toggle_button.text = "Start compass" except NotImplementedError: - import traceback; traceback.print_exc() - self.ids.status.text = "Compass is not implemented for your platform" + import traceback + traceback.print_exc() + status = "Compass is not implemented for your platform" + self.ids.status.text = status def get_readings(self, dt): val = compass.orientation @@ -40,9 +41,10 @@ def get_readings(self, dt): self.ids.y_label.text = "Y: " + str(val[1]) self.ids.z_label.text = "Z: " + str(val[2]) -class compassTestApp(App): + +class CompassTestApp(App): def build(self): - return compassTest() + return CompassTest() if __name__ == '__main__': - compassTestApp().run() + CompassTestApp().run() diff --git a/examples/gps/main.py b/examples/gps/main.py index 91e980cba..c09747d23 100644 --- a/examples/gps/main.py +++ b/examples/gps/main.py @@ -21,7 +21,8 @@ ToggleButton: text: 'Start' if self.state == 'normal' else 'Stop' - on_state: app.gps.start() if self.state == 'down' else app.gps.stop() + on_state: + app.gps.start() if self.state == 'down' else app.gps.stop() ''' @@ -45,7 +46,8 @@ def build(self): self.gps.configure(on_location=self.on_location, on_status=self.on_status) except NotImplementedError: - import traceback; traceback.print_exc() + import traceback + traceback.print_exc() self.gps_status = 'GPS is not implemented for your platform' return Builder.load_string(kv) diff --git a/examples/sms/main.py b/examples/sms/main.py index 8f7db0e8b..8ef30fe0b 100644 --- a/examples/sms/main.py +++ b/examples/sms/main.py @@ -36,6 +36,7 @@ class SmsInterface(BoxLayout): pass + class IntentButton(Button): sms_recipient = StringProperty() sms_message = StringProperty() @@ -43,6 +44,7 @@ class IntentButton(Button): def send_sms(self, *args): sms.send(recipient=self.sms_recipient, message=self.sms_message) + class SmsApp(App): def build(self): return SmsInterface() diff --git a/examples/text2speech/main.py b/examples/text2speech/main.py index f13f1d363..a2cbafaf1 100644 --- a/examples/text2speech/main.py +++ b/examples/text2speech/main.py @@ -2,12 +2,12 @@ kivy.require('1.8.0') from kivy.app import App -from kivy.properties import ObjectProperty from kivy.uix.boxlayout import BoxLayout from kivy.uix.popup import Popup from plyer import tts + class Text2SpeechDemo(BoxLayout): def do_read(self): try: @@ -16,13 +16,14 @@ def do_read(self): popup = ErrorPopup() popup.open() + class Text2SpeechDemoApp(App): def build(self): return Text2SpeechDemo() + class ErrorPopup(Popup): pass if __name__ == '__main__': Text2SpeechDemoApp().run() - \ No newline at end of file diff --git a/plyer/platforms/android/camera.py b/plyer/platforms/android/camera.py index 4a6201082..344296d0d 100644 --- a/plyer/platforms/android/camera.py +++ b/plyer/platforms/android/camera.py @@ -24,7 +24,7 @@ def _take_picture(self, on_complete, filename=None): parcelable = cast('android.os.Parcelable', uri) intent.putExtra(MediaStore.EXTRA_OUTPUT, parcelable) activity.startActivityForResult(intent, 0x123) - + def _take_video(self, on_complete, filename=None): assert(on_complete is not None) self.on_complete = on_complete @@ -35,8 +35,10 @@ def _take_video(self, on_complete, filename=None): uri = Uri.parse('file://' + filename) parcelable = cast('android.os.Parcelable', uri) intent.putExtra(MediaStore.EXTRA_OUTPUT, parcelable) - intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1) #0 = low quality, suitable for MMS messages, 1 = high quality - #intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, X) #Optional, allows limiting record time to X seconds. + + # 0 = low quality, suitable for MMS messages, + # 1 = high quality + intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1) activity.startActivityForResult(intent, 0x123) def _on_activity_result(self, requestCode, resultCode, intent): diff --git a/plyer/platforms/android/irblaster.py b/plyer/platforms/android/irblaster.py index 8b616fe73..6c4471779 100644 --- a/plyer/platforms/android/irblaster.py +++ b/plyer/platforms/android/irblaster.py @@ -9,6 +9,7 @@ else: ir_manager = None + class AndroidIrBlaster(IrBlaster): def _exists(self): if ir_manager and ir_manager.hasIrEmitter(): diff --git a/plyer/platforms/ios/gps.py b/plyer/platforms/ios/gps.py index 7e38c8c4c..4d6d665c6 100644 --- a/plyer/platforms/ios/gps.py +++ b/plyer/platforms/ios/gps.py @@ -18,7 +18,7 @@ def _configure(self): def _start(self): self._location_manager.delegate = self - + self._location_manager.requestWhenInUseAuthorization() # NSLocationWhenInUseUsageDescription key must exist in Info.plist # file. When the authorization prompt is displayed your app goes diff --git a/plyer/platforms/win/filechooser.py b/plyer/platforms/win/filechooser.py index 8c478c741..d0a5c12be 100644 --- a/plyer/platforms/win/filechooser.py +++ b/plyer/platforms/win/filechooser.py @@ -6,7 +6,9 @@ from plyer.facades import FileChooser from win32com.shell import shell, shellcon import os -import win32gui, win32con, pywintypes +import win32gui +import win32con +import pywintypes class Win32FileChooser(object): @@ -42,58 +44,68 @@ def __init__(self, **kwargs): def run(self): try: - if mode != "dir": - args = {} - - if self.path: - args["InitialDir"] = os.path.dirname(self.path) - args["File"] = os.path.splitext(os.path.dirname(self.path))[0] - args["DefExt"] = os.path.splitext(os.path.dirname(self.path))[1] - args["Title"] = self.title if self.title else "Pick a file..." - args["CustomFilter"] = 'Other file types\x00*.*\x00' - args["FilterIndex"] = 1 - - filters = "" - for f in self.filters: - if type(f) == str: - filters += (f + "\x00") * 2 - else: - filters += f[0] + "\x00" + ";".join(f[1:]) + "\x00" - args["Filter"] = filters - - flags = win32con.OFN_EXTENSIONDIFFERENT | win32con.OFN_OVERWRITEPROMPT + return self._run() + except (RuntimeError, pywintypes.error): + return None + + def _run(self): + if mode != "dir": + args = {} + + if self.path: + args["InitialDir"] = os.path.dirname(self.path) + path = os.path.splitext(os.path.dirname(self.path)) + args["File"] = path[0] + args["DefExt"] = path[1] + args["Title"] = self.title if self.title else "Pick a file..." + args["CustomFilter"] = 'Other file types\x00*.*\x00' + args["FilterIndex"] = 1 + + filters = "" + for f in self.filters: + if type(f) == str: + filters += (f + "\x00") * 2 + else: + filters += f[0] + "\x00" + ";".join(f[1:]) + "\x00" + args["Filter"] = filters + + flag_extension = win32con.OFN_EXTENSIONDIFFERENT + flag_overwriteprompt = win32con.OFN_OVERWRITEPROMPT + flags = flag_extension | flag_overwriteprompt + if self.multiple: + flags |= win32con.OFN_ALLOWmultiple | win32con.OFN_EXPLORER + if self.show_hidden: + flags |= win32con.OFN_FORCESHOWHIDDEN + args["Flags"] = flags + + if self.mode == "open": + self.fname, _, _ = win32gui.GetOpenFileNameW(**args) + elif self.mode == "save": + self.fname, _, _ = win32gui.GetSaveFileNameW(**args) + + if self.fname: if self.multiple: - flags |= win32con.OFN_ALLOWmultiple | win32con.OFN_EXPLORER - if self.show_hidden: - flags |= win32con.OFN_FORCESHOWHIDDEN - args["Flags"] = flags - - if self.mode == "open": - self.fname, self.customfilter, self.flags = win32gui.GetOpenFileNameW(**args) - elif self.mode == "save": - self.fname, self.customfilter, self.flags = win32gui.GetSaveFileNameW(**args) - - if self.fname: - if self.multiple: - seq = str(self.fname).split("\x00") - dir_n, base_n = seq[0], seq[1:] - self.selection = [os.path.join(dir_n, i) for i in base_n] - else: - self.selection = str(self.fname).split("\x00") - else: - # From http://timgolden.me.uk/python/win32_how_do_i/browse-for-a-folder.html - pidl, display_name, image_list = shell.SHBrowseForFolder( + seq = str(self.fname).split("\x00") + dir_n, base_n = seq[0], seq[1:] + self.selection = [ + os.path.join(dir_n, i) for i in base_n] + else: + self.selection = str(self.fname).split("\x00") + else: + # From + # http://goo.gl/UDqCqo + pidl, display_name, image_list = shell.SHBrowseForFolder( win32gui.GetDesktopWindow(), None, self.title if self.title else "Pick a folder...", 0, None, None) - self.selection = [str(shell.SHGetPathFromIDList (pidl))] + self.selection = [str(shell.SHGetPathFromIDList(pidl))] + + return self.selection - return self.selection - except (RuntimeError, pywintypes.error): - return None class WinFileChooser(FileChooser): """FileChooser implementation for Windows, using win3all. """ + def _file_selection_dialog(self, **kwargs): return Win32FileChooser(**kwargs).run() From a973ec5687f576131d357b34db50326ad30e8667 Mon Sep 17 00:00:00 2001 From: "andrzej.grymkowski" Date: Thu, 14 May 2015 10:56:37 +0200 Subject: [PATCH 3/5] update for docs --- plyer/platforms/linux/filechooser.py | 40 +++++++++++++-------------- plyer/platforms/macosx/filechooser.py | 8 +++--- plyer/platforms/win/filechooser.py | 10 +++---- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/plyer/platforms/linux/filechooser.py b/plyer/platforms/linux/filechooser.py index 093102a7e..59540cb39 100644 --- a/plyer/platforms/linux/filechooser.py +++ b/plyer/platforms/linux/filechooser.py @@ -11,25 +11,25 @@ class SubprocessFileChooser(object): - """A file chooser implementation that allows using + '''A file chooser implementation that allows using subprocess back-ends. Normally you only need to override _gen_cmdline, executable, separator and successretcode. - """ + ''' executable = "" - """The name of the executable of the back-end. - """ + '''The name of the executable of the back-end. + ''' separator = "|" - """The separator used by the back-end. Override this for automatic + '''The separator used by the back-end. Override this for automatic splitting, or override _split_output. - """ + ''' successretcode = 0 - """The return code which is returned when the user doesn't close the + '''The return code which is returned when the user doesn't close the dialog without choosing anything, or when the app doesn't crash. - """ + ''' path = None multiple = False @@ -60,15 +60,15 @@ def _run_command(self, cmd): time.sleep(0.1) def _split_output(self, out): - """This methods receives the output of the back-end and turns + '''This methods receives the output of the back-end and turns it into a list of paths. - """ + ''' return out.split(self.separator) def _gen_cmdline(self): - """Returns the command line of the back-end, based on the current + '''Returns the command line of the back-end, based on the current properties. You need to override this. - """ + ''' raise NotImplementedError() def run(self): @@ -76,12 +76,12 @@ def run(self): class ZenityFileChooser(SubprocessFileChooser): - """A FileChooser implementation using Zenity (on GNU/Linux). + '''A FileChooser implementation using Zenity (on GNU/Linux). Not implemented features: * show_hidden * preview - """ + ''' executable = "zenity" separator = "|" @@ -114,12 +114,12 @@ def _gen_cmdline(self): class KDialogFileChooser(SubprocessFileChooser): - """A FileChooser implementation using KDialog (on GNU/Linux). + '''A FileChooser implementation using KDialog (on GNU/Linux). Not implemented features: * show_hidden * preview - """ + ''' executable = "kdialog" separator = "\n" @@ -163,11 +163,11 @@ def _gen_cmdline(self): class YADFileChooser(SubprocessFileChooser): - """A NativeFileChooser implementation using YAD (on GNU/Linux). + '''A NativeFileChooser implementation using YAD (on GNU/Linux). Not implemented features: * show_hidden - """ + ''' executable = "yad" separator = "|?|" @@ -208,13 +208,13 @@ def _gen_cmdline(self): class LinuxFileChooser(FileChooser): - """FileChooser implementation for GNu/Linux. Accepts one additional + '''FileChooser implementation for GNu/Linux. Accepts one additional keyword argument, *desktop_override*, which, if set, overrides the back-end that will be used. Set it to "gnome" for Zenity, to "kde" for KDialog and to "yad" for YAD (Yet Another Dialog). If set to None or not set, a default one will be picked based on the running desktop environment and installed back-ends. - """ + ''' desktop = None if str(os.environ.get("XDG_CURRENT_DESKTOP")).lower() == "kde" \ diff --git a/plyer/platforms/macosx/filechooser.py b/plyer/platforms/macosx/filechooser.py index 5166fa489..5774cc4a6 100644 --- a/plyer/platforms/macosx/filechooser.py +++ b/plyer/platforms/macosx/filechooser.py @@ -15,7 +15,7 @@ class MacFileChooser(object): - """A native implementation of file chooser dialogs using Apple's API + '''A native implementation of file chooser dialogs using Apple's API through pyobjus. Not implemented features: @@ -25,7 +25,7 @@ class MacFileChooser(object): * multiple (only for save dialog. Available in open dialog) * icon * preview - """ + ''' mode = "open" path = None @@ -98,8 +98,8 @@ def run(self): class MacOSXFileChooser(FileChooser): - """FileChooser implementation for Windows, using win3all. - """ + '''FileChooser implementation for Windows, using win3all. + ''' def _file_selection_dialog(self, **kwargs): return MacFileChooser(**kwargs).run() diff --git a/plyer/platforms/win/filechooser.py b/plyer/platforms/win/filechooser.py index d0a5c12be..8d05146a6 100644 --- a/plyer/platforms/win/filechooser.py +++ b/plyer/platforms/win/filechooser.py @@ -12,7 +12,7 @@ class Win32FileChooser(object): - """A native implementation of NativeFileChooser using the + '''A native implementation of NativeFileChooser using the Win32 API on Windows. Not Implemented features (all dialogs): @@ -27,7 +27,7 @@ class Win32FileChooser(object): * show_hidden * filters * path - """ + ''' path = None multiple = False @@ -92,8 +92,7 @@ def _run(self): else: self.selection = str(self.fname).split("\x00") else: - # From - # http://goo.gl/UDqCqo + # From http://goo.gl/UDqCqo pidl, display_name, image_list = shell.SHBrowseForFolder( win32gui.GetDesktopWindow(), None, self.title if self.title else "Pick a folder...", 0, None, None) @@ -103,8 +102,7 @@ def _run(self): class WinFileChooser(FileChooser): - """FileChooser implementation for Windows, using win3all. - """ + '''FileChooser implementation for Windows, using win3all.''' def _file_selection_dialog(self, **kwargs): return Win32FileChooser(**kwargs).run() From fcd14f3c3d82eb709a78b2c11cf801009902751d Mon Sep 17 00:00:00 2001 From: "andrzej.grymkowski" Date: Thu, 14 May 2015 11:44:10 +0200 Subject: [PATCH 4/5] corrected win filechooser --- plyer/platforms/win/filechooser.py | 113 +++++++++++++++-------------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/plyer/platforms/win/filechooser.py b/plyer/platforms/win/filechooser.py index 8d05146a6..f7edc33c3 100644 --- a/plyer/platforms/win/filechooser.py +++ b/plyer/platforms/win/filechooser.py @@ -12,7 +12,7 @@ class Win32FileChooser(object): - '''A native implementation of NativeFileChooser using the + """A native implementation of NativeFileChooser using the Win32 API on Windows. Not Implemented features (all dialogs): @@ -27,7 +27,7 @@ class Win32FileChooser(object): * show_hidden * filters * path - ''' + """ path = None multiple = False @@ -44,65 +44,66 @@ def __init__(self, **kwargs): def run(self): try: - return self._run() + if mode != "dir": + args = {} + + if self.path: + args["InitialDir"] = os.path.dirname(self.path) + path = os.path.splitext(os.path.dirname(self.path)) + args["File"] = path[0] + args["DefExt"] = path[1] + args["Title"] = self.title if self.title else "Pick a file..." + args["CustomFilter"] = 'Other file types\x00*.*\x00' + args["FilterIndex"] = 1 + + filters = "" + for f in self.filters: + if type(f) == str: + filters += (f + "\x00") * 2 + else: + filters += f[0] + "\x00" + ";".join(f[1:]) + "\x00" + args["Filter"] = filters + + flags = (win32con.OFN_EXTENSIONDIFFERENT | + win32con.OFN_OVERWRITEPROMPT) + if self.multiple: + flags |= win32con.OFN_ALLOWmultiple | win32con.OFN_EXPLORER + if self.show_hidden: + flags |= win32con.OFN_FORCESHOWHIDDEN + args["Flags"] = flags + + if self.mode == "open": + self.fname, _, _ = win32gui.GetOpenFileNameW(**args) + elif self.mode == "save": + self.fname, _, _ = win32gui.GetSaveFileNameW(**args) + + if self.fname: + if self.multiple: + seq = str(self.fname).split("\x00") + dir_n, base_n = seq[0], seq[1:] + self.selection = [ + os.path.join(dir_n, i) for i in base_n] + else: + self.selection = str(self.fname).split("\x00") + else: + # From + # http://goo.gl/UDqCqo + pidl, display_name, image_list = shell.SHBrowseForFolder( + win32gui.GetDesktopWindow(), + None, + self.title if self.title else "Pick a folder...", + 0, None, None + ) + self.selection = [str(shell.SHGetPathFromIDList(pidl))] + + return self.selection except (RuntimeError, pywintypes.error): return None - def _run(self): - if mode != "dir": - args = {} - - if self.path: - args["InitialDir"] = os.path.dirname(self.path) - path = os.path.splitext(os.path.dirname(self.path)) - args["File"] = path[0] - args["DefExt"] = path[1] - args["Title"] = self.title if self.title else "Pick a file..." - args["CustomFilter"] = 'Other file types\x00*.*\x00' - args["FilterIndex"] = 1 - - filters = "" - for f in self.filters: - if type(f) == str: - filters += (f + "\x00") * 2 - else: - filters += f[0] + "\x00" + ";".join(f[1:]) + "\x00" - args["Filter"] = filters - - flag_extension = win32con.OFN_EXTENSIONDIFFERENT - flag_overwriteprompt = win32con.OFN_OVERWRITEPROMPT - flags = flag_extension | flag_overwriteprompt - if self.multiple: - flags |= win32con.OFN_ALLOWmultiple | win32con.OFN_EXPLORER - if self.show_hidden: - flags |= win32con.OFN_FORCESHOWHIDDEN - args["Flags"] = flags - - if self.mode == "open": - self.fname, _, _ = win32gui.GetOpenFileNameW(**args) - elif self.mode == "save": - self.fname, _, _ = win32gui.GetSaveFileNameW(**args) - - if self.fname: - if self.multiple: - seq = str(self.fname).split("\x00") - dir_n, base_n = seq[0], seq[1:] - self.selection = [ - os.path.join(dir_n, i) for i in base_n] - else: - self.selection = str(self.fname).split("\x00") - else: - # From http://goo.gl/UDqCqo - pidl, display_name, image_list = shell.SHBrowseForFolder( - win32gui.GetDesktopWindow(), None, - self.title if self.title else "Pick a folder...", 0, None, None) - self.selection = [str(shell.SHGetPathFromIDList(pidl))] - - return self.selection - class WinFileChooser(FileChooser): - '''FileChooser implementation for Windows, using win3all.''' + """FileChooser implementation for Windows, using win3all. + """ def _file_selection_dialog(self, **kwargs): return Win32FileChooser(**kwargs).run() From 9daf635fa35a69bb006bc79bf47ad0d8a77dd945 Mon Sep 17 00:00:00 2001 From: "andrzej.grymkowski" Date: Thu, 14 May 2015 11:52:16 +0200 Subject: [PATCH 5/5] another improvements --- plyer/platforms/linux/filechooser.py | 16 ++++++++++++---- plyer/platforms/win/filechooser.py | 15 +++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/plyer/platforms/linux/filechooser.py b/plyer/platforms/linux/filechooser.py index 59540cb39..545487bee 100644 --- a/plyer/platforms/linux/filechooser.py +++ b/plyer/platforms/linux/filechooser.py @@ -88,8 +88,11 @@ class ZenityFileChooser(SubprocessFileChooser): successretcode = 0 def _gen_cmdline(self): - cmdline = [which(self.executable), - "--file-selection", "--confirm-overwrite"] + cmdline = [ + which(self.executable), + "--file-selection", + "--confirm-overwrite" + ] if self.multiple: cmdline += ["--multiple"] if self.mode == "save": @@ -174,8 +177,13 @@ class YADFileChooser(SubprocessFileChooser): successretcode = 0 def _gen_cmdline(self): - cmdline = [which(self.executable), "--file-selection", - "--confirm-overwrite", "--geometry", "800x600+150+150"] + cmdline = [ + which(self.executable), + "--file-selection", + "--confirm-overwrite", + "--geometry", + "800x600+150+150" + ] if self.multiple: cmdline += ["--multiple", "--separator", self.separator] if self.mode == "save": diff --git a/plyer/platforms/win/filechooser.py b/plyer/platforms/win/filechooser.py index f7edc33c3..47320eaee 100644 --- a/plyer/platforms/win/filechooser.py +++ b/plyer/platforms/win/filechooser.py @@ -12,7 +12,7 @@ class Win32FileChooser(object): - """A native implementation of NativeFileChooser using the + '''A native implementation of NativeFileChooser using the Win32 API on Windows. Not Implemented features (all dialogs): @@ -27,7 +27,7 @@ class Win32FileChooser(object): * show_hidden * filters * path - """ + ''' path = None multiple = False @@ -81,13 +81,12 @@ def run(self): if self.multiple: seq = str(self.fname).split("\x00") dir_n, base_n = seq[0], seq[1:] - self.selection = [ - os.path.join(dir_n, i) for i in base_n] + self.selection = [os.path.join(dir_n, i) + for i in base_n] else: self.selection = str(self.fname).split("\x00") else: - # From - # http://goo.gl/UDqCqo + # From http://goo.gl/UDqCqo pidl, display_name, image_list = shell.SHBrowseForFolder( win32gui.GetDesktopWindow(), None, @@ -102,8 +101,8 @@ def run(self): class WinFileChooser(FileChooser): - """FileChooser implementation for Windows, using win3all. - """ + '''FileChooser implementation for Windows, using win3all. + ''' def _file_selection_dialog(self, **kwargs): return Win32FileChooser(**kwargs).run()