Replace file operations with functions

main
bashrc 2026-04-27 14:25:46 +01:00
parent df6fed2438
commit a0acf20a50
23 changed files with 428 additions and 476 deletions

14
data.py
View File

@ -37,6 +37,20 @@ def load_string(filename: str, exception_text: str) -> str:
return None 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: def load_list(filename: str, exception_text: str) -> str:
"""Loads a list from file """Loads a list from file
This is used to replace readlines This is used to replace readlines

View File

@ -14,6 +14,7 @@ from utils import get_config_param
from webapp_utils import html_header_with_website_markup from webapp_utils import html_header_with_website_markup
from webapp_utils import html_footer from webapp_utils import html_footer
from markdown import markdown_to_html from markdown import markdown_to_html
from data import load_string
def html_about(base_dir: str, http_prefix: str, 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.' about_text = 'Information about this instance goes here.'
if os.path.isfile(dir_str + '/about.md'): if os.path.isfile(dir_str + '/about.md'):
try: about_text = load_string(dir_str + '/about.md',
with open(dir_str + '/about.md', 'r', 'EX: html_about unable to read ' +
encoding='utf-8') as fp_about: dir_str + '/about.md')
about_text = markdown_to_html(fp_about.read()) if about_text:
except OSError: about_text = markdown_to_html(about_text)
print('EX: html_about unable to read ' + dir_str + '/about.md')
about_form = '' about_form = ''
css_filename = base_dir + '/epicyon-profile.css' css_filename = base_dir + '/epicyon-profile.css'

View File

@ -26,6 +26,7 @@ from webapp_utils import get_banner_file
from webapp_utils import edit_text_field from webapp_utils import edit_text_field
from shares import share_category_icon from shares import share_category_icon
from data import load_list from data import load_list
from data import load_string
def _links_exist(base_dir: str) -> bool: 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_filename = data_dir(base_dir) + '/links.txt'
links_str = '' links_str = ''
if os.path.isfile(links_filename): if os.path.isfile(links_filename):
try: links_str = load_string(links_filename,
with open(links_filename, 'r', encoding='utf-8') as fp_links: 'EX: html_edit_links unable to read ' +
links_str = fp_links.read() links_filename)
except OSError: if links_str is None:
print('EX: html_edit_links unable to read ' + links_str = ''
links_filename)
edit_links_form += \ edit_links_form += \
'<div class="container">' '<div class="container">'
@ -542,13 +542,12 @@ def html_edit_links(translate: {}, base_dir: str, path: str,
about_filename = data_dir(base_dir) + '/about.md' about_filename = data_dir(base_dir) + '/about.md'
about_str = '' about_str = ''
if os.path.isfile(about_filename): if os.path.isfile(about_filename):
try: about_str = \
with open(about_filename, 'r', load_string(about_filename,
encoding='utf-8') as fp_about: 'EX: html_edit_links unable to read 2 ' +
about_str = fp_about.read() about_filename)
except OSError: if about_str is None:
print('EX: html_edit_links unable to read 2 ' + about_str = ''
about_filename)
edit_links_form += \ edit_links_form += \
'<div class="container">' '<div class="container">'
@ -566,12 +565,11 @@ def html_edit_links(translate: {}, base_dir: str, path: str,
tos_filename = data_dir(base_dir) + '/tos.md' tos_filename = data_dir(base_dir) + '/tos.md'
tos_str = '' tos_str = ''
if os.path.isfile(tos_filename): if os.path.isfile(tos_filename):
try: tos_str = load_string(tos_filename,
with open(tos_filename, 'r', encoding='utf-8') as fp_tos: 'EX: html_edit_links unable to read 3 ' +
tos_str = fp_tos.read() tos_filename)
except OSError: if tos_str is None:
print('EX: html_edit_links unable to read 3 ' + tos_str = ''
tos_filename)
edit_links_form += \ edit_links_form += \
'<div class="container">' '<div class="container">'
@ -589,13 +587,12 @@ def html_edit_links(translate: {}, base_dir: str, path: str,
specification_filename = data_dir(base_dir) + '/activitypub.md' specification_filename = data_dir(base_dir) + '/activitypub.md'
specification_str = '' specification_str = ''
if os.path.isfile(specification_filename): if os.path.isfile(specification_filename):
try: specification_str = \
with open(specification_filename, 'r', load_string(specification_filename,
encoding='utf-8') as fp_specification: 'EX: html_edit_links unable to read 4 ' +
specification_str = fp_specification.read() specification_filename)
except OSError: if specification_str is None:
print('EX: html_edit_links unable to read 4 ' + specification_str = ''
specification_filename)
edit_links_form += \ edit_links_form += \
'<div class="container">' '<div class="container">'

View File

@ -36,6 +36,7 @@ from webapp_utils import header_buttons_front_screen
from webapp_utils import edit_text_field from webapp_utils import edit_text_field
from textmode import text_mode_browser from textmode import text_mode_browser
from data import load_list from data import load_list
from data import load_string
def _votes_indicator(total_votes: int, positive_voting: bool) -> str: 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_filename = data_dir(base_dir) + '/newswire.txt'
newswire_str = '' newswire_str = ''
if os.path.isfile(newswire_filename): if os.path.isfile(newswire_filename):
try: newswire_str = \
with open(newswire_filename, 'r', encoding='utf-8') as fp_news: load_string(newswire_filename,
newswire_str = fp_news.read() 'EX: html_edit_newswire unable to read ' +
except OSError: newswire_filename)
print('EX: html_edit_newswire unable to read ' + if newswire_str is None:
newswire_filename) newswire_str = ''
edit_newswire_form += \ edit_newswire_form += \
'<div class="container">' '<div class="container">'
@ -663,12 +664,12 @@ def html_edit_newswire(translate: {}, base_dir: str, path: str,
filter_filename = \ filter_filename = \
data_dir(base_dir) + '/news@' + domain + '/filters.txt' data_dir(base_dir) + '/news@' + domain + '/filters.txt'
if os.path.isfile(filter_filename): if os.path.isfile(filter_filename):
try: filter_str = \
with open(filter_filename, 'r', encoding='utf-8') as fp_filter: load_string(filter_filename,
filter_str = fp_filter.read() 'EX: html_edit_newswire unable to read 2 ' +
except OSError: filter_filename)
print('EX: html_edit_newswire unable to read 2 ' + if filter_str is None:
filter_filename) filter_str = ''
edit_newswire_form += \ edit_newswire_form += \
' <br><b><label class="labels">' + \ ' <br><b><label class="labels">' + \
@ -698,13 +699,12 @@ def html_edit_newswire(translate: {}, base_dir: str, path: str,
hashtag_rules_str = '' hashtag_rules_str = ''
hashtag_rules_filename = data_dir(base_dir) + '/hashtagrules.txt' hashtag_rules_filename = data_dir(base_dir) + '/hashtagrules.txt'
if os.path.isfile(hashtag_rules_filename): if os.path.isfile(hashtag_rules_filename):
try: hashtag_rules_str = \
with open(hashtag_rules_filename, 'r', load_string(hashtag_rules_filename,
encoding='utf-8') as fp_rules: 'EX: html_edit_newswire unable to read 3 ' +
hashtag_rules_str = fp_rules.read() hashtag_rules_filename)
except OSError: if hashtag_rules_str is None:
print('EX: html_edit_newswire unable to read 3 ' + hashtag_rules_str = ''
hashtag_rules_filename)
edit_newswire_form += \ edit_newswire_form += \
' <br><b><label class="labels">' + \ ' <br><b><label class="labels">' + \

View File

@ -54,6 +54,7 @@ from cache import get_person_from_cache
from person import get_person_notes from person import get_person_notes
from textmode import text_mode_browser from textmode import text_mode_browser
from data import load_list from data import load_list
from data import load_string
def _html_new_post_drop_down(scope_icon: str, scope_description: str, def _html_new_post_drop_down(scope_icon: str, scope_description: str,
@ -566,21 +567,20 @@ def html_new_post(edit_post_params: {},
# custom report header with any additional instructions # custom report header with any additional instructions
dir_str = data_dir(base_dir) dir_str = data_dir(base_dir)
if os.path.isfile(dir_str + '/report.txt'): if os.path.isfile(dir_str + '/report.txt'):
try: custom_report_text = \
with open(dir_str + '/report.txt', 'r', load_string(dir_str + '/report.txt',
encoding='utf-8') as fp_report: 'EX: html_new_post unable to read ' +
custom_report_text = fp_report.read() dir_str + '/report.txt [ex]')
if '</p>' not in custom_report_text: if custom_report_text is None:
custom_report_text = \ custom_report_text = ''
'<p class="login-subtext">' + \ if '</p>' not in custom_report_text:
custom_report_text + '</p>\n' custom_report_text = \
rep_str = '<p class="login-subtext">' '<p class="login-subtext">' + \
custom_report_text = \ custom_report_text + '</p>\n'
custom_report_text.replace('<p>', rep_str) rep_str = '<p class="login-subtext">'
new_post_text += custom_report_text custom_report_text = \
except OSError as exc: custom_report_text.replace('<p>', rep_str)
print('EX: html_new_post unable to read ' + new_post_text += custom_report_text
dir_str + '/report.txt ' + str(exc))
idx = 'This message only goes to moderators, even if it ' + \ idx = 'This message only goes to moderators, even if it ' + \
'mentions other fediverse addresses.' 'mentions other fediverse addresses.'
@ -610,13 +610,12 @@ def html_new_post(edit_post_params: {},
# load post template if it exists # load post template if it exists
dir_str = data_dir(base_dir) dir_str = data_dir(base_dir)
if os.path.isfile(dir_str + '/newpost.txt'): if os.path.isfile(dir_str + '/newpost.txt'):
try: new_post_text = \
with open(dir_str + '/newpost.txt', 'r', load_string(dir_str + '/newpost.txt',
encoding='utf-8') as fp_new: 'EX: html_new_post unable to read ' +
new_post_text = '<p>' + fp_new.read() + '</p>\n' dir_str + '/newpost.txt')
except OSError: if new_post_text is None:
print('EX: html_new_post unable to read ' + new_post_text = ''
dir_str + '/newpost.txt')
css_filename = base_dir + '/epicyon-profile.css' css_filename = base_dir + '/epicyon-profile.css'
if os.path.isfile(base_dir + '/epicyon.css'): if os.path.isfile(base_dir + '/epicyon.css'):

View File

@ -40,6 +40,9 @@ from webapp_utils import get_search_banner_file
from webapp_utils import get_content_warning_button from webapp_utils import get_content_warning_button
from webapp_utils import html_header_with_external_style from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer from webapp_utils import html_footer
from data import load_string
from data import save_string
from data import load_line
def get_hashtag_categories_feed(base_dir: str, def get_hashtag_categories_feed(base_dir: str,
@ -104,13 +107,12 @@ def html_hash_tag_swarm(base_dir: str, actor: str, translate: {}) -> str:
blocked_str = '' blocked_str = ''
global_blocking_filename = data_dir(base_dir) + '/blocking.txt' global_blocking_filename = data_dir(base_dir) + '/blocking.txt'
if os.path.isfile(global_blocking_filename): if os.path.isfile(global_blocking_filename):
try: blocked_str = \
with open(global_blocking_filename, 'r', load_string(global_blocking_filename,
encoding='utf-8') as fp_block: 'EX: html_hash_tag_swarm unable to read ' +
blocked_str = fp_block.read() global_blocking_filename)
except OSError: if blocked_str is None:
print('EX: html_hash_tag_swarm unable to read ' + blocked_str = ''
global_blocking_filename)
for _, _, files in os.walk(base_dir + '/tags'): for _, _, files in os.walk(base_dir + '/tags'):
for fname in files: for fname in files:
@ -142,17 +144,15 @@ def html_hash_tag_swarm(base_dir: str, actor: str, translate: {}) -> str:
if '#' + hash_tag_name + '\n' in blocked_str: if '#' + hash_tag_name + '\n' in blocked_str:
continue continue
try: last_tag = \
with open(tags_filename, 'r', encoding='utf-8') as fp_tags: load_line(tags_filename,
# only read one line, which saves time and memory 'EX: html_hash_tag_swarm unable to read 2 ' +
last_tag = fp_tags.readline() tags_filename)
if not last_tag.startswith(days_since_epoch_str): if last_tag is None:
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 continue
if not last_tag.startswith(days_since_epoch_str):
if not last_tag.startswith(days_since_epoch_str2):
continue
try: try:
with open(tags_filename, 'r', encoding='utf-8') as fp_tags: with open(tags_filename, 'r', encoding='utf-8') as fp_tags:
@ -348,14 +348,10 @@ def _update_cached_hashtag_swarm(base_dir: str, nickname: str, domain: str,
actor = local_actor_url(http_prefix, nickname, domain_full) actor = local_actor_url(http_prefix, nickname, domain_full)
new_swarm_str = html_hash_tag_swarm(base_dir, actor, translate) new_swarm_str = html_hash_tag_swarm(base_dir, actor, translate)
if new_swarm_str: if new_swarm_str:
try: if save_string(new_swarm_str, cached_hashtag_swarm_filename,
with open(cached_hashtag_swarm_filename, 'w+', 'EX: unable to write cached hashtag swarm ' +
encoding='utf-8') as fp_swarm: cached_hashtag_swarm_filename):
fp_swarm.write(new_swarm_str) return True
return True
except OSError:
print('EX: unable to write cached hashtag swarm ' +
cached_hashtag_swarm_filename)
remove_old_hashtags(base_dir, 3) remove_old_hashtags(base_dir, 3)
return False return False
@ -378,29 +374,21 @@ def _store_tag_name(base_dir: str, nickname: str,
published, post_url) published, post_url)
hashtag_added = False hashtag_added = False
if not os.path.isfile(tags_filename): if not os.path.isfile(tags_filename):
try: if save_string(tag_line, tags_filename,
with open(tags_filename, 'w+', encoding='utf-8') as fp_tags: 'EX: store_hash_tags unable to write ' + tags_filename):
fp_tags.write(tag_line) hashtag_added = True
hashtag_added = True
except OSError:
print('EX: store_hash_tags unable to write ' + tags_filename)
else: else:
content = '' content = load_string(tags_filename,
try: 'EX: store_hash_tags failed to read ' +
with open(tags_filename, 'r', encoding='utf-8') as fp_tags: tags_filename)
content = fp_tags.read() if content is None:
except OSError: content = ''
print('EX: store_hash_tags failed to read ' + tags_filename)
if post_url not in content: if post_url not in content:
content = tag_line + content content = tag_line + content
try: if save_string(content, tags_filename,
with open(tags_filename, 'w+', 'EX: Failed to write entry to tags file ' +
encoding='utf-8') as fp_tags2: tags_filename + ' [ex]'):
fp_tags2.write(content) hashtag_added = True
hashtag_added = True
except OSError as ex:
print('EX: Failed to write entry to tags file ' +
tags_filename + ' ' + str(ex))
if not hashtag_added: if not hashtag_added:
return False return False

View File

@ -22,6 +22,7 @@ from webapp_utils import html_footer
from webapp_utils import html_keyboard_navigation from webapp_utils import html_keyboard_navigation
from textmode import text_mode_browser from textmode import text_mode_browser
from textmode import get_text_mode_logo from textmode import get_text_mode_logo
from data import load_string
def html_get_login_credentials(login_params: str, def html_get_login_credentials(login_params: str,
@ -142,13 +143,11 @@ def html_login(translate: {},
dir_str = data_dir(base_dir) dir_str = data_dir(base_dir)
if os.path.isfile(dir_str + '/login.txt'): if os.path.isfile(dir_str + '/login.txt'):
# custom login message # custom login message
try: login_text = load_string(dir_str + '/login.txt',
with open(dir_str + '/login.txt', 'r', 'EX: html_login unable to read ' + dir_str +
encoding='utf-8') as fp_login: '/login.txt')
login_text = \ if login_text is None:
'<p class="login-text">' + fp_login.read() + '</p>' login_text = ''
except OSError:
print('EX: html_login unable to read ' + dir_str + '/login.txt')
css_filename = base_dir + '/epicyon-login.css' css_filename = base_dir + '/epicyon-login.css'
if os.path.isfile(base_dir + '/login.css'): if os.path.isfile(base_dir + '/login.css'):

View File

@ -15,6 +15,7 @@ from webapp_utils import html_header_with_website_markup
from webapp_utils import html_footer from webapp_utils import html_footer
from markdown import markdown_example_numbers from markdown import markdown_example_numbers
from markdown import markdown_to_html from markdown import markdown_to_html
from data import load_string
def html_manual(base_dir: str, http_prefix: str, def html_manual(base_dir: str, http_prefix: str,
@ -35,13 +36,12 @@ def html_manual(base_dir: str, http_prefix: str,
manual_text = 'User Manual.' manual_text = 'User Manual.'
if os.path.isfile(manual_filename): if os.path.isfile(manual_filename):
try: md_text = load_string(manual_filename,
with open(manual_filename, 'r', 'EX: html_manual unable to read ' +
encoding='utf-8') as fp_manual: manual_filename)
md_text = markdown_example_numbers(fp_manual.read()) if md_text:
manual_text = markdown_to_html(md_text) md_text = markdown_example_numbers(md_text)
except OSError: manual_text = markdown_to_html(md_text)
print('EX: html_manual unable to read ' + manual_filename)
manual_form = '' manual_form = ''
css_filename = base_dir + '/epicyon-profile.css' css_filename = base_dir + '/epicyon-profile.css'

View File

@ -11,6 +11,7 @@ import os
from utils import data_dir from utils import data_dir
from utils import string_ends_with from utils import string_ends_with
from utils import valid_url_prefix from utils import valid_url_prefix
from data import load_string
def load_peertube_instances(base_dir: str, peertube_instances: []) -> None: def load_peertube_instances(base_dir: str, peertube_instances: []) -> None:
@ -19,16 +20,13 @@ def load_peertube_instances(base_dir: str, peertube_instances: []) -> None:
peertube_list = None peertube_list = None
peertube_instances_filename = data_dir(base_dir) + '/peertube.txt' peertube_instances_filename = data_dir(base_dir) + '/peertube.txt'
if os.path.isfile(peertube_instances_filename): if os.path.isfile(peertube_instances_filename):
try: peertube_str = \
with open(peertube_instances_filename, 'r', load_string(peertube_instances_filename,
encoding='utf-8') as fp_inst: 'EX: load_peertube_instances unable to read ' +
peertube_str = fp_inst.read() peertube_instances_filename + ' [ex]')
if peertube_str: if peertube_str:
peertube_str = peertube_str.replace('\r', '') peertube_str = peertube_str.replace('\r', '')
peertube_list = peertube_str.split('\n') 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: if not peertube_list:
return return
for url in peertube_list: for url in peertube_list:

View File

@ -9,6 +9,7 @@ __module_group__ = "Timeline"
import os import os
from utils import acct_dir from utils import acct_dir
from data import save_string
def is_minimal(base_dir: str, domain: str, nickname: str) -> bool: def is_minimal(base_dir: str, domain: str, nickname: str) -> bool:
@ -39,8 +40,5 @@ def set_minimal(base_dir: str, domain: str, nickname: str,
except OSError: except OSError:
print('EX: set_minimal unable to delete ' + minimal_filename) print('EX: set_minimal unable to delete ' + minimal_filename)
elif not minimal and not minimal_file_exists: elif not minimal and not minimal_file_exists:
try: save_string('\n', minimal_filename,
with open(minimal_filename, 'w+', encoding='utf-8') as fp_min: 'EX: unable to write minimal ' + minimal_filename)
fp_min.write('\n')
except OSError:
print('EX: unable to write minimal ' + minimal_filename)

View File

@ -36,6 +36,7 @@ from blocking import is_blocked_domain
from blocking import is_blocked from blocking import is_blocked
from session import create_session from session import create_session
from data import load_list from data import load_list
from data import load_string
def html_moderation(default_timeline: str, def html_moderation(default_timeline: str,
@ -450,23 +451,22 @@ def html_moderation_info(translate: {}, base_dir: str,
suspended_filename = dir_str + '/suspended.txt' suspended_filename = dir_str + '/suspended.txt'
if os.path.isfile(suspended_filename): if os.path.isfile(suspended_filename):
try: suspended_str = \
with open(suspended_filename, 'r', encoding='utf-8') as fp_sus: load_string(suspended_filename,
suspended_str = fp_sus.read() 'EX: html_moderation_info unable to read ' +
info_form += '<div class="container">\n' suspended_filename + ' [ex]')
info_form += ' <br><b>' + \ if suspended_str:
translate['Suspended accounts'] + '</b>' info_form += '<div class="container">\n'
info_form += ' <br>' + \ info_form += ' <br><b>' + \
translate['These are currently suspended'] translate['Suspended accounts'] + '</b>'
info_form += \ info_form += ' <br>' + \
' <textarea id="message" ' + \ translate['These are currently suspended']
'name="suspended" style="height:200px" ' + \ info_form += \
'spellcheck="false">' + suspended_str + '</textarea>\n' ' <textarea id="message" ' + \
info_form += '</div>\n' 'name="suspended" style="height:200px" ' + \
info_shown = True 'spellcheck="false">' + suspended_str + '</textarea>\n'
except OSError as exc: info_form += '</div>\n'
print('EX: html_moderation_info unable to read ' + info_shown = True
suspended_filename + ' ' + str(exc))
blocking_filename = dir_str + '/blocking.txt' blocking_filename = dir_str + '/blocking.txt'
if os.path.isfile(blocking_filename): if os.path.isfile(blocking_filename):
@ -511,22 +511,21 @@ def html_moderation_info(translate: {}, base_dir: str,
filters_filename = dir_str + '/filters.txt' filters_filename = dir_str + '/filters.txt'
if os.path.isfile(filters_filename): if os.path.isfile(filters_filename):
try: filtered_str = \
with open(filters_filename, 'r', encoding='utf-8') as fp_filt: load_string(filters_filename,
filtered_str = fp_filt.read() 'EX: html_moderation_info unable to read ' +
info_form += '<div class="container">\n' filters_filename + ' [ex]')
info_form += \ if filtered_str:
' <br><b>' + \ info_form += '<div class="container">\n'
translate['Filtered words'] + '</b>' info_form += \
info_form += \ ' <br><b>' + \
' <textarea id="message" ' + \ translate['Filtered words'] + '</b>'
'name="filtered" style="height:700px" ' + \ info_form += \
'spellcheck="true">' + filtered_str + '</textarea>\n' ' <textarea id="message" ' + \
info_form += '</div>\n' 'name="filtered" style="height:700px" ' + \
info_shown = True 'spellcheck="true">' + filtered_str + '</textarea>\n'
except OSError as exc: info_form += '</div>\n'
print('EX: html_moderation_info unable to read ' + info_shown = True
filters_filename + ' ' + str(exc))
if not info_shown: if not info_shown:
info_form += \ info_form += \

View File

@ -42,6 +42,8 @@ from webapp_utils import minimizing_attached_images
from blocking import allowed_announce from blocking import allowed_announce
from filters import is_filtered from filters import is_filtered
from availability import get_availability from availability import get_availability
from data import load_string
from data import save_string
def _minimize_attached_images(base_dir: str, nickname: str, domain: str, def _minimize_attached_images(base_dir: str, nickname: str, domain: str,
@ -74,23 +76,18 @@ def _minimize_attached_images(base_dir: str, nickname: str, domain: str,
minimize_handles = '' minimize_handles = ''
if os.path.isfile(minimize_filename): if os.path.isfile(minimize_filename):
print('Minimize file exists') print('Minimize file exists')
try: minimize_handles = \
with open(minimize_filename, 'r', load_string(minimize_filename,
encoding='utf-8') as fp_minimize: 'EX: minimize_attached_images ' + minimize_filename)
minimize_handles = fp_minimize.read() if minimize_handles is None:
except OSError: minimize_handles = ''
print('EX: minimize_attached_images ' + minimize_filename)
else: else:
# create a new minimize file from the following file # create a new minimize file from the following file
print('Creating minimize file ' + minimize_filename) print('Creating minimize file ' + minimize_filename)
if add: if add:
try: save_string('\n', minimize_filename,
with open(minimize_filename, 'w+', 'EX: minimize_attached_images unable to write ' +
encoding='utf-8') as fp_min: minimize_filename)
fp_min.write('')
except OSError:
print('EX: minimize_attached_images unable to write ' +
minimize_filename)
# already in the minimize file? # already in the minimize file?
if handle + '\n' in minimize_handles: if handle + '\n' in minimize_handles:
@ -100,24 +97,16 @@ def _minimize_attached_images(base_dir: str, nickname: str, domain: str,
return return
# remove from minimize file # remove from minimize file
minimize_handles = minimize_handles.replace(handle + '\n', '') minimize_handles = minimize_handles.replace(handle + '\n', '')
try: save_string(minimize_handles, minimize_filename,
with open(minimize_filename, 'w+', 'EX: minimize_attached_images 3 ' + minimize_filename)
encoding='utf-8') as fp_min:
fp_min.write(minimize_handles)
except OSError:
print('EX: minimize_attached_images 3 ' + minimize_filename)
else: else:
print(handle + ' not in followingMinimizeImages.txt') print(handle + ' not in followingMinimizeImages.txt')
# not already in the minimize file # not already in the minimize file
if add: if add:
# append to the list of handles # append to the list of handles
minimize_handles += handle + '\n' minimize_handles += handle + '\n'
try: save_string(minimize_handles, minimize_filename,
with open(minimize_filename, 'w+', 'EX: minimize_attached_images 4 ' + minimize_filename)
encoding='utf-8') as fp_min:
fp_min.write(minimize_handles)
except OSError:
print('EX: minimize_attached_images 4 ' + minimize_filename)
def person_minimize_images(base_dir: str, nickname: str, domain: str, def person_minimize_images(base_dir: str, nickname: str, domain: str,

View File

@ -146,6 +146,7 @@ from session import get_json
from blog import html_blog_post_markdown from blog import html_blog_post_markdown
from blog import html_blog_post_gemini_links from blog import html_blog_post_gemini_links
from data import load_list from data import load_list
from data import save_string
# maximum length for display name within html posts # maximum length for display name within html posts
MAX_DISPLAY_NAME_LENGTH = 42 MAX_DISPLAY_NAME_LENGTH = 42
@ -571,12 +572,9 @@ def _save_individual_post_as_html_to_cache(base_dir: str,
if not os.path.isdir(html_post_cache_dir): if not os.path.isdir(html_post_cache_dir):
os.mkdir(html_post_cache_dir) os.mkdir(html_post_cache_dir)
try: if save_string(post_html, cached_post_filename,
with open(cached_post_filename, 'w+', encoding='utf-8') as fp_cache: 'ERROR: saving post to cache, [ex]'):
fp_cache.write(post_html) return True
return True
except OSError as ex:
print('ERROR: saving post to cache, ' + str(ex))
return False return False
@ -2609,13 +2607,9 @@ def individual_post_as_html(signing_priv_key_pem: str,
translate, actor_url, translate, actor_url,
theme_name, system_language, theme_name, system_language,
box_name) box_name)
try: save_string('\n', announce_filename + '.tts',
with open(announce_filename + '.tts', 'w+', 'EX: unable to write tts ' +
encoding='utf-8') as fp_tts: announce_filename + '.tts')
fp_tts.write('\n')
except OSError:
print('EX: unable to write tts ' +
announce_filename + '.tts')
is_announced = True is_announced = True

View File

@ -137,6 +137,7 @@ from git import get_repo_url
from reading import html_profile_book_list from reading import html_profile_book_list
from availability import get_availability from availability import get_availability
from data import load_list from data import load_list
from data import load_string
THEME_FORMATS = '.zip, .gz' THEME_FORMATS = '.zip, .gz'
BLOCKFILE_FORMATS = '.csv' BLOCKFILE_FORMATS = '.csv'
@ -1631,11 +1632,9 @@ def html_profile(signing_priv_key_pem: str,
pinned_filename = account_dir + '/pinToProfile.txt' pinned_filename = account_dir + '/pinToProfile.txt'
pinned_content = None pinned_content = None
if os.path.isfile(pinned_filename): if os.path.isfile(pinned_filename):
try: pinned_content = load_string(pinned_filename,
with open(pinned_filename, 'r', encoding='utf-8') as fp_pin: 'EX: html_profile unable to read ' +
pinned_content = fp_pin.read() pinned_filename)
except OSError:
print('EX: html_profile unable to read ' + pinned_filename)
# shared items attached to the actor # shared items attached to the actor
# https://codeberg.org/fediverse/fep/src/branch/main/fep/0837/fep-0837.md # https://codeberg.org/fediverse/fep/src/branch/main/fep/0837/fep-0837.md
@ -2420,12 +2419,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
moderators = '' moderators = ''
moderators_file = data_dir(base_dir) + '/moderators.txt' moderators_file = data_dir(base_dir) + '/moderators.txt'
if os.path.isfile(moderators_file): if os.path.isfile(moderators_file):
try: moderators = \
with open(moderators_file, 'r', encoding='utf-8') as fp_mod: load_string(moderators_file,
moderators = fp_mod.read() 'EX: _html_edit_profile_instance unable to read ' +
except OSError: moderators_file)
print('EX: _html_edit_profile_instance unable to read ' + if moderators is None:
moderators_file) moderators = ''
subtitle = translate['A list of moderator nicknames. One per line.'] subtitle = translate['A list of moderator nicknames. One per line.']
role_assign_str += \ role_assign_str += \
edit_text_area('<b>' + translate['Moderators'] + '</b>', subtitle, edit_text_area('<b>' + translate['Moderators'] + '</b>', subtitle,
@ -2434,12 +2433,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
editors = '' editors = ''
editors_file = data_dir(base_dir) + '/editors.txt' editors_file = data_dir(base_dir) + '/editors.txt'
if os.path.isfile(editors_file): if os.path.isfile(editors_file):
try: editors = \
with open(editors_file, 'r', encoding='utf-8') as fp_edit: load_string(editors_file,
editors = fp_edit.read() 'EX: _html_edit_profile_instance unable to read ' +
except OSError: editors_file)
print('EX: _html_edit_profile_instance unable to read ' + if editors is None:
editors_file) editors = ''
subtitle = translate['A list of editor nicknames. One per line.'] subtitle = translate['A list of editor nicknames. One per line.']
role_assign_str += \ role_assign_str += \
edit_text_area('<b>' + translate['Site Editors'] + '</b>', edit_text_area('<b>' + translate['Site Editors'] + '</b>',
@ -2449,12 +2448,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
counselors = '' counselors = ''
counselors_file = data_dir(base_dir) + '/counselors.txt' counselors_file = data_dir(base_dir) + '/counselors.txt'
if os.path.isfile(counselors_file): if os.path.isfile(counselors_file):
try: counselors = \
with open(counselors_file, 'r', encoding='utf-8') as fp_co: load_string(counselors_file,
counselors = fp_co.read() 'EX: _html_edit_profile_instance unable to read ' +
except OSError: counselors_file)
print('EX: _html_edit_profile_instance unable to read ' + if counselors is None:
counselors_file) counselors = ''
role_assign_str += \ role_assign_str += \
edit_text_area('<b>' + translate['Counselors'] + '</b>', None, edit_text_area('<b>' + translate['Counselors'] + '</b>', None,
'counselors', counselors, 200, '', False) 'counselors', counselors, 200, '', False)
@ -2463,12 +2462,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
artists = '' artists = ''
artists_file = data_dir(base_dir) + '/artists.txt' artists_file = data_dir(base_dir) + '/artists.txt'
if os.path.isfile(artists_file): if os.path.isfile(artists_file):
try: artists = \
with open(artists_file, 'r', encoding='utf-8') as fp_art: load_string(artists_file,
artists = fp_art.read() 'EX: _html_edit_profile_instance unable to read ' +
except OSError: artists_file)
print('EX: _html_edit_profile_instance unable to read ' + if artists is None:
artists_file) artists = ''
role_assign_str += \ role_assign_str += \
edit_text_area('<b>' + translate['Artists'] + '</b>', None, edit_text_area('<b>' + translate['Artists'] + '</b>', None,
'artists', artists, 200, '', False) 'artists', artists, 200, '', False)
@ -2477,12 +2476,12 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
devops = '' devops = ''
devops_file = data_dir(base_dir) + '/devops.txt' devops_file = data_dir(base_dir) + '/devops.txt'
if os.path.isfile(devops_file): if os.path.isfile(devops_file):
try: devops = \
with open(devops_file, 'r', encoding='utf-8') as fp_edit: load_string(devops_file,
devops = fp_edit.read() 'EX: _html_edit_profile_instance unable to read ' +
except OSError: devops_file)
print('EX: _html_edit_profile_instance unable to read ' + if devops is None:
devops_file) devops = ''
subtitle = translate['A list of devops nicknames. One per line.'] subtitle = translate['A list of devops nicknames. One per line.']
role_assign_str += \ role_assign_str += \
edit_text_area('<b>' + translate['Site DevOps'] + '</b>', edit_text_area('<b>' + translate['Site DevOps'] + '</b>',
@ -2602,13 +2601,12 @@ def _html_edit_profile_git_projects(base_dir: str, nickname: str, domain: str,
git_projects_filename = \ git_projects_filename = \
acct_dir(base_dir, nickname, domain) + '/gitprojects.txt' acct_dir(base_dir, nickname, domain) + '/gitprojects.txt'
if os.path.isfile(git_projects_filename): if os.path.isfile(git_projects_filename):
try: git_projects_str = \
with open(git_projects_filename, 'r', load_string(git_projects_filename,
encoding='utf-8') as fp_git: 'EX: _html_edit_profile_git_projects unable to read ' +
git_projects_str = fp_git.read() git_projects_filename)
except OSError: if git_projects_str is None:
print('EX: _html_edit_profile_git_projects unable to read ' + git_projects_str = ''
git_projects_filename)
edit_profile_form = begin_edit_section(translate['Git Projects']) edit_profile_form = begin_edit_section(translate['Git Projects'])
idx = 'List of project names that you wish to receive git patches for' idx = 'List of project names that you wish to receive git patches for'
@ -2657,57 +2655,56 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
filter_filename = \ filter_filename = \
acct_dir(base_dir, nickname, domain) + '/filters.txt' acct_dir(base_dir, nickname, domain) + '/filters.txt'
if os.path.isfile(filter_filename): if os.path.isfile(filter_filename):
try: filter_str = \
with open(filter_filename, 'r', encoding='utf-8') as fp_filter: load_string(filter_filename,
filter_str = fp_filter.read() 'EX: _html_edit_profile_filtering unable to read ' +
except OSError: filter_filename)
print('EX: _html_edit_profile_filtering unable to read ' + if filter_str is None:
filter_filename) filter_str = ''
filter_bio_str = '' filter_bio_str = ''
filter_bio_filename = \ filter_bio_filename = \
acct_dir(base_dir, nickname, domain) + '/filters_bio.txt' acct_dir(base_dir, nickname, domain) + '/filters_bio.txt'
if os.path.isfile(filter_bio_filename): if os.path.isfile(filter_bio_filename):
try: filter_bio_str = \
with open(filter_bio_filename, 'r', load_string(filter_bio_filename,
encoding='utf-8') as fp_filter: 'EX: _html_edit_profile_filtering unable to read ' +
filter_bio_str = fp_filter.read() filter_bio_filename)
except OSError: if filter_bio_str is None:
print('EX: _html_edit_profile_filtering unable to read ' + filter_bio_str = ''
filter_bio_filename)
switch_str = '' switch_str = ''
switch_filename = \ switch_filename = \
acct_dir(base_dir, nickname, domain) + '/replacewords.txt' acct_dir(base_dir, nickname, domain) + '/replacewords.txt'
if os.path.isfile(switch_filename): if os.path.isfile(switch_filename):
try: switch_str = \
with open(switch_filename, 'r', encoding='utf-8') as fp_switch: load_string(switch_filename,
switch_str = fp_switch.read() 'EX: _html_edit_profile_filtering unable to save ' +
except OSError: switch_filename)
print('EX: _html_edit_profile_filtering unable to save ' + if switch_str is None:
switch_filename) switch_str = ''
auto_tags = '' auto_tags = ''
auto_tags_filename = \ auto_tags_filename = \
acct_dir(base_dir, nickname, domain) + '/autotags.txt' acct_dir(base_dir, nickname, domain) + '/autotags.txt'
if os.path.isfile(auto_tags_filename): if os.path.isfile(auto_tags_filename):
try: auto_tags = \
with open(auto_tags_filename, 'r', encoding='utf-8') as fp_auto: load_string(auto_tags_filename,
auto_tags = fp_auto.read() 'EX: _html_edit_profile_filtering unable to read ' +
except OSError: auto_tags_filename)
print('EX: _html_edit_profile_filtering unable to read ' + if auto_tags is None:
auto_tags_filename) auto_tags = ''
auto_cw = '' auto_cw = ''
auto_cw_filename = \ auto_cw_filename = \
acct_dir(base_dir, nickname, domain) + '/autocw.txt' acct_dir(base_dir, nickname, domain) + '/autocw.txt'
if os.path.isfile(auto_cw_filename): if os.path.isfile(auto_cw_filename):
try: auto_cw = \
with open(auto_cw_filename, 'r', encoding='utf-8') as fp_cw: load_string(auto_cw_filename,
auto_cw = fp_cw.read() 'EX: _html_edit_profile_filtering unable to read ' +
except OSError: auto_cw_filename)
print('EX: _html_edit_profile_filtering unable to read ' + if auto_cw is None:
auto_cw_filename) auto_cw = ''
blocked_str = get_account_blocks(base_dir, nickname, domain, debug) blocked_str = get_account_blocks(base_dir, nickname, domain, debug)
@ -2715,25 +2712,23 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
dm_allowed_instances_filename = \ dm_allowed_instances_filename = \
acct_dir(base_dir, nickname, domain) + '/dmAllowedInstances.txt' acct_dir(base_dir, nickname, domain) + '/dmAllowedInstances.txt'
if os.path.isfile(dm_allowed_instances_filename): if os.path.isfile(dm_allowed_instances_filename):
try: dm_allowed_instances_str = \
with open(dm_allowed_instances_filename, 'r', load_string(dm_allowed_instances_filename,
encoding='utf-8') as fp_dm: 'EX: _html_edit_profile_filtering unable to read ' +
dm_allowed_instances_str = fp_dm.read() dm_allowed_instances_filename)
except OSError: if dm_allowed_instances_str is None:
print('EX: _html_edit_profile_filtering unable to read ' + dm_allowed_instances_str = ''
dm_allowed_instances_filename)
allowed_instances_str = '' allowed_instances_str = ''
allowed_instances_filename = \ allowed_instances_filename = \
acct_dir(base_dir, nickname, domain) + '/allowedinstances.txt' acct_dir(base_dir, nickname, domain) + '/allowedinstances.txt'
if os.path.isfile(allowed_instances_filename): if os.path.isfile(allowed_instances_filename):
try: allowed_instances_str = \
with open(allowed_instances_filename, 'r', load_string(allowed_instances_filename,
encoding='utf-8') as fp_allow: 'EX: _html_edit_profile_filtering unable to read ' +
allowed_instances_str = fp_allow.read() allowed_instances_filename)
except OSError: if allowed_instances_str is None:
print('EX: _html_edit_profile_filtering unable to read ' + allowed_instances_str = ''
allowed_instances_filename)
edit_profile_form = begin_edit_section(translate['Filtering and Blocking']) edit_profile_form = begin_edit_section(translate['Filtering and Blocking'])
@ -2753,13 +2748,12 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
city = '' city = ''
city_filename = acct_dir(base_dir, nickname, domain) + '/city.txt' city_filename = acct_dir(base_dir, nickname, domain) + '/city.txt'
if os.path.isfile(city_filename): if os.path.isfile(city_filename):
try: city1 = \
with open(city_filename, 'r', encoding='utf-8') as fp_city: load_string(city_filename,
city1 = fp_city.read() 'EX: _html_edit_profile_filtering unable to read ' +
city = remove_eol(city1) city_filename)
except OSError: if city1:
print('EX: _html_edit_profile_filtering unable to read ' + city = remove_eol(city1)
city_filename)
locations_filename = base_dir + '/custom_locations.txt' locations_filename = base_dir + '/custom_locations.txt'
if not os.path.isfile(locations_filename): if not os.path.isfile(locations_filename):
locations_filename = base_dir + '/locations.txt' locations_filename = base_dir + '/locations.txt'
@ -2901,13 +2895,9 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
robots_txt_filename = data_dir(base_dir) + '/robots.txt' robots_txt_filename = data_dir(base_dir) + '/robots.txt'
robots_txt = '' robots_txt = ''
if os.path.isfile(robots_txt_filename): if os.path.isfile(robots_txt_filename):
new_robots_txt = '' new_robots_txt = \
try: load_string(robots_txt_filename,
with open(robots_txt_filename, 'r', 'EX: error reading 2 ' + robots_txt_filename)
encoding='utf-8') as fp_robots:
new_robots_txt = fp_robots.read()
except OSError:
print('EX: error reading 2 ' + robots_txt_filename)
if new_robots_txt: if new_robots_txt:
robots_txt = new_robots_txt robots_txt = new_robots_txt
edit_profile_form += \ edit_profile_form += \
@ -3134,17 +3124,19 @@ def _html_edit_notifications(base_dir: str, nickname: str, domain: str,
ntfy_topic_file = \ ntfy_topic_file = \
acct_dir(base_dir, nickname, domain) + '/.ntfy_topic' acct_dir(base_dir, nickname, domain) + '/.ntfy_topic'
if os.path.isfile(ntfy_url_file): if os.path.isfile(ntfy_url_file):
try: ntfy_url_str = \
with open(ntfy_url_file, 'r', encoding='utf-8') as fp_ntfy: load_string(ntfy_url_file,
ntfy_url = fp_ntfy.read() 'EX: _html_edit_notifications unable to read ' +
except OSError: ntfy_url_file)
print('EX: unable to read ' + ntfy_url_file) if ntfy_url_str:
ntfy_url = ntfy_url_str
if os.path.isfile(ntfy_topic_file): if os.path.isfile(ntfy_topic_file):
try: ntfy_topic = \
with open(ntfy_topic_file, 'r', encoding='utf-8') as fp_ntfy: load_string(ntfy_topic_file,
ntfy_topic = fp_ntfy.read() 'EX: _html_edit_notifications unable to read ' +
except OSError: ntfy_topic_file)
print('EX: unable to read ' + ntfy_topic_file) if ntfy_topic is None:
ntfy_topic = ''
edit_profile_form = begin_edit_section(translate['Notifications']) edit_profile_form = begin_edit_section(translate['Notifications'])
edit_profile_form += edit_text_field(translate['ntfy URL'], edit_profile_form += edit_text_field(translate['ntfy URL'],

View File

@ -9,6 +9,7 @@ __module_group__ = "Web Interface"
import os import os
from utils import remove_html from utils import remove_html
from data import load_string
def _get_variable_from_css(css_str: str, variable: str) -> str: def _get_variable_from_css(css_str: str, variable: str) -> str:
@ -37,12 +38,11 @@ def get_pwa_theme_colors(css_filename: str) -> (str, str):
if not os.path.isfile(css_filename): if not os.path.isfile(css_filename):
return pwa_theme_color, pwa_theme_background_color return pwa_theme_color, pwa_theme_background_color
css_str = '' css_str = load_string(css_filename,
try: 'EX: get_pwa_theme_colors unable to read ' +
with open(css_filename, 'r', encoding='utf-8') as fp_css: css_filename)
css_str = fp_css.read() if css_str is None:
except OSError: css_str = ''
print('EX: get_pwa_theme_colors unable to read ' + css_filename)
pwa_theme_color = \ pwa_theme_color = \
_get_variable_from_css(css_str, 'pwa-theme-color') _get_variable_from_css(css_str, 'pwa-theme-color')

View File

@ -58,6 +58,8 @@ from maps import html_hashtag_maps
from session import get_json_valid from session import get_json_valid
from session import get_json from session import get_json
from data import load_list from data import load_list
from data import load_string
from data import save_string
def html_search_emoji(translate: {}, base_dir: str, search_str: str, def html_search_emoji(translate: {}, base_dir: str, search_str: str,
@ -525,23 +527,20 @@ def html_search(translate: {}, base_dir: str, path: str, domain: str,
acct_dir(base_dir, search_nickname, domain) + '/.hashtagSwarm' acct_dir(base_dir, search_nickname, domain) + '/.hashtagSwarm'
swarm_str = '' swarm_str = ''
if os.path.isfile(cached_hashtag_swarm_filename): if os.path.isfile(cached_hashtag_swarm_filename):
try: swarm_str = \
with open(cached_hashtag_swarm_filename, 'r', load_string(cached_hashtag_swarm_filename,
encoding='utf-8') as fp_swarm: 'EX: ' +
swarm_str = fp_swarm.read() 'html_search unable to read cached hashtag swarm ' +
except OSError: cached_hashtag_swarm_filename)
print('EX: html_search unable to read cached hashtag swarm ' + if swarm_str is None:
cached_hashtag_swarm_filename) swarm_str = ''
if not swarm_str: if not swarm_str:
swarm_str = html_hash_tag_swarm(base_dir, actor, translate) swarm_str = html_hash_tag_swarm(base_dir, actor, translate)
if swarm_str: if swarm_str:
try: save_string(swarm_str, cached_hashtag_swarm_filename,
with open(cached_hashtag_swarm_filename, 'w+', 'EX: ' +
encoding='utf-8') as fp_hash: 'html_search unable to save cached hashtag swarm ' +
fp_hash.write(swarm_str) cached_hashtag_swarm_filename)
except OSError:
print('EX: html_search unable to save cached hashtag swarm ' +
cached_hashtag_swarm_filename)
follow_str += ' <p class="hashtagswarm">' + swarm_str + '</p><br>\n' follow_str += ' <p class="hashtagswarm">' + swarm_str + '</p><br>\n'
follow_str += ' </center>\n' follow_str += ' </center>\n'

View File

@ -15,6 +15,7 @@ from webapp_utils import html_header_with_website_markup
from webapp_utils import html_footer from webapp_utils import html_footer
from markdown import markdown_example_numbers from markdown import markdown_example_numbers
from markdown import markdown_to_html from markdown import markdown_to_html
from data import load_string
def html_specification(base_dir: str, http_prefix: str, def html_specification(base_dir: str, http_prefix: str,
@ -35,14 +36,12 @@ def html_specification(base_dir: str, http_prefix: str,
specification_text = 'ActivityPub Protocol Specification.' specification_text = 'ActivityPub Protocol Specification.'
if os.path.isfile(specification_filename): if os.path.isfile(specification_filename):
try: md_text = load_string(specification_filename,
with open(specification_filename, 'r', 'EX: html_specification unable to read ' +
encoding='utf-8') as fp_specification: specification_filename)
md_text = markdown_example_numbers(fp_specification.read()) if md_text:
specification_text = markdown_to_html(md_text) md_text = markdown_example_numbers(md_text)
except OSError: specification_text = markdown_to_html(md_text)
print('EX: html_specification unable to read ' +
specification_filename)
specification_form = '' specification_form = ''
css_filename = base_dir + '/epicyon-profile.css' css_filename = base_dir + '/epicyon-profile.css'

View File

@ -51,6 +51,7 @@ from announce import is_announce
from announce import is_self_announce from announce import is_self_announce
from question import is_html_question from question import is_html_question
from question import is_question from question import is_question
from data import load_string
def _log_timeline_timing(enable_timing_log: bool, timeline_start_time, def _log_timeline_timing(enable_timing_log: bool, timeline_start_time,
@ -100,12 +101,10 @@ def _get_help_for_timeline(base_dir: str, box_name: str) -> str:
get_config_param(base_dir, 'instanceTitle') get_config_param(base_dir, 'instanceTitle')
if not instance_title: if not instance_title:
instance_title = 'Epicyon' instance_title = 'Epicyon'
help_text = '' help_text = \
try: load_string(help_filename,
with open(help_filename, 'r', encoding='utf-8') as fp_help: 'EX: _get_help_for_timeline unable to read ' +
help_text = fp_help.read() help_filename)
except OSError:
print('EX: _get_help_for_timeline unable to read ' + help_filename)
if help_text: if help_text:
if dangerous_markup(help_text, False, []): if dangerous_markup(help_text, False, []):
return '' return ''
@ -504,14 +503,15 @@ def html_timeline(default_timeline: str,
if os.path.isfile(calendar_file): if os.path.isfile(calendar_file):
new_calendar_event = True new_calendar_event = True
calendar_image = 'calendar_notify.png' calendar_image = 'calendar_notify.png'
try: calendar_path_str = \
with open(calendar_file, 'r', encoding='utf-8') as fp_cal: load_string(calendar_file,
calendar_path = fp_cal.read().replace('##sent##', '') 'EX: html_timeline unable to read ' +
calendar_path = remove_eol(calendar_path) calendar_file)
if '/calendar' not in calendar_path: if calendar_path_str:
calendar_path = '/calendar' calendar_path = calendar_path_str.replace('##sent##', '')
except OSError: calendar_path = remove_eol(calendar_path)
print('EX: html_timeline unable to read ' + calendar_file) if '/calendar' not in calendar_path:
calendar_path = '/calendar'
# should the DM button be highlighted? # should the DM button be highlighted?
new_dm = False new_dm = False

View File

@ -15,6 +15,7 @@ from utils import local_actor_url
from webapp_utils import html_header_with_external_style from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer from webapp_utils import html_footer
from markdown import markdown_to_html from markdown import markdown_to_html
from data import load_string
def html_terms_of_service(base_dir: str, def html_terms_of_service(base_dir: str,
@ -34,12 +35,12 @@ def html_terms_of_service(base_dir: str,
tos_text = 'Terms of Service go here.' tos_text = 'Terms of Service go here.'
if os.path.isfile(dir_str + '/tos.md'): if os.path.isfile(dir_str + '/tos.md'):
try: tos_text_str = \
with open(dir_str + '/tos.md', 'r', encoding='utf-8') as fp_tos: load_string(dir_str + '/tos.md',
tos_text = markdown_to_html(fp_tos.read()) 'EX: html_terms_of_service unable to read ' +
except OSError: dir_str + '/tos.md')
print('EX: html_terms_of_service unable to read ' + if tos_text_str:
dir_str + '/tos.md') tos_text = markdown_to_html(tos_text_str)
tos_form = '' tos_form = ''
css_filename = base_dir + '/epicyon-profile.css' css_filename = base_dir + '/epicyon-profile.css'

View File

@ -60,6 +60,8 @@ from blocking import allowed_announce
from shares import vf_proposal_from_share from shares import vf_proposal_from_share
from webapp_pwa import get_pwa_theme_colors from webapp_pwa import get_pwa_theme_colors
from data import load_list from data import load_list
from data import save_string
from data import load_string
def minimizing_attached_images(base_dir: str, nickname: str, domain: str, def minimizing_attached_images(base_dir: str, nickname: str, domain: str,
@ -80,12 +82,8 @@ def minimizing_attached_images(base_dir: str, nickname: str, domain: str,
if not os.path.isfile(following_filename): if not os.path.isfile(following_filename):
return False return False
# create a new minimize file from the following file # create a new minimize file from the following file
try: save_string('\n', minimize_filename,
with open(minimize_filename, 'w+', 'EX: minimizing_attached_images 2 ' + minimize_filename)
encoding='utf-8') as fp_min:
fp_min.write('')
except OSError:
print('EX: minimizing_attached_images 2 ' + minimize_filename)
return text_in_file(handle + '\n', minimize_filename, False) return text_in_file(handle + '\n', minimize_filename, False)
@ -100,12 +98,9 @@ def get_broken_link_substitute() -> str:
def html_following_list(base_dir: str, following_filename: str) -> str: def html_following_list(base_dir: str, following_filename: str) -> str:
"""Returns a list of handles being followed """Returns a list of handles being followed
""" """
msg = '' msg = load_string(following_filename,
try: 'EX: html_following_list unable to read ' +
with open(following_filename, 'r', encoding='utf-8') as fp_following: following_filename)
msg = fp_following.read()
except OSError:
print('EX: html_following_list unable to read ' + following_filename)
if msg: if msg:
following_list = msg.split('\n') following_list = msg.split('\n')
following_list.sort() following_list.sort()
@ -135,12 +130,9 @@ def csv_following_list(following_filename: str,
base_dir: str, nickname: str, domain: str) -> str: base_dir: str, nickname: str, domain: str) -> str:
"""Returns a csv of handles being followed """Returns a csv of handles being followed
""" """
msg = '' msg = load_string(following_filename,
try: 'EX: csv_following_list unable to read ' +
with open(following_filename, 'r', encoding='utf-8') as fp_following: following_filename)
msg = fp_following.read()
except OSError:
print('EX: csv_following_list unable to read ' + following_filename)
if msg: if msg:
following_list = msg.split('\n') following_list = msg.split('\n')
following_list.sort() following_list.sort()
@ -1118,16 +1110,14 @@ def load_individual_post_as_html_from_cache(base_dir: str,
tries = 0 tries = 0
while tries < 3: while tries < 3:
try: post_html = \
with open(cached_post_filename, 'r', load_string(cached_post_filename,
encoding='utf-8') as fp_cached: 'ERROR: load_individual_post_as_html_from_cache ' +
post_html = fp_cached.read() str(tries) + ' [ex]')
break if post_html is not None:
except OSError as ex: break
print('ERROR: load_individual_post_as_html_from_cache ' + # no sleep
str(tries) + ' ' + str(ex)) tries += 1
# no sleep
tries += 1
if post_html: if post_html:
return post_html return post_html
@ -2288,33 +2278,30 @@ def html_following_data_list(base_dir: str, nickname: str,
acct_dir(base_dir, nickname, domain) + '/' + following_type + '.txt' acct_dir(base_dir, nickname, domain) + '/' + following_type + '.txt'
msg = '' msg = ''
if os.path.isfile(following_filename): if os.path.isfile(following_filename):
try: msg = load_string(following_filename,
with open(following_filename, 'r', 'EX: html_following_data_list unable to read ' +
encoding='utf-8') as fp_following: following_filename)
msg = fp_following.read() if msg is not None:
# add your own handle, so that you can send DMs # add your own handle, so that you can send DMs
# to yourself as reminders # to yourself as reminders
msg += nickname + '@' + domain_full + '\n' msg += nickname + '@' + domain_full + '\n'
except OSError: else:
print('EX: html_following_data_list unable to read ' + msg = ''
following_filename)
if msg: if msg:
# include petnames # include petnames
petnames_filename = \ petnames_filename = \
acct_dir(base_dir, nickname, domain) + '/petnames.txt' acct_dir(base_dir, nickname, domain) + '/petnames.txt'
if use_petnames and os.path.isfile(petnames_filename): if use_petnames and os.path.isfile(petnames_filename):
following_list: list[str] = [] following_list: list[str] = []
try: pet_str = \
with open(petnames_filename, 'r', load_string(petnames_filename,
encoding='utf-8') as fp_petnames: 'EX: html_following_data_list unable to read ' +
pet_str = fp_petnames.read() petnames_filename)
# extract each petname and append it if pet_str is not None:
petnames_list = pet_str.split('\n') # extract each petname and append it
for pet in petnames_list: petnames_list = pet_str.split('\n')
following_list.append(pet.split(' ')[0]) for pet in petnames_list:
except OSError: following_list.append(pet.split(' ')[0])
print('EX: html_following_data_list unable to read ' +
petnames_filename)
# add the following.txt entries # add the following.txt entries
following_list += msg.split('\n') following_list += msg.split('\n')
else: else:
@ -2344,33 +2331,30 @@ def html_following_dropdown(base_dir: str, nickname: str,
acct_dir(base_dir, nickname, domain) + '/' + following_type + '.txt' acct_dir(base_dir, nickname, domain) + '/' + following_type + '.txt'
msg = '' msg = ''
if os.path.isfile(following_filename): if os.path.isfile(following_filename):
try: msg = load_string(following_filename,
with open(following_filename, 'r', 'EX: html_following_dropdown unable to read ' +
encoding='utf-8') as fp_following: following_filename)
msg = fp_following.read() if msg is not None:
# add your own handle, so that you can send DMs # add your own handle, so that you can send DMs
# to yourself as reminders # to yourself as reminders
msg += nickname + '@' + domain_full + '\n' msg += nickname + '@' + domain_full + '\n'
except OSError: else:
print('EX: html_following_dropdown unable to read ' + msg = ''
following_filename)
if msg: if msg:
# include petnames # include petnames
petnames_filename = \ petnames_filename = \
acct_dir(base_dir, nickname, domain) + '/petnames.txt' acct_dir(base_dir, nickname, domain) + '/petnames.txt'
if use_petnames and os.path.isfile(petnames_filename): if use_petnames and os.path.isfile(petnames_filename):
following_list: list[str] = [] following_list: list[str] = []
try: pet_str = \
with open(petnames_filename, 'r', load_string(petnames_filename,
encoding='utf-8') as fp_petnames: 'EX: html_following_dropdown unable to read ' +
pet_str = fp_petnames.read() petnames_filename)
# extract each petname and append it if pet_str is not None:
petnames_list = pet_str.split('\n') # extract each petname and append it
for pet in petnames_list: petnames_list = pet_str.split('\n')
following_list.append(pet.split(' ')[0]) for pet in petnames_list:
except OSError: following_list.append(pet.split(' ')[0])
print('EX: html_following_dropdown unable to read ' +
petnames_filename)
# add the following.txt entries # add the following.txt entries
following_list += msg.split('\n') following_list += msg.split('\n')
else: else:

View File

@ -16,6 +16,8 @@ from utils import acct_dir
from webapp_utils import html_header_with_external_style from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer from webapp_utils import html_footer
from markdown import markdown_to_html from markdown import markdown_to_html
from data import save_string
from data import load_string
def is_welcome_screen_complete(base_dir: str, def is_welcome_screen_complete(base_dir: str,
@ -37,12 +39,9 @@ def welcome_screen_is_complete(base_dir: str,
if not os.path.isdir(account_path): if not os.path.isdir(account_path):
return return
complete_filename = account_path + '/.welcome_complete' complete_filename = account_path + '/.welcome_complete'
try: save_string('\n', complete_filename,
with open(complete_filename, 'w+', encoding='utf-8') as fp_comp: 'EX: welcome_screen_is_complete unable to write ' +
fp_comp.write('\n') complete_filename)
except OSError:
print('EX: welcome_screen_is_complete unable to write ' +
complete_filename)
def html_welcome_screen(base_dir: str, nickname: str, def html_welcome_screen(base_dir: str, nickname: str,
@ -83,14 +82,14 @@ def html_welcome_screen(base_dir: str, nickname: str,
instance_title = 'Epicyon' instance_title = 'Epicyon'
if os.path.isfile(welcome_filename): if os.path.isfile(welcome_filename):
try: welcome_text = load_string(welcome_filename,
with open(welcome_filename, 'r', encoding='utf-8') as fp_wel: 'EX: html_welcome_screen unable to read ' +
welcome_text = fp_wel.read() welcome_filename)
welcome_text = welcome_text.replace('INSTANCE', instance_title) if welcome_text is not None:
welcome_text = markdown_to_html(remove_html(welcome_text)) welcome_text = welcome_text.replace('INSTANCE', instance_title)
except OSError: welcome_text = markdown_to_html(remove_html(welcome_text))
print('EX: html_welcome_screen unable to read ' + welcome_filename) else:
welcome_text = ''
welcome_form = '' welcome_form = ''
css_filename = base_dir + '/epicyon-welcome.css' css_filename = base_dir + '/epicyon-welcome.css'
if os.path.isfile(base_dir + '/welcome.css'): if os.path.isfile(base_dir + '/welcome.css'):

View File

@ -15,6 +15,7 @@ from utils import get_config_param
from webapp_utils import html_header_with_external_style from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer from webapp_utils import html_footer
from markdown import markdown_to_html from markdown import markdown_to_html
from data import load_string
def html_welcome_final(base_dir: str, nickname: str, def html_welcome_final(base_dir: str, nickname: str,
@ -52,13 +53,14 @@ def html_welcome_final(base_dir: str, nickname: str,
instance_title = 'Epicyon' instance_title = 'Epicyon'
if os.path.isfile(final_filename): if os.path.isfile(final_filename):
try: final_text = load_string(final_filename,
with open(final_filename, 'r', encoding='utf-8') as fp_final: 'EX: html_welcome_final unable to read ' +
final_text = fp_final.read() final_filename)
final_text = final_text.replace('INSTANCE', instance_title) if final_text is not None:
final_text = markdown_to_html(remove_html(final_text)) final_text = final_text.replace('INSTANCE', instance_title)
except OSError: final_text = markdown_to_html(remove_html(final_text))
print('EX: html_welcome_final unable to read ' + final_filename) else:
final_text = ''
final_form = '' final_form = ''
css_filename = base_dir + '/epicyon-welcome.css' css_filename = base_dir + '/epicyon-welcome.css'

View File

@ -22,6 +22,7 @@ from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer from webapp_utils import html_footer
from webapp_utils import edit_text_field from webapp_utils import edit_text_field
from markdown import markdown_to_html from markdown import markdown_to_html
from data import load_string
def html_welcome_profile(base_dir: str, nickname: str, domain: str, def html_welcome_profile(base_dir: str, nickname: str, domain: str,
@ -60,14 +61,14 @@ def html_welcome_profile(base_dir: str, nickname: str, domain: str,
instance_title = 'Epicyon' instance_title = 'Epicyon'
if os.path.isfile(profile_filename): if os.path.isfile(profile_filename):
try: profile_text = load_string(profile_filename,
with open(profile_filename, 'r', encoding='utf-8') as fp_pro: 'EX: html_welcome_profile unable to read ' +
profile_text = fp_pro.read() profile_filename)
profile_text = profile_text.replace('INSTANCE', instance_title) if profile_text is not None:
profile_text = markdown_to_html(remove_html(profile_text)) profile_text = profile_text.replace('INSTANCE', instance_title)
except OSError: profile_text = markdown_to_html(remove_html(profile_text))
print('EX: html_welcome_profile unable to read ' + else:
profile_filename) profile_text = ''
profile_form = '' profile_form = ''
css_filename = base_dir + '/epicyon-welcome.css' css_filename = base_dir + '/epicyon-welcome.css'