From 05a8039b1884d3f42edd35ee7a56745284a6c3a9 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Tue, 12 Sep 2023 09:52:38 +0300 Subject: [PATCH] auto-schedule-pro-v2: add clear option to remove custom setting value and clean up old code remnants --- .gitignore | 1 + modules/auto-schedule-pro-v2/main.py | 97 +++++++++------------------- 2 files changed, 32 insertions(+), 66 deletions(-) diff --git a/.gitignore b/.gitignore index 4d75c18..b250f05 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ config/* modules/irc-bridge/error.log __pycache__/ +modules/auto-schedule-pro-v2/preference-db diff --git a/modules/auto-schedule-pro-v2/main.py b/modules/auto-schedule-pro-v2/main.py index e5561f9..22dcbac 100644 --- a/modules/auto-schedule-pro-v2/main.py +++ b/modules/auto-schedule-pro-v2/main.py @@ -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="Назва"): - 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"Дата: {escaped_string_html(human_readable_date)}\n" - - if output_settings['teacher']: - result += f"Викладач: {escaped_string_html(lesson['teacher'])}\n" - - if output_settings['link']: - result += f"Посилання: {escaped_string_html(lesson['link'])}\n" - - if output_settings['comment'] and 'comment' in lesson: - result += f"Примітка: {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"{human_readable_date.capitalize()}:\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"{human_readable_date.capitalize()}:\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"Пари у {WEEKDAYS_ACCUSATIVE[selected_day % 7]}:\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"Такого налаштування не існує; переглянути наявні налаштування можна за допомогою команди !schedule-ctl list", "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"