From 31fef02d6b942d484ed6ea6ad45b7a9ca9991b6a Mon Sep 17 00:00:00 2001 From: PTCMode <1044871170@qq.com> Date: Sat, 14 Dec 2024 01:51:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[fix]=20=E6=B7=BB=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E4=B8=B4=E6=97=B6=E8=A7=A3=E5=86=B3firefox=E4=B8=8B?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E8=AF=8D=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E7=9A=84=E6=A0=B7=E5=BC=8F=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- styles/extensions/firefox-fix/manifest.json | 8 +++++++ styles/extensions/firefox-fix/style.less | 26 +++++++++++++++++++++ styles/extensions/firefox-fix/style.min.css | 1 + 3 files changed, 35 insertions(+) create mode 100644 styles/extensions/firefox-fix/manifest.json create mode 100644 styles/extensions/firefox-fix/style.less create mode 100644 styles/extensions/firefox-fix/style.min.css diff --git a/styles/extensions/firefox-fix/manifest.json b/styles/extensions/firefox-fix/manifest.json new file mode 100644 index 0000000..04ceae3 --- /dev/null +++ b/styles/extensions/firefox-fix/manifest.json @@ -0,0 +1,8 @@ +{ + "name": "Temporary fix for issues caused by css in Firefox.", + "i18n": { + "zh_CN": "临时修复Firefox中css导致的问题" + }, + "author": "PTCMode", + "type": "enhance" +} \ No newline at end of file diff --git a/styles/extensions/firefox-fix/style.less b/styles/extensions/firefox-fix/style.less new file mode 100644 index 0000000..7e2935c --- /dev/null +++ b/styles/extensions/firefox-fix/style.less @@ -0,0 +1,26 @@ +.physton-prompt { + .prompt-tags { + .prompt-tags-list { + .prompt-tag { + .prompt-tag-main { + --pp-pt-dsb-ptl-pt-promptTagMain-width: calc(100% + 4px); + .prompt-tag-edit { + --pp-pt-dsb-ptl-pt-ptm-promptTagEdit-width: calc(100% + 8px); + .prompt-tag-value { + --pp-pt-dsb-ptl-pt-ptm-pte-promptTagValue-width: calc(100% - 8px); + .character { + --pp-pt-dsb-ptl-pt-ptm-pte-ptv-character-white-space: nowrap; + } + } + .input-tag-edit { + --pp-pt-dsb-ptl-pt-ptm-pte-inputTagEdit-max-width: calc(100% - 8px); + } + .btn-tag-delete { + --pp-pt-dsb-ptl-pt-ptm-pte-btnTagDelete-width: 100%; + } + } + } + } + } + } +} diff --git a/styles/extensions/firefox-fix/style.min.css b/styles/extensions/firefox-fix/style.min.css new file mode 100644 index 0000000..1bdf10c --- /dev/null +++ b/styles/extensions/firefox-fix/style.min.css @@ -0,0 +1 @@ +.physton-prompt{.prompt-tags{.prompt-tags-list{.prompt-tag{.prompt-tag-main{--pp-pt-dsb-ptl-pt-promptTagMain-width:calc(100%+4px);.prompt-tag-edit{--pp-pt-dsb-ptl-pt-ptm-promptTagEdit-width:calc(100%+8px);.prompt-tag-value{--pp-pt-dsb-ptl-pt-ptm-pte-promptTagValue-width:calc(100%-8px);.character{--pp-pt-dsb-ptl-pt-ptm-pte-ptv-character-white-space:nowrap;}}.input-tag-edit{--pp-pt-dsb-ptl-pt-ptm-pte-inputTagEdit-max-width:calc(100%-8px);}.btn-tag-delete{--pp-pt-dsb-ptl-pt-ptm-pte-btnTagDelete-width:100%;}}}}}}} \ No newline at end of file From 29387173152539a609bc6a5d8cf960bcc9b2ddad Mon Sep 17 00:00:00 2001 From: PTCMode <1044871170@qq.com> Date: Sat, 14 Dec 2024 01:54:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[fix]=20=E4=BF=AE=E5=A4=8D=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=94=B1=E4=BA=8E=E9=81=97=E7=95=99=E9=94=81=E5=AF=BC?= =?UTF-8?q?=E8=87=B4UI=E5=8A=A0=E8=BD=BD=E5=8F=AF=E8=83=BD=E4=BC=9A?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/on_app_started.py | 23 ++- scripts/physton_prompt/get_lang.py | 4 +- scripts/physton_prompt/get_translate_apis.py | 7 +- scripts/physton_prompt/history.py | 9 +- scripts/physton_prompt/storage.py | 153 ++++++++++--------- scripts/physton_prompt/styles.py | 3 +- tests/privacy_api_config.py | 3 +- tests/translate.py | 3 +- 8 files changed, 103 insertions(+), 102 deletions(-) diff --git a/scripts/on_app_started.py b/scripts/on_app_started.py index 22be211..134ae7e 100644 --- a/scripts/on_app_started.py +++ b/scripts/on_app_started.py @@ -57,7 +57,6 @@ except Exception as e: def on_app_started(_: gr.Blocks, app: FastAPI): - st = Storage() hi = History() @app.get("/physton_prompt/get_version") @@ -109,7 +108,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI): @app.get("/physton_prompt/get_data") async def _get_data(key: str): - data = st.get(key) + data = Storage.get(key) data = privacy_translate_api_config(key, data) return {"data": data} @@ -118,7 +117,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI): keys = keys.split(',') datas = {} for key in keys: - datas[key] = st.get(key) + datas[key] = Storage.get(key) datas[key] = privacy_translate_api_config(key, datas[key]) return {"datas": datas} @@ -130,7 +129,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI): if 'data' not in data: return {"success": False, "message": get_lang('is_required', {'0': 'data'})} data['data'] = unprotected_translate_api_config(data['key'], data['data']) - st.set(data['key'], data['data']) + Storage.set(data['key'], data['data']) return {"success": True} @app.post("/physton_prompt/set_datas") @@ -140,12 +139,12 @@ def on_app_started(_: gr.Blocks, app: FastAPI): return {"success": False, "message": get_lang('is_not_dict', {'0': 'data'})} for key in data: data[key] = unprotected_translate_api_config(key, data[key]) - st.set(key, data[key]) + Storage.set(key, data[key]) return {"success": True} @app.get("/physton_prompt/get_data_list_item") async def _get_data_list_item(key: str, index: int): - return {"item": st.list_get(key, index)} + return {"item": Storage.list_get(key, index)} @app.post("/physton_prompt/push_data_list") async def _push_data_list(request: Request): @@ -154,7 +153,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI): return {"success": False, "message": get_lang('is_required', {'0': 'key'})} if 'item' not in data: return {"success": False, "message": get_lang('is_required', {'0': 'item'})} - st.list_push(data['key'], data['item']) + Storage.list_push(data['key'], data['item']) return {"success": True} @app.post("/physton_prompt/pop_data_list") @@ -162,14 +161,14 @@ def on_app_started(_: gr.Blocks, app: FastAPI): data = await request.json() if 'key' not in data: return {"success": False, "message": get_lang('is_required', {'0': 'key'})} - return {"success": True, 'item': st.list_pop(data['key'])} + return {"success": True, 'item': Storage.list_pop(data['key'])} @app.post("/physton_prompt/shift_data_list") async def _shift_data_list(request: Request): data = await request.json() if 'key' not in data: return {"success": False, "message": get_lang('is_required', {'0': 'key'})} - return {"success": True, 'item': st.list_shift(data['key'])} + return {"success": True, 'item': Storage.list_shift(data['key'])} @app.post("/physton_prompt/remove_data_list") async def _remove_data_list(request: Request): @@ -178,7 +177,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI): return {"success": False, "message": get_lang('is_required', {'0': 'key'})} if 'index' not in data: return {"success": False, "message": get_lang('is_required', {'0': 'index'})} - st.list_remove(data['key'], data['index']) + Storage.list_remove(data['key'], data['index']) return {"success": True} @app.post("/physton_prompt/clear_data_list") @@ -186,7 +185,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI): data = await request.json() if 'key' not in data: return {"success": False, "message": get_lang('is_required', {'0': 'key'})} - st.list_clear(data['key']) + Storage.list_clear(data['key']) return {"success": True} @app.get("/physton_prompt/get_histories") @@ -395,7 +394,7 @@ def on_app_started(_: gr.Blocks, app: FastAPI): return {"tags": get_group_tags(lang)} try: - translate_api = st.get('translateApi') + translate_api = Storage.get('translateApi') if translate_api == 'mbart50': mbart50_initialize() except Exception: diff --git a/scripts/physton_prompt/get_lang.py b/scripts/physton_prompt/get_lang.py index 1a9515a..b3a6f72 100644 --- a/scripts/physton_prompt/get_lang.py +++ b/scripts/physton_prompt/get_lang.py @@ -1,6 +1,4 @@ from scripts.physton_prompt.storage import Storage - -storage = Storage() from scripts.physton_prompt.get_i18n import get_i18n @@ -12,7 +10,7 @@ def replace_vars(text, vars): def get_lang(key, vars={}): i18n = get_i18n() - code = storage.get('languageCode') + code = Storage.get('languageCode') def find_lang(code): for item in i18n['languages']: diff --git a/scripts/physton_prompt/get_translate_apis.py b/scripts/physton_prompt/get_translate_apis.py index 1c6abae..459581b 100644 --- a/scripts/physton_prompt/get_translate_apis.py +++ b/scripts/physton_prompt/get_translate_apis.py @@ -2,17 +2,14 @@ import os import json import re from scripts.physton_prompt.storage import Storage -st = Storage() # from scripts.physton_prompt.storage import Storage translate_apis = {} -# st = Storage() def get_translate_apis(reload=False): global translate_apis - global st if reload or not translate_apis: translate_apis = {} current_dir = os.path.dirname(os.path.abspath(__file__)) @@ -26,7 +23,7 @@ def get_translate_apis(reload=False): # if 'config' not in item: # continue # config_name = 'translate_api.' + item['key'] - # config = st.get(config_name) + # config = Storage.get(config_name) # if not config: # config = {} # for config_item in item['config']: @@ -102,7 +99,7 @@ def unprotected_translate_api_config(data_key, data): if 'config' not in api_item or not api_item['config']: return data - storage_data = st.get(data_key) + storage_data = Storage.get(data_key) for config in api_item['config']: # 如果有 privacy 的属性并且为 True diff --git a/scripts/physton_prompt/history.py b/scripts/physton_prompt/history.py index d37fa0c..87ec7aa 100644 --- a/scripts/physton_prompt/history.py +++ b/scripts/physton_prompt/history.py @@ -17,26 +17,25 @@ class History: 'img2img_neg': [], } max = 100 - storage = Storage() def __init__(self): for type in self.histories: - self.histories[type] = self.storage.get('history.' + type) + self.histories[type] = Storage.get('history.' + type) if self.histories[type] is None: self.histories[type] = [] self.__save_histories(type) for type in self.favorites: - self.favorites[type] = self.storage.get('favorite.' + type) + self.favorites[type] = Storage.get('favorite.' + type) if self.favorites[type] is None: self.favorites[type] = [] self.__save_favorites(type) def __save_histories(self, type): - self.storage.set('history.' + type, self.histories[type]) + Storage.set('history.' + type, self.histories[type]) def __save_favorites(self, type): - self.storage.set('favorite.' + type, self.favorites[type]) + Storage.set('favorite.' + type, self.favorites[type]) def get_histories(self, type): histories = self.histories[type] diff --git a/scripts/physton_prompt/storage.py b/scripts/physton_prompt/storage.py index cc7b695..86ef4ea 100644 --- a/scripts/physton_prompt/storage.py +++ b/scripts/physton_prompt/storage.py @@ -1,5 +1,4 @@ import os -from pathlib import Path import json import time @@ -7,50 +6,62 @@ import time class Storage: storage_path = '' - def __init__(self): - pass + def __init__(): + Storage.__dispose_all_locks() - def __get_storage_path(self): - self.storage_path = os.path.dirname(os.path.abspath(__file__)) + '/../../storage' - self.storage_path = os.path.normpath(self.storage_path) - if not os.path.exists(self.storage_path): - os.makedirs(self.storage_path) + def __get_storage_path(): + Storage.storage_path = os.path.dirname(os.path.abspath(__file__)) + '/../../storage' + Storage.storage_path = os.path.normpath(Storage.storage_path) + if not os.path.exists(Storage.storage_path): + os.makedirs(Storage.storage_path) # old_storage_path = os.path.join(Path().absolute(), 'physton-prompt') # if os.path.exists(old_storage_path): # # 复制就的存储文件到新的存储文件夹 # for file in os.listdir(old_storage_path): # old_file_path = os.path.join(old_storage_path, file) - # new_file_path = os.path.join(self.storage_path, file) + # new_file_path = os.path.join(Storage.storage_path, file) # if not os.path.exists(new_file_path): # os.rename(old_file_path, new_file_path) # # 删除旧的存储文件夹 # os.rmdir(old_storage_path) - return self.storage_path + return Storage.storage_path - def __get_data_filename(self, key): - return self.__get_storage_path() + '/' + key + '.json' + def __get_data_filename(key): + return Storage.__get_storage_path() + '/' + key + '.json' - def __get_key_lock_filename(self, key): - return self.__get_storage_path() + '/' + key + '.lock' + def __get_key_lock_filename(key): + return Storage.__get_storage_path() + '/' + key + '.lock' - def __lock(self, key): - file_path = self.__get_key_lock_filename(key) + def __dispose_all_locks(): + directory = Storage.__get_storage_path() + for filename in os.listdir(directory): + # 检查文件是否以指定后缀结尾 + if filename.endswith('.lock'): + file_path = os.path.join(directory, filename) + try: + os.remove(file_path) + print(f"Disposed lock: {file_path}") + except Exception as e: + print(f"Dispose lock {file_path} failed: {e}") + + def __lock(key): + file_path = Storage.__get_key_lock_filename(key) with open(file_path, 'w') as f: f.write('1') - def __unlock(self, key): - file_path = self.__get_key_lock_filename(key) + def __unlock(key): + file_path = Storage.__get_key_lock_filename(key) if os.path.exists(file_path): os.remove(file_path) - def __is_locked(self, key): - file_path = self.__get_key_lock_filename(key) + def __is_locked(key): + file_path = Storage.__get_key_lock_filename(key) return os.path.exists(file_path) - def __get(self, key): - filename = self.__get_data_filename(key) + def __get(key): + filename = Storage.__get_data_filename(key) if not os.path.exists(filename): return None if os.path.getsize(filename) == 0: @@ -75,103 +86,103 @@ class Storage: return None return data - def __set(self, key, data): - file_path = self.__get_data_filename(key) + def __set(key, data): + file_path = Storage.__get_data_filename(key) with open(file_path, 'w') as f: json.dump(data, f, indent=4, ensure_ascii=True) - def set(self, key, data): - while self.__is_locked(key): + def set(key, data): + while Storage.__is_locked(key): time.sleep(0.01) - self.__lock(key) + Storage.__lock(key) try: - self.__set(key, data) - self.__unlock(key) + Storage.__set(key, data) + Storage.__unlock(key) except Exception as e: - self.__unlock(key) + Storage.__unlock(key) raise e - def get(self, key): - return self.__get(key) + def get(key): + return Storage.__get(key) - def delete(self, key): - file_path = self.__get_data_filename(key) + def delete(key): + file_path = Storage.__get_data_filename(key) if os.path.exists(file_path): os.remove(file_path) - def __get_list(self, key): - data = self.get(key) + def __get_list(key): + data = Storage.get(key) if not data: data = [] return data # 向列表中添加元素 - def list_push(self, key, item): - while self.__is_locked(key): + def list_push(key, item): + while Storage.__is_locked(key): time.sleep(0.01) - self.__lock(key) + Storage.__lock(key) try: - data = self.__get_list(key) + data = Storage.__get_list(key) data.append(item) - self.__set(key, data) - self.__unlock(key) + Storage.__set(key, data) + Storage.__unlock(key) except Exception as e: - self.__unlock(key) + Storage.__unlock(key) raise e # 从列表中删除和返回最后一个元素 - def list_pop(self, key): - while self.__is_locked(key): + def list_pop(key): + while Storage.__is_locked(key): time.sleep(0.01) - self.__lock(key) + Storage.__lock(key) try: - data = self.__get_list(key) + data = Storage.__get_list(key) item = data.pop() - self.__set(key, data) - self.__unlock(key) + Storage.__set(key, data) + Storage.__unlock(key) return item except Exception as e: - self.__unlock(key) + Storage.__unlock(key) raise e # 从列表中删除和返回第一个元素 - def list_shift(self, key): - while self.__is_locked(key): + def list_shift(key): + while Storage.__is_locked(key): time.sleep(0.01) - self.__lock(key) + Storage.__lock(key) try: - data = self.__get_list(key) + data = Storage.__get_list(key) item = data.pop(0) - self.__set(key, data) - self.__unlock(key) + Storage.__set(key, data) + Storage.__unlock(key) return item except Exception as e: - self.__unlock(key) + Storage.__unlock(key) raise e # 从列表中删除指定元素 - def list_remove(self, key, index): - while self.__is_locked(key): + def list_remove(key, index): + while Storage.__is_locked(key): time.sleep(0.01) - self.__lock(key) - data = self.__get_list(key) + Storage.__lock(key) + data = Storage.__get_list(key) data.pop(index) - self.__set(key, data) - self.__unlock(key) + Storage.__set(key, data) + Storage.__unlock(key) # 获取列表中指定位置的元素 - def list_get(self, key, index): - data = self.__get_list(key) + def list_get(key, index): + data = Storage.__get_list(key) return data[index] # 清空列表中的所有元素 - def list_clear(self, key): - while self.__is_locked(key): + def list_clear(key): + while Storage.__is_locked(key): time.sleep(0.01) - self.__lock(key) + Storage.__lock(key) try: - self.__set(key, []) - self.__unlock(key) + Storage.__set(key, []) + Storage.__unlock(key) except Exception as e: - self.__unlock(key) + Storage.__unlock(key) raise e diff --git a/scripts/physton_prompt/styles.py b/scripts/physton_prompt/styles.py index 9c73124..efb7866 100644 --- a/scripts/physton_prompt/styles.py +++ b/scripts/physton_prompt/styles.py @@ -1,7 +1,6 @@ import os from scripts.physton_prompt.storage import Storage -storage = Storage() styles_path = os.path.dirname(os.path.abspath(__file__)) + '/../../styles' styles_path = os.path.normpath(styles_path) @@ -58,7 +57,7 @@ def get_extension_css_list(): 'manifest': manifest, 'style': f'extensions/{dir}/style.min.css', } - css_item['selected'] = storage.get(css_item['dataName']) + css_item['selected'] = Storage.get(css_item['dataName']) css_list.append(css_item) return css_list diff --git a/tests/privacy_api_config.py b/tests/privacy_api_config.py index 4a2e0e1..42fca1c 100644 --- a/tests/privacy_api_config.py +++ b/tests/privacy_api_config.py @@ -3,9 +3,8 @@ import sys sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from scripts.physton_prompt.storage import Storage from scripts.physton_prompt.get_translate_apis import privacy_translate_api_config, unprotected_translate_api_config -st = Storage() key = 'translate_api.volcengine' -data = st.get(key) +data = Storage.get(key) data = privacy_translate_api_config(key, data) print(data) data = unprotected_translate_api_config(key, data) diff --git a/tests/translate.py b/tests/translate.py index 59e317d..35350ad 100644 --- a/tests/translate.py +++ b/tests/translate.py @@ -9,7 +9,6 @@ from scripts.physton_prompt.get_translate_apis import get_translate_apis from scripts.physton_prompt.storage import Storage i18n = get_i18n() -st = Storage() text = 'Hello World, I am a boy' tested_file = os.path.join(os.path.dirname(__file__), 'tested.json') @@ -37,7 +36,7 @@ def add_tested(api_key, from_lang, to_lang, translated_text): def test_api(api): print(f"开始测试 {api['name']}") config_name = 'translate_api.' + api['key'] - config = st.get(config_name) + config = Storage.get(config_name) if not config: config = {} for lang_code in api['support']: