修复fs.remove一处书写错误; 优化js交互

master
yutent 2023-09-06 14:16:57 +08:00
parent 80d486e87b
commit 5424d3ff74
1 changed files with 233 additions and 180 deletions

View File

@ -99,6 +99,7 @@ class WebEngine(WebKit2.WebView):
self.root = root self.root = root
return self.use(create_protocal(root)) return self.use(create_protocal(root))
def use(self, middle_ware = noop, extra = None): def use(self, middle_ware = noop, extra = None):
middle_ware(self, extra) middle_ware(self, extra)
return self return self
@ -135,101 +136,12 @@ class WebEngine(WebKit2.WebView):
output = env output = env
case 'fs': case 'fs':
filepath = params.get('filepath') _error, output = self._fs_setting(params)
if params['action'] == 'access':
try:
with open(filepath, params.get('mode')) as file:
output = True
except Exception as err:
output = False
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']
if params.get('mode').find('b') > -1:
buff = bytes(buff)
output = file.write(buff)
except Exception as err:
_error = err
elif params['action'] == 'exists':
output = os.path.exists(filepath)
elif params['action'] == 'list':
with os.scandir(filepath) as entries:
output = [{
"name": it.name,
"path": os.path.join(filepath, it.name),
"is_dir": it.is_dir(),
"size": it.stat().st_size,
"atime": int(it.stat().st_atime),
"mtime": int(it.stat().st_mtime),
} for it in entries]
elif params['action'] == 'remove':
if os.path.isfile(filepath):
output = os.remove(filepath)
elif os.path.isdir(filepath):
output = os.removedirs(filename)
elif params['action'] == 'rename':
if os.path.exists(filepath):
output = shutil.move(filepath, params['target'])
elif params['action'] == 'copy':
if os.path.exists(filepath):
output = shutil.copy2(filepath, params['target'])
elif params['action'] == 'isfile':
output = os.path.isfile(filepath)
elif params['action'] == 'isdir':
output = os.path.isdir(filepath)
case 'clipboard': case 'clipboard':
# 读文本 _error, output = self._clipboard_setting(params)
if params['action'] == 'wait_for_text':
output = self.clipboard.wait_for_text()
# 写文本
elif params['action'] == 'set_text':
self.clipboard.set_text(params['value'], -1)
# 写图片
elif params['action'] == 'set_image':
image = params['value']
# 前端传进来的值, 如果是路径的话, 直接读取
if type(image) == str:
image = GdkPixbuf.Pixbuf.new_from_file(image)
else:
image = dict_to_pixbuf(image)
self.clipboard.set_image(image)
self.clipboard.store()
# 读图片
elif params['action'] == 'wait_for_image':
output = self.clipboard.wait_for_image()
output = pixbuf_to_dict(output, 'noname.png')
# 清除剪切板
elif params['action'] == 'clear':
self.clipboard.clear()
# 退出app # 退出app
case 'quit': case 'quit':
@ -253,29 +165,7 @@ class WebEngine(WebKit2.WebView):
case 'keybinder': case 'keybinder':
keymap = params.get('value') _error, output = self._keybinder_setting(params)
shortcut_callback = params.get('shortcut_callback') or ''
if params['action'] == 'register':
# 绑定之前, 先解绑, 避免被重复绑定
if Keybinder:
Keybinder.unbind(keymap)
output = Keybinder.bind(
keymap,
lambda km : self.call_js(shortcut_callback)
)
elif params['action'] == 'unregister':
if Keybinder:
Keybinder.unbind(keymap)
output = True
elif params['action'] == 'supported':
if Keybinder:
output = Keybinder.supported()
else:
output = False
case 'tray': case 'tray':
@ -286,76 +176,24 @@ class WebEngine(WebKit2.WebView):
pass pass
case 'window': case 'window':
if params['action'] == 'fullscreen': _error, output = self._window_setting(params)
self.window.fullscreen()
elif params['action'] == 'unfullscreen':
self.window.unfullscreen()
elif params['action'] == 'maximize':
self.window.maximize()
elif params['action'] == 'unmaximize':
self.window.unmaximize()
elif params['action'] == 'set_title':
self.window.set_title(params['value'] or '')
elif params['action'] == 'resize':
self.window.resize(params['value'].get('width'), params['value'].get('height'))
elif params['action'] == 'set_opacity':
self.window.set_opacity(params['value'])
elif params['action'] == 'set_keep_above':
self.window.set_keep_above(params['value'])
elif params['action'] == 'set_keep_below':
self.window.set_keep_below(params['value'])
elif params['action'] == 'move':
self.window.move(params['value'].get('x'), params['value'].get('y'))
elif params['action'] == 'toggle_visible':
if self.is_visible():
self.window.hide()
else:
self.window.present()
elif params['action'] == 'hide':
self.window.hide()
elif params['action'] == 'show':
self.window.present()
elif params['action'] == 'is_visible':
output = self.window.is_visible()
case 'notify': case 'notify':
title = params.get('title') if self.notify is None:
summary = params.get('summary') _error = ImportError('Notify module not found. Need to install gir1.2-notify-0.7 if you use debian.')
icon = params.get('icon') else:
progress = params.get('progress') title = params.get('title')
urgency = params.get('urgency') summary = params.get('summary')
icon = params.get('icon')
progress = params.get('progress')
urgency = params.get('urgency')
self.notify.create(title, summary, icon, progress, urgency, params.get('callback')) self.notify.create(title, summary, icon, progress, urgency, params.get('callback'))
case 'proxy': case 'proxy':
dm = self.get_website_data_manager() _error, output = self._proxy_setting(params)
output = True
if params['action'] == 'disable':
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.NO_PROXY, None)
elif params['action'] == 'system':
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.DEFAULT, None)
else:
try:
proxy = WebKit2.NetworkProxySettings(params['url'], params['ignore'])
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.CUSTOM, proxy)
except Exception as err:
_error = err
output = False
case 'md5': case 'md5':
@ -365,3 +203,218 @@ class WebEngine(WebKit2.WebView):
if callback: if callback:
self.call_js(callback, output, _error) self.call_js(callback, output, _error)
def _fs_setting(self, params = {}):
_error = None
output = None
filepath = params.get('filepath')
match(params.get('action')):
case 'access':
try:
with open(filepath, params.get('mode')) as file:
output = True
except Exception as err:
output = False
case '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
case 'write':
# 调整以支持二进制数据写入
try:
with open(filepath, params.get('mode')) as file:
buff = params['content']
if params.get('mode').find('b') > -1:
buff = bytes(buff)
output = file.write(buff)
except Exception as err:
_error = err
case 'exists':
output = os.path.exists(filepath)
case 'list':
with os.scandir(filepath) as entries:
output = [{
"name": it.name,
"path": os.path.join(filepath, it.name),
"is_dir": it.is_dir(),
"size": it.stat().st_size,
"atime": int(it.stat().st_atime),
"mtime": int(it.stat().st_mtime),
} for it in entries]
case 'remove':
if os.path.isfile(filepath):
output = os.remove(filepath)
elif os.path.isdir(filepath):
output = os.removedirs(filepath)
case 'rename':
if os.path.exists(filepath):
output = shutil.move(filepath, params['target'])
case 'copy':
if os.path.exists(filepath):
output = shutil.copy2(filepath, params['target'])
case 'isfile':
output = os.path.isfile(filepath)
case 'isdir':
output = os.path.isdir(filepath)
return (_error, output)
def _clipboard_setting(self, params = {}):
_error = None
output = None
match(params.get('action')):
# 读文本
case 'wait_for_text':
output = self.clipboard.wait_for_text()
# 写文本
case 'set_text':
self.clipboard.set_text(params['value'], -1)
# 写图片
case 'set_image':
image = params['value']
# 前端传进来的值, 如果是路径的话, 直接读取
if type(image) == str:
image = GdkPixbuf.Pixbuf.new_from_file(image)
else:
image = dict_to_pixbuf(image)
self.clipboard.set_image(image)
self.clipboard.store()
# 读图片
case 'wait_for_image':
output = self.clipboard.wait_for_image()
output = pixbuf_to_dict(output, 'noname.png')
# 清除剪切板
case 'clear':
self.clipboard.clear()
return (_error, output)
def _keybinder_setting(self, params = {}):
_error = None
output = None
keymap = params.get('value')
shortcut_callback = params.get('shortcut_callback') or ''
if params['action'] == 'register':
# 绑定之前, 先解绑, 避免被重复绑定
if Keybinder:
Keybinder.unbind(keymap)
output = Keybinder.bind(
keymap,
lambda km : self.call_js(shortcut_callback)
)
elif params['action'] == 'unregister':
if Keybinder:
Keybinder.unbind(keymap)
output = True
elif params['action'] == 'supported':
if Keybinder:
output = Keybinder.supported()
else:
output = False
return (_error, output)
def _window_setting(self, params = {}):
_error = None
output = None
match(params.get('action')):
case 'fullscreen':
self.window.fullscreen()
case 'unfullscreen':
self.window.unfullscreen()
case 'maximize':
self.window.maximize()
case 'unmaximize':
self.window.unmaximize()
case 'set_title':
self.window.set_title(params['value'] or '')
case 'resize':
self.window.resize(params['value'].get('width'), params['value'].get('height'))
case 'set_opacity':
self.window.set_opacity(params['value'])
case 'set_keep_above':
self.window.set_keep_above(params['value'])
case 'set_keep_below':
self.window.set_keep_below(params['value'])
case 'move':
self.window.move(params['value'].get('x'), params['value'].get('y'))
case 'toggle_visible':
if self.window.is_visible():
self.window.hide()
else:
self.window.present()
case 'hide':
self.window.hide()
case 'show':
self.window.present()
case 'is_visible':
output = self.window.is_visible()
return (_error, output)
def _proxy_setting(self, params = {}):
dm = self.get_website_data_manager()
output = True
_error = None
if params['action'] == 'disable':
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.NO_PROXY, None)
elif params['action'] == 'system':
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.DEFAULT, None)
else:
try:
proxy = WebKit2.NetworkProxySettings(params['url'], params['ignore'])
dm.set_network_proxy_settings(WebKit2.NetworkProxyMode.CUSTOM, proxy)
except Exception as err:
_error = err
output = False
return (_error, output)