diff --git a/data.py b/data.py index dfea424ee..2535901ec 100644 --- a/data.py +++ b/data.py @@ -37,6 +37,20 @@ def load_string(filename: str, exception_text: str) -> str: return None +def load_line(filename: str, exception_text: str) -> str: + """Loads a line of text from file + """ + try: + with open(filename, 'r', encoding='utf-8') as fp: + text = fp.readline() + return text + except OSError as exc: + if '[ex]' in exception_text: + exception_text = exception_text.replace('[ex]', str(exc)) + print(exception_text) + return None + + def load_list(filename: str, exception_text: str) -> str: """Loads a list from file This is used to replace readlines diff --git a/webapp_about.py b/webapp_about.py index d03c20b92..f7c19bf65 100644 --- a/webapp_about.py +++ b/webapp_about.py @@ -14,6 +14,7 @@ from utils import get_config_param from webapp_utils import html_header_with_website_markup from webapp_utils import html_footer from markdown import markdown_to_html +from data import load_string def html_about(base_dir: str, http_prefix: str, @@ -34,12 +35,11 @@ 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'): - 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_text = load_string(dir_str + '/about.md', + 'EX: html_about unable to read ' + + dir_str + '/about.md') + if about_text: + about_text = markdown_to_html(about_text) about_form = '' css_filename = base_dir + '/epicyon-profile.css' diff --git a/webapp_column_left.py b/webapp_column_left.py index 6ac6906c2..eb9bf2517 100644 --- a/webapp_column_left.py +++ b/webapp_column_left.py @@ -26,6 +26,7 @@ from webapp_utils import get_banner_file from webapp_utils import edit_text_field from shares import share_category_icon from data import load_list +from data import load_string def _links_exist(base_dir: str) -> bool: @@ -513,12 +514,11 @@ 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): - 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) + links_str = load_string(links_filename, + 'EX: html_edit_links unable to read ' + + links_filename) + if links_str is None: + links_str = '' edit_links_form += \ '
' @@ -542,13 +542,12 @@ 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): - 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) + about_str = \ + load_string(about_filename, + 'EX: html_edit_links unable to read 2 ' + + about_filename) + if about_str is None: + about_str = '' edit_links_form += \ '
' @@ -566,12 +565,11 @@ 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): - 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) + tos_str = load_string(tos_filename, + 'EX: html_edit_links unable to read 3 ' + + tos_filename) + if tos_str is None: + tos_str = '' edit_links_form += \ '
' @@ -589,13 +587,12 @@ 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): - 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) + specification_str = \ + load_string(specification_filename, + 'EX: html_edit_links unable to read 4 ' + + specification_filename) + if specification_str is None: + specification_str = '' edit_links_form += \ '
' diff --git a/webapp_column_right.py b/webapp_column_right.py index 220264eb5..ce3370d39 100644 --- a/webapp_column_right.py +++ b/webapp_column_right.py @@ -36,6 +36,7 @@ from webapp_utils import header_buttons_front_screen from webapp_utils import edit_text_field from textmode import text_mode_browser from data import load_list +from data import load_string def _votes_indicator(total_votes: int, positive_voting: bool) -> str: @@ -637,12 +638,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): - 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) + newswire_str = \ + load_string(newswire_filename, + 'EX: html_edit_newswire unable to read ' + + newswire_filename) + if newswire_str is None: + newswire_str = '' edit_newswire_form += \ '
' @@ -663,12 +664,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): - 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) + filter_str = \ + load_string(filter_filename, + 'EX: html_edit_newswire unable to read 2 ' + + filter_filename) + if filter_str is None: + filter_str = '' edit_newswire_form += \ '