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