|
import os |
|
import json |
|
import re |
|
from scripts.physton_prompt.storage import Storage |
|
st = Storage() |
|
|
|
|
|
|
|
translate_apis = {} |
|
|
|
|
|
|
|
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__)) |
|
config_file = os.path.join(current_dir, '../../translate_apis.json') |
|
config_file = os.path.normpath(config_file) |
|
with open(config_file, 'r', encoding='utf8') as f: |
|
translate_apis = json.load(f) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return translate_apis |
|
|
|
|
|
def privacy_translate_api_config(data_key, data): |
|
|
|
if not data or not isinstance(data, dict): |
|
return data |
|
|
|
api = None |
|
if data_key == 'chatgpt_key': |
|
api = 'openai' |
|
else: |
|
start = 'translate_api.' |
|
if not data_key.startswith(start): |
|
return data |
|
api = data_key[len(start):] |
|
apis = get_translate_apis() |
|
find = False |
|
for group in apis['apis']: |
|
for item in group['children']: |
|
if item['key'] == api: |
|
find = item |
|
break |
|
if not find: |
|
return data |
|
api_item = find |
|
if 'config' not in api_item or not api_item['config']: |
|
return data |
|
|
|
for config in api_item['config']: |
|
|
|
if 'privacy' in config and config['privacy'] and config['type'] == 'input': |
|
if config['key'] in data: |
|
|
|
value = data[config['key']] |
|
if len(value) > 6: |
|
value = value[:6] + '*' * (len(value) - 6) |
|
data[config['key']] = value |
|
|
|
return data |
|
|
|
def unprotected_translate_api_config(data_key, data): |
|
api = None |
|
if data_key == 'chatgpt_key': |
|
api = 'openai' |
|
else: |
|
start = 'translate_api.' |
|
if not data_key.startswith(start): |
|
return data |
|
api = data_key[len(start):] |
|
|
|
apis = get_translate_apis() |
|
find = False |
|
for group in apis['apis']: |
|
for item in group['children']: |
|
if item['key'] == api: |
|
find = item |
|
break |
|
if not find: |
|
return data |
|
api_item = find |
|
if 'config' not in api_item or not api_item['config']: |
|
return data |
|
|
|
storage_data = st.get(data_key) |
|
|
|
for config in api_item['config']: |
|
|
|
if 'privacy' in config and config['privacy'] and config['type'] == 'input': |
|
if storage_data and config['key'] in storage_data: |
|
if config['key'] in data: |
|
value = data[config['key']] |
|
|
|
if '*' in value and value[:6] == storage_data[config['key']][:6]: |
|
data[config['key']] = storage_data[config['key']] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data |