Exception handling when reading from file

main
Bob Mottram 2024-07-13 23:56:02 +01:00
parent a602cc61eb
commit 0423a296eb
10 changed files with 247 additions and 147 deletions

View File

@ -3266,20 +3266,24 @@ def search_box_posts(base_dir: str, nickname: str, domain: str,
for root, _, fnames in os.walk(path):
for fname in fnames:
file_path = os.path.join(root, fname)
with open(file_path, 'r', encoding='utf-8') as post_file:
data = post_file.read().lower()
try:
with open(file_path, 'r', encoding='utf-8') as post_file:
data = post_file.read().lower()
not_found = False
for keyword in search_words:
if keyword not in data:
not_found = True
break
if not_found:
continue
not_found = False
for keyword in search_words:
if keyword not in data:
not_found = True
break
if not_found:
continue
res.append(file_path)
if len(res) >= max_results:
return res
res.append(file_path)
if len(res) >= max_results:
return res
except OSError as exc:
print('EX: search_box_posts unable to read ' +
file_path + ' ' + str(exc))
break
return res
@ -4473,8 +4477,12 @@ def load_account_timezones(base_dir: str) -> {}:
if not os.path.isfile(tz_filename):
continue
timezone = None
with open(tz_filename, 'r', encoding='utf-8') as fp_timezone:
timezone = fp_timezone.read().strip()
try:
with open(tz_filename, 'r', encoding='utf-8') as fp_timezone:
timezone = fp_timezone.read().strip()
except OSError:
print('EX: load_account_timezones unable to read ' +
tz_filename)
if timezone:
nickname = acct.split('@')[0]
account_timezone[nickname] = timezone
@ -4528,8 +4536,11 @@ def get_account_timezone(base_dir: str, nickname: str, domain: str) -> str:
if not os.path.isfile(tz_filename):
return None
timezone = None
with open(tz_filename, 'r', encoding='utf-8') as fp_timezone:
timezone = fp_timezone.read().strip()
try:
with open(tz_filename, 'r', encoding='utf-8') as fp_timezone:
timezone = fp_timezone.read().strip()
except OSError:
print('EX: get_account_timezone unable to read ' + tz_filename)
return timezone

View File

@ -34,8 +34,12 @@ def html_about(base_dir: str, http_prefix: str,
about_text = 'Information about this instance goes here.'
if os.path.isfile(dir_str + '/about.md'):
with open(dir_str + '/about.md', 'r', encoding='utf-8') as fp_about:
about_text = markdown_to_html(fp_about.read())
try:
with open(dir_str + '/about.md', 'r',
encoding='utf-8') as fp_about:
about_text = markdown_to_html(fp_about.read())
except OSError:
print('EX: html_about unable to read ' + dir_str + '/about.md')
about_form = ''
css_filename = base_dir + '/epicyon-profile.css'

View File

@ -222,8 +222,12 @@ def get_left_column_content(base_dir: str, nickname: str, domain_full: str,
links_file_contains_entries = False
links_list = None
if os.path.isfile(links_filename):
with open(links_filename, 'r', encoding='utf-8') as fp_links:
links_list = fp_links.readlines()
try:
with open(links_filename, 'r', encoding='utf-8') as fp_links:
links_list = fp_links.readlines()
except OSError:
print('EX: get_left_column_content unable to read ' +
links_filename)
if not front_page:
# show a number of shares
@ -497,8 +501,12 @@ def html_edit_links(translate: {}, base_dir: str, path: str,
links_filename = data_dir(base_dir) + '/links.txt'
links_str = ''
if os.path.isfile(links_filename):
with open(links_filename, 'r', encoding='utf-8') as fp_links:
links_str = fp_links.read()
try:
with open(links_filename, 'r', encoding='utf-8') as fp_links:
links_str = fp_links.read()
except OSError:
print('EX: html_edit_links unable to read ' +
links_filename)
edit_links_form += \
'<div class="container">'
@ -522,8 +530,13 @@ def html_edit_links(translate: {}, base_dir: str, path: str,
about_filename = data_dir(base_dir) + '/about.md'
about_str = ''
if os.path.isfile(about_filename):
with open(about_filename, 'r', encoding='utf-8') as fp_about:
about_str = fp_about.read()
try:
with open(about_filename, 'r',
encoding='utf-8') as fp_about:
about_str = fp_about.read()
except OSError:
print('EX: html_edit_links unable to read 2 ' +
about_filename)
edit_links_form += \
'<div class="container">'
@ -541,8 +554,12 @@ def html_edit_links(translate: {}, base_dir: str, path: str,
tos_filename = data_dir(base_dir) + '/tos.md'
tos_str = ''
if os.path.isfile(tos_filename):
with open(tos_filename, 'r', encoding='utf-8') as fp_tos:
tos_str = fp_tos.read()
try:
with open(tos_filename, 'r', encoding='utf-8') as fp_tos:
tos_str = fp_tos.read()
except OSError:
print('EX: html_edit_links unable to read 3 ' +
tos_filename)
edit_links_form += \
'<div class="container">'
@ -560,9 +577,13 @@ def html_edit_links(translate: {}, base_dir: str, path: str,
specification_filename = data_dir(base_dir) + '/activitypub.md'
specification_str = ''
if os.path.isfile(specification_filename):
with open(specification_filename, 'r',
encoding='utf-8') as fp_specification:
specification_str = fp_specification.read()
try:
with open(specification_filename, 'r',
encoding='utf-8') as fp_specification:
specification_str = fp_specification.read()
except OSError:
print('EX: html_edit_links unable to read 4 ' +
specification_filename)
edit_links_form += \
'<div class="container">'

View File

@ -376,16 +376,20 @@ def html_citations(base_dir: str, nickname: str, domain: str,
citations_selected = []
if os.path.isfile(citations_filename):
citations_separator = '#####'
with open(citations_filename, 'r', encoding='utf-8') as fp_cit:
citations = fp_cit.readlines()
for line in citations:
if citations_separator not in line:
continue
sections = line.strip().split(citations_separator)
if len(sections) != 3:
continue
date_str = sections[0]
citations_selected.append(date_str)
try:
with open(citations_filename, 'r', encoding='utf-8') as fp_cit:
citations = fp_cit.readlines()
for line in citations:
if citations_separator not in line:
continue
sections = line.strip().split(citations_separator)
if len(sections) != 3:
continue
date_str = sections[0]
citations_selected.append(date_str)
except OSError as exc:
print('EX: html_citations unable to read ' +
citations_filename + ' ' + str(exc))
# the css filename
css_filename = base_dir + '/epicyon-profile.css'
@ -617,8 +621,12 @@ def html_edit_newswire(translate: {}, base_dir: str, path: str,
newswire_filename = data_dir(base_dir) + '/newswire.txt'
newswire_str = ''
if os.path.isfile(newswire_filename):
with open(newswire_filename, 'r', encoding='utf-8') as fp_news:
newswire_str = fp_news.read()
try:
with open(newswire_filename, 'r', encoding='utf-8') as fp_news:
newswire_str = fp_news.read()
except OSError:
print('EX: html_edit_newswire unable to read ' +
newswire_filename)
edit_newswire_form += \
'<div class="container">'
@ -639,8 +647,12 @@ def html_edit_newswire(translate: {}, base_dir: str, path: str,
filter_filename = \
data_dir(base_dir) + '/news@' + 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 fp_filter:
filter_str = fp_filter.read()
except OSError:
print('EX: html_edit_newswire unable to read 2 ' +
filter_filename)
edit_newswire_form += \
' <br><b><label class="labels">' + \
@ -670,8 +682,13 @@ def html_edit_newswire(translate: {}, base_dir: str, path: str,
hashtag_rules_str = ''
hashtag_rules_filename = data_dir(base_dir) + '/hashtagrules.txt'
if os.path.isfile(hashtag_rules_filename):
with open(hashtag_rules_filename, 'r', encoding='utf-8') as rulesfile:
hashtag_rules_str = rulesfile.read()
try:
with open(hashtag_rules_filename, 'r',
encoding='utf-8') as fp_rules:
hashtag_rules_str = fp_rules.read()
except OSError:
print('EX: html_edit_newswire unable to read 3 ' +
hashtag_rules_filename)
edit_newswire_form += \
' <br><b><label class="labels">' + \

View File

@ -514,17 +514,21 @@ def html_new_post(edit_post_params: {},
# custom report header with any additional instructions
dir_str = data_dir(base_dir)
if os.path.isfile(dir_str + '/report.txt'):
with open(dir_str + '/report.txt', 'r',
encoding='utf-8') as file:
custom_report_text = file.read()
if '</p>' not in custom_report_text:
custom_report_text = \
'<p class="login-subtext">' + \
custom_report_text + '</p>\n'
rep_str = '<p class="login-subtext">'
custom_report_text = \
custom_report_text.replace('<p>', rep_str)
new_post_text += custom_report_text
try:
with open(dir_str + '/report.txt', 'r',
encoding='utf-8') as file:
custom_report_text = file.read()
if '</p>' not in custom_report_text:
custom_report_text = \
'<p class="login-subtext">' + \
custom_report_text + '</p>\n'
rep_str = '<p class="login-subtext">'
custom_report_text = \
custom_report_text.replace('<p>', rep_str)
new_post_text += custom_report_text
except OSError as exc:
print('EX: html_new_post unable to read ' +
dir_str + '/report.txt ' + str(exc))
idx = 'This message only goes to moderators, even if it ' + \
'mentions other fediverse addresses.'
@ -553,8 +557,12 @@ def html_new_post(edit_post_params: {},
dir_str = data_dir(base_dir)
if os.path.isfile(dir_str + '/newpost.txt'):
with open(dir_str + '/newpost.txt', 'r', encoding='utf-8') as file:
new_post_text = '<p>' + file.read() + '</p>\n'
try:
with open(dir_str + '/newpost.txt', 'r', encoding='utf-8') as file:
new_post_text = '<p>' + file.read() + '</p>\n'
except OSError:
print('EX: html_new_post unable to read ' +
dir_str + '/newpost.txt')
css_filename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'):
@ -870,19 +878,24 @@ def html_new_post(edit_post_params: {},
translate['Citations'] + ':</label></p>\n'
citations_str += ' <ul>\n'
citations_separator = '#####'
with open(citations_filename, 'r', encoding='utf-8') as cit_file:
citations = cit_file.readlines()
for line in citations:
if citations_separator not in line:
continue
sections = line.strip().split(citations_separator)
if len(sections) != 3:
continue
title = sections[1]
link = sections[2]
citations_str += \
' <li><a href="' + link + '"><cite>' + \
title + '</cite></a></li>'
try:
with open(citations_filename, 'r',
encoding='utf-8') as cit_file:
citations = cit_file.readlines()
for line in citations:
if citations_separator not in line:
continue
sections = line.strip().split(citations_separator)
if len(sections) != 3:
continue
title = sections[1]
link = sections[2]
citations_str += \
' <li><a href="' + link + '"><cite>' + \
title + '</cite></a></li>'
except OSError as exc:
print('EX: html_new_post unable to read ' +
citations_filename + ' ' + str(exc))
citations_str += ' </ul>\n'
citations_str += '</div>\n'

View File

@ -85,9 +85,13 @@ def html_hash_tag_swarm(base_dir: str, actor: str, translate: {}) -> str:
blocked_str = ''
global_blocking_filename = data_dir(base_dir) + '/blocking.txt'
if os.path.isfile(global_blocking_filename):
with open(global_blocking_filename, 'r',
encoding='utf-8') as fp_block:
blocked_str = fp_block.read()
try:
with open(global_blocking_filename, 'r',
encoding='utf-8') as fp_block:
blocked_str = fp_block.read()
except OSError:
print('EX: html_hash_tag_swarm unable to read ' +
global_blocking_filename)
for _, _, files in os.walk(base_dir + '/tags'):
for fname in files:
@ -121,12 +125,19 @@ def html_hash_tag_swarm(base_dir: str, actor: str, translate: {}) -> str:
continue
if '#' + hash_tag_name + '\n' in blocked_str:
continue
with open(tags_filename, 'r', encoding='utf-8') as fp_tags:
# only read one line, which saves time and memory
last_tag = fp_tags.readline()
if not last_tag.startswith(days_since_epoch_str):
if not last_tag.startswith(days_since_epoch_str2):
continue
try:
with open(tags_filename, 'r', encoding='utf-8') as fp_tags:
# only read one line, which saves time and memory
last_tag = fp_tags.readline()
if not last_tag.startswith(days_since_epoch_str):
if not last_tag.startswith(days_since_epoch_str2):
continue
except OSError:
print('EX: html_hash_tag_swarm unable to read 2 ' +
tags_filename)
continue
with open(tags_filename, 'r', encoding='utf-8') as fp_tags:
while True:
line = fp_tags.readline()

View File

@ -133,8 +133,11 @@ def html_login(translate: {},
dir_str = data_dir(base_dir)
if os.path.isfile(dir_str + '/login.txt'):
# custom login message
with open(dir_str + '/login.txt', 'r', encoding='utf-8') as file:
login_text = '<p class="login-text">' + file.read() + '</p>'
try:
with open(dir_str + '/login.txt', 'r', encoding='utf-8') as file:
login_text = '<p class="login-text">' + file.read() + '</p>'
except OSError:
print('EX: html_login unable to read ' + dir_str + '/login.txt')
css_filename = base_dir + '/epicyon-login.css'
if os.path.isfile(base_dir + '/login.css'):

View File

@ -35,10 +35,13 @@ def html_manual(base_dir: str, http_prefix: str,
manual_text = 'User Manual.'
if os.path.isfile(manual_filename):
with open(manual_filename, 'r',
encoding='utf-8') as fp_manual:
md_text = markdown_example_numbers(fp_manual.read())
manual_text = markdown_to_html(md_text)
try:
with open(manual_filename, 'r',
encoding='utf-8') as fp_manual:
md_text = markdown_example_numbers(fp_manual.read())
manual_text = markdown_to_html(md_text)
except OSError:
print('EX: html_manual unable to read ' + manual_filename)
manual_form = ''
css_filename = base_dir + '/epicyon-profile.css'

View File

@ -19,12 +19,16 @@ def load_peertube_instances(base_dir: str, peertube_instances: []) -> None:
peertube_list = None
peertube_instances_filename = data_dir(base_dir) + '/peertube.txt'
if os.path.isfile(peertube_instances_filename):
with open(peertube_instances_filename, 'r',
encoding='utf-8') as fp_inst:
peertube_str = fp_inst.read()
if peertube_str:
peertube_str = peertube_str.replace('\r', '')
peertube_list = peertube_str.split('\n')
try:
with open(peertube_instances_filename, 'r',
encoding='utf-8') as fp_inst:
peertube_str = fp_inst.read()
if peertube_str:
peertube_str = peertube_str.replace('\r', '')
peertube_list = peertube_str.split('\n')
except OSError as exc:
print('EX: load_peertube_instances unable to read ' +
peertube_instances_filename + ' ' + str(exc))
if not peertube_list:
return
for url in peertube_list:

View File

@ -426,19 +426,23 @@ def html_moderation_info(translate: {}, base_dir: str,
suspended_filename = dir_str + '/suspended.txt'
if os.path.isfile(suspended_filename):
with open(suspended_filename, 'r', encoding='utf-8') as fp_sus:
suspended_str = fp_sus.read()
info_form += '<div class="container">\n'
info_form += ' <br><b>' + \
translate['Suspended accounts'] + '</b>'
info_form += ' <br>' + \
translate['These are currently suspended']
info_form += \
' <textarea id="message" ' + \
'name="suspended" style="height:200px" spellcheck="false">' + \
suspended_str + '</textarea>\n'
info_form += '</div>\n'
info_shown = True
try:
with open(suspended_filename, 'r', encoding='utf-8') as fp_sus:
suspended_str = fp_sus.read()
info_form += '<div class="container">\n'
info_form += ' <br><b>' + \
translate['Suspended accounts'] + '</b>'
info_form += ' <br>' + \
translate['These are currently suspended']
info_form += \
' <textarea id="message" ' + \
'name="suspended" style="height:200px" ' + \
'spellcheck="false">' + suspended_str + '</textarea>\n'
info_form += '</div>\n'
info_shown = True
except OSError as exc:
print('EX: html_moderation_info unable to read ' +
suspended_filename + ' ' + str(exc))
blocking_filename = dir_str + '/blocking.txt'
if os.path.isfile(blocking_filename):
@ -446,52 +450,61 @@ def html_moderation_info(translate: {}, base_dir: str,
blocking_reasons_exist = False
if os.path.isfile(blocking_reasons_filename):
blocking_reasons_exist = True
with open(blocking_filename, 'r', encoding='utf-8') as fp_block:
blocked_lines = fp_block.readlines()
blocked_str = ''
if blocked_lines:
blocked_lines.sort()
for line in blocked_lines:
if not line:
continue
line = remove_eol(line).strip()
if blocking_reasons_exist:
reason = \
get_global_block_reason(line,
blocking_reasons_filename)
if reason:
blocked_str += \
line + ' - ' + reason + '\n'
try:
with open(blocking_filename, 'r', encoding='utf-8') as fp_block:
blocked_lines = fp_block.readlines()
blocked_str = ''
if blocked_lines:
blocked_lines.sort()
for line in blocked_lines:
if not line:
continue
blocked_str += line + '\n'
info_form += '<div class="container">\n'
info_form += \
' <br><b>' + \
translate['Blocked accounts and hashtags'] + '</b>'
info_form += \
' <br>' + \
translate[msg_str1]
info_form += \
' <textarea id="message" ' + \
'name="blocked" style="height:2000px" spellcheck="false">' + \
blocked_str + '</textarea>\n'
info_form += '</div>\n'
info_shown = True
line = remove_eol(line).strip()
if blocking_reasons_exist:
blocking_reasons_file = blocking_reasons_filename
reason = \
get_global_block_reason(line,
blocking_reasons_file)
if reason:
blocked_str += \
line + ' - ' + reason + '\n'
continue
blocked_str += line + '\n'
info_form += '<div class="container">\n'
info_form += \
' <br><b>' + \
translate['Blocked accounts and hashtags'] + '</b>'
info_form += \
' <br>' + \
translate[msg_str1]
info_form += \
' <textarea id="message" ' + \
'name="blocked" style="height:2000px" ' + \
'spellcheck="false">' + blocked_str + '</textarea>\n'
info_form += '</div>\n'
info_shown = True
except OSError as exc:
print('EX: html_moderation_info unable to read 2 ' +
blocking_filename + ' ' + str(exc))
filters_filename = dir_str + '/filters.txt'
if os.path.isfile(filters_filename):
with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
filtered_str = fp_filt.read()
info_form += '<div class="container">\n'
info_form += \
' <br><b>' + \
translate['Filtered words'] + '</b>'
info_form += \
' <textarea id="message" ' + \
'name="filtered" style="height:700px" spellcheck="true">' + \
filtered_str + '</textarea>\n'
info_form += '</div>\n'
info_shown = True
try:
with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
filtered_str = fp_filt.read()
info_form += '<div class="container">\n'
info_form += \
' <br><b>' + \
translate['Filtered words'] + '</b>'
info_form += \
' <textarea id="message" ' + \
'name="filtered" style="height:700px" ' + \
'spellcheck="true">' + filtered_str + '</textarea>\n'
info_form += '</div>\n'
info_shown = True
except OSError as exc:
print('EX: html_moderation_info unable to read ' +
filters_filename + ' ' + str(exc))
if not info_shown:
info_form += \