From 5818c78c4e4ee268e3660f713507d82678cd7247 Mon Sep 17 00:00:00 2001 From: bashrc Date: Wed, 29 Apr 2026 15:06:29 +0100 Subject: [PATCH] Replace file operations with functions --- migrate.py | 81 ++++++++++++++++++++++------------------------ newsdaemon.py | 71 +++++++++++++++------------------------- person.py | 89 ++++++++++++++++++++++++++------------------------- 3 files changed, 110 insertions(+), 131 deletions(-) diff --git a/migrate.py b/migrate.py index 3cf41ae98..a5303d519 100644 --- a/migrate.py +++ b/migrate.py @@ -21,6 +21,8 @@ from posts import get_user_url from follow import unfollow_account from person import get_actor_json from data import load_list +from data import save_string +from data import append_string def _move_following_handles_for_account(base_dir: str, @@ -168,40 +170,35 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str, acct_dir(base_dir, nickname, domain) + '/refollow.txt' # unfollow the old handle - with open(following_filename, 'w+', encoding='utf-8') as fp_foll2: - for follow_handle in following_handles: - if follow_handle.strip("\n").strip("\r").lower() != \ - handle_lower: - fp_foll2.write(follow_handle) - continue - handle_nickname = handle.split('@')[0] - handle_domain = handle.split('@')[1] - unfollow_account(base_dir, nickname, domain, - handle_nickname, - handle_domain, - debug, group_account, 'following.txt') - ctr += 1 - print('Unfollowed ' + handle + ' who has moved to ' + - moved_to_handle) + text = '' + for follow_handle in following_handles: + if follow_handle.strip("\n").strip("\r").lower() != \ + handle_lower: + text += follow_handle + continue + handle_nickname = handle.split('@')[0] + handle_domain = handle.split('@')[1] + unfollow_account(base_dir, nickname, domain, + handle_nickname, + handle_domain, + debug, group_account, 'following.txt') + ctr += 1 + print('Unfollowed ' + handle + ' who has moved to ' + + moved_to_handle) - # save the new handles to the refollow list - if os.path.isfile(refollow_filename): - try: - with open(refollow_filename, 'a+', - encoding='utf-8') as fp_refoll: - fp_refoll.write(moved_to_handle + '\n') - except OSError: - print('EX: ' + + # save the new handles to the refollow list + if os.path.isfile(refollow_filename): + append_string(moved_to_handle + '\n', refollow_filename, + 'EX: ' + '_update_moved_handle unable to append ' + refollow_filename) - else: - try: - with open(refollow_filename, 'w+', - encoding='utf-8') as fp_refoll: - fp_refoll.write(moved_to_handle + '\n') - except OSError: - print('EX: _update_moved_handle unable to write ' + - refollow_filename) + else: + save_string(moved_to_handle + '\n', refollow_filename, + 'EX: _update_moved_handle unable to write ' + + refollow_filename) + save_string(text, following_filename, + 'EX: _update_moved_handle unable to save ' + + following_filename) followers_filename = \ acct_dir(base_dir, nickname, domain) + '/followers.txt' @@ -216,18 +213,16 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str, handle_lower = handle.lower() # remove followers who have moved - try: - with open(followers_filename, 'w+', encoding='utf-8') as fp_foll4: - for follower_handle in follower_handles: - if follower_handle.strip("\n").strip("\r").lower() != \ - handle_lower: - fp_foll4.write(follower_handle) - else: - ctr += 1 - print('Removed follower who has moved ' + handle) - except OSError: - print('EX: _update_moved_handle unable to remove moved follower ' + - handle) + text = '' + for follower_handle in follower_handles: + if follower_handle.strip("\n").strip("\r").lower() != handle_lower: + text += follower_handle + else: + ctr += 1 + print('Removed follower who has moved ' + handle) + save_string(text, followers_filename, + 'EX: _update_moved_handle ' + + 'unable to remove moved follower ' + handle) return ctr diff --git a/newsdaemon.py b/newsdaemon.py index b5878b289..c1cfd0f79 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -43,6 +43,9 @@ from threads import thread_with_trace from webapp_hashtagswarm import store_hash_tags from cache import clear_from_post_caches from data import load_list +from data import load_string +from data import save_string +from data import append_string def _update_feeds_outbox_index(base_dir: str, domain: str, @@ -67,24 +70,17 @@ def _update_feeds_outbox_index(base_dir: str, domain: str, index_filename + ' ' + str(ex)) return - try: - with open(index_filename, 'w+', encoding='utf-8') as fp_feeds: - fp_feeds.write(post_id + '\n') - except OSError: - print('EX: _update_feeds_outbox_index unable to write ' + - index_filename) + save_string(post_id + '\n', index_filename, + 'EX: _update_feeds_outbox_index unable to write ' + + index_filename) def _save_arrived_time(post_filename: str, arrived: str) -> None: """Saves the time when an rss post arrived to a file """ - try: - with open(post_filename + '.arrived', 'w+', - encoding='utf-8') as fp_arrived: - fp_arrived.write(arrived) - except OSError: - print('EX: _save_arrived_time unable to write ' + - post_filename + '.arrived') + save_string(arrived, post_filename + '.arrived', + 'EX: _save_arrived_time unable to write ' + + post_filename + '.arrived') def _remove_control_characters(content: str) -> str: @@ -501,24 +497,17 @@ def _create_news_mirror(base_dir: str, domain: str, # remove the corresponding index entries if removals: - index_content = '' - try: - with open(mirror_index_filename, 'r', - encoding='utf-8') as fp_index: - index_content = fp_index.read() - for remove_post_id in removals: - index_content = \ - index_content.replace(remove_post_id + '\n', '') - except OSError: - print('EX: _create_news_mirror unable to read ' + - mirror_index_filename) - try: - with open(mirror_index_filename, 'w+', - encoding='utf-8') as fp_index: - fp_index.write(index_content) - except OSError: - print('EX: _create_news_mirror unable to write ' + - mirror_index_filename) + index_content: str = \ + load_string(mirror_index_filename, + 'EX: _create_news_mirror unable to read ' + + mirror_index_filename) + if index_content is not None: + for remove_post_id in removals: + index_content = \ + index_content.replace(remove_post_id + '\n', '') + save_string(index_content, mirror_index_filename, + 'EX: _create_news_mirror unable to write ' + + mirror_index_filename) mirror_article_dir = mirror_dir + '/' + post_id_number if os.path.isdir(mirror_article_dir): @@ -543,21 +532,13 @@ def _create_news_mirror(base_dir: str, domain: str, # append the post Id number to the index file if os.path.isfile(mirror_index_filename): - try: - with open(mirror_index_filename, 'a+', - encoding='utf-8') as fp_index: - fp_index.write(post_id_number + '\n') - except OSError: - print('EX: _create_news_mirror unable to append ' + - mirror_index_filename) + append_string(post_id_number + '\n', mirror_index_filename, + 'EX: _create_news_mirror unable to append ' + + mirror_index_filename) else: - try: - with open(mirror_index_filename, 'w+', - encoding='utf-8') as fp_index: - fp_index.write(post_id_number + '\n') - except OSError: - print('EX: _create_news_mirror unable to write ' + - mirror_index_filename) + save_string(post_id_number + '\n', mirror_index_filename, + 'EX: _create_news_mirror unable to write ' + + mirror_index_filename) return True diff --git a/person.py b/person.py index 8896b64e8..89a544fd1 100644 --- a/person.py +++ b/person.py @@ -1327,14 +1327,13 @@ def reenable_account(base_dir: str, nickname: str, domain: str) -> None: suspended_filename) if lines is None: return - try: - with open(suspended_filename, 'w+', encoding='utf-8') as fp_sus: - for suspended in lines: - if suspended.strip('\n').strip('\r') != nickname: - fp_sus.write(suspended) - except OSError as ex: - print('EX: reenable_account unable to save ' + - suspended_filename + ' ' + str(ex)) + text = '' + for suspended in lines: + if suspended.strip('\n').strip('\r') != nickname: + text += suspended + save_string(text, suspended_filename, + 'EX: reenable_account unable to save ' + + suspended_filename + ' [ex]') account_dir = acct_dir(base_dir, nickname, domain) _unsuspend_media_for_account(base_dir, account_dir) @@ -1482,14 +1481,13 @@ def _remove_tags_for_nickname(base_dir: str, nickname: str, tag_filename) if lines is None: continue - try: - with open(tag_filename, 'w+', encoding='utf-8') as fp_tag: - for tagline in lines: - if match_str not in tagline: - fp_tag.write(tagline) - except OSError: - print('EX: _remove_tags_for_nickname unable to write ' + - tag_filename) + text = '' + for tagline in lines: + if match_str not in tagline: + text += tagline + save_string(text, tag_filename, + 'EX: _remove_tags_for_nickname unable to write ' + + tag_filename) def _remove_account_media(base_dir: str, nickname: str, domain: str) -> None: @@ -1665,25 +1663,28 @@ def is_person_snoozed(base_dir: str, nickname: str, domain: str, return False # remove the snooze entry if it has timed out replace_str = None - try: - with open(snoozed_filename, 'r', encoding='utf-8') as fp_snoozed: - for line in fp_snoozed: - # is this the entry for the actor? - if line.startswith(snooze_actor + ' '): - snoozed_time_str1 = line.split(' ')[1] - snoozed_time_str = remove_eol(snoozed_time_str1) - # is there a time appended? - if snoozed_time_str.isdigit(): - snoozed_time = int(snoozed_time_str) - curr_time = get_current_time_int() - # has the snooze timed out? - if int(curr_time - snoozed_time) > 60 * 60 * 24: - replace_str = line - else: - replace_str = line - break - except OSError: - print('EX: is_person_snoozed unable to read ' + snoozed_filename) + + snoozed_list: list[str] = \ + load_list(snoozed_filename, + 'EX: is_person_snoozed unable to read ' + snoozed_filename) + if snoozed_list is not None: + for line in snoozed_list: + # is this the entry for the actor? + if not line.startswith(snooze_actor + ' '): + continue + snoozed_time_str1 = line.split(' ')[1] + snoozed_time_str = remove_eol(snoozed_time_str1) + # is there a time appended? + if snoozed_time_str.isdigit(): + snoozed_time = int(snoozed_time_str) + curr_time = get_current_time_int() + # has the snooze timed out? + if int(curr_time - snoozed_time) > 60 * 60 * 24: + replace_str = line + else: + replace_str = line + break + if replace_str: content = load_string(snoozed_filename, 'EX: is_person_snoozed unable to read 2 ' + @@ -1731,14 +1732,16 @@ def person_unsnooze(base_dir: str, nickname: str, domain: str, if not text_in_file(snooze_actor + ' ', snoozed_filename): return replace_str = None - try: - with open(snoozed_filename, 'r', encoding='utf-8') as fp_snoozed: - for line in fp_snoozed: - if line.startswith(snooze_actor + ' '): - replace_str = line - break - except OSError: - print('EX: person_unsnooze unable to read ' + snoozed_filename) + + snoozed_list: list[str] = \ + load_list(snoozed_filename, + 'EX: person_unsnooze unable to read ' + snoozed_filename) + if snoozed_list is not None: + for line in snoozed_list: + if line.startswith(snooze_actor + ' '): + replace_str = line + break + if replace_str: content = load_string(snoozed_filename, 'EX: person_unsnooze unable to read 2 ' +