Replace file operations with functions

main
bashrc 2026-04-26 19:12:29 +01:00
parent 1e80710fb0
commit 2ff561c867
1 changed files with 58 additions and 83 deletions

View File

@ -87,6 +87,8 @@ from speaker import update_speaker
from webapp_post import individual_post_as_html from webapp_post import individual_post_as_html
from webapp_hashtagswarm import store_hash_tags from webapp_hashtagswarm import store_hash_tags
from data import save_string from data import save_string
from data import append_string
from data import load_string
def inbox_update_index(boxname: str, base_dir: str, handle: str, def inbox_update_index(boxname: str, base_dir: str, handle: str,
@ -161,20 +163,16 @@ def _notify_moved(base_dir: str, domain_full: str,
prev_actor_handle + ' ' + new_actor_handle + ' ' + url prev_actor_handle + ' ' + new_actor_handle + ' ' + url
if os.path.isfile(moved_file): if os.path.isfile(moved_file):
try: prev_moved_str = \
with open(moved_file, 'r', load_string(moved_file,
encoding='utf-8') as fp_move: 'EX: _notify_moved unable to read ' +
prev_moved_str = fp_move.read() moved_file)
if prev_moved_str == moved_str: if prev_moved_str:
continue if prev_moved_str == moved_str:
except OSError: continue
print('EX: _notify_moved unable to read ' + moved_file) save_string(moved_str, moved_file,
try: 'EX: ERROR: unable to save moved notification ' +
with open(moved_file, 'w+', encoding='utf-8') as fp_move: moved_file)
fp_move.write(moved_str)
except OSError:
print('EX: ERROR: unable to save moved notification ' +
moved_file)
break break
@ -277,29 +275,29 @@ def _person_receive_update(base_dir: str,
new_nickname and new_domain_full: new_nickname and new_domain_full:
new_actor = prev_nickname + '@' + prev_domain_full + ' ' + \ new_actor = prev_nickname + '@' + prev_domain_full + ' ' + \
new_nickname + '@' + new_domain_full new_nickname + '@' + new_domain_full
refollow_str = '' refollow_str: str = ''
refollow_filename = data_dir(base_dir) + '/actors_moved.txt' refollow_filename = data_dir(base_dir) + '/actors_moved.txt'
refollow_file_exists = False refollow_file_exists = False
if os.path.isfile(refollow_filename): if os.path.isfile(refollow_filename):
try: refollow_str = \
with open(refollow_filename, 'r', load_string(refollow_filename,
encoding='utf-8') as fp_refollow: 'EX: _person_receive_update unable to read ' +
refollow_str = fp_refollow.read() refollow_filename)
refollow_file_exists = True if refollow_str is not None:
except OSError: refollow_file_exists = True
print('EX: _person_receive_update unable to read ' +
refollow_filename)
if new_actor not in refollow_str: if new_actor not in refollow_str:
refollow_type = 'w+' refollow_type = 'w+'
if refollow_file_exists: if refollow_file_exists:
refollow_type = 'a+' refollow_type = 'a+'
try: if refollow_type == 'w+':
with open(refollow_filename, refollow_type, save_string(new_actor + '\n', refollow_filename,
encoding='utf-8') as fp_refollow: 'EX: ' +
fp_refollow.write(new_actor + '\n') '_person_receive_update unable to write to ' +
except OSError: refollow_filename)
print('EX: _person_receive_update unable to write to ' + else:
refollow_filename) append_string(new_actor + '\n', refollow_filename,
'EX: _person_receive_update ' +
'unable to write to ' + refollow_filename)
prev_avatar_url = \ prev_avatar_url = \
get_person_avatar_url(base_dir, person_json['id'], get_person_avatar_url(base_dir, person_json['id'],
person_cache) person_cache)
@ -897,26 +895,18 @@ def _like_notify(base_dir: str, domain: str,
# was there a previous like notification? # was there a previous like notification?
if os.path.isfile(prev_like_file): if os.path.isfile(prev_like_file):
# is it the same as the current notification ? # is it the same as the current notification ?
try: prev_like_str = \
with open(prev_like_file, 'r', encoding='utf-8') as fp_like: load_string(prev_like_file,
prev_like_str = fp_like.read() 'EX: _like_notify unable to read ' + prev_like_file)
if prev_like_str == like_str: if prev_like_str:
return if prev_like_str == like_str:
except OSError: return
print('EX: _like_notify unable to read ' + prev_like_file) save_string(like_str, prev_like_file,
try: 'EX: ERROR: unable to save previous like notification ' +
with open(prev_like_file, 'w+', encoding='utf-8') as fp_like: prev_like_file)
fp_like.write(like_str) save_string(like_str, like_file,
except OSError: 'EX: ERROR: unable to write like notification file ' +
print('EX: ERROR: unable to save previous like notification ' + like_file)
prev_like_file)
try:
with open(like_file, 'w+', encoding='utf-8') as fp_like:
fp_like.write(like_str)
except OSError:
print('EX: ERROR: unable to write like notification file ' +
like_file)
def _reaction_notify(base_dir: str, domain: str, onion_domain: str, def _reaction_notify(base_dir: str, domain: str, onion_domain: str,
@ -965,26 +955,19 @@ def _reaction_notify(base_dir: str, domain: str, onion_domain: str,
# was there a previous reaction notification? # was there a previous reaction notification?
if os.path.isfile(prev_reaction_file): if os.path.isfile(prev_reaction_file):
# is it the same as the current notification ? # is it the same as the current notification ?
try: prev_reaction_str = \
with open(prev_reaction_file, 'r', encoding='utf-8') as fp_react: load_string(prev_reaction_file,
prev_reaction_str = fp_react.read() 'EX: _reaction_notify unable to read ' +
if prev_reaction_str == reaction_str: prev_reaction_file)
return if prev_reaction_str is not None:
except OSError: if prev_reaction_str == reaction_str:
print('EX: _reaction_notify unable to read ' + prev_reaction_file) return
try: save_string(reaction_str, prev_reaction_file,
with open(prev_reaction_file, 'w+', encoding='utf-8') as fp_react: 'EX: ERROR: unable to save previous reaction notification ' +
fp_react.write(reaction_str) prev_reaction_file)
except OSError: save_string(reaction_str, reaction_file,
print('EX: ERROR: unable to save previous reaction notification ' + 'EX: ERROR: unable to write reaction notification file ' +
prev_reaction_file) reaction_file)
try:
with open(reaction_file, 'w+', encoding='utf-8') as fp_react:
fp_react.write(reaction_str)
except OSError:
print('EX: ERROR: unable to write reaction notification file ' +
reaction_file)
def receive_like(recent_posts_cache: {}, def receive_like(recent_posts_cache: {},
@ -2013,12 +1996,8 @@ def receive_announce(recent_posts_cache: {},
if mitm: if mitm:
post_filename_mitm = \ post_filename_mitm = \
post_filename.replace('.json', '') + '.mitm' post_filename.replace('.json', '') + '.mitm'
try: save_string('\n', post_filename_mitm,
with open(post_filename_mitm, 'w+', 'EX: unable to write mitm ' + post_filename_mitm)
encoding='utf-8') as fp_mitm:
fp_mitm.write('\n')
except OSError:
print('EX: unable to write mitm ' + post_filename_mitm)
minimize_all_images = False minimize_all_images = False
if nickname in min_images_for_accounts: if nickname in min_images_for_accounts:
minimize_all_images = True minimize_all_images = True
@ -2156,13 +2135,9 @@ def receive_announce(recent_posts_cache: {},
translate, lookup_actor, translate, lookup_actor,
theme_name, system_language, theme_name, system_language,
'inbox') 'inbox')
try: save_string('\n', post_filename + '.tts',
with open(post_filename + '.tts', 'w+', 'EX: unable to write recent post ' +
encoding='utf-8') as fp_tts: post_filename)
fp_tts.write('\n')
except OSError:
print('EX: unable to write recent post ' +
post_filename)
if debug: if debug:
print('DEBUG: Obtaining actor for announce post ' + print('DEBUG: Obtaining actor for announce post ' +