Update settings.py

master
melia19791012 2024-04-23 21:37:07 +08:00 committed by GitHub
parent 8072620ede
commit 41dbb749a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 173 additions and 130 deletions

View File

@ -1,19 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#encoding=utf-8 #encoding=utf-8
try: try:
# for Python2 import tkinter.font as tkfont
import tkMessageBox as messagebox from tkinter import *
import ttk from tkinter import messagebox, ttk
from Tkinter import * from tkinter.filedialog import asksaveasfilename
except ImportError: except Exception as e:
# for Python3 pass
try:
import tkinter.font as tkfont
from tkinter import *
from tkinter import messagebox, ttk
from tkinter.filedialog import asksaveasfilename
except Exception as e:
pass
import asyncio import asyncio
import base64 import base64
@ -41,7 +34,7 @@ try:
except Exception as exc: except Exception as exc:
pass pass
CONST_APP_VERSION = "MaxBot (2024.04.09)" CONST_APP_VERSION = "MaxBot (2024.04.10)"
CONST_MAXBOT_ANSWER_ONLINE_FILE = "MAXBOT_ONLINE_ANSWER.txt" CONST_MAXBOT_ANSWER_ONLINE_FILE = "MAXBOT_ONLINE_ANSWER.txt"
CONST_MAXBOT_CONFIG_FILE = "settings.json" CONST_MAXBOT_CONFIG_FILE = "settings.json"
@ -694,8 +687,11 @@ def get_default_config():
def read_last_url_from_file(): def read_last_url_from_file():
ret = "" ret = ""
if os.path.exists(CONST_MAXBOT_LAST_URL_FILE): if os.path.exists(CONST_MAXBOT_LAST_URL_FILE):
with open(CONST_MAXBOT_LAST_URL_FILE, "r") as text_file: try:
ret = text_file.readline() with open(CONST_MAXBOT_LAST_URL_FILE, "r") as text_file:
ret = text_file.readline()
except Exception as e:
pass
return ret return ret
def load_json(): def load_json():
@ -706,13 +702,16 @@ def load_json():
config_dict = None config_dict = None
if os.path.isfile(config_filepath): if os.path.isfile(config_filepath):
with open(config_filepath) as json_data: try:
config_dict = json.load(json_data) with open(config_filepath) as json_data:
config_dict = json.load(json_data)
except Exception as e:
pass
else: else:
config_dict = get_default_config() config_dict = get_default_config()
return config_filepath, config_dict return config_filepath, config_dict
def btn_restore_defaults_clicked(language_code): def btn_restore_defaults_clicked():
app_root = util.get_app_root() app_root = util.get_app_root()
config_filepath = os.path.join(app_root, CONST_MAXBOT_CONFIG_FILE) config_filepath = os.path.join(app_root, CONST_MAXBOT_CONFIG_FILE)
if os.path.exists(str(config_filepath)): if os.path.exists(str(config_filepath)):
@ -723,6 +722,8 @@ def btn_restore_defaults_clicked(language_code):
pass pass
config_dict = get_default_config() config_dict = get_default_config()
language_code = get_language_code_by_name(config_dict["language"])
messagebox.showinfo(translate[language_code]["restore_defaults"], translate[language_code]["done"]) messagebox.showinfo(translate[language_code]["restore_defaults"], translate[language_code]["done"])
global root global root
@ -731,8 +732,11 @@ def btn_restore_defaults_clicked(language_code):
def do_maxbot_idle(): def do_maxbot_idle():
app_root = util.get_app_root() app_root = util.get_app_root()
idle_filepath = os.path.join(app_root, CONST_MAXBOT_INT28_FILE) idle_filepath = os.path.join(app_root, CONST_MAXBOT_INT28_FILE)
with open(CONST_MAXBOT_INT28_FILE, "w") as text_file: try:
text_file.write("") with open(CONST_MAXBOT_INT28_FILE, "w") as text_file:
text_file.write("")
except Exception as e:
pass
def btn_idle_clicked(language_code): def btn_idle_clicked(language_code):
do_maxbot_idle() do_maxbot_idle()
@ -748,7 +752,7 @@ def btn_resume_clicked(language_code):
do_maxbot_resume() do_maxbot_resume()
update_maxbot_runtime_status() update_maxbot_runtime_status()
def btn_launcher_clicked(language_code): def btn_launcher_clicked():
Root_Dir = "" Root_Dir = ""
save_ret = btn_save_act(slience_mode=True) save_ret = btn_save_act(slience_mode=True)
if save_ret: if save_ret:
@ -1087,7 +1091,6 @@ def btn_save_act(slience_mode=False):
# min value is 20 seconds. # min value is 20 seconds.
config_dict["advanced"]["reset_browser_interval"] = 20 config_dict["advanced"]["reset_browser_interval"] = 20
# save config. # save config.
if is_all_data_correct: if is_all_data_correct:
if not slience_mode: if not slience_mode:
@ -1104,7 +1107,7 @@ def btn_save_act(slience_mode=False):
return is_all_data_correct return is_all_data_correct
def btn_run_clicked(language_code): def btn_run_clicked():
print('run button pressed.') print('run button pressed.')
Root_Dir = "" Root_Dir = ""
save_ret = btn_save_act(slience_mode=True) save_ret = btn_save_act(slience_mode=True)
@ -1146,10 +1149,14 @@ def launch_maxbot():
def show_preview_text(): def show_preview_text():
if os.path.exists(CONST_MAXBOT_ANSWER_ONLINE_FILE): if os.path.exists(CONST_MAXBOT_ANSWER_ONLINE_FILE):
answer_text = "" answer_text = ""
with open(CONST_MAXBOT_ANSWER_ONLINE_FILE, "r") as text_file: try:
answer_text = text_file.readline() with open(CONST_MAXBOT_ANSWER_ONLINE_FILE, "r") as text_file:
answer_text = text_file.readline()
except Exception as e:
pass
answer_text = util.format_config_keyword_for_json(answer_text) if len(answer_text) > 0:
answer_text = util.format_config_keyword_for_json(answer_text)
date_array = [] date_array = []
try: try:
@ -1157,12 +1164,14 @@ def show_preview_text():
except Exception as exc: except Exception as exc:
date_array = [] date_array = []
global lbl_online_dictionary_preview_data if len(date_array) > 0:
if 'lbl_online_dictionary_preview_data' in globals(): preview_string = text=','.join(date_array)
try: global lbl_online_dictionary_preview_data
lbl_online_dictionary_preview_data.config(text=','.join(date_array)) if 'lbl_online_dictionary_preview_data' in globals():
except Exception as exc: try:
pass lbl_online_dictionary_preview_data.config(preview_string)
except Exception as exc:
pass
def btn_preview_text_clicked(): def btn_preview_text_clicked():
global txt_remote_url global txt_remote_url
@ -1182,13 +1191,14 @@ def btn_preview_text_clicked():
url_array = [] url_array = []
force_write = False force_write = False
if len(url_array)==1: if len(url_array)>0:
force_write = True if len(url_array)==1:
for each_url in url_array: force_write = True
#print("new_remote_url:", new_remote_url) for each_url in url_array:
is_write_to_file = util.save_url_to_file(each_url, CONST_MAXBOT_ANSWER_ONLINE_FILE, force_write=force_write) #print("new_remote_url:", new_remote_url)
if is_write_to_file: is_write_to_file = util.save_url_to_file(each_url, CONST_MAXBOT_ANSWER_ONLINE_FILE, force_write=force_write)
break if is_write_to_file:
break
show_preview_text() show_preview_text()
@ -2600,108 +2610,138 @@ def change_maxbot_status_by_keyword():
def check_maxbot_config_unsaved(config_dict): def check_maxbot_config_unsaved(config_dict):
# alert not saved config. # alert not saved config.
global combo_homepage selected_tab_index = -1
global combo_ticket_number
global txt_date_keyword global tabControl
global txt_area_keyword if 'tabControl' in globals():
global txt_keyword_exclude selected_tab_index = tabControl.index(tabControl.select())
global txt_idle_keyword if selected_tab_index==0:
global txt_resume_keyword global combo_homepage
global txt_idle_keyword_second global combo_ticket_number
global txt_resume_keyword_second
try: global txt_date_keyword
date_keyword = "" global txt_area_keyword
if 'txt_date_keyword' in globals(): global txt_keyword_exclude
date_keyword = txt_date_keyword.get("1.0",END).strip()
date_keyword = util.format_config_keyword_for_json(date_keyword)
area_keyword = "" global txt_date_keyword_highlightthickness
if 'txt_area_keyword' in globals(): if not 'txt_date_keyword_highlightthickness' in globals():
area_keyword = txt_area_keyword.get("1.0",END).strip() txt_date_keyword_highlightthickness = 0
area_keyword = util.format_config_keyword_for_json(area_keyword)
keyword_exclude = "" global txt_area_keyword_highlightthickness
if 'txt_keyword_exclude' in globals(): if not 'txt_area_keyword_highlightthickness' in globals():
keyword_exclude = txt_keyword_exclude.get("1.0",END).strip() txt_area_keyword_highlightthickness = 0
keyword_exclude = util.format_config_keyword_for_json(keyword_exclude)
idle_keyword = "" global txt_keyword_exclude_highlightthickness
if 'txt_idle_keyword' in globals(): if not 'txt_keyword_exclude_highlightthickness' in globals():
idle_keyword = txt_idle_keyword.get("1.0",END).strip() txt_keyword_exclude_highlightthickness = 0
idle_keyword = util.format_config_keyword_for_json(idle_keyword)
resume_keyword = "" try:
if 'txt_resume_keyword' in globals(): date_keyword = ""
resume_keyword = txt_resume_keyword.get("1.0",END).strip() if 'txt_date_keyword' in globals():
resume_keyword = util.format_config_keyword_for_json(resume_keyword) date_keyword = txt_date_keyword.get("1.0",END).strip()
date_keyword = util.format_config_keyword_for_json(date_keyword)
idle_keyword_second = "" area_keyword = ""
if 'txt_idle_keyword_second' in globals(): if 'txt_area_keyword' in globals():
idle_keyword_second = txt_idle_keyword_second.get("1.0",END).strip() area_keyword = txt_area_keyword.get("1.0",END).strip()
idle_keyword_second = util.format_config_keyword_for_json(idle_keyword_second) area_keyword = util.format_config_keyword_for_json(area_keyword)
resume_keyword_second = "" keyword_exclude = ""
if 'txt_resume_keyword_second' in globals(): if 'txt_keyword_exclude' in globals():
resume_keyword_second = txt_resume_keyword_second.get("1.0",END).strip() keyword_exclude = txt_keyword_exclude.get("1.0",END).strip()
resume_keyword_second = util.format_config_keyword_for_json(resume_keyword_second) keyword_exclude = util.format_config_keyword_for_json(keyword_exclude)
highlightthickness = 0 highlightthickness = 0
if 'combo_homepage' in globals(): if 'combo_homepage' in globals():
if len(combo_homepage.get().strip())>0: if len(combo_homepage.get().strip())>0:
if config_dict["homepage"] != combo_homepage.get().strip(): if config_dict["homepage"] != combo_homepage.get().strip():
highlightthickness = 2 highlightthickness = 2
if highlightthickness > 0: highlightthickness = 0
showHideBlocks() if 'combo_ticket_number' in globals():
if len(combo_ticket_number.get().strip())>0:
if config_dict["ticket_number"] != int(combo_ticket_number.get().strip()):
highlightthickness = 2
# fail, tkinter combobox border style is not working anymore
#combo_ticket_number.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0 highlightthickness = 0
if 'combo_ticket_number' in globals(): if config_dict["date_auto_select"]["date_keyword"] != date_keyword:
if len(combo_ticket_number.get().strip())>0: highlightthickness = 2
if config_dict["ticket_number"] != int(combo_ticket_number.get().strip()):
highlightthickness = 2
# fail, tkinter combobox border style is not working anymore
#combo_ticket_number.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0 if txt_date_keyword_highlightthickness != highlightthickness:
if config_dict["date_auto_select"]["date_keyword"] != date_keyword: txt_date_keyword_highlightthickness = highlightthickness
highlightthickness = 2 txt_date_keyword.config(highlightthickness=highlightthickness, highlightbackground="red")
txt_date_keyword.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0 highlightthickness = 0
if config_dict["area_auto_select"]["area_keyword"] != area_keyword: if config_dict["area_auto_select"]["area_keyword"] != area_keyword:
highlightthickness = 2 highlightthickness = 2
txt_area_keyword.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0 if txt_area_keyword_highlightthickness != highlightthickness:
if config_dict["keyword_exclude"] != keyword_exclude: txt_area_keyword_highlightthickness = highlightthickness
highlightthickness = 2 txt_area_keyword.config(highlightthickness=highlightthickness, highlightbackground="red")
txt_keyword_exclude.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0 highlightthickness = 0
if config_dict["advanced"]["idle_keyword"] != idle_keyword: if config_dict["keyword_exclude"] != keyword_exclude:
highlightthickness = 2 highlightthickness = 2
txt_idle_keyword.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0 if txt_keyword_exclude_highlightthickness != highlightthickness:
if config_dict["advanced"]["resume_keyword"] != resume_keyword: txt_keyword_exclude_highlightthickness = highlightthickness
highlightthickness = 2 txt_keyword_exclude.config(highlightthickness=highlightthickness, highlightbackground="red")
txt_resume_keyword.config(highlightthickness=highlightthickness, highlightbackground="red") except Exception as exc:
#print(exc)
pass
highlightthickness = 0 if selected_tab_index==5:
if config_dict["advanced"]["idle_keyword_second"] != idle_keyword_second: global txt_idle_keyword
highlightthickness = 2 global txt_resume_keyword
txt_idle_keyword_second.config(highlightthickness=highlightthickness, highlightbackground="red") global txt_idle_keyword_second
global txt_resume_keyword_second
highlightthickness = 0 try:
if config_dict["advanced"]["resume_keyword_second"] != resume_keyword_second: idle_keyword = ""
highlightthickness = 2 if 'txt_idle_keyword' in globals():
txt_resume_keyword_second.config(highlightthickness=highlightthickness, highlightbackground="red") idle_keyword = txt_idle_keyword.get("1.0",END).strip()
except Exception as exc: idle_keyword = util.format_config_keyword_for_json(idle_keyword)
#print(exc)
pass resume_keyword = ""
if 'txt_resume_keyword' in globals():
resume_keyword = txt_resume_keyword.get("1.0",END).strip()
resume_keyword = util.format_config_keyword_for_json(resume_keyword)
idle_keyword_second = ""
if 'txt_idle_keyword_second' in globals():
idle_keyword_second = txt_idle_keyword_second.get("1.0",END).strip()
idle_keyword_second = util.format_config_keyword_for_json(idle_keyword_second)
resume_keyword_second = ""
if 'txt_resume_keyword_second' in globals():
resume_keyword_second = txt_resume_keyword_second.get("1.0",END).strip()
resume_keyword_second = util.format_config_keyword_for_json(resume_keyword_second)
highlightthickness = 0
if config_dict["advanced"]["idle_keyword"] != idle_keyword:
highlightthickness = 2
txt_idle_keyword.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0
if config_dict["advanced"]["resume_keyword"] != resume_keyword:
highlightthickness = 2
txt_resume_keyword.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0
if config_dict["advanced"]["idle_keyword_second"] != idle_keyword_second:
highlightthickness = 2
txt_idle_keyword_second.config(highlightthickness=highlightthickness, highlightbackground="red")
highlightthickness = 0
if config_dict["advanced"]["resume_keyword_second"] != resume_keyword_second:
highlightthickness = 2
txt_resume_keyword_second.config(highlightthickness=highlightthickness, highlightbackground="red")
except Exception as exc:
#print(exc)
pass
def settgins_gui_timer(): def settgins_gui_timer():
while True: while True:
@ -2737,8 +2777,11 @@ def sync_status_to_extension(status):
status_json={} status_json={}
status_json["status"]=status status_json["status"]=status
#print("dump json to path:", target_path) #print("dump json to path:", target_path)
with open(target_path, 'w') as outfile: try:
json.dump(status_json, outfile) with open(target_path, 'w') as outfile:
json.dump(status_json, outfile)
except Exception as e:
pass
def update_maxbot_runtime_status(): def update_maxbot_runtime_status():
is_paused = False is_paused = False
@ -2989,19 +3032,19 @@ def get_action_bar(root, language_code):
global btn_restore_defaults global btn_restore_defaults
global btn_launcher global btn_launcher
btn_run = ttk.Button(frame_action, text=translate[language_code]['run'], command= lambda: btn_run_clicked(language_code)) btn_run = ttk.Button(frame_action, text=translate[language_code]['run'], command=btn_run_clicked)
btn_run.grid(column=0, row=0) btn_run.grid(column=0, row=0)
btn_save = ttk.Button(frame_action, text=translate[language_code]['save'], command= lambda: btn_save_clicked() ) btn_save = ttk.Button(frame_action, text=translate[language_code]['save'], command=btn_save_clicked)
btn_save.grid(column=1, row=0) btn_save.grid(column=1, row=0)
btn_exit = ttk.Button(frame_action, text=translate[language_code]['exit'], command=btn_exit_clicked) btn_exit = ttk.Button(frame_action, text=translate[language_code]['exit'], command=btn_exit_clicked)
#btn_exit.grid(column=2, row=0) #btn_exit.grid(column=2, row=0)
btn_launcher = ttk.Button(frame_action, text=translate[language_code]['config_launcher'], command= lambda: btn_launcher_clicked(language_code)) btn_launcher = ttk.Button(frame_action, text=translate[language_code]['config_launcher'], command= btn_launcher_clicked)
btn_launcher.grid(column=2, row=0) btn_launcher.grid(column=2, row=0)
btn_restore_defaults = ttk.Button(frame_action, text=translate[language_code]['restore_defaults'], command= lambda: btn_restore_defaults_clicked(language_code)) btn_restore_defaults = ttk.Button(frame_action, text=translate[language_code]['restore_defaults'], command= btn_restore_defaults_clicked)
btn_restore_defaults.grid(column=3, row=0) btn_restore_defaults.grid(column=3, row=0)
return frame_action return frame_action