From 2aaa711a8984cd1dd536f2982435c3e60c451290 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 4 Sep 2023 19:11:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B3=A8=E5=85=A5,=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BC=82=E5=B8=B8=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dist-packages/webengine/gtk3/__init__.py | 9 ++-- .../webengine/gtk3/_webengine.py | 51 ++++++++++++------- .../dist-packages/webengine/gtk3/inject.js | 12 ++++- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/__init__.py b/usr/lib/python3/dist-packages/webengine/gtk3/__init__.py index e75f3e9..6371c5d 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/__init__.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/__init__.py @@ -2,10 +2,13 @@ # @author yutent # @date 2023/08/31 11:55:25 -from webengine.gtk3._webengine import WebEngine -from webengine.gtk3._settings import create_setting -from webengine.gtk3._hotreload import create_hmr_server +# from webengine.gtk3._webengine import WebEngine +# from webengine.gtk3._settings import create_setting +# from webengine.gtk3._hotreload import create_hmr_server +from ._webengine import WebEngine +from ._settings import create_setting +from ._hotreload import create_hmr_server build = (0, 3, 0) version = '.'.join(map(str, build)) diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py b/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py index 2f85a1a..a09da4c 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py @@ -112,8 +112,10 @@ class WebEngine(WebKit2.WebView): im.notify_cursor_area(p.x - x, p.y - y, 0, 0) # 修正输入法跟随 - def call_js(self, method, data = None): - scripts = 'native.$emit("' + method + '",' + json.dumps(data) + ')' + def call_js(self, method, data = None, err = None): + if err is not None: + err = str(err) + scripts = 'native.$emit("' + method + '", ' + json.dumps(err) + ', ' + json.dumps(data) + ')' self.evaluate_javascript(scripts, -1) @@ -125,6 +127,7 @@ class WebEngine(WebKit2.WebView): callback = data.get('callback') params = data.get('data') output = None + _error = None match event: @@ -134,15 +137,25 @@ class WebEngine(WebKit2.WebView): case 'fs': filepath = params.get('filepath') - if params['action'] == 'read': - with open(filepath, params.get('mode')) as file: - output = file.read() - if params.get('mode').find('b') > -1: - output = list(output) + if params['action'] == 'access': + try: + with open(filepath, params.get('mode')) as file: + output = True + except Exception as err: + output = False - if params['action'] == 'write': - - # 调整以支持二进制数据写入 + elif params['action'] == 'read': + try: + with open(filepath, params.get('mode')) as file: + output = file.read() + if params.get('mode').find('b') > -1: + output = list(output) + except Exception as err: + _error = err + + elif params['action'] == 'write': + # 调整以支持二进制数据写入 + try: with open(filepath, params.get('mode')) as file: buff = params['content'] @@ -150,11 +163,13 @@ class WebEngine(WebKit2.WebView): buff = bytes(buff) output = file.write(buff) + except Exception as err: + _error = err - if params['action'] == 'exists': + elif params['action'] == 'exists': output = os.path.exists(filepath) - if params['action'] == 'list': + elif params['action'] == 'list': with os.scandir(filepath) as entries: output = [{ "name": it.name, @@ -165,24 +180,24 @@ class WebEngine(WebKit2.WebView): "mtime": int(it.stat().st_mtime), } for it in entries] - if params['action'] == 'remove': + elif params['action'] == 'remove': if os.path.isfile(filepath): output = os.remove(filepath) elif os.path.isdir(filepath): output = os.removedirs(filename) - if params['action'] == 'rename': + elif params['action'] == 'rename': if os.path.exists(filepath): output = shutil.move(filepath, params['target']) - if params['action'] == 'copy': + elif params['action'] == 'copy': if os.path.exists(filepath): output = shutil.copy2(filepath, params['target']) - if params['action'] == 'isfile': + elif params['action'] == 'isfile': output = os.path.isfile(filepath) - if params['action'] == 'isdir': + elif params['action'] == 'isdir': output = os.path.isdir(filepath) @@ -331,5 +346,5 @@ class WebEngine(WebKit2.WebView): # 有回调则返回结果 if callback: - self.call_js(callback, output) + self.call_js(callback, output, _error) \ No newline at end of file diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/inject.js b/usr/lib/python3/dist-packages/webengine/gtk3/inject.js index 98d55a4..2654c83 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/inject.js +++ b/usr/lib/python3/dist-packages/webengine/gtk3/inject.js @@ -64,7 +64,13 @@ function handler(event, data = {}, once = true) { if (typeof once === 'boolean') { callback = rand() - native[once ? '$once' : '$on'](callback, _.resolve) + native[once ? '$once' : '$on'](callback, (err, res) => { + if (err) { + _.reject(err) + } else { + _.resolve(res) + } + }) } else { _.resolve(true) } @@ -187,6 +193,10 @@ Object.assign(native, { return handler('quit', {}, null) }, fs: { + access(filepath, mode = 'r') { + return handler('fs', { action: 'access', mode, filepath }) + }, + read(filepath, mode = 'r') { return handler('fs', { action: 'read', mode, filepath }).then(r => mode.includes('b') ? new Uint8Array(r) : r