Add exception handling when saving files

main
Bob Mottram 2024-02-01 13:30:59 +00:00
parent 966ec3389f
commit 83d5dde8ec
9 changed files with 288 additions and 136 deletions

View File

@ -190,8 +190,8 @@ def _mark_post_as_read(actor: str, post_id: str, post_category: str) -> None:
read_file.write(post_id + content)
except OSError as ex:
print('EX: Failed to mark post as read 1 ' + str(ex))
else
try::
else:
try:
with open(read_posts_filename, 'w+',
encoding='utf-8') as read_file:
read_file.write(post_id + '\n')

View File

@ -158,32 +158,49 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
# save the new handles to the refollow list
if os.path.isfile(refollow_filename):
with open(refollow_filename, 'a+',
encoding='utf-8') as refoll:
refoll.write(moved_to_handle + '\n')
try:
with open(refollow_filename, 'a+',
encoding='utf-8') as refoll:
refoll.write(moved_to_handle + '\n')
except OSError:
print('EX: ' +
'_update_moved_handle unable to append ' +
refollow_filename)
else:
with open(refollow_filename, 'w+',
encoding='utf-8') as refoll:
refoll.write(moved_to_handle + '\n')
try:
with open(refollow_filename, 'w+',
encoding='utf-8') as refoll:
refoll.write(moved_to_handle + '\n')
except OSError:
print('EX: _update_moved_handle unable to write ' +
refollow_filename)
followers_filename = \
acct_dir(base_dir, nickname, domain) + '/followers.txt'
if os.path.isfile(followers_filename):
follower_handles = []
with open(followers_filename, 'r', encoding='utf-8') as foll3:
follower_handles = foll3.readlines()
try:
with open(followers_filename, 'r', encoding='utf-8') as foll3:
follower_handles = foll3.readlines()
except OSError:
print('EX: _update_moved_handle unable to read ' +
followers_filename)
handle_lower = handle.lower()
# remove followers who have moved
with open(followers_filename, 'w+', encoding='utf-8') as foll4:
for follower_handle in follower_handles:
if follower_handle.strip("\n").strip("\r").lower() != \
handle_lower:
foll4.write(follower_handle)
else:
ctr += 1
print('Removed follower who has moved ' + handle)
try:
with open(followers_filename, 'w+', encoding='utf-8') as foll4:
for follower_handle in follower_handles:
if follower_handle.strip("\n").strip("\r").lower() != \
handle_lower:
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)
return ctr

View File

@ -43,20 +43,32 @@ def _notify_on_post_arrival(base_dir: str, nickname: str, domain: str,
following_handles = ''
if os.path.isfile(notify_on_post_filename):
print('notify file exists')
with open(notify_on_post_filename, 'r',
encoding='utf-8') as calendar_file:
following_handles = calendar_file.read()
try:
with open(notify_on_post_filename, 'r',
encoding='utf-8') as calendar_file:
following_handles = calendar_file.read()
except OSError:
print('EX: _notify_on_post_arrival unable to read 1 ' +
notify_on_post_filename)
else:
# create a new notifyOnPost file from the following file
print('Creating notifyOnPost file ' + notify_on_post_filename)
following_handles = ''
with open(following_filename, 'r',
encoding='utf-8') as following_file:
following_handles = following_file.read()
try:
with open(following_filename, 'r',
encoding='utf-8') as following_file:
following_handles = following_file.read()
except OSError:
print('EX: _notify_on_post_arrival unable to read 2 ' +
following_filename)
if add:
with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles + handle + '\n')
try:
with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles + handle + '\n')
except OSError:
print('EX: _notify_on_post_arrival unable to write 1' +
notify_on_post_filename)
# already in the notifyOnPost file?
if handle + '\n' in following_handles or \
@ -74,18 +86,26 @@ def _notify_on_post_arrival(base_dir: str, nickname: str, domain: str,
new_following_handles += followed + '\n'
following_handles = new_following_handles
with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles)
try:
with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles)
except OSError:
print('EX: _notify_on_post_arrival unable to write 2' +
notify_on_post_filename)
else:
print(handle + ' not in notifyOnPost.txt')
# not already in the notifyOnPost file
if add:
# append to the list of handles
following_handles += handle + '\n'
with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles)
try:
with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles)
except OSError:
print('EX: _notify_on_post_arrival unable to write 3' +
notify_on_post_filename)
def add_notify_on_post(base_dir: str, nickname: str, domain: str,
@ -118,7 +138,11 @@ def notify_when_person_posts(base_dir: str, nickname: str, domain: str,
handle = following_nickname + '@' + following_domain
if not os.path.isfile(notify_on_post_filename):
# create a new notifyOnPost file
with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write('')
try:
with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write('')
except OSError:
print('EX: notify_when_person_posts unable to write ' +
notify_on_post_filename)
return text_in_file(handle + '\n', notify_on_post_filename, False)

View File

@ -2786,13 +2786,21 @@ def thread_send_post(session, post_json_str: str, federation_list: [],
# save the log file
post_log_filename = base_dir + '/post.log'
if os.path.isfile(post_log_filename):
with open(post_log_filename, 'a+',
encoding='utf-8') as log_file:
log_file.write(log_str + '\n')
try:
with open(post_log_filename, 'a+',
encoding='utf-8') as log_file:
log_file.write(log_str + '\n')
except OSError:
print('EX: thread_send_post unable to append ' +
post_log_filename)
else:
with open(post_log_filename, 'w+',
encoding='utf-8') as log_file:
log_file.write(log_str + '\n')
try:
with open(post_log_filename, 'w+',
encoding='utf-8') as log_file:
log_file.write(log_str + '\n')
except OSError:
print('EX: thread_send_post unable to write ' +
post_log_filename)
if post_result:
_remove_send_block(base_dir, nickname, domain, inbox_url)
@ -5108,16 +5116,25 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
index_ctr = 0
# get the existing index entries as a string
new_index = ''
with open(index_filename, 'r', encoding='utf-8') as index_file:
for post_id in index_file:
new_index += post_id
index_ctr += 1
if index_ctr >= max_posts_in_box:
break
try:
with open(index_filename, 'r', encoding='utf-8') as index_file:
for post_id in index_file:
new_index += post_id
index_ctr += 1
if index_ctr >= max_posts_in_box:
break
except OSError as ex:
print('EX: archive_posts_for_person unable to read ' +
index_filename + ' ' + str(ex))
# save the new index file
if len(new_index) > 0:
with open(index_filename, 'w+', encoding='utf-8') as index_file:
index_file.write(new_index)
try:
with open(index_filename, 'w+',
encoding='utf-8') as index_file:
index_file.write(new_index)
except OSError:
print('EX: archive_posts_for_person unable to write ' +
index_filename)
posts_in_box_dict = {}
posts_ctr = 0
@ -5559,9 +5576,13 @@ def check_domains(session, base_dir: str,
update_follower_warnings = True
if update_follower_warnings and follower_warning_str:
with open(follower_warning_filename, 'w+',
encoding='utf-8') as fp_warn:
fp_warn.write(follower_warning_str)
try:
with open(follower_warning_filename, 'w+',
encoding='utf-8') as fp_warn:
fp_warn.write(follower_warning_str)
except OSError:
print('EX: check_domains unable to write ' +
follower_warning_filename)
if not single_check:
print(follower_warning_str)
@ -5640,9 +5661,13 @@ def _reject_announce(announce_filename: str,
# reject the post referenced by the announce activity object
if not os.path.isfile(announce_filename + '.reject'):
with open(announce_filename + '.reject', 'w+',
encoding='utf-8') as reject_announce_file:
reject_announce_file.write('\n')
try:
with open(announce_filename + '.reject', 'w+',
encoding='utf-8') as reject_announce_file:
reject_announce_file.write('\n')
except OSError:
print('EX: _reject_announce unable to write ' +
announce_filename + '.reject')
def download_announce(session, base_dir: str, http_prefix: str,

View File

@ -179,9 +179,14 @@ def _update_post_schedule(base_dir: str, handle: str, httpd,
# write the new schedule index file
schedule_index_file = \
acct_handle_dir(base_dir, handle) + '/schedule.index'
with open(schedule_index_file, 'w+', encoding='utf-8') as schedule_file:
for line in index_lines:
schedule_file.write(line)
try:
with open(schedule_index_file, 'w+',
encoding='utf-8') as schedule_file:
for line in index_lines:
schedule_file.write(line)
except OSError:
print('EX: _update_post_schedule unable to write ' +
schedule_index_file)
def run_post_schedule(base_dir: str, httpd, max_scheduled_posts: int):

View File

@ -1781,8 +1781,12 @@ def _generate_next_shares_token_update(base_dir: str,
next_update_sec = curr_time + next_update_interval
updated = True
if updated:
with open(token_update_filename, 'w+', encoding='utf-8') as fp_tok:
fp_tok.write(str(next_update_sec))
try:
with open(token_update_filename, 'w+', encoding='utf-8') as fp_tok:
fp_tok.write(str(next_update_sec))
except OSError:
print('EX: _generate_next_shares_token_update unable to write' +
token_update_filename)
def _regenerate_shares_token(base_dir: str, domain_full: str,

102
theme.py
View File

@ -397,8 +397,11 @@ def _set_theme_from_dict(base_dir: str, name: str,
css = set_css_param(css, 'language-direction', 'rtl')
filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
try:
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
except OSError:
print('EX: _set_theme_from_dict unable to write ' + filename)
screen_name = (
'login', 'follow', 'options', 'search', 'welcome'
@ -417,11 +420,14 @@ def _set_background_format(base_dir: str,
css_filename = base_dir + '/' + background_type + '.css'
if not os.path.isfile(css_filename):
return
with open(css_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read()
css = css.replace('background.jpg', 'background.' + extension)
with open(css_filename, 'w+', encoding='utf-8') as cssfile2:
cssfile2.write(css)
try:
with open(css_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read()
css = css.replace('background.jpg', 'background.' + extension)
with open(css_filename, 'w+', encoding='utf-8') as cssfile2:
cssfile2.write(css)
except OSError as ex:
print('EX: _set_background_format ' + css_filename + ' ' + str(ex))
def enable_grayscale(base_dir: str) -> None:
@ -432,19 +438,28 @@ def enable_grayscale(base_dir: str) -> None:
template_filename = base_dir + '/' + filename
if not os.path.isfile(template_filename):
continue
with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read()
if 'grayscale' not in css:
css = \
css.replace('body, html {',
'body, html {\n filter: grayscale(100%);')
filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
try:
with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read()
if 'grayscale' not in css:
css = \
css.replace('body, html {',
'body, html {\n' +
' filter: grayscale(100%);')
filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
except OSError as ex:
print('EX: enable_grayscale unable to read ' +
template_filename + ' ' + str(ex))
grayscale_filename = base_dir + '/accounts/.grayscale'
if not os.path.isfile(grayscale_filename):
with open(grayscale_filename, 'w+', encoding='utf-8') as grayfile:
grayfile.write(' ')
try:
with open(grayscale_filename, 'w+', encoding='utf-8') as grayfile:
grayfile.write(' ')
except OSError as ex:
print('EX: enable_grayscale unable to write ' +
grayscale_filename + ' ' + str(ex))
def disable_grayscale(base_dir: str) -> None:
@ -455,14 +470,18 @@ def disable_grayscale(base_dir: str) -> None:
template_filename = base_dir + '/' + filename
if not os.path.isfile(template_filename):
continue
with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read()
if 'grayscale' in css:
css = \
css.replace('\n filter: grayscale(100%);', '')
filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
try:
with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read()
if 'grayscale' in css:
css = \
css.replace('\n filter: grayscale(100%);', '')
filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
except OSError as ex:
print('EX: disable_grayscale unable to read ' +
template_filename + ' ' + str(ex))
grayscale_filename = base_dir + '/accounts/.grayscale'
if os.path.isfile(grayscale_filename):
try:
@ -488,8 +507,11 @@ def _set_dyslexic_font(base_dir: str) -> bool:
"') format('woff2')")
css = set_css_param(css, "*font-family", "'OpenDyslexic'")
filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
try:
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
except OSError:
print('EX: _set_dyslexic_font unable to write ' + filename)
return False
@ -528,8 +550,11 @@ def _set_custom_font(base_dir: str):
css = set_css_param(css, "*font-family", "'CustomFont'")
css = set_css_param(css, "header-font", "'CustomFont'")
filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
try:
with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css)
except OSError:
print('EX: _set_custom_font unable to write ' + filename)
def set_theme_from_designer(base_dir: str, theme_name: str, domain: str,
@ -863,8 +888,11 @@ def _set_clear_cache_flag(base_dir: str) -> None:
if not os.path.isdir(base_dir + '/accounts'):
return
flag_filename = base_dir + '/accounts/.clear_cache'
with open(flag_filename, 'w+', encoding='utf-8') as fp_flag:
fp_flag.write('\n')
try:
with open(flag_filename, 'w+', encoding='utf-8') as fp_flag:
fp_flag.write('\n')
except OSError:
print('EX: _set_clear_cache_flag unable to write ' + flag_filename)
def set_theme(base_dir: str, name: str, domain: str,
@ -942,9 +970,13 @@ def update_default_themes_list(base_dir: str) -> None:
"""
theme_names = get_themes_list(base_dir)
default_themes_filename = base_dir + '/defaultthemes.txt'
with open(default_themes_filename, 'w+', encoding='utf-8') as fp_def:
for name in theme_names:
fp_def.write(name + '\n')
try:
with open(default_themes_filename, 'w+', encoding='utf-8') as fp_def:
for name in theme_names:
fp_def.write(name + '\n')
except OSError:
print('EX: update_default_themes_list unable to write ' +
default_themes_filename)
def scan_themes_for_scripts(base_dir: str) -> bool:

117
utils.py
View File

@ -485,9 +485,13 @@ def refresh_newswire(base_dir: str):
refresh_newswire_filename = base_dir + '/accounts/.refresh_newswire'
if os.path.isfile(refresh_newswire_filename):
return
with open(refresh_newswire_filename, 'w+',
encoding='utf-8') as refresh_file:
refresh_file.write('\n')
try:
with open(refresh_newswire_filename, 'w+',
encoding='utf-8') as refresh_file:
refresh_file.write('\n')
except OSError:
print('EX: refresh_newswire unable to write ' +
refresh_newswire_filename)
def get_sha_256(msg: str):
@ -1731,8 +1735,13 @@ def _set_default_pet_name(base_dir: str, nickname: str, domain: str,
follow_nickname + '@' + follow_domain + '\n'
if not os.path.isfile(petnames_filename):
# if there is no existing petnames lookup file
with open(petnames_filename, 'w+', encoding='utf-8') as petnames_file:
petnames_file.write(petname_lookup_entry)
try:
with open(petnames_filename, 'w+',
encoding='utf-8') as petnames_file:
petnames_file.write(petname_lookup_entry)
except OSError:
print('EX: _set_default_pet_name unable to write ' +
petnames_filename)
return
with open(petnames_filename, 'r', encoding='utf-8') as petnames_file:
@ -1792,15 +1801,23 @@ def follow_person(base_dir: str, nickname: str, domain: str,
if text_in_file(handle_to_follow, unfollowed_filename):
# remove them from the unfollowed file
new_lines = ''
with open(unfollowed_filename, 'r',
encoding='utf-8') as unfoll_file:
lines = unfoll_file.readlines()
for line in lines:
if handle_to_follow not in line:
new_lines += line
with open(unfollowed_filename, 'w+',
encoding='utf-8') as unfoll_file:
unfoll_file.write(new_lines)
try:
with open(unfollowed_filename, 'r',
encoding='utf-8') as unfoll_file:
lines = unfoll_file.readlines()
for line in lines:
if handle_to_follow not in line:
new_lines += line
except OSError:
print('EX: follow_person unable to read ' +
unfollowed_filename)
try:
with open(unfollowed_filename, 'w+',
encoding='utf-8') as unfoll_file:
unfoll_file.write(new_lines)
except OSError:
print('EX: follow_person unable to write ' +
unfollowed_filename)
if not os.path.isdir(base_dir + '/accounts'):
os.mkdir(base_dir + '/accounts')
@ -1831,8 +1848,11 @@ def follow_person(base_dir: str, nickname: str, domain: str,
' creating new following file to follow ' +
handle_to_follow +
', filename is ' + filename)
with open(filename, 'w+', encoding='utf-8') as foll_file:
foll_file.write(handle_to_follow + '\n')
try:
with open(filename, 'w+', encoding='utf-8') as foll_file:
foll_file.write(handle_to_follow + '\n')
except OSError:
print('EX: follow_person unable to write ' + filename)
if follow_file.endswith('following.txt'):
# Default to adding new follows to the calendar.
@ -2126,18 +2146,22 @@ def remove_moderation_post_from_index(base_dir: str, post_url: str,
return
post_id = remove_id_ending(post_url)
if text_in_file(post_id, moderation_index_file):
with open(moderation_index_file, 'r',
encoding='utf-8') as file1:
lines = file1.readlines()
with open(moderation_index_file, 'w+',
encoding='utf-8') as file2:
for line in lines:
if line.strip("\n").strip("\r") != post_id:
file2.write(line)
continue
if debug:
print('DEBUG: removed ' + post_id +
' from moderation index')
try:
with open(moderation_index_file, 'r',
encoding='utf-8') as file1:
lines = file1.readlines()
with open(moderation_index_file, 'w+',
encoding='utf-8') as file2:
for line in lines:
if line.strip("\n").strip("\r") != post_id:
file2.write(line)
continue
if debug:
print('DEBUG: removed ' + post_id +
' from moderation index')
except OSError as ex:
print('EX: remove_moderation_post_from_index unable to read ' +
moderation_index_file + ' ' + str(ex))
def _is_reply_to_blog_post(base_dir: str, nickname: str, domain: str,
@ -2295,9 +2319,13 @@ def _remove_post_id_from_tag_index(tag_index_filename: str,
'unable to delete tag index ' + str(tag_index_filename))
else:
# write the new hashtag index without the given post in it
with open(tag_index_filename, 'w+',
encoding='utf-8') as index_file:
index_file.write(newlines)
try:
with open(tag_index_filename, 'w+',
encoding='utf-8') as index_file:
index_file.write(newlines)
except OSError:
print('EX: _remove_post_id_from_tag_index unable to write ' +
tag_index_filename)
def _delete_hashtags_on_post(base_dir: str, post_json_object: {}) -> None:
@ -2364,8 +2392,13 @@ def _delete_conversation_post(base_dir: str, nickname: str, domain: str,
return False
conversation_str = conversation_str.replace(post_id + '\n', '')
if conversation_str:
with open(conversation_filename, 'w+', encoding='utf-8') as conv_file:
conv_file.write(conversation_str)
try:
with open(conversation_filename, 'w+',
encoding='utf-8') as conv_file:
conv_file.write(conversation_str)
except OSError:
print('EX: _delete_conversation_post unable to write ' +
conversation_filename)
else:
if os.path.isfile(conversation_filename + '.muted'):
try:
@ -3454,9 +3487,13 @@ def reject_post_id(base_dir: str, nickname: str, domain: str,
if recent_posts_cache['html'].get(post_url):
del recent_posts_cache['html'][post_url]
with open(post_filename + '.reject', 'w+',
encoding='utf-8') as reject_file:
reject_file.write('\n')
try:
with open(post_filename + '.reject', 'w+',
encoding='utf-8') as reject_file:
reject_file.write('\n')
except OSError:
print('EX: reject_post_id unable to write ' +
post_filename + '.reject')
def is_chat_message(post_json_object: {}) -> bool:
@ -4246,8 +4283,12 @@ def set_account_timezone(base_dir: str, nickname: str, domain: str,
tz_filename = \
acct_dir(base_dir, nickname, domain) + '/timezone.txt'
timezone = timezone.strip()
with open(tz_filename, 'w+', encoding='utf-8') as fp_timezone:
fp_timezone.write(timezone)
try:
with open(tz_filename, 'w+', encoding='utf-8') as fp_timezone:
fp_timezone.write(timezone)
except OSError:
print('EX: set_account_timezone unable to write ' +
tz_filename)
def _is_onion_request(calling_domain: str, referer_domain: str,

View File

@ -36,8 +36,12 @@ def welcome_screen_is_complete(base_dir: str,
if not os.path.isdir(account_path):
return
complete_filename = account_path + '/.welcome_complete'
with open(complete_filename, 'w+', encoding='utf-8') as fp_comp:
fp_comp.write('\n')
try:
with open(complete_filename, 'w+', encoding='utf-8') as fp_comp:
fp_comp.write('\n')
except OSError:
print('EX: welcome_screen_is_complete unable to write ' +
complete_filename)
def html_welcome_screen(base_dir: str, nickname: str,