add Format
This commit is contained in:
@@ -6,42 +6,50 @@ from pythonosc import udp_client
|
|||||||
import customtkinter
|
import customtkinter
|
||||||
|
|
||||||
# global
|
# global
|
||||||
|
PATH_CONFIG = "./config.json"
|
||||||
OSC_IP_ADDRESS = "127.0.0.1"
|
OSC_IP_ADDRESS = "127.0.0.1"
|
||||||
OSC_PORT = 9000
|
OSC_PORT = 9000
|
||||||
TARGET_LANG = "EN-US"
|
TARGET_LANG = "EN-US"
|
||||||
CHOICE_TRANSLATOR = "DeepL"
|
CHOICE_TRANSLATOR = "DeepL"
|
||||||
AUTH_KEY = None
|
AUTH_KEY = None
|
||||||
TRANSLATOR = None
|
TRANSLATOR = None
|
||||||
PATH_CONFIG = "./config.json"
|
MESSAGE_FORMAT = "[message]([translation])"
|
||||||
|
|
||||||
# load config
|
# load config
|
||||||
if os.path.isfile(PATH_CONFIG) is False:
|
if os.path.isfile(PATH_CONFIG) is not False:
|
||||||
with open(PATH_CONFIG, 'w') as fp:
|
with open(PATH_CONFIG, 'r') as fp:
|
||||||
|
config = json.load(fp)
|
||||||
|
if "OSC_IP_ADDRESS" in config.keys():
|
||||||
|
OSC_IP_ADDRESS = config["OSC_IP_ADDRESS"]
|
||||||
|
if "OSC_PORT" in config.keys():
|
||||||
|
OSC_PORT = config["OSC_PORT"]
|
||||||
|
if "TARGET_LANG" in config.keys():
|
||||||
|
TARGET_LANG = config["TARGET_LANG"]
|
||||||
|
if "CHOICE_TRANSLATOR" in config.keys():
|
||||||
|
CHOICE_TRANSLATOR = config["CHOICE_TRANSLATOR"]
|
||||||
|
if "AUTH_KEY" in config.keys():
|
||||||
|
AUTH_KEY = config["AUTH_KEY"]
|
||||||
|
if "MESSAGE_FORMAT" in config.keys():
|
||||||
|
MESSAGE_FORMAT = config["MESSAGE_FORMAT"]
|
||||||
|
|
||||||
|
with open(PATH_CONFIG, 'w') as fp:
|
||||||
config = {
|
config = {
|
||||||
"OSC_IP_ADDRESS": "127.0.0.1",
|
"OSC_IP_ADDRESS": OSC_IP_ADDRESS,
|
||||||
"OSC_PORT": 9000,
|
"OSC_PORT": OSC_PORT,
|
||||||
"TARGET_LANG": "EN-US",
|
"TARGET_LANG": TARGET_LANG,
|
||||||
"CHOICE_TRANSLATOR": "DeepL",
|
"CHOICE_TRANSLATOR": CHOICE_TRANSLATOR,
|
||||||
"AUTH_KEY": None
|
"AUTH_KEY": AUTH_KEY,
|
||||||
|
"MESSAGE_FORMAT": MESSAGE_FORMAT,
|
||||||
}
|
}
|
||||||
json.dump(config, fp, indent=4)
|
json.dump(config, fp, indent=4)
|
||||||
|
|
||||||
with open(PATH_CONFIG, 'r') as fp:
|
|
||||||
config = json.load(fp)
|
|
||||||
if "OSC_IP_ADDRESS" in config.keys():
|
|
||||||
OSC_IP_ADDRESS = config["OSC_IP_ADDRESS"]
|
|
||||||
if "OSC_PORT" in config.keys():
|
|
||||||
OSC_PORT = config["OSC_PORT"]
|
|
||||||
if "TARGET_LANG" in config.keys():
|
|
||||||
TARGET_LANG = config["TARGET_LANG"]
|
|
||||||
if "CHOICE_TRANSLATOR" in config.keys():
|
|
||||||
CHOICE_TRANSLATOR = config["CHOICE_TRANSLATOR"]
|
|
||||||
if "AUTH_KEY" in config.keys():
|
|
||||||
AUTH_KEY = config["AUTH_KEY"]
|
|
||||||
|
|
||||||
# deepl connect
|
# deepl connect
|
||||||
if AUTH_KEY is not None:
|
if AUTH_KEY is not None:
|
||||||
TRANSLATOR = deepl.Translator(AUTH_KEY)
|
TRANSLATOR = deepl.Translator(AUTH_KEY)
|
||||||
|
try:
|
||||||
|
TRANSLATOR.translate_text(" ", target_lang="EN-US")
|
||||||
|
except:
|
||||||
|
TRANSLATOR = None
|
||||||
|
|
||||||
# GUI
|
# GUI
|
||||||
customtkinter.set_appearance_mode("System")
|
customtkinter.set_appearance_mode("System")
|
||||||
@@ -50,30 +58,37 @@ customtkinter.set_default_color_theme("blue")
|
|||||||
class ToplevelWindow_config(customtkinter.CTkToplevel):
|
class ToplevelWindow_config(customtkinter.CTkToplevel):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.geometry(f"{280}x{150}")
|
self.geometry(f"{450}x{160}")
|
||||||
self.resizable(False, False)
|
self.resizable(False, False)
|
||||||
|
|
||||||
self.title("Config")
|
self.title("Config")
|
||||||
self.label_ip_address = customtkinter.CTkLabel(self, text="OSC IP address:", fg_color="transparent")
|
self.label_ip_address = customtkinter.CTkLabel(self, text="OSC IP address:", fg_color="transparent")
|
||||||
self.label_ip_address.grid(row=0, column=0, columnspan=1, padx=5, pady=5, sticky="nsew")
|
self.label_ip_address.grid(row=0, column=0, columnspan=1, padx=5, pady=5, sticky="nsew")
|
||||||
self.entry_ip_address = customtkinter.CTkEntry(self, placeholder_text=OSC_IP_ADDRESS)
|
self.entry_ip_address = customtkinter.CTkEntry(self, width=300, placeholder_text=OSC_IP_ADDRESS)
|
||||||
self.entry_ip_address.grid(row=0, column=2, columnspan=1, padx=1, pady=5, sticky="nsew")
|
self.entry_ip_address.grid(row=0, column=1, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||||
self.button_ip_address = customtkinter.CTkButton(self, text="✓", width=1, command=self.update_ip_address)
|
self.button_ip_address = customtkinter.CTkButton(self, text="✓", width=1, command=self.update_ip_address)
|
||||||
self.button_ip_address.grid(row=0, column=3, columnspan=1, padx=1, pady=5, sticky="nsew")
|
self.button_ip_address.grid(row=0, column=2, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||||
|
|
||||||
self.label_port = customtkinter.CTkLabel(self, text="OSC Port:", fg_color="transparent")
|
self.label_port = customtkinter.CTkLabel(self, text="OSC Port:", fg_color="transparent")
|
||||||
self.label_port.grid(row=1, column=0, columnspan=1, padx=5, pady=5, sticky="nsew")
|
self.label_port.grid(row=1, column=0, columnspan=1, padx=5, pady=5, sticky="nsew")
|
||||||
self.entry_port = customtkinter.CTkEntry(self, placeholder_text=OSC_PORT)
|
self.entry_port = customtkinter.CTkEntry(self, placeholder_text=OSC_PORT)
|
||||||
self.entry_port.grid(row=1, column=2, columnspan=1, padx=1, pady=5, sticky="nsew")
|
self.entry_port.grid(row=1, column=1, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||||
self.button_port = customtkinter.CTkButton(self, text="✓", width=1, command=self.update_port)
|
self.button_port = customtkinter.CTkButton(self, text="✓", width=1, command=self.update_port)
|
||||||
self.button_port.grid(row=1, column=3, columnspan=1, padx=1, pady=5, sticky="nsew")
|
self.button_port.grid(row=1, column=2, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||||
|
|
||||||
self.label_authkey = customtkinter.CTkLabel(self, text="DeepL Auth Key:", fg_color="transparent")
|
self.label_authkey = customtkinter.CTkLabel(self, text="DeepL Auth Key:", fg_color="transparent")
|
||||||
self.label_authkey.grid(row=2, column=0, columnspan=1, padx=5, pady=5, sticky="nsew")
|
self.label_authkey.grid(row=2, column=0, columnspan=1, padx=5, pady=5, sticky="nsew")
|
||||||
self.entry_authkey = customtkinter.CTkEntry(self, placeholder_text=AUTH_KEY)
|
self.entry_authkey = customtkinter.CTkEntry(self, placeholder_text=AUTH_KEY)
|
||||||
self.entry_authkey.grid(row=2, column=2, columnspan=1, padx=1, pady=5, sticky="nsew")
|
self.entry_authkey.grid(row=2, column=1, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||||
self.button_authkey = customtkinter.CTkButton(self, text="✓", width=1, command=self.update_authkey)
|
self.button_authkey = customtkinter.CTkButton(self, text="✓", width=1, command=self.update_authkey)
|
||||||
self.button_authkey.grid(row=2, column=3, columnspan=1, padx=1, pady=5, sticky="nsew")
|
self.button_authkey.grid(row=2, column=2, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||||
|
|
||||||
|
self.label_message_format = customtkinter.CTkLabel(self, text="Message Format:", fg_color="transparent")
|
||||||
|
self.label_message_format.grid(row=3, column=0, columnspan=1, padx=5, pady=5, sticky="nsew")
|
||||||
|
self.entry_message_format = customtkinter.CTkEntry(self, placeholder_text=MESSAGE_FORMAT)
|
||||||
|
self.entry_message_format.grid(row=3, column=1, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||||
|
self.button_message_format = customtkinter.CTkButton(self, text="✓", width=1, command=self.update_message_format)
|
||||||
|
self.button_message_format.grid(row=3, column=2, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||||
|
|
||||||
def update_ip_address(self):
|
def update_ip_address(self):
|
||||||
global OSC_IP_ADDRESS
|
global OSC_IP_ADDRESS
|
||||||
@@ -102,11 +117,22 @@ class ToplevelWindow_config(customtkinter.CTkToplevel):
|
|||||||
config["AUTH_KEY"] = AUTH_KEY
|
config["AUTH_KEY"] = AUTH_KEY
|
||||||
with open(PATH_CONFIG, "w") as fp:
|
with open(PATH_CONFIG, "w") as fp:
|
||||||
json.dump(config, fp, indent=4)
|
json.dump(config, fp, indent=4)
|
||||||
try:
|
|
||||||
TRANSLATOR = deepl.Translator(AUTH_KEY)
|
TRANSLATOR = deepl.Translator(AUTH_KEY)
|
||||||
|
try:
|
||||||
|
TRANSLATOR.translate_text(" ", target_lang="EN-US")
|
||||||
except:
|
except:
|
||||||
TRANSLATOR = None
|
TRANSLATOR = None
|
||||||
|
|
||||||
|
def update_message_format(self):
|
||||||
|
global MESSAGE_FORMAT
|
||||||
|
MESSAGE_FORMAT = self.entry_message_format.get()
|
||||||
|
with open(PATH_CONFIG, "r") as fp:
|
||||||
|
config = json.load(fp)
|
||||||
|
config["MESSAGE_FORMAT"] = MESSAGE_FORMAT
|
||||||
|
with open(PATH_CONFIG, "w") as fp:
|
||||||
|
json.dump(config, fp, indent=4)
|
||||||
|
|
||||||
class App(customtkinter.CTk):
|
class App(customtkinter.CTk):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@@ -146,7 +172,7 @@ class App(customtkinter.CTk):
|
|||||||
self.entry.bind("<Return>", self.press_key)
|
self.entry.bind("<Return>", self.press_key)
|
||||||
|
|
||||||
if TRANSLATOR is None:
|
if TRANSLATOR is None:
|
||||||
self.textbox.insert("0.0", f"Auth Keyを設定してください\n")
|
self.textbox.insert("0.0", f"Auth Keyを設定してないか間違っています\n")
|
||||||
|
|
||||||
self.combobox_language.set(TARGET_LANG)
|
self.combobox_language.set(TARGET_LANG)
|
||||||
self.combobox_translator.set(CHOICE_TRANSLATOR)
|
self.combobox_translator.set(CHOICE_TRANSLATOR)
|
||||||
@@ -177,14 +203,14 @@ class App(customtkinter.CTk):
|
|||||||
|
|
||||||
def press_key(self, event):
|
def press_key(self, event):
|
||||||
if TRANSLATOR is None:
|
if TRANSLATOR is None:
|
||||||
self.textbox.insert("0.0", f"Auth Keyを設定してください\n")
|
self.textbox.insert("0.0", f"Auth Keyを設定してないか間違っています\n")
|
||||||
else:
|
else:
|
||||||
entry = self.entry.get()
|
entry = self.entry.get()
|
||||||
|
|
||||||
# translate
|
# translate
|
||||||
if CHOICE_TRANSLATOR != "Disable":
|
if CHOICE_TRANSLATOR != "Disable":
|
||||||
result = TRANSLATOR.translate_text(entry, target_lang=TARGET_LANG)
|
result = TRANSLATOR.translate_text(entry, target_lang=TARGET_LANG)
|
||||||
chat_message = f"{entry} ({result.text})"
|
chat_message = MESSAGE_FORMAT.replace("[message]", entry).replace("[translation]", result.text)
|
||||||
else:
|
else:
|
||||||
chat_message = f"{entry}"
|
chat_message = f"{entry}"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user