Replace file operations with functions

main
bashrc 2026-04-29 14:23:57 +01:00
parent 721907ca4d
commit c10291fa12
1 changed files with 130 additions and 133 deletions

View File

@ -185,23 +185,23 @@ def no_of_followers_on_domain(base_dir: str, handle: str,
"""Returns the number of followers of the given handle from the """Returns the number of followers of the given handle from the
given domain given domain
""" """
filename = acct_handle_dir(base_dir, handle) + '/' + follow_file filename: str = acct_handle_dir(base_dir, handle) + '/' + follow_file
if not os.path.isfile(filename): if not os.path.isfile(filename):
return 0 return 0
ctr: int = 0 ctr: int = 0
try: followers_list: list[str] = \
with open(filename, 'r', encoding='utf-8') as fp_followers: load_list(filename,
for follower_handle in fp_followers: 'EX: no_of_followers_on_domain unable to read ' + filename +
' [ex]')
if followers_list is not None:
for follower_handle in followers_list:
if '@' not in follower_handle: if '@' not in follower_handle:
continue continue
follower_domain = follower_handle.split('@')[1] follower_domain = follower_handle.split('@')[1]
follower_domain = remove_eol(follower_domain) follower_domain = remove_eol(follower_domain)
if domain == follower_domain: if domain == follower_domain:
ctr += 1 ctr += 1
except OSError as exc:
print('EX: no_of_followers_on_domain unable to read ' + filename +
' ' + str(exc))
return ctr return ctr
@ -2274,13 +2274,10 @@ def regenerate_index_for_box(base_dir: str,
index_lines.sort(reverse=True) index_lines.sort(reverse=True)
result = '' result = ''
try:
with open(box_index_filename, 'w+', encoding='utf-8') as fp_box:
for line in index_lines: for line in index_lines:
result += line + '\n' result += line + '\n'
fp_box.write(line + '\n') save_string(result, box_index_filename,
except OSError: 'EX: unable to generate index for ' + box_name + ' ' + result)
print('EX: unable to generate index for ' + box_name + ' ' + result)
print('Index generated for ' + box_name + '\n' + result) print('Index generated for ' + box_name + '\n' + result)
@ -2883,9 +2880,12 @@ def create_report_post(base_dir: str,
moderators_list: list[str] = [] moderators_list: list[str] = []
moderators_file = data_dir(base_dir) + '/moderators.txt' moderators_file = data_dir(base_dir) + '/moderators.txt'
if os.path.isfile(moderators_file): if os.path.isfile(moderators_file):
try: moderators_list2: list[str] = \
with open(moderators_file, 'r', encoding='utf-8') as fp_mod: load_list(moderators_file,
for line in fp_mod: 'EX: create_report_post unable to read ' +
moderators_file)
if moderators_list2 is not None:
for line in moderators_list2:
line = line.strip('\n').strip('\r') line = line.strip('\n').strip('\r')
if line.startswith('#'): if line.startswith('#'):
continue continue
@ -2912,8 +2912,6 @@ def create_report_post(base_dir: str,
local_actor_url(http_prefix, line, domain_full) local_actor_url(http_prefix, line, domain_full)
if moderator_actor not in moderators_list: if moderator_actor not in moderators_list:
moderators_list.append(moderator_actor) moderators_list.append(moderator_actor)
except OSError:
print('EX: create_report_post unable to read ' + moderators_file)
if not moderators_list: if not moderators_list:
# if there are no moderators then the admin becomes the moderator # if there are no moderators then the admin becomes the moderator
admin_nickname = get_config_param(base_dir, 'admin') admin_nickname = get_config_param(base_dir, 'admin')
@ -3387,9 +3385,12 @@ def group_followers_by_domain(base_dir: str, nickname: str, domain: str) -> {}:
if not os.path.isfile(followers_filename): if not os.path.isfile(followers_filename):
return None return None
grouped = {} grouped = {}
try: followers_list: list[str] = \
with open(followers_filename, 'r', encoding='utf-8') as fp_foll: load_list(followers_filename,
for follower_handle in fp_foll: 'EX: group_followers_by_domain unable to read ' +
followers_filename)
if followers_list is not None:
for follower_handle in followers_list:
if '@' not in follower_handle: if '@' not in follower_handle:
continue continue
fhandle1 = follower_handle.strip() fhandle1 = follower_handle.strip()
@ -3399,9 +3400,6 @@ def group_followers_by_domain(base_dir: str, nickname: str, domain: str) -> {}:
grouped[follower_domain] = [fhandle] grouped[follower_domain] = [fhandle]
else: else:
grouped[follower_domain].append(fhandle) grouped[follower_domain].append(fhandle)
except OSError:
print('EX: group_followers_by_domain unable to read ' +
followers_filename)
return grouped return grouped
@ -5603,16 +5601,16 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
index_ctr: int = 0 index_ctr: int = 0
# get the existing index entries as a string # get the existing index entries as a string
new_index = '' new_index = ''
try: index_list: list[str] = \
with open(index_filename, 'r', encoding='utf-8') as fp_index: load_list(index_filename,
for post_id in fp_index: 'EX: archive_posts_for_person unable to read ' +
index_filename + ' [ex]')
if index_list is not None:
for post_id in index_list:
new_index += post_id new_index += post_id
index_ctr += 1 index_ctr += 1
if index_ctr >= max_posts_in_box: if index_ctr >= max_posts_in_box:
break break
except OSError as ex:
print('EX: archive_posts_for_person unable to read ' +
index_filename + ' ' + str(ex))
# save the new index file # save the new index file
if new_index: if new_index:
save_string(new_index, index_filename, save_string(new_index, index_filename,
@ -6195,10 +6193,12 @@ def populate_replies_json(base_dir: str, nickname: str, domain: str,
pub_str = 'https://www.w3.org/ns/activitystreams#Public' pub_str = 'https://www.w3.org/ns/activitystreams#Public'
# populate the items list with replies # populate the items list with replies
replies_boxes = ('outbox', 'inbox') replies_boxes = ('outbox', 'inbox')
try: replies_list: list[str] = \
with open(post_replies_filename, 'r', load_list(post_replies_filename,
encoding='utf-8') as fp_replies: 'EX: populate_replies_json unable to read ' +
for message_id in fp_replies: post_replies_filename)
if replies_list is not None:
for message_id in replies_list:
reply_found: bool = False reply_found: bool = False
# examine inbox and outbox # examine inbox and outbox
for boxname in replies_boxes: for boxname in replies_boxes:
@ -6252,9 +6252,6 @@ def populate_replies_json(base_dir: str, nickname: str, domain: str,
if authorized or \ if authorized or \
pub_str in pjo['object']['to']: pub_str in pjo['object']['to']:
ordered_items.append(pjo) ordered_items.append(pjo)
except OSError:
print('EX: populate_replies_json unable to read ' +
post_replies_filename)
def _reject_announce(announce_filename: str, def _reject_announce(announce_filename: str,