From ca35c413fe1bc023625b2e0c0b0d33b1c9260579 Mon Sep 17 00:00:00 2001 From: j Date: Thu, 31 Jan 2019 19:11:13 +0530 Subject: [PATCH] fix windows select folder --- oml/__main__.py | 11 ++++++++--- oml/api.py | 23 +++++------------------ oml/ui.py | 22 ++++++++++++---------- oml/utils.py | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/oml/__main__.py b/oml/__main__.py index 3c54ef6..9bb7491 100644 --- a/oml/__main__.py +++ b/oml/__main__.py @@ -3,11 +3,12 @@ import os import sys +from os.path import normpath, dirname, abspath, join +import site + +base = normpath(dirname(dirname(dirname(abspath(__file__))))) if sys.platform == 'win32': - from os.path import normpath, dirname, abspath, join - import site - base = normpath(dirname(dirname(dirname(abspath(__file__))))) for site_packages in ( join(base, 'openmedialibrary'), join(base, 'platform', 'Shared', 'lib', 'python3.4', 'site-packages'), @@ -20,6 +21,7 @@ if sys.platform == 'win32': unrar_dll = join(base, 'platform_win32', 'unrar.dll') if os.path.exists(unrar_dll): os.environ['UNRAR_LIB_PATH'] = unrar_dll + os.environ['TCL_LIBRARY'] = join(base, 'platform_win32', 'tcl', 'tcl8.6') import api import commands @@ -27,6 +29,9 @@ import server if len(sys.argv) > 1 and sys.argv[1] == 'server': server.run() +if len(sys.argv) > 1 and sys.argv[1] == 'ui': + import ui + ui.main(sys.argv[2:]) else: commands.main() if sys.platform == 'win32': diff --git a/oml/api.py b/oml/api.py index bf44566..1975c7d 100644 --- a/oml/api.py +++ b/oml/api.py @@ -13,6 +13,7 @@ from oxtornado import actions import item.api import user.api import update +import utils import logging logger = logging.getLogger(__name__) @@ -20,8 +21,8 @@ logger = logging.getLogger(__name__) def win32_ui(type): base = normpath(dirname(dirname(dirname(abspath(__file__))))) cmd = [ - join(base, 'platform_win32', 'python.exe'), - join(base, 'openmediablirary', 'oml', 'ui.py'), type + join('..', 'platform_win32', 'pythonw.exe'), + join('oml', 'ui.py'), type ] return cmd @@ -31,14 +32,7 @@ def selectFolder(data): path } ''' - cmd = ['./ctl', 'ui', 'folder'] - if sys.platform == 'win32': - cmd = win32_ui('folder') - if 'base' in data: - cmd += [data['base']] - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True) - stdout, stderr = p.communicate() - path = stdout.decode('utf-8').strip() + path = utils.ctl_output('ui', 'folder') if path == 'None': path = None return { @@ -53,14 +47,7 @@ def selectFile(data): path } ''' - cmd = ['./ctl', 'ui', 'file'] - if sys.platform == 'win32': - cmd = win32_ui('file') - if 'base' in data: - cmd += [data['base']] - p = subprocess.Popen(cmd, stdout=subprocess.PIPE) - stdout, stderr = p.communicate() - path = stdout.decode('utf-8').strip() + path = utils.ctl_output('ui', 'file') if path == 'None': path = None return { diff --git a/oml/ui.py b/oml/ui.py index 5b9dc26..40e79b8 100644 --- a/oml/ui.py +++ b/oml/ui.py @@ -93,6 +93,17 @@ class TkUI: filename = tkinter.filedialog.askopenfilename(parent=self.root, title=data.get("title", "Select File")) return short_home(filename) +def main(args): + base = '~' + if len(args) >= 2: + base = args[1] + base = os.path.expanduser(base) + if os.path.exists(base): + os.chdir(base) + if args and args[0] == 'folder': + print(ui.selectFolder({})) + else: + print(ui.selectFile({})) if use_Gtk: ui = GtkUI() @@ -100,13 +111,4 @@ else: ui = TkUI() if __name__ == '__main__': - base = '~' - if len(sys.argv) >= 3: - base = sys.argv[2] - base = os.path.expanduser(base) - if os.path.exists(base): - os.chdir(base) - if len(sys.argv) >= 2 and sys.argv[1] == 'folder': - print(ui.selectFolder({})) - else: - print(ui.selectFile({})) + main(sys.argv[1:]) diff --git a/oml/utils.py b/oml/utils.py index 6e6d924..c080426 100644 --- a/oml/utils.py +++ b/oml/utils.py @@ -409,6 +409,24 @@ def ctl(*args): subprocess.Popen([os.path.join(settings.base_dir, 'ctl')] + list(args), close_fds=True, start_new_session=True) +def ctl_output(*args): + import settings + if sys.platform == 'win32': + platform_win32 = os.path.join('..', 'platform_win32') + python = os.path.join(platform_win32, 'python.exe') + cmd = [python, 'oml'] + list(args) + startupinfo = subprocess.STARTUPINFO() + startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW + startupinfo.wShowWindow = subprocess.SW_HIDE + p = subprocess.Popen(cmd, cwd=settings.base_dir, startupinfo=startupinfo, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + else: + p = subprocess.Popen([os.path.join(settings.base_dir, 'ctl')] + list(args), + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate() + logger.debug('ctl_output%s -> %s [%s]', args, stdout, stderr) + return stdout.decode('utf-8').strip() + def user_sort_key(u): return ox.sort_string(str(u.get('index', '')) + 'Z' + (u.get('name') or ''))