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,11 +170,11 @@ 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]
@ -186,22 +188,17 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
# 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:
fp_refoll.write(moved_to_handle + '\n')
except OSError:
print('EX: _update_moved_handle unable to write ' +
refollow_filename) refollow_filename)
save_string(text, following_filename,
'EX: _update_moved_handle unable to save ' +
following_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() != \ if follower_handle.strip("\n").strip("\r").lower() != handle_lower:
handle_lower: text += follower_handle
fp_foll4.write(follower_handle)
else: else:
ctr += 1 ctr += 1
print('Removed follower who has moved ' + handle) print('Removed follower who has moved ' + handle)
except OSError: save_string(text, followers_filename,
print('EX: _update_moved_handle unable to remove moved follower ' + 'EX: _update_moved_handle ' +
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,23 +70,16 @@ 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')
except OSError:
print('EX: _update_feeds_outbox_index unable to write ' +
index_filename) 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:
fp_arrived.write(arrived)
except OSError:
print('EX: _save_arrived_time unable to write ' +
post_filename + '.arrived') post_filename + '.arrived')
@ -501,23 +497,16 @@ 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)
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_index_filename)
mirror_article_dir = mirror_dir + '/' + post_id_number mirror_article_dir = mirror_dir + '/' + post_id_number
@ -543,20 +532,12 @@ 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:
fp_index.write(post_id_number + '\n')
except OSError:
print('EX: _create_news_mirror unable to append ' +
mirror_index_filename) 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:
fp_index.write(post_id_number + '\n')
except OSError:
print('EX: _create_news_mirror unable to write ' +
mirror_index_filename) 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:
fp_sus.write(suspended) text += suspended
except OSError as ex: save_string(text, suspended_filename,
print('EX: reenable_account unable to save ' + 'EX: reenable_account unable to save ' +
suspended_filename + ' ' + str(ex)) suspended_filename + ' [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,13 +1481,12 @@ 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:
fp_tag.write(tagline) text += tagline
except OSError: save_string(text, tag_filename,
print('EX: _remove_tags_for_nickname unable to write ' + 'EX: _remove_tags_for_nickname unable to write ' +
tag_filename) tag_filename)
@ -1665,11 +1663,15 @@ 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,
'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? # is this the entry for the actor?
if line.startswith(snooze_actor + ' '): if not line.startswith(snooze_actor + ' '):
continue
snoozed_time_str1 = line.split(' ')[1] snoozed_time_str1 = line.split(' ')[1]
snoozed_time_str = remove_eol(snoozed_time_str1) snoozed_time_str = remove_eol(snoozed_time_str1)
# is there a time appended? # is there a time appended?
@ -1682,8 +1684,7 @@ def is_person_snoozed(base_dir: str, nickname: str, domain: str,
else: else:
replace_str = line replace_str = line
break break
except OSError:
print('EX: is_person_snoozed unable to read ' + snoozed_filename)
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,
'EX: person_unsnooze unable to read ' + snoozed_filename)
if snoozed_list is not None:
for line in snoozed_list:
if line.startswith(snooze_actor + ' '): if line.startswith(snooze_actor + ' '):
replace_str = line replace_str = line
break break
except OSError:
print('EX: person_unsnooze unable to read ' + snoozed_filename)
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 ' +