auto-schedule-pro-v2: add clear option to remove custom setting value and clean up old code remnants

This commit is contained in:
dymik739 2023-09-12 09:52:38 +03:00
parent a22fb2b4b1
commit 05a8039b18
2 changed files with 32 additions and 66 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
config/*
modules/irc-bridge/error.log
__pycache__/
modules/auto-schedule-pro-v2/preference-db

View File

@ -79,6 +79,28 @@ def set_preference_by_id(user_id, name, value):
writefile(f"preference-db/{user_id}.json", final_data)
def clear_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")
preferences = json.loads(raw_prefs)
except Exception as e:
preferences = {}
else:
preferences = {}
if name in preferences:
del preferences[name]
final_data = json.dumps(preferences)
writefile(f"preference-db/{user_id}.json", final_data)
def load_template(template, part):
return readfile(f"templates/{template}/{part}.msg")
@ -131,33 +153,6 @@ def get_name_of_lesson_type(lesson_type):
if lesson_type in lesson_types_to_strings:
return lesson_types_to_strings[lesson_type]
'''
def generate_lesson_description(lesson, start_datetime, end_datetime, current_day, current_week, overrides={},
custom_name_prefix="<b>Назва</b>"):
output_settings = {"name": True, "date": True, "teacher": True, "link": True, "comment": True}
output_settings.update(overrides)
result = ""
if output_settings['name']:
result += f"{custom_name_prefix}: {escaped_string_html(lesson['name'])} ({escaped_string_html(get_name_of_lesson_type(lesson['type']))})\n"
if output_settings['date']:
human_readable_date = get_human_readable_date(start_datetime, end_datetime,
current_day, current_week)
result += f"<b>Дата</b>: {escaped_string_html(human_readable_date)}\n"
if output_settings['teacher']:
result += f"<b>Викладач</b>: {escaped_string_html(lesson['teacher'])}\n"
if output_settings['link']:
result += f"<b>Посилання</b>: {escaped_string_html(lesson['link'])}\n"
if output_settings['comment'] and 'comment' in lesson:
result += f"<b>Примітка</b>: {escaped_string_html(lesson['comment'])}\n"
return result
'''
def generate_lesson_description(lesson, start_datetime, end_datetime, current_day, current_week, overrides={},
custom_name_prefix="Назва", template="legacy-vibrant"):
@ -201,6 +196,7 @@ def generate_lesson_description(lesson, start_datetime, end_datetime, current_da
return total_result
def get_schedule_data_from(filename):
raw_schedule = json.loads(readfile(filename))
@ -253,41 +249,6 @@ def get_lesson_description(schedule, reference_time, lesson_time, current_day, c
lesson_start_datetime = datetime.fromtimestamp(reference_time + lesson_time)
lesson_end_datetime = datetime.fromtimestamp(reference_time + lesson_time + 5400)
'''
if lesson_record.__class__ == dict:
if force_date_at_top:
user_defined_overrides = dict(overrides)
internal_overrides = dict(overrides)
internal_overrides['date'] = False
description = generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime, current_day,
current_week, overrides=internal_overrides)
if 'date' in user_defined_overrides and not user_defined_overrides['date']:
return description
else:
human_readable_date = get_human_readable_date(lesson_start_datetime, lesson_end_datetime,
current_day, current_week)
return f"<b><u>{human_readable_date.capitalize()}</u></b>:\n" + description
else:
return generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime, current_day,
current_week, overrides=overrides)
elif lesson_record.__class__ == list:
user_defined_overrides = dict(overrides)
internal_overrides = dict(overrides)
internal_overrides['date'] = False
descriptions = [generate_lesson_description(i, lesson_start_datetime, lesson_end_datetime, current_day,
current_week, overrides=internal_overrides, custom_name_prefix=custom_name_prefix) for i in lesson_record]
if 'date' in user_defined_overrides and not user_defined_overrides['date']:
return "\n".join(descriptions)
else:
human_readable_date = get_human_readable_date(lesson_start_datetime, lesson_end_datetime,
current_day, current_week)
return f"<b><u>{human_readable_date.capitalize()}</u></b>:\n" + "\n".join(descriptions)
'''
return generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime, current_day,
current_week, overrides=overrides, custom_name_prefix=custom_name_prefix, template=template)
@ -366,12 +327,12 @@ def process(message, path):
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":
elif base_command == "!schedule-ctl" and len(full_command) >= 2:
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 full_command[1] == "set":
elif full_command[1] == "set" and len(full_command) == 4:
prefs = get_all_preferences_by_id(message.from_user.id)
if full_command[2] in prefs:
@ -389,9 +350,13 @@ def process(message, path):
else:
return f"Такого налаштування не існує; переглянути наявні налаштування можна за допомогою команди <u>!schedule-ctl list</u>", "HTML"
elif full_command[1] == "get":
elif full_command[1] == "get" and len(full_command) == 3:
requested_preference = get_preference_by_id(message.from_user.id, full_command[2])
return f"Налаштування {full_command[2]} має значення {requested_preference}", "HTML"
elif full_command[1] == "clear" and len(full_command) == 3:
reset_preference_by_id(message.from_user.id, full_command[2])
return f"Очищено значення для налаштування {full_command[2]}, надалі для нього використовуватиметься стандартне значення", "HTML"
else:
return "Такої команди не існує", "HTML"
return "Такої команди не існує (або був використаний помилковий синтаксис)", "HTML"