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
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

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_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'

View File

@ -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_str = load_string(links_filename,
'EX: html_edit_links unable to read ' +
links_filename)
if links_str is None:
links_str = ''
edit_links_form += \
'<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_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_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 += \
'<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_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_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 += \
'<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_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_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 += \
'<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 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_str = \
load_string(newswire_filename,
'EX: html_edit_newswire unable to read ' +
newswire_filename)
if newswire_str is None:
newswire_str = ''
edit_newswire_form += \
'<div class="container">'
@ -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_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 += \
' <br><b><label class="labels">' + \
@ -698,13 +699,12 @@ 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):
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_str = \
load_string(hashtag_rules_filename,
'EX: html_edit_newswire unable to read 3 ' +
hashtag_rules_filename)
if hashtag_rules_str is None:
hashtag_rules_str = ''
edit_newswire_form += \
' <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 textmode import text_mode_browser
from data import load_list
from data import load_string
def _html_new_post_drop_down(scope_icon: str, scope_description: str,
@ -566,10 +567,12 @@ 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'):
try:
with open(dir_str + '/report.txt', 'r',
encoding='utf-8') as fp_report:
custom_report_text = fp_report.read()
custom_report_text = \
load_string(dir_str + '/report.txt',
'EX: html_new_post unable to read ' +
dir_str + '/report.txt [ex]')
if custom_report_text is None:
custom_report_text = ''
if '</p>' not in custom_report_text:
custom_report_text = \
'<p class="login-subtext">' + \
@ -578,9 +581,6 @@ def html_new_post(edit_post_params: {},
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.'
@ -610,13 +610,12 @@ def html_new_post(edit_post_params: {},
# load post template if it exists
dir_str = data_dir(base_dir)
if os.path.isfile(dir_str + '/newpost.txt'):
try:
with open(dir_str + '/newpost.txt', 'r',
encoding='utf-8') as fp_new:
new_post_text = '<p>' + fp_new.read() + '</p>\n'
except OSError:
print('EX: html_new_post unable to read ' +
new_post_text = \
load_string(dir_str + '/newpost.txt',
'EX: html_new_post unable to read ' +
dir_str + '/newpost.txt')
if new_post_text is None:
new_post_text = ''
css_filename = base_dir + '/epicyon-profile.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 html_header_with_external_style
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,
@ -104,13 +107,12 @@ 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):
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 ' +
blocked_str = \
load_string(global_blocking_filename,
'EX: html_hash_tag_swarm unable to read ' +
global_blocking_filename)
if blocked_str is None:
blocked_str = ''
for _, _, files in os.walk(base_dir + '/tags'):
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:
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()
last_tag = \
load_line(tags_filename,
'EX: html_hash_tag_swarm unable to read 2 ' +
tags_filename)
if last_tag is None:
continue
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
try:
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)
new_swarm_str = html_hash_tag_swarm(base_dir, actor, translate)
if new_swarm_str:
try:
with open(cached_hashtag_swarm_filename, 'w+',
encoding='utf-8') as fp_swarm:
fp_swarm.write(new_swarm_str)
if save_string(new_swarm_str, cached_hashtag_swarm_filename,
'EX: unable to write cached hashtag swarm ' +
cached_hashtag_swarm_filename):
return True
except OSError:
print('EX: unable to write cached hashtag swarm ' +
cached_hashtag_swarm_filename)
remove_old_hashtags(base_dir, 3)
return False
@ -378,29 +374,21 @@ def _store_tag_name(base_dir: str, nickname: str,
published, post_url)
hashtag_added = False
if not os.path.isfile(tags_filename):
try:
with open(tags_filename, 'w+', encoding='utf-8') as fp_tags:
fp_tags.write(tag_line)
if save_string(tag_line, tags_filename,
'EX: store_hash_tags unable to write ' + tags_filename):
hashtag_added = True
except OSError:
print('EX: store_hash_tags unable to write ' + tags_filename)
else:
content = load_string(tags_filename,
'EX: store_hash_tags failed to read ' +
tags_filename)
if content is None:
content = ''
try:
with open(tags_filename, 'r', encoding='utf-8') as fp_tags:
content = fp_tags.read()
except OSError:
print('EX: store_hash_tags failed to read ' + tags_filename)
if post_url not in content:
content = tag_line + content
try:
with open(tags_filename, 'w+',
encoding='utf-8') as fp_tags2:
fp_tags2.write(content)
if save_string(content, tags_filename,
'EX: Failed to write entry to tags file ' +
tags_filename + ' [ex]'):
hashtag_added = True
except OSError as ex:
print('EX: Failed to write entry to tags file ' +
tags_filename + ' ' + str(ex))
if not hashtag_added:
return False

View File

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

@ -11,6 +11,7 @@ import os
from utils import data_dir
from utils import string_ends_with
from utils import valid_url_prefix
from data import load_string
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_instances_filename = data_dir(base_dir) + '/peertube.txt'
if os.path.isfile(peertube_instances_filename):
try:
with open(peertube_instances_filename, 'r',
encoding='utf-8') as fp_inst:
peertube_str = fp_inst.read()
peertube_str = \
load_string(peertube_instances_filename,
'EX: load_peertube_instances unable to read ' +
peertube_instances_filename + ' [ex]')
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

@ -9,6 +9,7 @@ __module_group__ = "Timeline"
import os
from utils import acct_dir
from data import save_string
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:
print('EX: set_minimal unable to delete ' + minimal_filename)
elif not minimal and not minimal_file_exists:
try:
with open(minimal_filename, 'w+', encoding='utf-8') as fp_min:
fp_min.write('\n')
except OSError:
print('EX: unable to write minimal ' + minimal_filename)
save_string('\n', minimal_filename,
'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 session import create_session
from data import load_list
from data import load_string
def html_moderation(default_timeline: str,
@ -450,9 +451,11 @@ def html_moderation_info(translate: {}, base_dir: str,
suspended_filename = dir_str + '/suspended.txt'
if os.path.isfile(suspended_filename):
try:
with open(suspended_filename, 'r', encoding='utf-8') as fp_sus:
suspended_str = fp_sus.read()
suspended_str = \
load_string(suspended_filename,
'EX: html_moderation_info unable to read ' +
suspended_filename + ' [ex]')
if suspended_str:
info_form += '<div class="container">\n'
info_form += ' <br><b>' + \
translate['Suspended accounts'] + '</b>'
@ -464,9 +467,6 @@ def html_moderation_info(translate: {}, base_dir: str,
'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):
@ -511,9 +511,11 @@ def html_moderation_info(translate: {}, base_dir: str,
filters_filename = dir_str + '/filters.txt'
if os.path.isfile(filters_filename):
try:
with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
filtered_str = fp_filt.read()
filtered_str = \
load_string(filters_filename,
'EX: html_moderation_info unable to read ' +
filters_filename + ' [ex]')
if filtered_str:
info_form += '<div class="container">\n'
info_form += \
' <br><b>' + \
@ -524,9 +526,6 @@ def html_moderation_info(translate: {}, base_dir: str,
'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 += \

View File

@ -42,6 +42,8 @@ from webapp_utils import minimizing_attached_images
from blocking import allowed_announce
from filters import is_filtered
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,
@ -74,22 +76,17 @@ def _minimize_attached_images(base_dir: str, nickname: str, domain: str,
minimize_handles = ''
if os.path.isfile(minimize_filename):
print('Minimize file exists')
try:
with open(minimize_filename, 'r',
encoding='utf-8') as fp_minimize:
minimize_handles = fp_minimize.read()
except OSError:
print('EX: minimize_attached_images ' + minimize_filename)
minimize_handles = \
load_string(minimize_filename,
'EX: minimize_attached_images ' + minimize_filename)
if minimize_handles is None:
minimize_handles = ''
else:
# create a new minimize file from the following file
print('Creating minimize file ' + minimize_filename)
if add:
try:
with open(minimize_filename, 'w+',
encoding='utf-8') as fp_min:
fp_min.write('')
except OSError:
print('EX: minimize_attached_images unable to write ' +
save_string('\n', minimize_filename,
'EX: minimize_attached_images unable to write ' +
minimize_filename)
# already in the minimize file?
@ -100,24 +97,16 @@ def _minimize_attached_images(base_dir: str, nickname: str, domain: str,
return
# remove from minimize file
minimize_handles = minimize_handles.replace(handle + '\n', '')
try:
with open(minimize_filename, 'w+',
encoding='utf-8') as fp_min:
fp_min.write(minimize_handles)
except OSError:
print('EX: minimize_attached_images 3 ' + minimize_filename)
save_string(minimize_handles, minimize_filename,
'EX: minimize_attached_images 3 ' + minimize_filename)
else:
print(handle + ' not in followingMinimizeImages.txt')
# not already in the minimize file
if add:
# append to the list of handles
minimize_handles += handle + '\n'
try:
with open(minimize_filename, 'w+',
encoding='utf-8') as fp_min:
fp_min.write(minimize_handles)
except OSError:
print('EX: minimize_attached_images 4 ' + minimize_filename)
save_string(minimize_handles, minimize_filename,
'EX: minimize_attached_images 4 ' + minimize_filename)
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_gemini_links
from data import load_list
from data import save_string
# maximum length for display name within html posts
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):
os.mkdir(html_post_cache_dir)
try:
with open(cached_post_filename, 'w+', encoding='utf-8') as fp_cache:
fp_cache.write(post_html)
if save_string(post_html, cached_post_filename,
'ERROR: saving post to cache, [ex]'):
return True
except OSError as ex:
print('ERROR: saving post to cache, ' + str(ex))
return False
@ -2609,12 +2607,8 @@ def individual_post_as_html(signing_priv_key_pem: str,
translate, actor_url,
theme_name, system_language,
box_name)
try:
with open(announce_filename + '.tts', 'w+',
encoding='utf-8') as fp_tts:
fp_tts.write('\n')
except OSError:
print('EX: unable to write tts ' +
save_string('\n', announce_filename + '.tts',
'EX: unable to write tts ' +
announce_filename + '.tts')
is_announced = True

View File

@ -137,6 +137,7 @@ from git import get_repo_url
from reading import html_profile_book_list
from availability import get_availability
from data import load_list
from data import load_string
THEME_FORMATS = '.zip, .gz'
BLOCKFILE_FORMATS = '.csv'
@ -1631,11 +1632,9 @@ def html_profile(signing_priv_key_pem: str,
pinned_filename = account_dir + '/pinToProfile.txt'
pinned_content = None
if os.path.isfile(pinned_filename):
try:
with open(pinned_filename, 'r', encoding='utf-8') as fp_pin:
pinned_content = fp_pin.read()
except OSError:
print('EX: html_profile unable to read ' + pinned_filename)
pinned_content = load_string(pinned_filename,
'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
@ -2420,12 +2419,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):
try:
with open(moderators_file, 'r', encoding='utf-8') as fp_mod:
moderators = fp_mod.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
moderators = \
load_string(moderators_file,
'EX: _html_edit_profile_instance unable to read ' +
moderators_file)
if moderators is None:
moderators = ''
subtitle = translate['A list of moderator nicknames. One per line.']
role_assign_str += \
edit_text_area('<b>' + translate['Moderators'] + '</b>', subtitle,
@ -2434,12 +2433,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):
try:
with open(editors_file, 'r', encoding='utf-8') as fp_edit:
editors = fp_edit.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
editors = \
load_string(editors_file,
'EX: _html_edit_profile_instance unable to read ' +
editors_file)
if editors is None:
editors = ''
subtitle = translate['A list of editor nicknames. One per line.']
role_assign_str += \
edit_text_area('<b>' + translate['Site Editors'] + '</b>',
@ -2449,12 +2448,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):
try:
with open(counselors_file, 'r', encoding='utf-8') as fp_co:
counselors = fp_co.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
counselors = \
load_string(counselors_file,
'EX: _html_edit_profile_instance unable to read ' +
counselors_file)
if counselors is None:
counselors = ''
role_assign_str += \
edit_text_area('<b>' + translate['Counselors'] + '</b>', None,
'counselors', counselors, 200, '', False)
@ -2463,12 +2462,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):
try:
with open(artists_file, 'r', encoding='utf-8') as fp_art:
artists = fp_art.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
artists = \
load_string(artists_file,
'EX: _html_edit_profile_instance unable to read ' +
artists_file)
if artists is None:
artists = ''
role_assign_str += \
edit_text_area('<b>' + translate['Artists'] + '</b>', None,
'artists', artists, 200, '', False)
@ -2477,12 +2476,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):
try:
with open(devops_file, 'r', encoding='utf-8') as fp_edit:
devops = fp_edit.read()
except OSError:
print('EX: _html_edit_profile_instance unable to read ' +
devops = \
load_string(devops_file,
'EX: _html_edit_profile_instance unable to read ' +
devops_file)
if devops is None:
devops = ''
subtitle = translate['A list of devops nicknames. One per line.']
role_assign_str += \
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 = \
acct_dir(base_dir, nickname, domain) + '/gitprojects.txt'
if os.path.isfile(git_projects_filename):
try:
with open(git_projects_filename, 'r',
encoding='utf-8') as fp_git:
git_projects_str = fp_git.read()
except OSError:
print('EX: _html_edit_profile_git_projects unable to read ' +
git_projects_str = \
load_string(git_projects_filename,
'EX: _html_edit_profile_git_projects unable to read ' +
git_projects_filename)
if git_projects_str is None:
git_projects_str = ''
edit_profile_form = begin_edit_section(translate['Git Projects'])
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 = \
acct_dir(base_dir, nickname, 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_profile_filtering unable to read ' +
filter_str = \
load_string(filter_filename,
'EX: _html_edit_profile_filtering unable to read ' +
filter_filename)
if filter_str is None:
filter_str = ''
filter_bio_str = ''
filter_bio_filename = \
acct_dir(base_dir, nickname, domain) + '/filters_bio.txt'
if os.path.isfile(filter_bio_filename):
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_str = \
load_string(filter_bio_filename,
'EX: _html_edit_profile_filtering unable to read ' +
filter_bio_filename)
if filter_bio_str is None:
filter_bio_str = ''
switch_str = ''
switch_filename = \
acct_dir(base_dir, nickname, domain) + '/replacewords.txt'
if os.path.isfile(switch_filename):
try:
with open(switch_filename, 'r', encoding='utf-8') as fp_switch:
switch_str = fp_switch.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to save ' +
switch_str = \
load_string(switch_filename,
'EX: _html_edit_profile_filtering unable to save ' +
switch_filename)
if switch_str is None:
switch_str = ''
auto_tags = ''
auto_tags_filename = \
acct_dir(base_dir, nickname, domain) + '/autotags.txt'
if os.path.isfile(auto_tags_filename):
try:
with open(auto_tags_filename, 'r', encoding='utf-8') as fp_auto:
auto_tags = fp_auto.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
auto_tags = \
load_string(auto_tags_filename,
'EX: _html_edit_profile_filtering unable to read ' +
auto_tags_filename)
if auto_tags is None:
auto_tags = ''
auto_cw = ''
auto_cw_filename = \
acct_dir(base_dir, nickname, domain) + '/autocw.txt'
if os.path.isfile(auto_cw_filename):
try:
with open(auto_cw_filename, 'r', encoding='utf-8') as fp_cw:
auto_cw = fp_cw.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
auto_cw = \
load_string(auto_cw_filename,
'EX: _html_edit_profile_filtering unable to read ' +
auto_cw_filename)
if auto_cw is None:
auto_cw = ''
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 = \
acct_dir(base_dir, nickname, domain) + '/dmAllowedInstances.txt'
if os.path.isfile(dm_allowed_instances_filename):
try:
with open(dm_allowed_instances_filename, 'r',
encoding='utf-8') as fp_dm:
dm_allowed_instances_str = fp_dm.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
dm_allowed_instances_str = \
load_string(dm_allowed_instances_filename,
'EX: _html_edit_profile_filtering unable to read ' +
dm_allowed_instances_filename)
if dm_allowed_instances_str is None:
dm_allowed_instances_str = ''
allowed_instances_str = ''
allowed_instances_filename = \
acct_dir(base_dir, nickname, domain) + '/allowedinstances.txt'
if os.path.isfile(allowed_instances_filename):
try:
with open(allowed_instances_filename, 'r',
encoding='utf-8') as fp_allow:
allowed_instances_str = fp_allow.read()
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
allowed_instances_str = \
load_string(allowed_instances_filename,
'EX: _html_edit_profile_filtering unable to read ' +
allowed_instances_filename)
if allowed_instances_str is None:
allowed_instances_str = ''
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_filename = acct_dir(base_dir, nickname, domain) + '/city.txt'
if os.path.isfile(city_filename):
try:
with open(city_filename, 'r', encoding='utf-8') as fp_city:
city1 = fp_city.read()
city = remove_eol(city1)
except OSError:
print('EX: _html_edit_profile_filtering unable to read ' +
city1 = \
load_string(city_filename,
'EX: _html_edit_profile_filtering unable to read ' +
city_filename)
if city1:
city = remove_eol(city1)
locations_filename = base_dir + '/custom_locations.txt'
if not os.path.isfile(locations_filename):
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 = ''
if os.path.isfile(robots_txt_filename):
new_robots_txt = ''
try:
with open(robots_txt_filename, 'r',
encoding='utf-8') as fp_robots:
new_robots_txt = fp_robots.read()
except OSError:
print('EX: error reading 2 ' + robots_txt_filename)
new_robots_txt = \
load_string(robots_txt_filename,
'EX: error reading 2 ' + robots_txt_filename)
if new_robots_txt:
robots_txt = new_robots_txt
edit_profile_form += \
@ -3134,17 +3124,19 @@ def _html_edit_notifications(base_dir: str, nickname: str, domain: str,
ntfy_topic_file = \
acct_dir(base_dir, nickname, domain) + '/.ntfy_topic'
if os.path.isfile(ntfy_url_file):
try:
with open(ntfy_url_file, 'r', encoding='utf-8') as fp_ntfy:
ntfy_url = fp_ntfy.read()
except OSError:
print('EX: unable to read ' + ntfy_url_file)
ntfy_url_str = \
load_string(ntfy_url_file,
'EX: _html_edit_notifications unable to read ' +
ntfy_url_file)
if ntfy_url_str:
ntfy_url = ntfy_url_str
if os.path.isfile(ntfy_topic_file):
try:
with open(ntfy_topic_file, 'r', encoding='utf-8') as fp_ntfy:
ntfy_topic = fp_ntfy.read()
except OSError:
print('EX: unable to read ' + ntfy_topic_file)
ntfy_topic = \
load_string(ntfy_topic_file,
'EX: _html_edit_notifications unable to read ' +
ntfy_topic_file)
if ntfy_topic is None:
ntfy_topic = ''
edit_profile_form = begin_edit_section(translate['Notifications'])
edit_profile_form += edit_text_field(translate['ntfy URL'],

View File

@ -9,6 +9,7 @@ __module_group__ = "Web Interface"
import os
from utils import remove_html
from data import load_string
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):
return pwa_theme_color, pwa_theme_background_color
css_str = load_string(css_filename,
'EX: get_pwa_theme_colors unable to read ' +
css_filename)
if css_str is None:
css_str = ''
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

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

View File

@ -15,6 +15,7 @@ from webapp_utils import html_header_with_website_markup
from webapp_utils import html_footer
from markdown import markdown_example_numbers
from markdown import markdown_to_html
from data import load_string
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.'
if os.path.isfile(specification_filename):
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 ' +
md_text = load_string(specification_filename,
'EX: html_specification unable to read ' +
specification_filename)
if md_text:
md_text = markdown_example_numbers(md_text)
specification_text = markdown_to_html(md_text)
specification_form = ''
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 question import is_html_question
from question import is_question
from data import load_string
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')
if not instance_title:
instance_title = 'Epicyon'
help_text = ''
try:
with open(help_filename, 'r', encoding='utf-8') as fp_help:
help_text = fp_help.read()
except OSError:
print('EX: _get_help_for_timeline unable to read ' + help_filename)
help_text = \
load_string(help_filename,
'EX: _get_help_for_timeline unable to read ' +
help_filename)
if help_text:
if dangerous_markup(help_text, False, []):
return ''
@ -504,14 +503,15 @@ def html_timeline(default_timeline: str,
if os.path.isfile(calendar_file):
new_calendar_event = True
calendar_image = 'calendar_notify.png'
try:
with open(calendar_file, 'r', encoding='utf-8') as fp_cal:
calendar_path = fp_cal.read().replace('##sent##', '')
calendar_path_str = \
load_string(calendar_file,
'EX: html_timeline unable to read ' +
calendar_file)
if calendar_path_str:
calendar_path = calendar_path_str.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

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_footer
from markdown import markdown_to_html
from data import load_string
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.'
if os.path.isfile(dir_str + '/tos.md'):
try:
with open(dir_str + '/tos.md', 'r', encoding='utf-8') as fp_tos:
tos_text = markdown_to_html(fp_tos.read())
except OSError:
print('EX: html_terms_of_service unable to read ' +
tos_text_str = \
load_string(dir_str + '/tos.md',
'EX: html_terms_of_service unable to read ' +
dir_str + '/tos.md')
if tos_text_str:
tos_text = markdown_to_html(tos_text_str)
tos_form = ''
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 webapp_pwa import get_pwa_theme_colors
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,
@ -80,12 +82,8 @@ def minimizing_attached_images(base_dir: str, nickname: str, domain: str,
if not os.path.isfile(following_filename):
return False
# create a new minimize file from the following file
try:
with open(minimize_filename, 'w+',
encoding='utf-8') as fp_min:
fp_min.write('')
except OSError:
print('EX: minimizing_attached_images 2 ' + minimize_filename)
save_string('\n', minimize_filename,
'EX: minimizing_attached_images 2 ' + minimize_filename)
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:
"""Returns a list of handles being followed
"""
msg = ''
try:
with open(following_filename, 'r', encoding='utf-8') as fp_following:
msg = fp_following.read()
except OSError:
print('EX: html_following_list unable to read ' + following_filename)
msg = load_string(following_filename,
'EX: html_following_list unable to read ' +
following_filename)
if msg:
following_list = msg.split('\n')
following_list.sort()
@ -135,12 +130,9 @@ def csv_following_list(following_filename: str,
base_dir: str, nickname: str, domain: str) -> str:
"""Returns a csv of handles being followed
"""
msg = ''
try:
with open(following_filename, 'r', encoding='utf-8') as fp_following:
msg = fp_following.read()
except OSError:
print('EX: csv_following_list unable to read ' + following_filename)
msg = load_string(following_filename,
'EX: csv_following_list unable to read ' +
following_filename)
if msg:
following_list = msg.split('\n')
following_list.sort()
@ -1118,14 +1110,12 @@ def load_individual_post_as_html_from_cache(base_dir: str,
tries = 0
while tries < 3:
try:
with open(cached_post_filename, 'r',
encoding='utf-8') as fp_cached:
post_html = fp_cached.read()
post_html = \
load_string(cached_post_filename,
'ERROR: load_individual_post_as_html_from_cache ' +
str(tries) + ' [ex]')
if post_html is not None:
break
except OSError as ex:
print('ERROR: load_individual_post_as_html_from_cache ' +
str(tries) + ' ' + str(ex))
# no sleep
tries += 1
if 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'
msg = ''
if os.path.isfile(following_filename):
try:
with open(following_filename, 'r',
encoding='utf-8') as fp_following:
msg = fp_following.read()
msg = load_string(following_filename,
'EX: html_following_data_list unable to read ' +
following_filename)
if msg is not None:
# 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)
else:
msg = ''
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: list[str] = []
try:
with open(petnames_filename, 'r',
encoding='utf-8') as fp_petnames:
pet_str = fp_petnames.read()
pet_str = \
load_string(petnames_filename,
'EX: html_following_data_list unable to read ' +
petnames_filename)
if pet_str is not None:
# 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:
@ -2344,33 +2331,30 @@ def html_following_dropdown(base_dir: str, nickname: str,
acct_dir(base_dir, nickname, domain) + '/' + following_type + '.txt'
msg = ''
if os.path.isfile(following_filename):
try:
with open(following_filename, 'r',
encoding='utf-8') as fp_following:
msg = fp_following.read()
msg = load_string(following_filename,
'EX: html_following_dropdown unable to read ' +
following_filename)
if msg is not None:
# 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)
else:
msg = ''
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: list[str] = []
try:
with open(petnames_filename, 'r',
encoding='utf-8') as fp_petnames:
pet_str = fp_petnames.read()
pet_str = \
load_string(petnames_filename,
'EX: html_following_dropdown unable to read ' +
petnames_filename)
if pet_str is not None:
# 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

@ -16,6 +16,8 @@ from utils import acct_dir
from webapp_utils import html_header_with_external_style
from webapp_utils import html_footer
from markdown import markdown_to_html
from data import save_string
from data import load_string
def is_welcome_screen_complete(base_dir: str,
@ -37,11 +39,8 @@ def welcome_screen_is_complete(base_dir: str,
if not os.path.isdir(account_path):
return
complete_filename = account_path + '/.welcome_complete'
try:
with open(complete_filename, 'w+', encoding='utf-8') as fp_comp:
fp_comp.write('\n')
except OSError:
print('EX: welcome_screen_is_complete unable to write ' +
save_string('\n', complete_filename,
'EX: welcome_screen_is_complete unable to write ' +
complete_filename)
@ -83,14 +82,14 @@ def html_welcome_screen(base_dir: str, nickname: str,
instance_title = 'Epicyon'
if os.path.isfile(welcome_filename):
try:
with open(welcome_filename, 'r', encoding='utf-8') as fp_wel:
welcome_text = fp_wel.read()
welcome_text = load_string(welcome_filename,
'EX: html_welcome_screen unable to read ' +
welcome_filename)
if welcome_text is not None:
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)
else:
welcome_text = ''
welcome_form = ''
css_filename = base_dir + '/epicyon-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_footer
from markdown import markdown_to_html
from data import load_string
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'
if os.path.isfile(final_filename):
try:
with open(final_filename, 'r', encoding='utf-8') as fp_final:
final_text = fp_final.read()
final_text = load_string(final_filename,
'EX: html_welcome_final unable to read ' +
final_filename)
if final_text is not None:
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)
else:
final_text = ''
final_form = ''
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 edit_text_field
from markdown import markdown_to_html
from data import load_string
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'
if os.path.isfile(profile_filename):
try:
with open(profile_filename, 'r', encoding='utf-8') as fp_pro:
profile_text = fp_pro.read()
profile_text = load_string(profile_filename,
'EX: html_welcome_profile unable to read ' +
profile_filename)
if profile_text is not None:
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)
else:
profile_text = ''
profile_form = ''
css_filename = base_dir + '/epicyon-welcome.css'