Exception handling when reading from file

main
Bob Mottram 2024-07-13 21:27:19 +01:00
parent 21e2095696
commit a602cc61eb
10 changed files with 266 additions and 125 deletions

View File

@ -1212,15 +1212,19 @@ def html_profile(signing_priv_key_pem: str,
follow_requests_filename = \
acct_dir(base_dir, nickname, domain) + '/followrequests.txt'
if os.path.isfile(follow_requests_filename):
with open(follow_requests_filename, 'r',
encoding='utf-8') as foll_file:
for line in foll_file:
if len(line) > 0:
follow_approvals = True
followers_button = 'buttonhighlighted'
if selected == 'followers':
followers_button = 'buttonselectedhighlighted'
break
try:
with open(follow_requests_filename, 'r',
encoding='utf-8') as foll_file:
for line in foll_file:
if len(line) > 0:
follow_approvals = True
followers_button = 'buttonhighlighted'
if selected == 'followers':
followers_button = 'buttonselectedhighlighted'
break
except OSError:
print('EX: html_profile unable to read ' +
follow_requests_filename)
if selected == 'followers':
if follow_approvals:
curr_follower_domains = \
@ -1323,8 +1327,11 @@ def html_profile(signing_priv_key_pem: str,
pinned_filename = account_dir + '/pinToProfile.txt'
pinned_content = None
if os.path.isfile(pinned_filename):
with open(pinned_filename, 'r', encoding='utf-8') as pin_file:
pinned_content = pin_file.read()
try:
with open(pinned_filename, 'r', encoding='utf-8') as pin_file:
pinned_content = pin_file.read()
except OSError:
print('EX: html_profile unable to read ' + pinned_filename)
# shared items attached to the actor
# https://codeberg.org/fediverse/fep/src/branch/main/fep/0837/fep-0837.md
@ -2076,8 +2083,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
moderators = ''
moderators_file = data_dir(base_dir) + '/moderators.txt'
if os.path.isfile(moderators_file):
with open(moderators_file, 'r', encoding='utf-8') as mod_file:
moderators = mod_file.read()
try:
with open(moderators_file, 'r', encoding='utf-8') as mod_file:
moderators = mod_file.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
moderators_file)
subtitle = translate['A list of moderator nicknames. One per line.']
role_assign_str += \
edit_text_area('<b>' + translate['Moderators'] + '</b>', subtitle,
@ -2086,8 +2097,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
editors = ''
editors_file = data_dir(base_dir) + '/editors.txt'
if os.path.isfile(editors_file):
with open(editors_file, 'r', encoding='utf-8') as edit_file:
editors = edit_file.read()
try:
with open(editors_file, 'r', encoding='utf-8') as edit_file:
editors = edit_file.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
editors_file)
subtitle = translate['A list of editor nicknames. One per line.']
role_assign_str += \
edit_text_area('<b>' + translate['Site Editors'] + '</b>',
@ -2097,8 +2112,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
counselors = ''
counselors_file = data_dir(base_dir) + '/counselors.txt'
if os.path.isfile(counselors_file):
with open(counselors_file, 'r', encoding='utf-8') as co_file:
counselors = co_file.read()
try:
with open(counselors_file, 'r', encoding='utf-8') as co_file:
counselors = co_file.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
counselors_file)
role_assign_str += \
edit_text_area('<b>' + translate['Counselors'] + '</b>', None,
'counselors', counselors, 200, '', False)
@ -2107,8 +2126,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
artists = ''
artists_file = data_dir(base_dir) + '/artists.txt'
if os.path.isfile(artists_file):
with open(artists_file, 'r', encoding='utf-8') as art_file:
artists = art_file.read()
try:
with open(artists_file, 'r', encoding='utf-8') as art_file:
artists = art_file.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
artists_file)
role_assign_str += \
edit_text_area('<b>' + translate['Artists'] + '</b>', None,
'artists', artists, 200, '', False)
@ -2117,8 +2140,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
devops = ''
devops_file = data_dir(base_dir) + '/devops.txt'
if os.path.isfile(devops_file):
with open(devops_file, 'r', encoding='utf-8') as edit_file:
devops = edit_file.read()
try:
with open(devops_file, 'r', encoding='utf-8') as edit_file:
devops = edit_file.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
devops_file)
subtitle = translate['A list of devops nicknames. One per line.']
role_assign_str += \
edit_text_area('<b>' + translate['Site DevOps'] + '</b>',
@ -2238,8 +2265,13 @@ def _html_edit_profile_git_projects(base_dir: str, nickname: str, domain: str,
git_projects_filename = \
acct_dir(base_dir, nickname, domain) + '/gitprojects.txt'
if os.path.isfile(git_projects_filename):
with open(git_projects_filename, 'r', encoding='utf-8') as git_file:
git_projects_str = git_file.read()
try:
with open(git_projects_filename, 'r',
encoding='utf-8') as git_file:
git_projects_str = git_file.read()
except OSError:
print('EX: _html_edit_profile_git_projects unable to read ' +
git_projects_filename)
edit_profile_form = begin_edit_section(translate['Git Projects'])
idx = 'List of project names that you wish to receive git patches for'
@ -2284,36 +2316,57 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
filter_filename = \
acct_dir(base_dir, nickname, domain) + '/filters.txt'
if os.path.isfile(filter_filename):
with open(filter_filename, 'r', encoding='utf-8') as filterfile:
filter_str = filterfile.read()
try:
with open(filter_filename, 'r', encoding='utf-8') as filterfile:
filter_str = filterfile.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
filter_filename)
filter_bio_str = ''
filter_bio_filename = \
acct_dir(base_dir, nickname, domain) + '/filters_bio.txt'
if os.path.isfile(filter_bio_filename):
with open(filter_bio_filename, 'r', encoding='utf-8') as filterfile:
filter_bio_str = filterfile.read()
try:
with open(filter_bio_filename, 'r',
encoding='utf-8') as fp_filter:
filter_bio_str = fp_filter.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
filter_bio_filename)
switch_str = ''
switch_filename = \
acct_dir(base_dir, nickname, domain) + '/replacewords.txt'
if os.path.isfile(switch_filename):
with open(switch_filename, 'r', encoding='utf-8') as switchfile:
switch_str = switchfile.read()
try:
with open(switch_filename, 'r', encoding='utf-8') as switchfile:
switch_str = switchfile.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to save ' +
switch_filename)
auto_tags = ''
auto_tags_filename = \
acct_dir(base_dir, nickname, domain) + '/autotags.txt'
if os.path.isfile(auto_tags_filename):
with open(auto_tags_filename, 'r', encoding='utf-8') as auto_file:
auto_tags = auto_file.read()
try:
with open(auto_tags_filename, 'r', encoding='utf-8') as auto_file:
auto_tags = auto_file.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
auto_tags_filename)
auto_cw = ''
auto_cw_filename = \
acct_dir(base_dir, nickname, domain) + '/autocw.txt'
if os.path.isfile(auto_cw_filename):
with open(auto_cw_filename, 'r', encoding='utf-8') as cw_file:
auto_cw = cw_file.read()
try:
with open(auto_cw_filename, 'r', encoding='utf-8') as cw_file:
auto_cw = cw_file.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
auto_cw_filename)
blocked_str = get_account_blocks(base_dir, nickname, domain)
@ -2321,17 +2374,25 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
dm_allowed_instances_filename = \
acct_dir(base_dir, nickname, domain) + '/dmAllowedInstances.txt'
if os.path.isfile(dm_allowed_instances_filename):
with open(dm_allowed_instances_filename, 'r',
encoding='utf-8') as dm_file:
dm_allowed_instances_str = dm_file.read()
try:
with open(dm_allowed_instances_filename, 'r',
encoding='utf-8') as dm_file:
dm_allowed_instances_str = dm_file.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
dm_allowed_instances_filename)
allowed_instances_str = ''
allowed_instances_filename = \
acct_dir(base_dir, nickname, domain) + '/allowedinstances.txt'
if os.path.isfile(allowed_instances_filename):
with open(allowed_instances_filename, 'r',
encoding='utf-8') as allow_file:
allowed_instances_str = allow_file.read()
try:
with open(allowed_instances_filename, 'r',
encoding='utf-8') as allow_file:
allowed_instances_str = allow_file.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
allowed_instances_filename)
edit_profile_form = begin_edit_section(translate['Filtering and Blocking'])
@ -2351,16 +2412,24 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
city = ''
city_filename = acct_dir(base_dir, nickname, domain) + '/city.txt'
if os.path.isfile(city_filename):
with open(city_filename, 'r', encoding='utf-8') as city_file:
city1 = city_file.read()
city = remove_eol(city1)
try:
with open(city_filename, 'r', encoding='utf-8') as city_file:
city1 = city_file.read()
city = remove_eol(city1)
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
city_filename)
locations_filename = base_dir + '/custom_locations.txt'
if not os.path.isfile(locations_filename):
locations_filename = base_dir + '/locations.txt'
cities = []
with open(locations_filename, 'r', encoding='utf-8') as loc_file:
cities = loc_file.readlines()
cities.sort()
try:
with open(locations_filename, 'r', encoding='utf-8') as loc_file:
cities = loc_file.readlines()
cities.sort()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
locations_filename)
edit_profile_form += ' <select id="cityDropdown" ' + \
'name="cityDropdown" class="theme">\n'
city = city.lower()

View File

@ -38,8 +38,11 @@ def get_pwa_theme_colors(css_filename: str) -> (str, str):
return pwa_theme_color, pwa_theme_background_color
css_str = ''
with open(css_filename, 'r', encoding='utf-8') as fp_css:
css_str = fp_css.read()
try:
with open(css_filename, 'r', encoding='utf-8') as fp_css:
css_str = fp_css.read()
except OSError:
print('EX: get_pwa_theme_colors unable to read ' + css_filename)
pwa_theme_color = \
_get_variable_from_css(css_str, 'pwa-theme-color')

View File

@ -884,8 +884,12 @@ def html_hashtag_search(nickname: str, domain: str, port: int,
nickname = None
# read the index
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines()
lines = []
try:
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines()
except OSError:
print('EX: html_hashtag_search unable to read ' + hashtag_index_file)
# read the css
css_filename = base_dir + '/epicyon-profile.css'
@ -1308,8 +1312,11 @@ def hashtag_search_rss(nickname: str, domain: str, port: int,
# read the index
lines = []
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines()
try:
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines()
except OSError:
print('EX: hashtag_search_rss unable to read ' + hashtag_index_file)
if not lines:
return None
@ -1419,8 +1426,12 @@ def hashtag_search_json(nickname: str, domain: str, port: int,
# read the index
lines = []
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines()
try:
with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines()
except OSError:
print('EX: hashtag_search_json unable to read ' +
hashtag_index_file)
if not lines:
return None

View File

@ -35,10 +35,14 @@ def html_specification(base_dir: str, http_prefix: str,
specification_text = 'ActivityPub Protocol Specification.'
if os.path.isfile(specification_filename):
with open(specification_filename, 'r',
encoding='utf-8') as fp_specification:
md_text = markdown_example_numbers(fp_specification.read())
specification_text = markdown_to_html(md_text)
try:
with open(specification_filename, 'r',
encoding='utf-8') as fp_specification:
md_text = markdown_example_numbers(fp_specification.read())
specification_text = markdown_to_html(md_text)
except OSError:
print('EX: html_specification unable to read ' +
specification_filename)
specification_form = ''
css_filename = base_dir + '/epicyon-profile.css'

View File

@ -93,8 +93,13 @@ def _get_help_for_timeline(base_dir: str, box_name: str) -> str:
get_config_param(base_dir, 'instanceTitle')
if not instance_title:
instance_title = 'Epicyon'
with open(help_filename, 'r', encoding='utf-8') as help_file:
help_text = help_file.read()
help_text = ''
try:
with open(help_filename, 'r', encoding='utf-8') as help_file:
help_text = help_file.read()
except OSError:
print('EX: _get_help_for_timeline unable to read ' + help_filename)
if help_text:
if dangerous_markup(help_text, False, []):
return ''
help_text = help_text.replace('INSTANCE', instance_title)
@ -532,11 +537,14 @@ def html_timeline(default_timeline: str,
if os.path.isfile(calendar_file):
new_calendar_event = True
calendar_image = 'calendar_notify.png'
with open(calendar_file, 'r', encoding='utf-8') as calfile:
calendar_path = calfile.read().replace('##sent##', '')
calendar_path = remove_eol(calendar_path)
if '/calendar' not in calendar_path:
calendar_path = '/calendar'
try:
with open(calendar_file, 'r', encoding='utf-8') as calfile:
calendar_path = calfile.read().replace('##sent##', '')
calendar_path = remove_eol(calendar_path)
if '/calendar' not in calendar_path:
calendar_path = '/calendar'
except OSError:
print('EX: html_timeline unable to read ' + calendar_file)
# should the DM button be highlighted?
new_dm = False
@ -693,21 +701,27 @@ def html_timeline(default_timeline: str,
follow_requests_filename = \
acct_dir(base_dir, nickname, domain) + '/followrequests.txt'
if os.path.isfile(follow_requests_filename):
with open(follow_requests_filename, 'r',
encoding='utf-8') as foll_file:
for line in foll_file:
if len(line) > 0:
# show follow approvals icon
follow_approvals = \
'<a href="' + users_path + \
'/followers#buttonheader" ' + \
'accesskey="' + access_keys['followButton'] + '">' + \
'<img loading="lazy" decoding="async" ' + \
'class="timelineicon" alt="' + \
translate['Approve follow requests'] + \
'" title="' + translate['Approve follow requests'] + \
'" src="/icons/person.png"/></a>\n'
break
try:
with open(follow_requests_filename, 'r',
encoding='utf-8') as foll_file:
for line in foll_file:
if len(line) > 0:
# show follow approvals icon
follow_approvals = \
'<a href="' + users_path + \
'/followers#buttonheader" ' + \
'accesskey="' + \
access_keys['followButton'] + '">' + \
'<img loading="lazy" decoding="async" ' + \
'class="timelineicon" alt="' + \
translate['Approve follow requests'] + \
'" title="' + \
translate['Approve follow requests'] + \
'" src="/icons/person.png"/></a>\n'
break
except OSError:
print('EX: html_timeline unable to read ' +
follow_requests_filename)
_log_timeline_timing(enable_timing_log, timeline_start_time, box_name, '3')

View File

@ -34,8 +34,12 @@ def html_terms_of_service(base_dir: str,
tos_text = 'Terms of Service go here.'
if os.path.isfile(dir_str + '/tos.md'):
with open(dir_str + '/tos.md', 'r', encoding='utf-8') as file:
tos_text = markdown_to_html(file.read())
try:
with open(dir_str + '/tos.md', 'r', encoding='utf-8') as file:
tos_text = markdown_to_html(file.read())
except OSError:
print('EX: html_terms_of_service unable to read ' +
dir_str + '/tos.md')
tos_form = ''
css_filename = base_dir + '/epicyon-profile.css'

View File

@ -94,8 +94,13 @@ def get_broken_link_substitute() -> str:
def html_following_list(base_dir: str, following_filename: str) -> str:
"""Returns a list of handles being followed
"""
with open(following_filename, 'r', encoding='utf-8') as following_file:
msg = following_file.read()
msg = ''
try:
with open(following_filename, 'r', encoding='utf-8') as following_file:
msg = following_file.read()
except OSError:
print('EX: html_following_list unable to read ' + following_filename)
if msg:
following_list = msg.split('\n')
following_list.sort()
if following_list:
@ -122,8 +127,13 @@ def csv_following_list(following_filename: str,
base_dir: str, nickname: str, domain: str) -> str:
"""Returns a csv of handles being followed
"""
with open(following_filename, 'r', encoding='utf-8') as following_file:
msg = following_file.read()
msg = ''
try:
with open(following_filename, 'r', encoding='utf-8') as following_file:
msg = following_file.read()
except OSError:
print('EX: csv_following_list unable to read ' + following_filename)
if msg:
following_list = msg.split('\n')
following_list.sort()
if following_list:
@ -2208,27 +2218,35 @@ def html_following_data_list(base_dir: str, nickname: str,
list_str = '<datalist id="' + following_type + 'Handles">\n'
following_filename = \
acct_dir(base_dir, nickname, domain) + '/' + following_type + '.txt'
msg = None
msg = ''
if os.path.isfile(following_filename):
with open(following_filename, 'r',
encoding='utf-8') as following_file:
msg = following_file.read()
# add your own handle, so that you can send DMs
# to yourself as reminders
msg += nickname + '@' + domain_full + '\n'
try:
with open(following_filename, 'r',
encoding='utf-8') as following_file:
msg = following_file.read()
# add your own handle, so that you can send DMs
# to yourself as reminders
msg += nickname + '@' + domain_full + '\n'
except OSError:
print('EX: html_following_data_list unable to read ' +
following_filename)
if msg:
# include petnames
petnames_filename = \
acct_dir(base_dir, nickname, domain) + '/petnames.txt'
if use_petnames and os.path.isfile(petnames_filename):
following_list = []
with open(petnames_filename, 'r',
encoding='utf-8') as petnames_file:
pet_str = petnames_file.read()
# extract each petname and append it
petnames_list = pet_str.split('\n')
for pet in petnames_list:
following_list.append(pet.split(' ')[0])
try:
with open(petnames_filename, 'r',
encoding='utf-8') as fp_petnames:
pet_str = fp_petnames.read()
# extract each petname and append it
petnames_list = pet_str.split('\n')
for pet in petnames_list:
following_list.append(pet.split(' ')[0])
except OSError:
print('EX: html_following_data_list unable to read ' +
petnames_filename)
# add the following.txt entries
following_list += msg.split('\n')
else:
@ -2256,27 +2274,35 @@ def html_following_dropdown(base_dir: str, nickname: str,
list_str = '<select name="searchtext">\n'
following_filename = \
acct_dir(base_dir, nickname, domain) + '/' + following_type + '.txt'
msg = None
msg = ''
if os.path.isfile(following_filename):
with open(following_filename, 'r',
encoding='utf-8') as following_file:
msg = following_file.read()
# add your own handle, so that you can send DMs
# to yourself as reminders
msg += nickname + '@' + domain_full + '\n'
try:
with open(following_filename, 'r',
encoding='utf-8') as fp_following:
msg = fp_following.read()
# add your own handle, so that you can send DMs
# to yourself as reminders
msg += nickname + '@' + domain_full + '\n'
except OSError:
print('EX: html_following_dropdown unable to read ' +
following_filename)
if msg:
# include petnames
petnames_filename = \
acct_dir(base_dir, nickname, domain) + '/petnames.txt'
if use_petnames and os.path.isfile(petnames_filename):
following_list = []
with open(petnames_filename, 'r',
encoding='utf-8') as petnames_file:
pet_str = petnames_file.read()
# extract each petname and append it
petnames_list = pet_str.split('\n')
for pet in petnames_list:
following_list.append(pet.split(' ')[0])
try:
with open(petnames_filename, 'r',
encoding='utf-8') as fp_petnames:
pet_str = fp_petnames.read()
# extract each petname and append it
petnames_list = pet_str.split('\n')
for pet in petnames_list:
following_list.append(pet.split(' ')[0])
except OSError:
print('EX: html_following_dropdown unable to read ' +
petnames_filename)
# add the following.txt entries
following_list += msg.split('\n')
else:

View File

@ -83,10 +83,13 @@ def html_welcome_screen(base_dir: str, nickname: str,
instance_title = 'Epicyon'
if os.path.isfile(welcome_filename):
with open(welcome_filename, 'r', encoding='utf-8') as fp_wel:
welcome_text = fp_wel.read()
welcome_text = welcome_text.replace('INSTANCE', instance_title)
welcome_text = markdown_to_html(remove_html(welcome_text))
try:
with open(welcome_filename, 'r', encoding='utf-8') as fp_wel:
welcome_text = fp_wel.read()
welcome_text = welcome_text.replace('INSTANCE', instance_title)
welcome_text = markdown_to_html(remove_html(welcome_text))
except OSError:
print('EX: html_welcome_screen unable to read ' + welcome_filename)
welcome_form = ''
css_filename = base_dir + '/epicyon-welcome.css'

View File

@ -52,10 +52,13 @@ def html_welcome_final(base_dir: str, nickname: str,
instance_title = 'Epicyon'
if os.path.isfile(final_filename):
with open(final_filename, 'r', encoding='utf-8') as final_file:
final_text = final_file.read()
final_text = final_text.replace('INSTANCE', instance_title)
final_text = markdown_to_html(remove_html(final_text))
try:
with open(final_filename, 'r', encoding='utf-8') as fp_final:
final_text = fp_final.read()
final_text = final_text.replace('INSTANCE', instance_title)
final_text = markdown_to_html(remove_html(final_text))
except OSError:
print('EX: html_welcome_final unable to read ' + final_filename)
final_form = ''
css_filename = base_dir + '/epicyon-welcome.css'

View File

@ -59,10 +59,14 @@ def html_welcome_profile(base_dir: str, nickname: str, domain: str,
instance_title = 'Epicyon'
if os.path.isfile(profile_filename):
with open(profile_filename, 'r', encoding='utf-8') as fp_pro:
profile_text = fp_pro.read()
profile_text = profile_text.replace('INSTANCE', instance_title)
profile_text = markdown_to_html(remove_html(profile_text))
try:
with open(profile_filename, 'r', encoding='utf-8') as fp_pro:
profile_text = fp_pro.read()
profile_text = profile_text.replace('INSTANCE', instance_title)
profile_text = markdown_to_html(remove_html(profile_text))
except OSError:
print('EX: html_welcome_profile unable to read ' +
profile_filename)
profile_form = ''
css_filename = base_dir + '/epicyon-welcome.css'