Replace file operations with functions

main
bashrc 2026-04-29 15:06:29 +01:00
parent c10291fa12
commit 5818c78c4e
3 changed files with 110 additions and 131 deletions

View File

@ -21,6 +21,8 @@ from posts import get_user_url
from follow import unfollow_account from follow import unfollow_account
from person import get_actor_json from person import get_actor_json
from data import load_list from data import load_list
from data import save_string
from data import append_string
def _move_following_handles_for_account(base_dir: str, 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' acct_dir(base_dir, nickname, domain) + '/refollow.txt'
# unfollow the old handle # unfollow the old handle
with open(following_filename, 'w+', encoding='utf-8') as fp_foll2: text = ''
for follow_handle in following_handles: for follow_handle in following_handles:
if follow_handle.strip("\n").strip("\r").lower() != \ if follow_handle.strip("\n").strip("\r").lower() != \
handle_lower: handle_lower:
fp_foll2.write(follow_handle) text += follow_handle
continue continue
handle_nickname = handle.split('@')[0] handle_nickname = handle.split('@')[0]
handle_domain = handle.split('@')[1] handle_domain = handle.split('@')[1]
unfollow_account(base_dir, nickname, domain, unfollow_account(base_dir, nickname, domain,
handle_nickname, handle_nickname,
handle_domain, handle_domain,
debug, group_account, 'following.txt') debug, group_account, 'following.txt')
ctr += 1 ctr += 1
print('Unfollowed ' + handle + ' who has moved to ' + print('Unfollowed ' + handle + ' who has moved to ' +
moved_to_handle) moved_to_handle)
# save the new handles to the refollow list # save the new handles to the refollow list
if os.path.isfile(refollow_filename): if os.path.isfile(refollow_filename):
try: append_string(moved_to_handle + '\n', refollow_filename,
with open(refollow_filename, 'a+', 'EX: ' +
encoding='utf-8') as fp_refoll:
fp_refoll.write(moved_to_handle + '\n')
except OSError:
print('EX: ' +
'_update_moved_handle unable to append ' + '_update_moved_handle unable to append ' +
refollow_filename) refollow_filename)
else: else:
try: save_string(moved_to_handle + '\n', refollow_filename,
with open(refollow_filename, 'w+', 'EX: _update_moved_handle unable to write ' +
encoding='utf-8') as fp_refoll: refollow_filename)
fp_refoll.write(moved_to_handle + '\n') save_string(text, following_filename,
except OSError: 'EX: _update_moved_handle unable to save ' +
print('EX: _update_moved_handle unable to write ' + following_filename)
refollow_filename)
followers_filename = \ followers_filename = \
acct_dir(base_dir, nickname, domain) + '/followers.txt' 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() handle_lower = handle.lower()
# remove followers who have moved # remove followers who have moved
try: text = ''
with open(followers_filename, 'w+', encoding='utf-8') as fp_foll4: for follower_handle in follower_handles:
for follower_handle in follower_handles: if follower_handle.strip("\n").strip("\r").lower() != handle_lower:
if follower_handle.strip("\n").strip("\r").lower() != \ text += follower_handle
handle_lower: else:
fp_foll4.write(follower_handle) ctr += 1
else: print('Removed follower who has moved ' + handle)
ctr += 1 save_string(text, followers_filename,
print('Removed follower who has moved ' + handle) 'EX: _update_moved_handle ' +
except OSError: 'unable to remove moved follower ' + handle)
print('EX: _update_moved_handle unable to remove moved follower ' +
handle)
return ctr return ctr

View File

@ -43,6 +43,9 @@ from threads import thread_with_trace
from webapp_hashtagswarm import store_hash_tags from webapp_hashtagswarm import store_hash_tags
from cache import clear_from_post_caches from cache import clear_from_post_caches
from data import load_list 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, 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)) index_filename + ' ' + str(ex))
return return
try: save_string(post_id + '\n', index_filename,
with open(index_filename, 'w+', encoding='utf-8') as fp_feeds: 'EX: _update_feeds_outbox_index unable to write ' +
fp_feeds.write(post_id + '\n') index_filename)
except OSError:
print('EX: _update_feeds_outbox_index unable to write ' +
index_filename)
def _save_arrived_time(post_filename: str, arrived: str) -> None: def _save_arrived_time(post_filename: str, arrived: str) -> None:
"""Saves the time when an rss post arrived to a file """Saves the time when an rss post arrived to a file
""" """
try: save_string(arrived, post_filename + '.arrived',
with open(post_filename + '.arrived', 'w+', 'EX: _save_arrived_time unable to write ' +
encoding='utf-8') as fp_arrived: post_filename + '.arrived')
fp_arrived.write(arrived)
except OSError:
print('EX: _save_arrived_time unable to write ' +
post_filename + '.arrived')
def _remove_control_characters(content: str) -> str: 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 # remove the corresponding index entries
if removals: if removals:
index_content = '' index_content: str = \
try: load_string(mirror_index_filename,
with open(mirror_index_filename, 'r', 'EX: _create_news_mirror unable to read ' +
encoding='utf-8') as fp_index: mirror_index_filename)
index_content = fp_index.read() if index_content is not None:
for remove_post_id in removals: for remove_post_id in removals:
index_content = \ index_content = \
index_content.replace(remove_post_id + '\n', '') index_content.replace(remove_post_id + '\n', '')
except OSError: save_string(index_content, mirror_index_filename,
print('EX: _create_news_mirror unable to read ' + 'EX: _create_news_mirror unable to write ' +
mirror_index_filename) 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)
mirror_article_dir = mirror_dir + '/' + post_id_number mirror_article_dir = mirror_dir + '/' + post_id_number
if os.path.isdir(mirror_article_dir): 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 # append the post Id number to the index file
if os.path.isfile(mirror_index_filename): if os.path.isfile(mirror_index_filename):
try: append_string(post_id_number + '\n', mirror_index_filename,
with open(mirror_index_filename, 'a+', 'EX: _create_news_mirror unable to append ' +
encoding='utf-8') as fp_index: mirror_index_filename)
fp_index.write(post_id_number + '\n')
except OSError:
print('EX: _create_news_mirror unable to append ' +
mirror_index_filename)
else: else:
try: save_string(post_id_number + '\n', mirror_index_filename,
with open(mirror_index_filename, 'w+', 'EX: _create_news_mirror unable to write ' +
encoding='utf-8') as fp_index: mirror_index_filename)
fp_index.write(post_id_number + '\n')
except OSError:
print('EX: _create_news_mirror unable to write ' +
mirror_index_filename)
return True return True

View File

@ -1327,14 +1327,13 @@ def reenable_account(base_dir: str, nickname: str, domain: str) -> None:
suspended_filename) suspended_filename)
if lines is None: if lines is None:
return return
try: text = ''
with open(suspended_filename, 'w+', encoding='utf-8') as fp_sus: for suspended in lines:
for suspended in lines: if suspended.strip('\n').strip('\r') != nickname:
if suspended.strip('\n').strip('\r') != nickname: text += suspended
fp_sus.write(suspended) save_string(text, suspended_filename,
except OSError as ex: 'EX: reenable_account unable to save ' +
print('EX: reenable_account unable to save ' + suspended_filename + ' [ex]')
suspended_filename + ' ' + str(ex))
account_dir = acct_dir(base_dir, nickname, domain) account_dir = acct_dir(base_dir, nickname, domain)
_unsuspend_media_for_account(base_dir, account_dir) _unsuspend_media_for_account(base_dir, account_dir)
@ -1482,14 +1481,13 @@ def _remove_tags_for_nickname(base_dir: str, nickname: str,
tag_filename) tag_filename)
if lines is None: if lines is None:
continue continue
try: text = ''
with open(tag_filename, 'w+', encoding='utf-8') as fp_tag: for tagline in lines:
for tagline in lines: if match_str not in tagline:
if match_str not in tagline: text += tagline
fp_tag.write(tagline) save_string(text, tag_filename,
except OSError: 'EX: _remove_tags_for_nickname unable to write ' +
print('EX: _remove_tags_for_nickname unable to write ' + tag_filename)
tag_filename)
def _remove_account_media(base_dir: str, nickname: str, domain: str) -> None: 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 return False
# remove the snooze entry if it has timed out # remove the snooze entry if it has timed out
replace_str = None replace_str = None
try:
with open(snoozed_filename, 'r', encoding='utf-8') as fp_snoozed: snoozed_list: list[str] = \
for line in fp_snoozed: load_list(snoozed_filename,
# is this the entry for the actor? 'EX: is_person_snoozed unable to read ' + snoozed_filename)
if line.startswith(snooze_actor + ' '): if snoozed_list is not None:
snoozed_time_str1 = line.split(' ')[1] for line in snoozed_list:
snoozed_time_str = remove_eol(snoozed_time_str1) # is this the entry for the actor?
# is there a time appended? if not line.startswith(snooze_actor + ' '):
if snoozed_time_str.isdigit(): continue
snoozed_time = int(snoozed_time_str) snoozed_time_str1 = line.split(' ')[1]
curr_time = get_current_time_int() snoozed_time_str = remove_eol(snoozed_time_str1)
# has the snooze timed out? # is there a time appended?
if int(curr_time - snoozed_time) > 60 * 60 * 24: if snoozed_time_str.isdigit():
replace_str = line snoozed_time = int(snoozed_time_str)
else: curr_time = get_current_time_int()
replace_str = line # has the snooze timed out?
break if int(curr_time - snoozed_time) > 60 * 60 * 24:
except OSError: replace_str = line
print('EX: is_person_snoozed unable to read ' + snoozed_filename) else:
replace_str = line
break
if replace_str: if replace_str:
content = load_string(snoozed_filename, content = load_string(snoozed_filename,
'EX: is_person_snoozed unable to read 2 ' + '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): if not text_in_file(snooze_actor + ' ', snoozed_filename):
return return
replace_str = None replace_str = None
try:
with open(snoozed_filename, 'r', encoding='utf-8') as fp_snoozed: snoozed_list: list[str] = \
for line in fp_snoozed: load_list(snoozed_filename,
if line.startswith(snooze_actor + ' '): 'EX: person_unsnooze unable to read ' + snoozed_filename)
replace_str = line if snoozed_list is not None:
break for line in snoozed_list:
except OSError: if line.startswith(snooze_actor + ' '):
print('EX: person_unsnooze unable to read ' + snoozed_filename) replace_str = line
break
if replace_str: if replace_str:
content = load_string(snoozed_filename, content = load_string(snoozed_filename,
'EX: person_unsnooze unable to read 2 ' + 'EX: person_unsnooze unable to read 2 ' +