auto-schedule-pro-v2: hotfixing many issues at once to make the module work properly

This commit is contained in:
dymik739 2023-09-11 22:29:52 +03:00
parent 1381ddb007
commit a22fb2b4b1
3 changed files with 36 additions and 32 deletions

View File

@ -60,7 +60,7 @@ class ModuleV1:
return self.RESPONSE, self.FORMAT
except Exception as e:
print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"")
print(f"[ERROR] module v1: traceback:\ntraceback.format_exc()")
print(f"[ERROR] module v1: traceback:\n{traceback.format_exc()}")
return "", None

View File

@ -1,6 +1,6 @@
from datetime import datetime
import json
import os
def readfile(filename):
with open(module_path + filename) as f:
@ -44,14 +44,14 @@ def get_preference_by_id(user_id, name):
if not name in preferences:
return None
return preferences['name']
return preferences[name]
def get_all_preferences_by_id(user_id):
user_preferences = {"output-style": "legacy-vibrant"}
for i in default_preferences:
override = get_preferences_by_id(i)
for i in user_preferences:
override = get_preference_by_id(user_id, i)
if override != None:
user_preferences[i] = override
@ -62,6 +62,8 @@ def set_preference_by_id(user_id, name, value):
if not os.path.exists(module_path + "preference-db/"):
os.mkdir(module_path + "preference-db/")
preferences = {}
if os.path.exists(module_path + f"preference-db/{user_id}.json"):
try:
raw_prefs = readfile(f"preference-db/{user_id}.json")
@ -74,7 +76,7 @@ def set_preference_by_id(user_id, name, value):
preferences[name] = value
final_data = json.dumps(preferences)
writefile(final_data)
writefile(f"preference-db/{user_id}.json", final_data)
def load_template(template, part):
@ -166,7 +168,7 @@ def generate_lesson_description(lesson, start_datetime, end_datetime, current_da
if lesson.__class__ == dict:
active_template = load_template(template, "single")
for i in ['name', 'teacher', 'link', 'comment']:
for i in ['name', 'teacher', 'link']:
active_template = active_template.replace(f"%{i.upper()}%", lesson[i])
@ -188,11 +190,11 @@ def generate_lesson_description(lesson, start_datetime, end_datetime, current_da
for l in lesson:
active_template = load_template(template, "multiple")
for i in ['name', 'teacher', 'link', 'comment']:
active_template = active_template.replace(f"%{i.upper()}%", escaped_string(lesson[i]))
for i in ['name', 'teacher', 'link']:
active_template = active_template.replace(f"%{i.upper()}%", l[i])
active_template = active_template.replace("%DATE%", human_readable_date)
active_template = active_template.replace("%TYPE%", get_name_of_lesson_type(lesson['type']))
active_template = active_template.replace("%TYPE%", get_name_of_lesson_type(l['type']))
active_template = active_template.replace("%NAME_PREFIX%", custom_name_prefix)
total_result += active_template + "\n"
@ -288,7 +290,7 @@ def get_lesson_description(schedule, reference_time, lesson_time, current_day, c
'''
return generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime, current_day,
current_week, overrides=overrides, custom_name_prefix=custom_name_prefix)
current_week, overrides=overrides, custom_name_prefix=custom_name_prefix, template=template)
def process(message, path):
@ -319,7 +321,9 @@ def process(message, path):
reference_time = int(current_time.strftime("%s")) - current_seconds
output_style_preference = get_preference_by_id(message['from']['id'], "output-style")
output_style_preference = get_preference_by_id(message.from_user.id, "output-style")
if output_style_preference == None:
output_style_preference = "legacy-vibrant"
if base_command == "!пара":
# easter egg
@ -360,34 +364,34 @@ def process(message, path):
current_week, overrides=preferences, custom_name_prefix="Назва", template=output_style_preference)
for lesson_time in lesson_list]
return f"<b><u>Пари у {WEEKDAYS_ACCUSATIVE[selected_day % 7]}</u></b>:\n\n\n" + "\n\n".join(lesson_descriptions_list), "HTML"
return f"<b><u>Пари у {WEEKDAYS_ACCUSATIVE[selected_day % 7]}</u></b>:\n\n\n" + "\n".join(lesson_descriptions_list), "HTML"
elif base_command == "!schedule-ctl":
if base_command[1] == "list":
prefs = get_all_preferences_by_id(message['from']['id'])
return "Ваші персональні налаштування:\n" + [f"- {k} = {v}" for k, v in prefs.items()]
if full_command[1] == "list":
prefs = get_all_preferences_by_id(message.from_user.id)
return "Ваші персональні налаштування:\n" + '\n'.join([f"- {k} = {v}" for k, v in prefs.items()]), "HTML"
elif base_command[1] == "set":
prefs = get_all_preferences_by_id(message['from']['id'])
elif full_command[1] == "set":
prefs = get_all_preferences_by_id(message.from_user.id)
if base_command[2] in prefs:
if base_command[2] == "output-style":
if base_command[3] not in os.listdir(module_path + "templates/"):
return f"Стилю {base_command[3]} не існує; доступні варіанти: " \
if full_command[2] in prefs:
if full_command[2] == "output-style":
if full_command[3] not in os.listdir(module_path + "templates/"):
return f"Стилю {full_command[3]} не існує; доступні варіанти: " \
+ ', '.join(os.listdir(module_path + "templates/")), "HTML"
previous_value = prefs[base_command[2]]
prefs[base_command[2]] = base_command[3]
previous_value = prefs[full_command[2]]
prefs[full_command[2]] = full_command[3]
set_preference_by_id(message['from']['id'], base_command[2], base_command[3])
set_preference_by_id(message.from_user.id, full_command[2], full_command[3])
return f"Змінено значення {base_command[2]}: {previous_value} -> {base_command[3]}", "HTML"
return f"Змінено значення {full_command[2]}: {previous_value} -> {full_command[3]}", "HTML"
else:
return f"Такого налаштування не існує; переглянути наявні налаштування можна за допомогою команди <u>!schedule-ctl list</u>", "HTML"
elif base_command[1] == "get":
requested_preference = get_preference_by_id(message['from']['id'], base_command[2])
return f"Налаштування {base_command[2]} має значення {requested_preference}", "HTML"
elif full_command[1] == "get":
requested_preference = get_preference_by_id(message.from_user.id, full_command[2])
return f"Налаштування {full_command[2]} має значення {requested_preference}", "HTML"
else:
return "Такої команди не існує", "HTML"

View File

@ -1 +1 @@
%DATE%:
<b><u>%DATE%</u></b>: