update config file
This commit is contained in:
@@ -1,17 +1,117 @@
|
||||
import os
|
||||
import json
|
||||
import deepl
|
||||
from pythonosc import osc_message_builder
|
||||
from pythonosc import udp_client
|
||||
import customtkinter
|
||||
|
||||
# global
|
||||
OSC_IP_ADDRESS = "127.0.0.1"
|
||||
OSC_PORT = 9000
|
||||
TARGET_LANG = "EN-US"
|
||||
CHOICE_TRANSLATOR = "DeepL"
|
||||
AUTH_KEY = None
|
||||
TRANSLATOR = None
|
||||
PATH_CONFIG = "./config.json"
|
||||
|
||||
# load config
|
||||
if os.path.isfile(PATH_CONFIG) is False:
|
||||
with open(PATH_CONFIG, 'w') as fp:
|
||||
config = {
|
||||
"OSC_IP_ADDRESS": "127.0.0.1",
|
||||
"OSC_PORT": 9000,
|
||||
"TARGET_LANG": "EN-US",
|
||||
"CHOICE_TRANSLATOR": "DeepL",
|
||||
"AUTH_KEY": None
|
||||
}
|
||||
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
|
||||
if AUTH_KEY is not None:
|
||||
TRANSLATOR = deepl.Translator(AUTH_KEY)
|
||||
|
||||
# GUI
|
||||
customtkinter.set_appearance_mode("System")
|
||||
customtkinter.set_default_color_theme("blue")
|
||||
|
||||
class ToplevelWindow_config(customtkinter.CTkToplevel):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.geometry(f"{280}x{150}")
|
||||
self.resizable(False, False)
|
||||
|
||||
self.title("Config")
|
||||
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.entry_ip_address = customtkinter.CTkEntry(self, placeholder_text=OSC_IP_ADDRESS)
|
||||
self.entry_ip_address.grid(row=0, column=2, 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.grid(row=0, column=3, columnspan=1, padx=1, pady=5, sticky="nsew")
|
||||
|
||||
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.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.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.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.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.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")
|
||||
|
||||
def update_ip_address(self):
|
||||
global OSC_IP_ADDRESS
|
||||
OSC_IP_ADDRESS = self.entry_ip_address.get()
|
||||
with open(PATH_CONFIG, "r") as fp:
|
||||
config = json.load(fp)
|
||||
config["OSC_IP_ADDRESS"] = OSC_IP_ADDRESS
|
||||
with open(PATH_CONFIG, "w") as fp:
|
||||
json.dump(config, fp, indent=4)
|
||||
|
||||
def update_port(self):
|
||||
global OSC_PORT
|
||||
OSC_PORT = self.entry_port.get()
|
||||
with open(PATH_CONFIG, "r") as fp:
|
||||
config = json.load(fp)
|
||||
config["OSC_PORT"] = OSC_PORT
|
||||
with open(PATH_CONFIG, "w") as fp:
|
||||
json.dump(config, fp, indent=4)
|
||||
|
||||
def update_authkey(self):
|
||||
global AUTH_KEY
|
||||
global TRANSLATOR
|
||||
AUTH_KEY = self.entry_authkey.get()
|
||||
with open(PATH_CONFIG, "r") as fp:
|
||||
config = json.load(fp)
|
||||
config["AUTH_KEY"] = AUTH_KEY
|
||||
with open(PATH_CONFIG, "w") as fp:
|
||||
json.dump(config, fp, indent=4)
|
||||
try:
|
||||
TRANSLATOR = deepl.Translator(AUTH_KEY)
|
||||
except:
|
||||
TRANSLATOR = None
|
||||
|
||||
class App(customtkinter.CTk):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self.title("VRC ChatBox translator")
|
||||
self.title("VRC ChatBox Translator")
|
||||
self.geometry(f"{400}x{180}")
|
||||
self.grid_columnconfigure(1, weight=1)
|
||||
self.grid_rowconfigure(0, weight=1)
|
||||
@@ -24,8 +124,9 @@ class App(customtkinter.CTk):
|
||||
self.combobox_translator.grid(row=0, column=0, padx=10, pady=(10, 5), sticky="w")
|
||||
self.combobox_language = customtkinter.CTkComboBox(self.sidebar_frame, command=self.combobox_language_callback)
|
||||
self.combobox_language.grid(row=1, column=0, padx=10, pady=(5, 10), sticky="w")
|
||||
self.token_input_button = customtkinter.CTkButton(self.sidebar_frame, text="auth key", command=self.open_token_input_button_event)
|
||||
self.token_input_button.grid(row=5, column=0, padx=10, pady=(10, 10), sticky="s")
|
||||
self.button_config = customtkinter.CTkButton(self.sidebar_frame, text="config", command=self.open_config)
|
||||
self.button_config.grid(row=4, column=0, padx=10, pady=(10, 10), sticky="s")
|
||||
self.config_window = None
|
||||
|
||||
# create textbox
|
||||
self.textbox = customtkinter.CTkTextbox(self)
|
||||
@@ -36,70 +137,69 @@ class App(customtkinter.CTk):
|
||||
self.entry.grid(row=1, column=1, columnspan=2, padx=(10, 10), pady=(5, 10), sticky="nsew")
|
||||
|
||||
# set default values
|
||||
self.combobox_translator.configure(values=["DeepL", "None"],)
|
||||
self.combobox_translator.configure(values=["DeepL", "Disable"],)
|
||||
self.combobox_language.configure(values=[
|
||||
"EN-US","JA","BG","CS","DA","DE","EL","EN","EN-GB","ES","ET","FI","FR","HU",
|
||||
"JA","BG","CS","DA","DE","EL","EN","EN-US","EN-GB","ES","ET","FI","FR","HU",
|
||||
"ID","IT","KO","LT","LV","NB","NL","PL","PT","PT-BR","PT-PT","RO","RU","SK",
|
||||
"SL","SV","TR","UK","ZH",
|
||||
],)
|
||||
self.entry.bind("<Return>", self.press_key)
|
||||
|
||||
# setting OSC
|
||||
self.ip_address = "127.0.0.1"
|
||||
self.port = 9000
|
||||
if TRANSLATOR is None:
|
||||
self.textbox.insert("0.0", f"Auth Keyを設定してください\n")
|
||||
|
||||
# setting DeepL
|
||||
try:
|
||||
with open('./setting.json', 'r') as fp:
|
||||
self.auth_key = json.load(fp)["auth_key"]
|
||||
self.translator = deepl.Translator(self.auth_key)
|
||||
except:
|
||||
self.textbox.insert("0.0", f"auth key を設定してください\n")
|
||||
self.combobox_language.set(TARGET_LANG)
|
||||
self.combobox_translator.set(CHOICE_TRANSLATOR)
|
||||
|
||||
self.target_lang = 'EN-US'
|
||||
self.combobox_language.set(self.target_lang)
|
||||
self.choice_translator = 'DeepL'
|
||||
self.combobox_translator.set(self.choice_translator)
|
||||
|
||||
def open_config(self):
|
||||
if self.config_window is None or not self.config_window.winfo_exists():
|
||||
self.config_window = ToplevelWindow_config(self)
|
||||
self.config_window.focus()
|
||||
|
||||
def combobox_translator_callback(self, choice):
|
||||
self.choice_translator = choice
|
||||
global CHOICE_TRANSLATOR
|
||||
CHOICE_TRANSLATOR = choice
|
||||
with open(PATH_CONFIG, "r") as fp:
|
||||
config = json.load(fp)
|
||||
config["CHOICE_TRANSLATOR"] = CHOICE_TRANSLATOR
|
||||
with open(PATH_CONFIG, "w") as fp:
|
||||
json.dump(config, fp, indent=4)
|
||||
|
||||
def combobox_language_callback(self, choice):
|
||||
self.target_lang = choice
|
||||
|
||||
def open_token_input_button_event(self):
|
||||
dialog = customtkinter.CTkInputDialog(text="Type auth key:", title="auth key")
|
||||
input = dialog.get_input()
|
||||
|
||||
if input != "":
|
||||
with open('./setting.json', 'w') as fp:
|
||||
json.dump({'auth_key': input}, fp, indent=4)
|
||||
|
||||
self.auth_key = input
|
||||
self.translator = deepl.Translator(self.auth_key)
|
||||
global TARGET_LANG
|
||||
TARGET_LANG = choice
|
||||
with open(PATH_CONFIG, "r") as fp:
|
||||
config = json.load(fp)
|
||||
config["TARGET_LANG"] = TARGET_LANG
|
||||
with open(PATH_CONFIG, "w") as fp:
|
||||
json.dump(config, fp, indent=4)
|
||||
|
||||
def press_key(self, event):
|
||||
entry = self.entry.get()
|
||||
|
||||
# translate
|
||||
if self.choice_translator != "None":
|
||||
result = self.translator.translate_text(entry, target_lang=self.target_lang)
|
||||
chat_message = f"{entry} ({result.text})"
|
||||
if TRANSLATOR is None:
|
||||
self.textbox.insert("0.0", f"Auth Keyを設定してください\n")
|
||||
else:
|
||||
chat_message = f"{entry}"
|
||||
entry = self.entry.get()
|
||||
|
||||
# send OSC message
|
||||
message = osc_message_builder.OscMessageBuilder(address="/chatbox/input")
|
||||
message.add_arg(f"{chat_message}")
|
||||
message.add_arg(True)
|
||||
message.add_arg(True)
|
||||
message = message.build()
|
||||
client = udp_client.SimpleUDPClient(self.ip_address, self.port)
|
||||
client.send(message)
|
||||
# translate
|
||||
if CHOICE_TRANSLATOR != "Disable":
|
||||
result = TRANSLATOR.translate_text(entry, target_lang=TARGET_LANG)
|
||||
chat_message = f"{entry} ({result.text})"
|
||||
else:
|
||||
chat_message = f"{entry}"
|
||||
|
||||
# delete Entry message
|
||||
self.textbox.insert("0.0", f"{chat_message}\n")
|
||||
self.entry.delete(0, customtkinter.END)
|
||||
# send OSC message
|
||||
message = osc_message_builder.OscMessageBuilder(address="/chatbox/input")
|
||||
message.add_arg(f"{chat_message}")
|
||||
message.add_arg(True)
|
||||
message.add_arg(True)
|
||||
message = message.build()
|
||||
client = udp_client.SimpleUDPClient(OSC_IP_ADDRESS, OSC_PORT)
|
||||
client.send(message)
|
||||
|
||||
# delete Entry message
|
||||
self.textbox.insert("0.0", f"{chat_message}\n")
|
||||
self.entry.delete(0, customtkinter.END)
|
||||
|
||||
app = App()
|
||||
app.mainloop()
|
||||
Reference in New Issue
Block a user