File pointer naming convention

main
Bob Mottram 2024-07-16 13:20:58 +01:00
parent 9544505bfa
commit a89a3211a1
20 changed files with 141 additions and 130 deletions

View File

@ -68,11 +68,11 @@ def remove_filter(base_dir: str, nickname: str, domain: str,
new_filters_filename = filters_filename + '.new' new_filters_filename = filters_filename + '.new'
try: try:
with open(filters_filename, 'r', encoding='utf-8') as fp_filt: with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
with open(new_filters_filename, 'w+', encoding='utf-8') as fpnew: with open(new_filters_filename, 'w+', encoding='utf-8') as fp_new:
for line in fp_filt: for line in fp_filt:
line = remove_eol(line) line = remove_eol(line)
if line != words: if line != words:
fpnew.write(line + '\n') fp_new.write(line + '\n')
except OSError as ex: except OSError as ex:
print('EX: unable to remove filter ' + print('EX: unable to remove filter ' +
filters_filename + ' ' + str(ex)) filters_filename + ' ' + str(ex))
@ -94,11 +94,11 @@ def remove_global_filter(base_dir: str, words: str) -> bool:
new_filters_filename = filters_filename + '.new' new_filters_filename = filters_filename + '.new'
try: try:
with open(filters_filename, 'r', encoding='utf-8') as fp_filt: with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
with open(new_filters_filename, 'w+', encoding='utf-8') as fpnew: with open(new_filters_filename, 'w+', encoding='utf-8') as fp_new:
for line in fp_filt: for line in fp_filt:
line = remove_eol(line) line = remove_eol(line)
if line != words: if line != words:
fpnew.write(line + '\n') fp_new.write(line + '\n')
except OSError as ex: except OSError as ex:
print('EX: unable to remove global filter ' + print('EX: unable to remove global filter ' +
filters_filename + ' ' + str(ex)) filters_filename + ' ' + str(ex))

View File

@ -144,18 +144,18 @@ def _remove_from_follow_base(base_dir: str,
return return
try: try:
with open(approve_follows_filename + '.new', 'w+', with open(approve_follows_filename + '.new', 'w+',
encoding='utf-8') as approvefilenew: encoding='utf-8') as fp_approve_new:
with open(approve_follows_filename, 'r', with open(approve_follows_filename, 'r',
encoding='utf-8') as fp_approve: encoding='utf-8') as fp_approve:
if not accept_deny_actor: if not accept_deny_actor:
for approve_handle in fp_approve: for approve_handle in fp_approve:
accept_deny_handle = accept_or_deny_handle accept_deny_handle = accept_or_deny_handle
if not approve_handle.startswith(accept_deny_handle): if not approve_handle.startswith(accept_deny_handle):
approvefilenew.write(approve_handle) fp_approve_new.write(approve_handle)
else: else:
for approve_handle in fp_approve: for approve_handle in fp_approve:
if accept_deny_actor not in approve_handle: if accept_deny_actor not in approve_handle:
approvefilenew.write(approve_handle) fp_approve_new.write(approve_handle)
except OSError as ex: except OSError as ex:
print('EX: _remove_from_follow_base ' + print('EX: _remove_from_follow_base ' +
approve_follows_filename + ' ' + str(ex)) approve_follows_filename + ' ' + str(ex))

View File

@ -23,8 +23,8 @@ def _text_in_file2(text: str, filename: str,
if not case_sensitive: if not case_sensitive:
text = text.lower() text = text.lower()
try: try:
with open(filename, 'r', encoding='utf-8') as file: with open(filename, 'r', encoding='utf-8') as fp_file:
content = file.read() content = fp_file.read()
if content: if content:
if not case_sensitive: if not case_sensitive:
content = content.lower() content = content.lower()

View File

@ -185,8 +185,8 @@ def set_headers_etag(self, media_filename: str, file_format: str,
if os.path.isfile(media_filename + '.etag'): if os.path.isfile(media_filename + '.etag'):
try: try:
with open(media_filename + '.etag', 'r', with open(media_filename + '.etag', 'r',
encoding='utf-8') as efile: encoding='utf-8') as fp_media:
etag = efile.read() etag = fp_media.read()
except OSError: except OSError:
print('EX: _set_headers_etag ' + print('EX: _set_headers_etag ' +
'unable to read ' + media_filename + '.etag') 'unable to read ' + media_filename + '.etag')
@ -194,8 +194,8 @@ def set_headers_etag(self, media_filename: str, file_format: str,
etag = md5(data).hexdigest() # nosec etag = md5(data).hexdigest() # nosec
try: try:
with open(media_filename + '.etag', 'w+', with open(media_filename + '.etag', 'w+',
encoding='utf-8') as efile: encoding='utf-8') as fp_media:
efile.write(etag) fp_media.write(etag)
except OSError: except OSError:
print('EX: _set_headers_etag ' + print('EX: _set_headers_etag ' +
'unable to write ' + media_filename + '.etag') 'unable to write ' + media_filename + '.etag')

View File

@ -438,8 +438,8 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str,
content = tag_line + content content = tag_line + content
try: try:
with open(tags_filename, 'w+', with open(tags_filename, 'w+',
encoding='utf-8') as tags_file2: encoding='utf-8') as fp_tags2:
tags_file2.write(content) fp_tags2.write(content)
hashtag_added = True hashtag_added = True
except OSError as ex: except OSError as ex:
print('EX: Failed to write entry to tags file ' + print('EX: Failed to write entry to tags file ' +
@ -3490,8 +3490,8 @@ def _receive_announce(recent_posts_cache: {},
'inbox') 'inbox')
try: try:
with open(post_filename + '.tts', 'w+', with open(post_filename + '.tts', 'w+',
encoding='utf-8') as ttsfile: encoding='utf-8') as fp_tts:
ttsfile.write('\n') fp_tts.write('\n')
except OSError: except OSError:
print('EX: unable to write recent post ' + print('EX: unable to write recent post ' +
post_filename) post_filename)

View File

@ -221,18 +221,18 @@ def manual_approve_follow_request(session, session_onion, session_i2p,
try: try:
with open(approve_follows_filename + '.new', 'w+', with open(approve_follows_filename + '.new', 'w+',
encoding='utf-8') as approvefilenew: encoding='utf-8') as fp_approve_new:
update_approved_followers = False update_approved_followers = False
follow_activity_filename = None follow_activity_filename = None
with open(approve_follows_filename, 'r', with open(approve_follows_filename, 'r',
encoding='utf-8') as approvefile: encoding='utf-8') as fp_approve:
for handle_of_follow_requester in approvefile: for handle_of_follow_requester in fp_approve:
# is this the approved follow? # is this the approved follow?
appr_handl = approve_handle_full appr_handl = approve_handle_full
if not handle_of_follow_requester.startswith(appr_handl): if not handle_of_follow_requester.startswith(appr_handl):
# this isn't the approved follow so it will remain # this isn't the approved follow so it will remain
# in the requests file # in the requests file
approvefilenew.write(handle_of_follow_requester) fp_approve_new.write(handle_of_follow_requester)
continue continue
handle_of_follow_requester = \ handle_of_follow_requester = \

View File

@ -552,8 +552,9 @@ def _update_etag(media_filename: str) -> None:
etag = sha1(data).hexdigest() # nosec etag = sha1(data).hexdigest() # nosec
# save the hash # save the hash
try: try:
with open(media_filename + '.etag', 'w+', encoding='utf-8') as efile: with open(media_filename + '.etag', 'w+',
efile.write(etag) encoding='utf-8') as fp_media:
fp_media.write(etag)
except OSError: except OSError:
print('EX: _update_etag unable to write ' + print('EX: _update_etag unable to write ' +
str(media_filename) + '.etag') str(media_filename) + '.etag')

View File

@ -140,8 +140,8 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
if os.path.isfile(following_filename): if os.path.isfile(following_filename):
following_handles = [] following_handles = []
try: try:
with open(following_filename, 'r', encoding='utf-8') as foll1: with open(following_filename, 'r', encoding='utf-8') as fp_foll1:
following_handles = foll1.readlines() following_handles = fp_foll1.readlines()
except OSError: except OSError:
print('EX: _update_moved_handle unable to read ' + print('EX: _update_moved_handle unable to read ' +
following_filename) following_filename)
@ -153,11 +153,11 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
acct_dir(base_dir, nickname, domain) + '/refollow.txt' acct_dir(base_dir, nickname, domain) + '/refollow.txt'
# unfollow the old handle # unfollow the old handle
with open(following_filename, 'w+', encoding='utf-8') as foll2: with open(following_filename, 'w+', encoding='utf-8') as fp_foll2:
for follow_handle in following_handles: for follow_handle in following_handles:
if follow_handle.strip("\n").strip("\r").lower() != \ if follow_handle.strip("\n").strip("\r").lower() != \
handle_lower: handle_lower:
foll2.write(follow_handle) fp_foll2.write(follow_handle)
continue continue
handle_nickname = handle.split('@')[0] handle_nickname = handle.split('@')[0]
handle_domain = handle.split('@')[1] handle_domain = handle.split('@')[1]
@ -173,8 +173,8 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
if os.path.isfile(refollow_filename): if os.path.isfile(refollow_filename):
try: try:
with open(refollow_filename, 'a+', with open(refollow_filename, 'a+',
encoding='utf-8') as refoll: encoding='utf-8') as fp_refoll:
refoll.write(moved_to_handle + '\n') fp_refoll.write(moved_to_handle + '\n')
except OSError: except OSError:
print('EX: ' + print('EX: ' +
'_update_moved_handle unable to append ' + '_update_moved_handle unable to append ' +
@ -182,8 +182,8 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
else: else:
try: try:
with open(refollow_filename, 'w+', with open(refollow_filename, 'w+',
encoding='utf-8') as refoll: encoding='utf-8') as fp_refoll:
refoll.write(moved_to_handle + '\n') fp_refoll.write(moved_to_handle + '\n')
except OSError: except OSError:
print('EX: _update_moved_handle unable to write ' + print('EX: _update_moved_handle unable to write ' +
refollow_filename) refollow_filename)
@ -193,8 +193,8 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
if os.path.isfile(followers_filename): if os.path.isfile(followers_filename):
follower_handles = [] follower_handles = []
try: try:
with open(followers_filename, 'r', encoding='utf-8') as foll3: with open(followers_filename, 'r', encoding='utf-8') as fp_foll3:
follower_handles = foll3.readlines() follower_handles = fp_foll3.readlines()
except OSError: except OSError:
print('EX: _update_moved_handle unable to read ' + print('EX: _update_moved_handle unable to read ' +
followers_filename) followers_filename)
@ -203,11 +203,11 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
# remove followers who have moved # remove followers who have moved
try: try:
with open(followers_filename, 'w+', encoding='utf-8') as foll4: with open(followers_filename, 'w+', encoding='utf-8') as fp_foll4:
for follower_handle in follower_handles: for follower_handle in follower_handles:
if follower_handle.strip("\n").strip("\r").lower() != \ if follower_handle.strip("\n").strip("\r").lower() != \
handle_lower: handle_lower:
foll4.write(follower_handle) fp_foll4.write(follower_handle)
else: else:
ctr += 1 ctr += 1
print('Removed follower who has moved ' + handle) print('Removed follower who has moved ' + handle)

View File

@ -762,8 +762,8 @@ def create_person(base_dir: str, nickname: str, domain: str, port: int,
follow_dms_filename = \ follow_dms_filename = \
acct_dir(base_dir, nickname, domain) + '/.followDMs' acct_dir(base_dir, nickname, domain) + '/.followDMs'
try: try:
with open(follow_dms_filename, 'w+', encoding='utf-8') as ffile: with open(follow_dms_filename, 'w+', encoding='utf-8') as fp_foll:
ffile.write('\n') fp_foll.write('\n')
except OSError: except OSError:
print('EX: create_person unable to write ' + follow_dms_filename) print('EX: create_person unable to write ' + follow_dms_filename)
@ -772,8 +772,8 @@ def create_person(base_dir: str, nickname: str, domain: str, port: int,
notify_likes_filename = \ notify_likes_filename = \
acct_dir(base_dir, nickname, domain) + '/.notifyLikes' acct_dir(base_dir, nickname, domain) + '/.notifyLikes'
try: try:
with open(notify_likes_filename, 'w+', encoding='utf-8') as nfile: with open(notify_likes_filename, 'w+', encoding='utf-8') as fp_lik:
nfile.write('\n') fp_lik.write('\n')
except OSError: except OSError:
print('EX: create_person unable to write 2 ' + print('EX: create_person unable to write 2 ' +
notify_likes_filename) notify_likes_filename)
@ -784,8 +784,8 @@ def create_person(base_dir: str, nickname: str, domain: str, port: int,
acct_dir(base_dir, nickname, domain) + '/.notifyReactions' acct_dir(base_dir, nickname, domain) + '/.notifyReactions'
try: try:
with open(notify_reactions_filename, 'w+', with open(notify_reactions_filename, 'w+',
encoding='utf-8') as nfile: encoding='utf-8') as fp_notify:
nfile.write('\n') fp_notify.write('\n')
except OSError: except OSError:
print('EX: create_person unable to write 3 ' + print('EX: create_person unable to write 3 ' +
notify_reactions_filename) notify_reactions_filename)
@ -1592,8 +1592,8 @@ def is_person_snoozed(base_dir: str, nickname: str, domain: str,
if content: if content:
try: try:
with open(snoozed_filename, 'w+', with open(snoozed_filename, 'w+',
encoding='utf-8') as snoozfile: encoding='utf-8') as fp_snooze:
snoozfile.write(content) fp_snooze.write(content)
except OSError: except OSError:
print('EX: is_person_snoozed unable to write ' + print('EX: is_person_snoozed unable to write ' +
snoozed_filename) snoozed_filename)
@ -1655,8 +1655,8 @@ def person_unsnooze(base_dir: str, nickname: str, domain: str,
if content is not None: if content is not None:
try: try:
with open(snoozed_filename, 'w+', with open(snoozed_filename, 'w+',
encoding='utf-8') as snoozfile: encoding='utf-8') as fp_snooze:
snoozfile.write(content) fp_snooze.write(content)
except OSError: except OSError:
print('EX: unable to write ' + snoozed_filename) print('EX: unable to write ' + snoozed_filename)

View File

@ -327,12 +327,12 @@ def set_roles_from_list(base_dir: str, domain: str, admin_nickname: str,
roles_list = fields[list_name].split(',') roles_list = fields[list_name].split(',')
try: try:
with open(roles_filename, 'w+', with open(roles_filename, 'w+',
encoding='utf-8') as rolesfile: encoding='utf-8') as fp_roles:
for roles_nick in roles_list: for roles_nick in roles_list:
roles_nick = roles_nick.strip() roles_nick = roles_nick.strip()
roles_dir = acct_dir(base_dir, roles_nick, domain) roles_dir = acct_dir(base_dir, roles_nick, domain)
if os.path.isdir(roles_dir): if os.path.isdir(roles_dir):
rolesfile.write(roles_nick + '\n') fp_roles.write(roles_nick + '\n')
except OSError as ex: except OSError as ex:
print('EX: unable to write ' + list_name + ' ' + print('EX: unable to write ' + list_name + ' ' +
roles_filename + ' ' + str(ex)) roles_filename + ' ' + str(ex))
@ -347,12 +347,12 @@ def set_roles_from_list(base_dir: str, domain: str, admin_nickname: str,
roles_list = fields[list_name].split('\n') roles_list = fields[list_name].split('\n')
try: try:
with open(roles_filename, 'w+', with open(roles_filename, 'w+',
encoding='utf-8') as rolesfile: encoding='utf-8') as fp_roles:
for roles_nick in roles_list: for roles_nick in roles_list:
roles_nick = roles_nick.strip() roles_nick = roles_nick.strip()
roles_dir = acct_dir(base_dir, roles_nick, domain) roles_dir = acct_dir(base_dir, roles_nick, domain)
if os.path.isdir(roles_dir): if os.path.isdir(roles_dir):
rolesfile.write(roles_nick + '\n') fp_roles.write(roles_nick + '\n')
except OSError as ex: except OSError as ex:
print('EX: unable to write ' + list_name + ' ' + print('EX: unable to write ' + list_name + ' ' +
roles_filename + ' ' + str(ex)) roles_filename + ' ' + str(ex))

View File

@ -4532,8 +4532,8 @@ def _test_translation_labels() -> None:
if source_file.startswith('flycheck_'): if source_file.startswith('flycheck_'):
continue continue
source_str = '' source_str = ''
with open(source_file, 'r', encoding='utf-8') as file_source: with open(source_file, 'r', encoding='utf-8') as fp_source:
source_str = file_source.read() source_str = fp_source.read()
if not source_str: if not source_str:
continue continue
if 'translate[' not in source_str: if 'translate[' not in source_str:
@ -5173,8 +5173,8 @@ def _test_post_variable_names():
if source_file.startswith('flycheck_'): if source_file.startswith('flycheck_'):
continue continue
source_str = '' source_str = ''
with open(source_file, 'r', encoding='utf-8') as file_source: with open(source_file, 'r', encoding='utf-8') as fp_source:
source_str = file_source.read() source_str = fp_source.read()
if not source_str: if not source_str:
continue continue
if ' name="' not in source_str: if ' name="' not in source_str:
@ -5208,8 +5208,8 @@ def _test_config_param_names():
if source_file.startswith('flycheck_'): if source_file.startswith('flycheck_'):
continue continue
source_str = '' source_str = ''
with open(source_file, 'r', encoding='utf-8') as file_source: with open(source_file, 'r', encoding='utf-8') as fp_source:
source_str = file_source.read() source_str = fp_source.read()
if not source_str: if not source_str:
continue continue
for fname in fnames: for fname in fnames:
@ -5256,8 +5256,8 @@ def _test_source_contains_no_tabs():
if source_file.startswith('flycheck_'): if source_file.startswith('flycheck_'):
continue continue
source_str = '' source_str = ''
with open(source_file, 'r', encoding='utf-8') as file_source: with open(source_file, 'r', encoding='utf-8') as fp_source:
source_str = file_source.read() source_str = fp_source.read()
if not source_str: if not source_str:
continue continue
if '\t' in source_str: if '\t' in source_str:
@ -5279,8 +5279,8 @@ def _test_checkbox_names():
if source_file.startswith('flycheck_'): if source_file.startswith('flycheck_'):
continue continue
source_str = '' source_str = ''
with open(source_file, 'r', encoding='utf-8') as file_source: with open(source_file, 'r', encoding='utf-8') as fp_source:
source_str = file_source.read() source_str = fp_source.read()
if not source_str: if not source_str:
continue continue
for fname in fnames: for fname in fnames:
@ -5319,8 +5319,8 @@ def _test_post_field_names(source_file: str, fieldnames: []):
fnames.append(field + '.get') fnames.append(field + '.get')
source_str = '' source_str = ''
with open(source_file, 'r', encoding='utf-8') as file_source: with open(source_file, 'r', encoding='utf-8') as fp_source:
source_str = file_source.read() source_str = fp_source.read()
if not source_str: if not source_str:
return return
for fname in fnames: for fname in fnames:
@ -6810,8 +6810,11 @@ def _test_spoofed_geolocation() -> None:
kml_str += '</Document>\n' kml_str += '</Document>\n'
kml_str += '</kml>' kml_str += '</kml>'
with open('unittest_decoy.kml', 'w+', encoding='utf-8') as kmlfile: try:
kmlfile.write(kml_str) with open('unittest_decoy.kml', 'w+', encoding='utf-8') as fp_kml:
fp_kml.write(kml_str)
except OSError:
print('EX: unable to write unittest_decoy.kml')
def _test_skills() -> None: def _test_skills() -> None:

View File

@ -363,8 +363,8 @@ def _set_theme_from_dict(base_dir: str, name: str,
if not os.path.isfile(template_filename): if not os.path.isfile(template_filename):
continue continue
with open(template_filename, 'r', encoding='utf-8') as cssfile: with open(template_filename, 'r', encoding='utf-8') as fp_css:
css = cssfile.read() css = fp_css.read()
for param_name, param_value in theme_params.items(): for param_name, param_value in theme_params.items():
if param_name == 'newswire-publish-icon': if param_name == 'newswire-publish-icon':
if param_value.lower() == 'true': if param_value.lower() == 'true':
@ -405,8 +405,8 @@ def _set_theme_from_dict(base_dir: str, name: str,
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
try: try:
with open(filename, 'w+', encoding='utf-8') as cssfile: with open(filename, 'w+', encoding='utf-8') as fp_css:
cssfile.write(css) fp_css.write(css)
except OSError: except OSError:
print('EX: _set_theme_from_dict unable to write ' + filename) print('EX: _set_theme_from_dict unable to write ' + filename)
@ -428,11 +428,11 @@ def _set_background_format(base_dir: str,
if not os.path.isfile(css_filename): if not os.path.isfile(css_filename):
return return
try: try:
with open(css_filename, 'r', encoding='utf-8') as cssfile: with open(css_filename, 'r', encoding='utf-8') as fp_css:
css = cssfile.read() css = fp_css.read()
css = css.replace('background.jpg', 'background.' + extension) css = css.replace('background.jpg', 'background.' + extension)
with open(css_filename, 'w+', encoding='utf-8') as cssfile2: with open(css_filename, 'w+', encoding='utf-8') as fp_css2:
cssfile2.write(css) fp_css2.write(css)
except OSError as ex: except OSError as ex:
print('EX: _set_background_format ' + css_filename + ' ' + str(ex)) print('EX: _set_background_format ' + css_filename + ' ' + str(ex))
@ -446,24 +446,24 @@ def enable_grayscale(base_dir: str) -> None:
if not os.path.isfile(template_filename): if not os.path.isfile(template_filename):
continue continue
try: try:
with open(template_filename, 'r', encoding='utf-8') as cssfile: with open(template_filename, 'r', encoding='utf-8') as fp_css:
css = cssfile.read() css = fp_css.read()
if 'grayscale' not in css: if 'grayscale' not in css:
css = \ css = \
css.replace('body, html {', css.replace('body, html {',
'body, html {\n' + 'body, html {\n' +
' filter: grayscale(100%);') ' filter: grayscale(100%);')
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile: with open(filename, 'w+', encoding='utf-8') as fp_css:
cssfile.write(css) fp_css.write(css)
except OSError as ex: except OSError as ex:
print('EX: enable_grayscale unable to read ' + print('EX: enable_grayscale unable to read ' +
template_filename + ' ' + str(ex)) template_filename + ' ' + str(ex))
grayscale_filename = data_dir(base_dir) + '/.grayscale' grayscale_filename = data_dir(base_dir) + '/.grayscale'
if not os.path.isfile(grayscale_filename): if not os.path.isfile(grayscale_filename):
try: try:
with open(grayscale_filename, 'w+', encoding='utf-8') as grayfile: with open(grayscale_filename, 'w+', encoding='utf-8') as fp_gray:
grayfile.write(' ') fp_gray.write(' ')
except OSError as ex: except OSError as ex:
print('EX: enable_grayscale unable to write ' + print('EX: enable_grayscale unable to write ' +
grayscale_filename + ' ' + str(ex)) grayscale_filename + ' ' + str(ex))
@ -478,14 +478,14 @@ def disable_grayscale(base_dir: str) -> None:
if not os.path.isfile(template_filename): if not os.path.isfile(template_filename):
continue continue
try: try:
with open(template_filename, 'r', encoding='utf-8') as cssfile: with open(template_filename, 'r', encoding='utf-8') as fp_css:
css = cssfile.read() css = fp_css.read()
if 'grayscale' in css: if 'grayscale' in css:
css = \ css = \
css.replace('\n filter: grayscale(100%);', '') css.replace('\n filter: grayscale(100%);', '')
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
with open(filename, 'w+', encoding='utf-8') as cssfile: with open(filename, 'w+', encoding='utf-8') as fp_css:
cssfile.write(css) fp_css.write(css)
except OSError as ex: except OSError as ex:
print('EX: disable_grayscale unable to read ' + print('EX: disable_grayscale unable to read ' +
template_filename + ' ' + str(ex)) template_filename + ' ' + str(ex))
@ -506,8 +506,8 @@ def _set_dyslexic_font(base_dir: str) -> bool:
template_filename = base_dir + '/' + filename template_filename = base_dir + '/' + filename
if not os.path.isfile(template_filename): if not os.path.isfile(template_filename):
continue continue
with open(template_filename, 'r', encoding='utf-8') as cssfile: with open(template_filename, 'r', encoding='utf-8') as fp_css:
css = cssfile.read() css = fp_css.read()
css = \ css = \
set_css_param(css, "*src", set_css_param(css, "*src",
"url('./fonts/OpenDyslexic-Regular.woff2" + "url('./fonts/OpenDyslexic-Regular.woff2" +
@ -515,8 +515,8 @@ def _set_dyslexic_font(base_dir: str) -> bool:
css = set_css_param(css, "*font-family", "'OpenDyslexic'") css = set_css_param(css, "*font-family", "'OpenDyslexic'")
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
try: try:
with open(filename, 'w+', encoding='utf-8') as cssfile: with open(filename, 'w+', encoding='utf-8') as fp_css:
cssfile.write(css) fp_css.write(css)
except OSError: except OSError:
print('EX: _set_dyslexic_font unable to write ' + filename) print('EX: _set_dyslexic_font unable to write ' + filename)
return False return False
@ -546,8 +546,8 @@ def _set_custom_font(base_dir: str):
template_filename = base_dir + '/' + filename template_filename = base_dir + '/' + filename
if not os.path.isfile(template_filename): if not os.path.isfile(template_filename):
continue continue
with open(template_filename, 'r', encoding='utf-8') as cssfile: with open(template_filename, 'r', encoding='utf-8') as fp_css:
css = cssfile.read() css = fp_css.read()
css = \ css = \
set_css_param(css, "*src", set_css_param(css, "*src",
"url('./fonts/custom." + "url('./fonts/custom." +
@ -558,8 +558,8 @@ def _set_custom_font(base_dir: str):
css = set_css_param(css, "header-font", "'CustomFont'") css = set_css_param(css, "header-font", "'CustomFont'")
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
try: try:
with open(filename, 'w+', encoding='utf-8') as cssfile: with open(filename, 'w+', encoding='utf-8') as fp_css:
cssfile.write(css) fp_css.write(css)
except OSError: except OSError:
print('EX: _set_custom_font unable to write ' + filename) print('EX: _set_custom_font unable to write ' + filename)

View File

@ -321,16 +321,19 @@ def text_in_file(text: str, filename: str,
""" """
if not case_sensitive: if not case_sensitive:
text = text.lower() text = text.lower()
content = None
try: try:
with open(filename, 'r', encoding='utf-8') as file: with open(filename, 'r', encoding='utf-8') as fp_file:
content = file.read() content = fp_file.read()
if content:
if not case_sensitive:
content = content.lower()
if text in content:
return True
except OSError: except OSError:
print('EX: unable to find text in missing file ' + filename) print('EX: unable to find text in missing file ' + filename)
if content:
if not case_sensitive:
content = content.lower()
if text in content:
return True
return False return False
@ -594,8 +597,8 @@ def set_accounts_data_dir(base_dir: str, accounts_data_path: str) -> None:
path = None path = None
try: try:
with open(accounts_data_path_filename, 'r', with open(accounts_data_path_filename, 'r',
encoding='utf-8') as file: encoding='utf-8') as fp_accounts:
path = file.read() path = fp_accounts.read()
except OSError: except OSError:
print('EX: unable to read ' + accounts_data_path_filename) print('EX: unable to read ' + accounts_data_path_filename)
if path.strip() == accounts_data_path: if path.strip() == accounts_data_path:
@ -604,8 +607,8 @@ def set_accounts_data_dir(base_dir: str, accounts_data_path: str) -> None:
try: try:
with open(accounts_data_path_filename, 'w+', with open(accounts_data_path_filename, 'w+',
encoding='utf-8') as file: encoding='utf-8') as fp_accounts:
file.write(accounts_data_path) fp_accounts.write(accounts_data_path)
except OSError: except OSError:
print('EX: unable to write ' + accounts_data_path_filename) print('EX: unable to write ' + accounts_data_path_filename)
@ -629,8 +632,8 @@ def data_dir(base_dir: str) -> str:
path = None path = None
try: try:
with open(accounts_data_path_filename, 'r', with open(accounts_data_path_filename, 'r',
encoding='utf-8') as file: encoding='utf-8') as fp_accounts:
path = file.read() path = fp_accounts.read()
except OSError: except OSError:
print('EX: unable to read ' + accounts_data_path_filename) print('EX: unable to read ' + accounts_data_path_filename)
if path: if path:
@ -833,8 +836,8 @@ def is_editor(base_dir: str, nickname: str) -> bool:
return True return True
return False return False
with open(editors_file, 'r', encoding='utf-8') as editors: with open(editors_file, 'r', encoding='utf-8') as fp_editors:
lines = editors.readlines() lines = fp_editors.readlines()
if len(lines) == 0: if len(lines) == 0:
admin_name = get_config_param(base_dir, 'admin') admin_name = get_config_param(base_dir, 'admin')
if admin_name: if admin_name:
@ -859,8 +862,8 @@ def is_artist(base_dir: str, nickname: str) -> bool:
return True return True
return False return False
with open(artists_file, 'r', encoding='utf-8') as artists: with open(artists_file, 'r', encoding='utf-8') as fp_artists:
lines = artists.readlines() lines = fp_artists.readlines()
if len(lines) == 0: if len(lines) == 0:
admin_name = get_config_param(base_dir, 'admin') admin_name = get_config_param(base_dir, 'admin')
if admin_name: if admin_name:
@ -1210,8 +1213,8 @@ def get_followers_of_person(base_dir: str,
continue continue
if not os.path.isfile(filename): if not os.path.isfile(filename):
continue continue
with open(filename, 'r', encoding='utf-8') as followingfile: with open(filename, 'r', encoding='utf-8') as fp_following:
for following_handle in followingfile: for following_handle in fp_following:
following_handle2 = remove_eol(following_handle) following_handle2 = remove_eol(following_handle)
if following_handle2 == handle: if following_handle2 == handle:
if account not in followers: if account not in followers:
@ -2383,13 +2386,13 @@ def remove_moderation_post_from_index(base_dir: str, post_url: str,
if text_in_file(post_id, moderation_index_file): if text_in_file(post_id, moderation_index_file):
try: try:
with open(moderation_index_file, 'r', with open(moderation_index_file, 'r',
encoding='utf-8') as file1: encoding='utf-8') as fp_mod1:
lines = file1.readlines() lines = fp_mod1.readlines()
with open(moderation_index_file, 'w+', with open(moderation_index_file, 'w+',
encoding='utf-8') as file2: encoding='utf-8') as fp_mod2:
for line in lines: for line in lines:
if line.strip("\n").strip("\r") != post_id: if line.strip("\n").strip("\r") != post_id:
file2.write(line) fp_mod2.write(line)
continue continue
if debug: if debug:
print('DEBUG: removed ' + post_id + print('DEBUG: removed ' + post_id +

View File

@ -516,8 +516,8 @@ def html_new_post(edit_post_params: {},
if os.path.isfile(dir_str + '/report.txt'): if os.path.isfile(dir_str + '/report.txt'):
try: try:
with open(dir_str + '/report.txt', 'r', with open(dir_str + '/report.txt', 'r',
encoding='utf-8') as file: encoding='utf-8') as fp_report:
custom_report_text = file.read() custom_report_text = fp_report.read()
if '</p>' not in custom_report_text: if '</p>' not in custom_report_text:
custom_report_text = \ custom_report_text = \
'<p class="login-subtext">' + \ '<p class="login-subtext">' + \
@ -558,8 +558,9 @@ def html_new_post(edit_post_params: {},
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: try:
with open(dir_str + '/newpost.txt', 'r', encoding='utf-8') as file: with open(dir_str + '/newpost.txt', 'r',
new_post_text = '<p>' + file.read() + '</p>\n' encoding='utf-8') as fp_new:
new_post_text = '<p>' + fp_new.read() + '</p>\n'
except OSError: except OSError:
print('EX: html_new_post unable to read ' + print('EX: html_new_post unable to read ' +
dir_str + '/newpost.txt') dir_str + '/newpost.txt')

View File

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

View File

@ -2373,8 +2373,8 @@ def individual_post_as_html(signing_priv_key_pem: str,
box_name) box_name)
try: try:
with open(announce_filename + '.tts', 'w+', with open(announce_filename + '.tts', 'w+',
encoding='utf-8') as ttsfile: encoding='utf-8') as fp_tts:
ttsfile.write('\n') fp_tts.write('\n')
except OSError: except OSError:
print('EX: unable to write tts ' + print('EX: unable to write tts ' +
announce_filename + '.tts') announce_filename + '.tts')

View File

@ -2317,8 +2317,8 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
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: try:
with open(filter_filename, 'r', encoding='utf-8') as filterfile: with open(filter_filename, 'r', encoding='utf-8') as fp_filter:
filter_str = filterfile.read() filter_str = fp_filter.read()
except OSError: except OSError:
print('EX: _html_edit_profile_filtering unable to read ' + print('EX: _html_edit_profile_filtering unable to read ' +
filter_filename) filter_filename)
@ -2340,8 +2340,8 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
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: try:
with open(switch_filename, 'r', encoding='utf-8') as switchfile: with open(switch_filename, 'r', encoding='utf-8') as fp_switch:
switch_str = switchfile.read() switch_str = fp_switch.read()
except OSError: except OSError:
print('EX: _html_edit_profile_filtering unable to save ' + print('EX: _html_edit_profile_filtering unable to save ' +
switch_filename) switch_filename)

View File

@ -538,8 +538,8 @@ def html_timeline(default_timeline: str,
new_calendar_event = True new_calendar_event = True
calendar_image = 'calendar_notify.png' calendar_image = 'calendar_notify.png'
try: try:
with open(calendar_file, 'r', encoding='utf-8') as calfile: with open(calendar_file, 'r', encoding='utf-8') as fp_cal:
calendar_path = calfile.read().replace('##sent##', '') calendar_path = fp_cal.read().replace('##sent##', '')
calendar_path = remove_eol(calendar_path) calendar_path = remove_eol(calendar_path)
if '/calendar' not in calendar_path: if '/calendar' not in calendar_path:
calendar_path = '/calendar' calendar_path = '/calendar'

View File

@ -35,8 +35,8 @@ 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: try:
with open(dir_str + '/tos.md', 'r', encoding='utf-8') as file: with open(dir_str + '/tos.md', 'r', encoding='utf-8') as fp_tos:
tos_text = markdown_to_html(file.read()) tos_text = markdown_to_html(fp_tos.read())
except OSError: except OSError:
print('EX: html_terms_of_service unable to read ' + print('EX: html_terms_of_service unable to read ' +
dir_str + '/tos.md') dir_str + '/tos.md')

View File

@ -1098,8 +1098,9 @@ def load_individual_post_as_html_from_cache(base_dir: str,
tries = 0 tries = 0
while tries < 3: while tries < 3:
try: try:
with open(cached_post_filename, 'r', encoding='utf-8') as file: with open(cached_post_filename, 'r',
post_html = file.read() encoding='utf-8') as fp_cached:
post_html = fp_cached.read()
break break
except OSError as ex: except OSError as ex:
print('ERROR: load_individual_post_as_html_from_cache ' + print('ERROR: load_individual_post_as_html_from_cache ' +