Explicitly set file encoding

merge-requests/30/head
Bob Mottram 2022-06-09 15:46:30 +01:00
parent ee8ce74bad
commit 4308f7501a
59 changed files with 839 additions and 588 deletions

View File

@ -165,7 +165,7 @@ def _accept_follow(base_dir: str, message_json: {},
acct_dir(base_dir, nickname, accepted_domain_full) + '/unfollowed.txt' acct_dir(base_dir, nickname, accepted_domain_full) + '/unfollowed.txt'
if os.path.isfile(unfollowed_filename): if os.path.isfile(unfollowed_filename):
if followed_nickname + '@' + followed_domain_full in \ if followed_nickname + '@' + followed_domain_full in \
open(unfollowed_filename).read(): open(unfollowed_filename, encoding='utf-8').read():
if debug: if debug:
print('DEBUG: follow accept arrived for ' + print('DEBUG: follow accept arrived for ' +
nickname + '@' + accepted_domain_full + nickname + '@' + accepted_domain_full +

20
auth.py
View File

@ -143,7 +143,7 @@ def authorize_basic(base_dir: str, path: str, auth_header: str,
return False return False
provided_password = plain.split(':')[1] provided_password = plain.split(':')[1]
try: try:
with open(password_file, 'r') as passfile: with open(password_file, 'r', encoding='utf-8') as passfile:
for line in passfile: for line in passfile:
if not line.startswith(nickname + ':'): if not line.startswith(nickname + ':'):
continue continue
@ -177,10 +177,11 @@ def store_basic_credentials(base_dir: str,
password_file = base_dir + '/accounts/passwords' password_file = base_dir + '/accounts/passwords'
store_str = nickname + ':' + _hash_password(password) store_str = nickname + ':' + _hash_password(password)
if os.path.isfile(password_file): if os.path.isfile(password_file):
if nickname + ':' in open(password_file).read(): if nickname + ':' in open(password_file, encoding='utf-8').read():
try: try:
with open(password_file, 'r') as fin: with open(password_file, 'r', encoding='utf-8') as fin:
with open(password_file + '.new', 'w+') as fout: with open(password_file + '.new', 'w+',
encoding='utf-8') as fout:
for line in fin: for line in fin:
if not line.startswith(nickname + ':'): if not line.startswith(nickname + ':'):
fout.write(line) fout.write(line)
@ -199,14 +200,14 @@ def store_basic_credentials(base_dir: str,
else: else:
# append to password file # append to password file
try: try:
with open(password_file, 'a+') as passfile: with open(password_file, 'a+', encoding='utf-8') as passfile:
passfile.write(store_str + '\n') passfile.write(store_str + '\n')
except OSError: except OSError:
print('EX: unable to append password') print('EX: unable to append password')
return False return False
else: else:
try: try:
with open(password_file, 'w+') as passfile: with open(password_file, 'w+', encoding='utf-8') as passfile:
passfile.write(store_str + '\n') passfile.write(store_str + '\n')
except OSError: except OSError:
print('EX: unable to create password file') print('EX: unable to create password file')
@ -221,8 +222,9 @@ def remove_password(base_dir: str, nickname: str) -> None:
password_file = base_dir + '/accounts/passwords' password_file = base_dir + '/accounts/passwords'
if os.path.isfile(password_file): if os.path.isfile(password_file):
try: try:
with open(password_file, 'r') as fin: with open(password_file, 'r', encoding='utf-8') as fin:
with open(password_file + '.new', 'w+') as fout: with open(password_file + '.new', 'w+',
encoding='utf-8') as fout:
for line in fin: for line in fin:
if not line.startswith(nickname + ':'): if not line.startswith(nickname + ':'):
fout.write(line) fout.write(line)
@ -289,7 +291,7 @@ def record_login_failure(base_dir: str, ip_address: str,
curr_time = datetime.datetime.utcnow() curr_time = datetime.datetime.utcnow()
curr_time_str = curr_time.strftime("%Y-%m-%d %H:%M:%SZ") curr_time_str = curr_time.strftime("%Y-%m-%d %H:%M:%SZ")
try: try:
with open(failure_log, write_type) as fp_fail: with open(failure_log, write_type, encoding='utf-8') as fp_fail:
# here we use a similar format to an ssh log, so that # here we use a similar format to an ssh log, so that
# systems such as fail2ban can parse it # systems such as fail2ban can parse it
fp_fail.write(curr_time_str + ' ' + fp_fail.write(curr_time_str + ' ' +

View File

@ -46,11 +46,12 @@ def add_global_block(base_dir: str,
# is the handle already blocked? # is the handle already blocked?
block_handle = block_nickname + '@' + block_domain block_handle = block_nickname + '@' + block_domain
if os.path.isfile(blocking_filename): if os.path.isfile(blocking_filename):
if block_handle in open(blocking_filename).read(): if block_handle in open(blocking_filename,
encoding='utf-8').read():
return False return False
# block an account handle or domain # block an account handle or domain
try: try:
with open(blocking_filename, 'a+') as block_file: with open(blocking_filename, 'a+', encoding='utf-8') as block_file:
block_file.write(block_handle + '\n') block_file.write(block_handle + '\n')
except OSError: except OSError:
print('EX: unable to save blocked handle ' + block_handle) print('EX: unable to save blocked handle ' + block_handle)
@ -59,11 +60,12 @@ def add_global_block(base_dir: str,
block_hashtag = block_nickname block_hashtag = block_nickname
# is the hashtag already blocked? # is the hashtag already blocked?
if os.path.isfile(blocking_filename): if os.path.isfile(blocking_filename):
if block_hashtag + '\n' in open(blocking_filename).read(): if block_hashtag + '\n' in \
open(blocking_filename, encoding='utf-8').read():
return False return False
# block a hashtag # block a hashtag
try: try:
with open(blocking_filename, 'a+') as block_file: with open(blocking_filename, 'a+', encoding='utf-8') as block_file:
block_file.write(block_hashtag + '\n') block_file.write(block_hashtag + '\n')
except OSError: except OSError:
print('EX: unable to save blocked hashtag ' + block_hashtag) print('EX: unable to save blocked hashtag ' + block_hashtag)
@ -83,17 +85,20 @@ def add_block(base_dir: str, nickname: str, domain: str,
blocking_filename = acct_dir(base_dir, nickname, domain) + '/blocking.txt' blocking_filename = acct_dir(base_dir, nickname, domain) + '/blocking.txt'
block_handle = block_nickname + '@' + block_domain block_handle = block_nickname + '@' + block_domain
if os.path.isfile(blocking_filename): if os.path.isfile(blocking_filename):
if block_handle + '\n' in open(blocking_filename).read(): if block_handle + '\n' in open(blocking_filename,
encoding='utf-8').read():
return False return False
# if we are following then unfollow # if we are following then unfollow
following_filename = \ following_filename = \
acct_dir(base_dir, nickname, domain) + '/following.txt' acct_dir(base_dir, nickname, domain) + '/following.txt'
if os.path.isfile(following_filename): if os.path.isfile(following_filename):
if block_handle + '\n' in open(following_filename).read(): if block_handle + '\n' in open(following_filename,
encoding='utf-8').read():
following_str = '' following_str = ''
try: try:
with open(following_filename, 'r') as foll_file: with open(following_filename, 'r',
encoding='utf-8') as foll_file:
following_str = foll_file.read() following_str = foll_file.read()
except OSError: except OSError:
print('EX: Unable to read following ' + following_filename) print('EX: Unable to read following ' + following_filename)
@ -103,7 +108,8 @@ def add_block(base_dir: str, nickname: str, domain: str,
following_str = following_str.replace(block_handle + '\n', '') following_str = following_str.replace(block_handle + '\n', '')
try: try:
with open(following_filename, 'w+') as foll_file: with open(following_filename, 'w+',
encoding='utf-8') as foll_file:
foll_file.write(following_str) foll_file.write(following_str)
except OSError: except OSError:
print('EX: Unable to write following ' + following_str) print('EX: Unable to write following ' + following_str)
@ -116,7 +122,8 @@ def add_block(base_dir: str, nickname: str, domain: str,
if block_handle + '\n' in open(followers_filename).read(): if block_handle + '\n' in open(followers_filename).read():
followers_str = '' followers_str = ''
try: try:
with open(followers_filename, 'r') as foll_file: with open(followers_filename, 'r',
encoding='utf-8') as foll_file:
followers_str = foll_file.read() followers_str = foll_file.read()
except OSError: except OSError:
print('EX: Unable to read followers ' + followers_filename) print('EX: Unable to read followers ' + followers_filename)
@ -126,14 +133,15 @@ def add_block(base_dir: str, nickname: str, domain: str,
followers_str = followers_str.replace(block_handle + '\n', '') followers_str = followers_str.replace(block_handle + '\n', '')
try: try:
with open(followers_filename, 'w+') as foll_file: with open(followers_filename, 'w+',
encoding='utf-8') as foll_file:
foll_file.write(followers_str) foll_file.write(followers_str)
except OSError: except OSError:
print('EX: Unable to write followers ' + followers_str) print('EX: Unable to write followers ' + followers_str)
return False return False
try: try:
with open(blocking_filename, 'a+') as block_file: with open(blocking_filename, 'a+', encoding='utf-8') as block_file:
block_file.write(block_handle + '\n') block_file.write(block_handle + '\n')
except OSError: except OSError:
print('EX: unable to append block handle ' + block_handle) print('EX: unable to append block handle ' + block_handle)
@ -150,10 +158,13 @@ def remove_global_block(base_dir: str,
if not unblock_nickname.startswith('#'): if not unblock_nickname.startswith('#'):
unblock_handle = unblock_nickname + '@' + unblock_domain unblock_handle = unblock_nickname + '@' + unblock_domain
if os.path.isfile(unblocking_filename): if os.path.isfile(unblocking_filename):
if unblock_handle in open(unblocking_filename).read(): if unblock_handle in open(unblocking_filename,
encoding='utf-8').read():
try: try:
with open(unblocking_filename, 'r') as fp_unblock: with open(unblocking_filename, 'r',
with open(unblocking_filename + '.new', 'w+') as fpnew: encoding='utf-8') as fp_unblock:
with open(unblocking_filename + '.new', 'w+',
encoding='utf-8') as fpnew:
for line in fp_unblock: for line in fp_unblock:
handle = \ handle = \
line.replace('\n', '').replace('\r', '') line.replace('\n', '').replace('\r', '')
@ -177,8 +188,10 @@ def remove_global_block(base_dir: str,
if os.path.isfile(unblocking_filename): if os.path.isfile(unblocking_filename):
if unblock_hashtag + '\n' in open(unblocking_filename).read(): if unblock_hashtag + '\n' in open(unblocking_filename).read():
try: try:
with open(unblocking_filename, 'r') as fp_unblock: with open(unblocking_filename, 'r',
with open(unblocking_filename + '.new', 'w+') as fpnew: encoding='utf-8') as fp_unblock:
with open(unblocking_filename + '.new', 'w+',
encoding='utf-8') as fpnew:
for line in fp_unblock: for line in fp_unblock:
block_line = \ block_line = \
line.replace('\n', '').replace('\r', '') line.replace('\n', '').replace('\r', '')
@ -209,10 +222,13 @@ def remove_block(base_dir: str, nickname: str, domain: str,
acct_dir(base_dir, nickname, domain) + '/blocking.txt' acct_dir(base_dir, nickname, domain) + '/blocking.txt'
unblock_handle = unblock_nickname + '@' + unblock_domain unblock_handle = unblock_nickname + '@' + unblock_domain
if os.path.isfile(unblocking_filename): if os.path.isfile(unblocking_filename):
if unblock_handle in open(unblocking_filename).read(): if unblock_handle in open(unblocking_filename,
encoding='utf-8').read():
try: try:
with open(unblocking_filename, 'r') as fp_unblock: with open(unblocking_filename, 'r',
with open(unblocking_filename + '.new', 'w+') as fpnew: encoding='utf-8') as fp_unblock:
with open(unblocking_filename + '.new', 'w+',
encoding='utf-8') as fpnew:
for line in fp_unblock: for line in fp_unblock:
handle = line.replace('\n', '').replace('\r', '') handle = line.replace('\n', '').replace('\r', '')
if unblock_handle not in line: if unblock_handle not in line:
@ -244,7 +260,8 @@ def is_blocked_hashtag(base_dir: str, hashtag: str) -> bool:
hashtag = hashtag.strip('\n').strip('\r') hashtag = hashtag.strip('\n').strip('\r')
if not hashtag.startswith('#'): if not hashtag.startswith('#'):
hashtag = '#' + hashtag hashtag = '#' + hashtag
if hashtag + '\n' in open(global_blocking_filename).read(): if hashtag + '\n' in open(global_blocking_filename,
encoding='utf-8').read():
return True return True
return False return False
@ -263,7 +280,8 @@ def get_domain_blocklist(base_dir: str) -> str:
if not os.path.isfile(global_blocking_filename): if not os.path.isfile(global_blocking_filename):
return blocked_str return blocked_str
try: try:
with open(global_blocking_filename, 'r') as fp_blocked: with open(global_blocking_filename, 'r',
encoding='utf-8') as fp_blocked:
blocked_str += fp_blocked.read() blocked_str += fp_blocked.read()
except OSError: except OSError:
print('EX: unable to read ' + global_blocking_filename) print('EX: unable to read ' + global_blocking_filename)
@ -287,7 +305,8 @@ def update_blocked_cache(base_dir: str,
if not os.path.isfile(global_blocking_filename): if not os.path.isfile(global_blocking_filename):
return blocked_cache_last_updated return blocked_cache_last_updated
try: try:
with open(global_blocking_filename, 'r') as fp_blocked: with open(global_blocking_filename, 'r',
encoding='utf-8') as fp_blocked:
blocked_lines = fp_blocked.readlines() blocked_lines = fp_blocked.readlines()
# remove newlines # remove newlines
for index, _ in enumerate(blocked_lines): for index, _ in enumerate(blocked_lines):
@ -384,37 +403,44 @@ def is_blocked(base_dir: str, nickname: str, domain: str,
else: else:
global_blocks_filename = base_dir + '/accounts/blocking.txt' global_blocks_filename = base_dir + '/accounts/blocking.txt'
if os.path.isfile(global_blocks_filename): if os.path.isfile(global_blocks_filename):
if '*@' + block_domain in open(global_blocks_filename).read(): if '*@' + block_domain in open(global_blocks_filename,
encoding='utf-8').read():
return True return True
if block_handle: if block_handle:
block_str = block_handle + '\n' block_str = block_handle + '\n'
if block_str in open(global_blocks_filename).read(): if block_str in open(global_blocks_filename,
encoding='utf-8').read():
return True return True
else: else:
# instance allow list # instance allow list
allow_filename = base_dir + '/accounts/allowedinstances.txt' allow_filename = base_dir + '/accounts/allowedinstances.txt'
short_domain = _get_short_domain(block_domain) short_domain = _get_short_domain(block_domain)
if not short_domain: if not short_domain:
if block_domain + '\n' not in open(allow_filename).read(): if block_domain + '\n' not in open(allow_filename,
encoding='utf-8').read():
return True return True
else: else:
if short_domain + '\n' not in open(allow_filename).read(): if short_domain + '\n' not in open(allow_filename,
encoding='utf-8').read():
return True return True
# account level allow list # account level allow list
account_dir = acct_dir(base_dir, nickname, domain) account_dir = acct_dir(base_dir, nickname, domain)
allow_filename = account_dir + '/allowedinstances.txt' allow_filename = account_dir + '/allowedinstances.txt'
if os.path.isfile(allow_filename): if os.path.isfile(allow_filename):
if block_domain + '\n' not in open(allow_filename).read(): if block_domain + '\n' not in open(allow_filename,
encoding='utf-8').read():
return True return True
# account level block list # account level block list
blocking_filename = account_dir + '/blocking.txt' blocking_filename = account_dir + '/blocking.txt'
if os.path.isfile(blocking_filename): if os.path.isfile(blocking_filename):
if '*@' + block_domain + '\n' in open(blocking_filename).read(): if '*@' + block_domain + '\n' in open(blocking_filename,
encoding='utf-8').read():
return True return True
if block_handle: if block_handle:
if block_handle + '\n' in open(blocking_filename).read(): if block_handle + '\n' in open(blocking_filename,
encoding='utf-8').read():
return True return True
return False return False
@ -606,7 +632,8 @@ def mute_post(base_dir: str, nickname: str, domain: str, port: int,
print('MUTE: cached post not found ' + cached_post_filename) print('MUTE: cached post not found ' + cached_post_filename)
try: try:
with open(post_filename + '.muted', 'w+') as mute_file: with open(post_filename + '.muted', 'w+',
encoding='utf-8') as mute_file:
mute_file.write('\n') mute_file.write('\n')
except OSError: except OSError:
print('EX: Failed to save mute file ' + post_filename + '.muted') print('EX: Failed to save mute file ' + post_filename + '.muted')
@ -926,7 +953,8 @@ def set_broch_mode(base_dir: str, domain_full: str, enabled: bool) -> None:
if not os.path.isfile(following_filename): if not os.path.isfile(following_filename):
continue continue
try: try:
with open(following_filename, 'r') as foll_file: with open(following_filename, 'r',
encoding='utf-8') as foll_file:
follow_list = foll_file.readlines() follow_list = foll_file.readlines()
for handle in follow_list: for handle in follow_list:
if '@' not in handle: if '@' not in handle:
@ -942,7 +970,8 @@ def set_broch_mode(base_dir: str, domain_full: str, enabled: bool) -> None:
# write the allow file # write the allow file
try: try:
with open(allow_filename, 'w+') as allow_file: with open(allow_filename, 'w+',
encoding='utf-8') as allow_file:
allow_file.write(domain_full + '\n') allow_file.write(domain_full + '\n')
for allowed in allowed_domains: for allowed in allowed_domains:
allow_file.write(allowed + '\n') allow_file.write(allowed + '\n')

21
blog.py
View File

@ -41,7 +41,7 @@ from cache import get_person_from_cache
def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {}, def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {},
nickname: str, domain: str, domain_full: str, nickname: str, domain: str, domain_full: str,
post_id: str, depth=0) -> int: post_id: str, depth: int = 0) -> int:
"""Returns the number of replies on the post """Returns the number of replies on the post
This is recursive, so can handle replies to replies This is recursive, so can handle replies to replies
""" """
@ -73,7 +73,7 @@ def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {},
replies = 0 replies = 0
lines = [] lines = []
try: try:
with open(post_filename, 'r') as post_file: with open(post_filename, 'r', encoding='utf-8') as post_file:
lines = post_file.readlines() lines = post_file.readlines()
except OSError: except OSError:
print('EX: failed to read blog ' + post_filename) print('EX: failed to read blog ' + post_filename)
@ -96,7 +96,7 @@ def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {},
print('Rewriting ' + post_filename + ' to remove ' + print('Rewriting ' + post_filename + ' to remove ' +
str(len(removals)) + ' entries') str(len(removals)) + ' entries')
try: try:
with open(post_filename, 'w+') as post_file: with open(post_filename, 'w+', encoding='utf-8') as post_file:
for reply_post_id in lines: for reply_post_id in lines:
reply_post_id = \ reply_post_id = \
reply_post_id.replace('\n', '').replace('\r', '') reply_post_id.replace('\n', '').replace('\r', '')
@ -111,7 +111,7 @@ def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {},
def _get_blog_replies(base_dir: str, http_prefix: str, translate: {}, def _get_blog_replies(base_dir: str, http_prefix: str, translate: {},
nickname: str, domain: str, domain_full: str, nickname: str, domain: str, domain_full: str,
post_id: str, depth=0) -> str: post_id: str, depth: int = 0) -> str:
"""Returns a string containing html blog posts """Returns a string containing html blog posts
""" """
if depth > 4: if depth > 4:
@ -140,7 +140,8 @@ def _get_blog_replies(base_dir: str, http_prefix: str, translate: {},
post_id.replace('/', '#') + '.html' post_id.replace('/', '#') + '.html'
if os.path.isfile(post_filename): if os.path.isfile(post_filename):
try: try:
with open(post_filename, 'r') as post_file: with open(post_filename, 'r',
encoding='utf-8') as post_file:
return post_file.read() + '\n' return post_file.read() + '\n'
except OSError: except OSError:
print('EX: unable to read blog 3 ' + post_filename) print('EX: unable to read blog 3 ' + post_filename)
@ -148,7 +149,7 @@ def _get_blog_replies(base_dir: str, http_prefix: str, translate: {},
lines = [] lines = []
try: try:
with open(post_filename, 'r') as post_file: with open(post_filename, 'r', encoding='utf-8') as post_file:
lines = post_file.readlines() lines = post_file.readlines()
except OSError: except OSError:
print('EX: unable to read blog 4 ' + post_filename) print('EX: unable to read blog 4 ' + post_filename)
@ -165,7 +166,7 @@ def _get_blog_replies(base_dir: str, http_prefix: str, translate: {},
if not os.path.isfile(post_filename): if not os.path.isfile(post_filename):
continue continue
try: try:
with open(post_filename, 'r') as post_file: with open(post_filename, 'r', encoding='utf-8') as post_file:
replies_str += post_file.read() + '\n' replies_str += post_file.read() + '\n'
except OSError: except OSError:
print('EX: unable to read blog replies ' + post_filename) print('EX: unable to read blog replies ' + post_filename)
@ -779,7 +780,8 @@ def html_edit_blog(media_instance: bool, translate: {},
if os.path.isfile(base_dir + '/accounts/newpost.txt'): if os.path.isfile(base_dir + '/accounts/newpost.txt'):
try: try:
with open(base_dir + '/accounts/newpost.txt', 'r') as file: with open(base_dir + '/accounts/newpost.txt', 'r',
encoding='utf-8') as file:
edit_blog_text = '<p>' + file.read() + '</p>' edit_blog_text = '<p>' + file.read() + '</p>'
except OSError: except OSError:
print('EX: unable to read ' + base_dir + '/accounts/newpost.txt') print('EX: unable to read ' + base_dir + '/accounts/newpost.txt')
@ -927,7 +929,8 @@ def path_contains_blog_link(base_dir: str,
acct_dir(base_dir, nickname, domain) + '/tlblogs.index' acct_dir(base_dir, nickname, domain) + '/tlblogs.index'
if not os.path.isfile(blog_index_filename): if not os.path.isfile(blog_index_filename):
return None, None return None, None
if '#' + user_ending2[1] + '.' not in open(blog_index_filename).read(): if '#' + user_ending2[1] + '.' not in open(blog_index_filename,
encoding='utf-8').read():
return None, None return None, None
message_id = local_actor_url(http_prefix, nickname, domain_full) + \ message_id = local_actor_url(http_prefix, nickname, domain_full) + \
'/statuses/' + user_ending2[1] '/statuses/' + user_ending2[1]

View File

@ -72,17 +72,20 @@ def undo_bookmarks_collection_entry(recent_posts_cache: {},
else: else:
bookmark_index = post_filename.strip() bookmark_index = post_filename.strip()
bookmark_index = bookmark_index.replace('\n', '').replace('\r', '') bookmark_index = bookmark_index.replace('\n', '').replace('\r', '')
if bookmark_index not in open(bookmarks_index_filename).read(): if bookmark_index not in open(bookmarks_index_filename,
encoding='utf-8').read():
return return
index_str = '' index_str = ''
try: try:
with open(bookmarks_index_filename, 'r') as index_file: with open(bookmarks_index_filename, 'r',
encoding='utf-8') as index_file:
index_str = index_file.read().replace(bookmark_index + '\n', '') index_str = index_file.read().replace(bookmark_index + '\n', '')
except OSError: except OSError:
print('EX: unable to read ' + bookmarks_index_filename) print('EX: unable to read ' + bookmarks_index_filename)
if index_str: if index_str:
try: try:
with open(bookmarks_index_filename, 'w+') as bmi_file: with open(bookmarks_index_filename, 'w+',
encoding='utf-8') as bmi_file:
bmi_file.write(index_str) bmi_file.write(index_str)
except OSError: except OSError:
print('EX: unable to write bookmarks index ' + print('EX: unable to write bookmarks index ' +
@ -236,9 +239,11 @@ def update_bookmarks_collection(recent_posts_cache: {},
acct_dir(base_dir, nickname, domain) + '/bookmarks.index' acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
bookmark_index = post_filename.split('/')[-1] bookmark_index = post_filename.split('/')[-1]
if os.path.isfile(bookmarks_index_filename): if os.path.isfile(bookmarks_index_filename):
if bookmark_index not in open(bookmarks_index_filename).read(): if bookmark_index not in open(bookmarks_index_filename,
encoding='utf-8').read():
try: try:
with open(bookmarks_index_filename, 'r+') as bmi_file: with open(bookmarks_index_filename, 'r+',
encoding='utf-8') as bmi_file:
content = bmi_file.read() content = bmi_file.read()
if bookmark_index + '\n' not in content: if bookmark_index + '\n' not in content:
bmi_file.seek(0, 0) bmi_file.seek(0, 0)
@ -250,7 +255,8 @@ def update_bookmarks_collection(recent_posts_cache: {},
bookmarks_index_filename + ' ' + str(ex)) bookmarks_index_filename + ' ' + str(ex))
else: else:
try: try:
with open(bookmarks_index_filename, 'w+') as bm_file: with open(bookmarks_index_filename, 'w+',
encoding='utf-8') as bm_file:
bm_file.write(bookmark_index + '\n') bm_file.write(bookmark_index + '\n')
except OSError: except OSError:
print('EX: unable to write bookmarks index ' + print('EX: unable to write bookmarks index ' +

View File

@ -29,7 +29,7 @@ def get_hashtag_category(base_dir: str, hashtag: str) -> str:
category_str = None category_str = None
try: try:
with open(category_filename, 'r') as category_file: with open(category_filename, 'r', encoding='utf-8') as category_file:
category_str = category_file.read() category_str = category_file.read()
except OSError: except OSError:
print('EX: unable to read category ' + category_filename) print('EX: unable to read category ' + category_filename)
@ -60,7 +60,7 @@ def get_hashtag_categories(base_dir: str,
hashtag = catfile.split('.')[0] hashtag = catfile.split('.')[0]
if len(hashtag) > MAX_TAG_LENGTH: if len(hashtag) > MAX_TAG_LENGTH:
continue continue
with open(category_filename, 'r') as fp_category: with open(category_filename, 'r', encoding='utf-8') as fp_category:
category_str = fp_category.read() category_str = fp_category.read()
if not category_str: if not category_str:
@ -120,7 +120,8 @@ def update_hashtag_categories(base_dir: str) -> None:
# save a list of available categories for quick lookup # save a list of available categories for quick lookup
try: try:
with open(category_list_filename, 'w+') as fp_category: with open(category_list_filename, 'w+',
encoding='utf-8') as fp_category:
fp_category.write(category_list_str) fp_category.write(category_list_str)
except OSError: except OSError:
print('EX: unable to write category ' + category_list_filename) print('EX: unable to write category ' + category_list_filename)
@ -171,7 +172,7 @@ def set_hashtag_category(base_dir: str, hashtag: str, category: str,
category_written = False category_written = False
try: try:
with open(category_filename, 'w+') as fp_category: with open(category_filename, 'w+', encoding='utf-8') as fp_category:
fp_category.write(category) fp_category.write(category)
category_written = True category_written = True
except OSError as ex: except OSError as ex:

View File

@ -211,7 +211,7 @@ def spoof_geolocation(base_dir: str,
"", "", 0) "", "", 0)
cities = [] cities = []
try: try:
with open(locations_filename, 'r') as loc_file: with open(locations_filename, 'r', encoding='utf-8') as loc_file:
cities = loc_file.readlines() cities = loc_file.readlines()
except OSError: except OSError:
print('EX: unable to read locations ' + locations_filename) print('EX: unable to read locations ' + locations_filename)
@ -223,7 +223,7 @@ def spoof_geolocation(base_dir: str,
if os.path.isfile(nogo_filename): if os.path.isfile(nogo_filename):
nogo_list = [] nogo_list = []
try: try:
with open(nogo_filename, 'r') as nogo_file: with open(nogo_filename, 'r', encoding='utf-8') as nogo_file:
nogo_list = nogo_file.readlines() nogo_list = nogo_file.readlines()
except OSError: except OSError:
print('EX: unable to read ' + nogo_filename) print('EX: unable to read ' + nogo_filename)
@ -319,7 +319,7 @@ def get_spoofed_city(city: str, base_dir: str,
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: try:
with open(city_filename, 'r') as city_file: with open(city_filename, 'r', encoding='utf-8') as city_file:
city = city_file.read().replace('\n', '') city = city_file.read().replace('\n', '')
except OSError: except OSError:
print('EX: unable to read ' + city_filename) print('EX: unable to read ' + city_filename)

View File

@ -228,7 +228,7 @@ def dangerous_css(filename: str, allow_local_network_access: bool) -> bool:
content = None content = None
try: try:
with open(filename, 'r') as css_file: with open(filename, 'r', encoding='utf-8') as css_file:
content = css_file.read().lower() content = css_file.read().lower()
except OSError: except OSError:
print('EX: unable to read css file ' + filename) print('EX: unable to read css file ' + filename)
@ -281,7 +281,8 @@ def switch_words(base_dir: str, nickname: str, domain: str, content: str,
if not os.path.isfile(switch_words_filename): if not os.path.isfile(switch_words_filename):
return content return content
try: try:
with open(switch_words_filename, 'r') as words_file: with open(switch_words_filename, 'r',
encoding='utf-8') as words_file:
rules = words_file.readlines() rules = words_file.readlines()
except OSError: except OSError:
print('EX: unable to read switches ' + switch_words_filename) print('EX: unable to read switches ' + switch_words_filename)
@ -376,7 +377,8 @@ def _update_common_emoji(base_dir: str, emoji_content: str) -> None:
common_emoji = None common_emoji = None
if os.path.isfile(common_emoji_filename): if os.path.isfile(common_emoji_filename):
try: try:
with open(common_emoji_filename, 'r') as fp_emoji: with open(common_emoji_filename, 'r',
encoding='utf-8') as fp_emoji:
common_emoji = fp_emoji.readlines() common_emoji = fp_emoji.readlines()
except OSError: except OSError:
print('EX: unable to load common emoji file') print('EX: unable to load common emoji file')
@ -400,7 +402,8 @@ def _update_common_emoji(base_dir: str, emoji_content: str) -> None:
new_common_emoji.append(str(1).zfill(16) + ' ' + emoji_content) new_common_emoji.append(str(1).zfill(16) + ' ' + emoji_content)
new_common_emoji.sort(reverse=True) new_common_emoji.sort(reverse=True)
try: try:
with open(common_emoji_filename, 'w+') as fp_emoji: with open(common_emoji_filename, 'w+',
encoding='utf-8') as fp_emoji:
for line in new_common_emoji: for line in new_common_emoji:
fp_emoji.write(line + '\n') fp_emoji.write(line + '\n')
except OSError: except OSError:
@ -409,7 +412,8 @@ def _update_common_emoji(base_dir: str, emoji_content: str) -> None:
else: else:
line = str(1).zfill(16) + ' ' + emoji_content + '\n' line = str(1).zfill(16) + ' ' + emoji_content + '\n'
try: try:
with open(common_emoji_filename, 'w+') as fp_emoji: with open(common_emoji_filename, 'w+',
encoding='utf-8') as fp_emoji:
fp_emoji.write(line) fp_emoji.write(line)
except OSError: except OSError:
print('EX: error writing common emoji 2') print('EX: error writing common emoji 2')
@ -929,19 +933,19 @@ def remove_long_words(content: str, max_word_length: int,
continue continue
if 'https:' in word_str: if 'https:' in word_str:
continue continue
elif 'http:' in word_str: if 'http:' in word_str:
continue continue
elif 'i2p:' in word_str: if 'i2p:' in word_str:
continue continue
elif 'gnunet:' in word_str: if 'gnunet:' in word_str:
continue continue
elif 'dat:' in word_str: if 'dat:' in word_str:
continue continue
elif 'rad:' in word_str: if 'rad:' in word_str:
continue continue
elif 'hyper:' in word_str: if 'hyper:' in word_str:
continue continue
elif 'briar:' in word_str: if 'briar:' in word_str:
continue continue
if '<' in word_str: if '<' in word_str:
replace_word = word_str.split('<', 1)[0] replace_word = word_str.split('<', 1)[0]
@ -973,7 +977,7 @@ def _load_auto_tags(base_dir: str, nickname: str, domain: str) -> []:
if not os.path.isfile(filename): if not os.path.isfile(filename):
return [] return []
try: try:
with open(filename, 'r') as tags_file: with open(filename, 'r', encoding='utf-8') as tags_file:
return tags_file.readlines() return tags_file.readlines()
except OSError: except OSError:
print('EX: unable to read auto tags ' + filename) print('EX: unable to read auto tags ' + filename)
@ -1052,7 +1056,8 @@ def add_html_tags(base_dir: str, http_prefix: str,
if os.path.isfile(following_filename): if os.path.isfile(following_filename):
following = [] following = []
try: try:
with open(following_filename, 'r') as foll_file: with open(following_filename, 'r',
encoding='utf-8') as foll_file:
following = foll_file.readlines() following = foll_file.readlines()
except OSError: except OSError:
print('EX: unable to read ' + following_filename) print('EX: unable to read ' + following_filename)
@ -1557,7 +1562,7 @@ def import_emoji(base_dir: str, import_filename: str, session) -> None:
return return
emoji_dict = load_json(base_dir + '/emoji/default_emoji.json', 0, 1) emoji_dict = load_json(base_dir + '/emoji/default_emoji.json', 0, 1)
added = 0 added = 0
with open(import_filename, "r") as fp_emoji: with open(import_filename, "r", encoding='utf-8') as fp_emoji:
lines = fp_emoji.readlines() lines = fp_emoji.readlines()
for line in lines: for line in lines:
url = line.split(', ')[0] url = line.split(', ')[0]
@ -1713,7 +1718,8 @@ def remove_script(content: str, log_filename: str,
if os.path.isfile(log_filename): if os.path.isfile(log_filename):
write_type = 'w+' write_type = 'w+'
try: try:
with open(log_filename, write_type) as fp_log: with open(log_filename, write_type,
encoding='utf-8') as fp_log:
fp_log.write(log_str) fp_log.write(log_str)
except OSError: except OSError:
print('EX: cannot append to svg script log') print('EX: cannot append to svg script log')

View File

@ -43,15 +43,18 @@ def update_conversation(base_dir: str, nickname: str, domain: str,
post_id = remove_id_ending(post_json_object['object']['id']) post_id = remove_id_ending(post_json_object['object']['id'])
if not os.path.isfile(conversation_filename): if not os.path.isfile(conversation_filename):
try: try:
with open(conversation_filename, 'w+') as conv_file: with open(conversation_filename, 'w+',
encoding='utf-8') as conv_file:
conv_file.write(post_id + '\n') conv_file.write(post_id + '\n')
return True return True
except OSError: except OSError:
print('EX: update_conversation ' + print('EX: update_conversation ' +
'unable to write to ' + conversation_filename) 'unable to write to ' + conversation_filename)
elif post_id + '\n' not in open(conversation_filename).read(): elif post_id + '\n' not in open(conversation_filename,
encoding='utf-8').read():
try: try:
with open(conversation_filename, 'a+') as conv_file: with open(conversation_filename, 'a+',
encoding='utf-8') as conv_file:
conv_file.write(post_id + '\n') conv_file.write(post_id + '\n')
return True return True
except OSError: except OSError:
@ -72,7 +75,8 @@ def mute_conversation(base_dir: str, nickname: str, domain: str,
if os.path.isfile(conversation_filename + '.muted'): if os.path.isfile(conversation_filename + '.muted'):
return return
try: try:
with open(conversation_filename + '.muted', 'w+') as conv_file: with open(conversation_filename + '.muted', 'w+',
encoding='utf-8') as conv_file:
conv_file.write('\n') conv_file.write('\n')
except OSError: except OSError:
print('EX: unable to write mute ' + conversation_filename) print('EX: unable to write mute ' + conversation_filename)

View File

@ -60,7 +60,7 @@ def load_known_web_bots(base_dir: str) -> []:
return [] return []
crawlers_str = None crawlers_str = None
try: try:
with open(known_bots_filename, 'r') as fp_crawlers: with open(known_bots_filename, 'r', encoding='utf-8') as fp_crawlers:
crawlers_str = fp_crawlers.read() crawlers_str = fp_crawlers.read()
except OSError: except OSError:
print('EX: unable to load web bots from ' + print('EX: unable to load web bots from ' +
@ -88,7 +88,7 @@ def _save_known_web_bots(base_dir: str, known_bots: []) -> bool:
for crawler in known_bots: for crawler in known_bots:
known_bots_str += crawler.strip() + '\n' known_bots_str += crawler.strip() + '\n'
try: try:
with open(known_bots_filename, 'w+') as fp_crawlers: with open(known_bots_filename, 'w+', encoding='utf-8') as fp_crawlers:
fp_crawlers.write(known_bots_str) fp_crawlers.write(known_bots_str)
except OSError: except OSError:
print("EX: unable to save known web bots to " + print("EX: unable to save known web bots to " +

184
daemon.py
View File

@ -510,7 +510,7 @@ class PubServer(BaseHTTPRequestHandler):
if os.path.isfile(votes_filename): if os.path.isfile(votes_filename):
# have we already voted on this? # have we already voted on this?
if message_id in open(votes_filename).read(): if message_id in open(votes_filename, encoding='utf-8').read():
print('Already voted on message ' + message_id) print('Already voted on message ' + message_id)
return return
@ -580,7 +580,8 @@ class PubServer(BaseHTTPRequestHandler):
self.server.debug) self.server.debug)
# record the vote # record the vote
try: try:
with open(votes_filename, 'a+') as votes_file: with open(votes_filename, 'a+',
encoding='utf-8') as votes_file:
votes_file.write(message_id + '\n') votes_file.write(message_id + '\n')
except OSError: except OSError:
print('EX: unable to write vote ' + print('EX: unable to write vote ' +
@ -892,7 +893,8 @@ class PubServer(BaseHTTPRequestHandler):
etag = None etag = None
if os.path.isfile(media_filename + '.etag'): if os.path.isfile(media_filename + '.etag'):
try: try:
with open(media_filename + '.etag', 'r') as efile: with open(media_filename + '.etag', 'r',
encoding='utf-8') as efile:
etag = efile.read() etag = efile.read()
except OSError: except OSError:
print('EX: _set_headers_etag ' + print('EX: _set_headers_etag ' +
@ -900,7 +902,8 @@ class PubServer(BaseHTTPRequestHandler):
if not etag: if not etag:
etag = md5(data).hexdigest() # nosec etag = md5(data).hexdigest() # nosec
try: try:
with open(media_filename + '.etag', 'w+') as efile: with open(media_filename + '.etag', 'w+',
encoding='utf-8') as efile:
efile.write(etag) efile.write(etag)
except OSError: except OSError:
print('EX: _set_headers_etag ' + print('EX: _set_headers_etag ' +
@ -926,7 +929,8 @@ class PubServer(BaseHTTPRequestHandler):
# load the etag from file # load the etag from file
curr_etag = '' curr_etag = ''
try: try:
with open(media_filename + '.etag', 'r') as efile: with open(media_filename + '.etag', 'r',
encoding='utf-8') as efile:
curr_etag = efile.read() curr_etag = efile.read()
except OSError: except OSError:
print('EX: _etag_exists unable to read ' + print('EX: _etag_exists unable to read ' +
@ -1953,9 +1957,9 @@ class PubServer(BaseHTTPRequestHandler):
# to be authorized to use an account you don't own # to be authorized to use an account you don't own
if '/' + nickname + '/' in self.path: if '/' + nickname + '/' in self.path:
return True return True
elif '/' + nickname + '?' in self.path: if '/' + nickname + '?' in self.path:
return True return True
elif self.path.endswith('/' + nickname): if self.path.endswith('/' + nickname):
return True return True
if self.server.debug: if self.server.debug:
print('AUTH: nickname ' + nickname + print('AUTH: nickname ' + nickname +
@ -2128,14 +2132,16 @@ class PubServer(BaseHTTPRequestHandler):
salt = create_password(32) salt = create_password(32)
if os.path.isfile(salt_filename): if os.path.isfile(salt_filename):
try: try:
with open(salt_filename, 'r') as fp_salt: with open(salt_filename, 'r',
encoding='utf-8') as fp_salt:
salt = fp_salt.read() salt = fp_salt.read()
except OSError as ex: except OSError as ex:
print('EX: Unable to read salt for ' + print('EX: Unable to read salt for ' +
login_nickname + ' ' + str(ex)) login_nickname + ' ' + str(ex))
else: else:
try: try:
with open(salt_filename, 'w+') as fp_salt: with open(salt_filename, 'w+',
encoding='utf-8') as fp_salt:
fp_salt.write(salt) fp_salt.write(salt)
except OSError as ex: except OSError as ex:
print('EX: Unable to save salt for ' + print('EX: Unable to save salt for ' +
@ -2149,7 +2155,8 @@ class PubServer(BaseHTTPRequestHandler):
base_dir + '/accounts/' + \ base_dir + '/accounts/' + \
login_handle + '/.token' login_handle + '/.token'
try: try:
with open(token_filename, 'w+') as fp_tok: with open(token_filename, 'w+',
encoding='utf-8') as fp_tok:
fp_tok.write(token) fp_tok.write(token)
except OSError as ex: except OSError as ex:
print('EX: Unable to save token for ' + print('EX: Unable to save token for ' +
@ -2960,7 +2967,8 @@ class PubServer(BaseHTTPRequestHandler):
feat_filename = features_blocked_filename feat_filename = features_blocked_filename
feat_written = False feat_written = False
try: try:
with open(feat_filename, 'w+') as nofile: with open(feat_filename, 'w+',
encoding='utf-8') as nofile:
nofile.write('\n') nofile.write('\n')
feat_written = True feat_written = True
except OSError as ex: except OSError as ex:
@ -3004,7 +3012,8 @@ class PubServer(BaseHTTPRequestHandler):
if os.path.isdir(account_dir): if os.path.isdir(account_dir):
nw_filename = newswire_mod_filename nw_filename = newswire_mod_filename
try: try:
with open(nw_filename, 'w+') as modfile: with open(nw_filename, 'w+',
encoding='utf-8') as modfile:
modfile.write('\n') modfile.write('\n')
except OSError: except OSError:
print('EX: unable to write ' + nw_filename) print('EX: unable to write ' + nw_filename)
@ -4781,7 +4790,8 @@ class PubServer(BaseHTTPRequestHandler):
links_str += '\n' links_str += '\n'
links_str += fields['newColLink'] + '\n' links_str += fields['newColLink'] + '\n'
try: try:
with open(links_filename, 'w+') as linksfile: with open(links_filename, 'w+',
encoding='utf-8') as linksfile:
linksfile.write(links_str) linksfile.write(links_str)
except OSError: except OSError:
print('EX: _links_update unable to write ' + print('EX: _links_update unable to write ' +
@ -4791,7 +4801,8 @@ class PubServer(BaseHTTPRequestHandler):
# the text area is empty but there is a new link added # the text area is empty but there is a new link added
links_str = fields['newColLink'] + '\n' links_str = fields['newColLink'] + '\n'
try: try:
with open(links_filename, 'w+') as linksfile: with open(links_filename, 'w+',
encoding='utf-8') as linksfile:
linksfile.write(links_str) linksfile.write(links_str)
except OSError: except OSError:
print('EX: _links_update unable to write ' + print('EX: _links_update unable to write ' +
@ -4812,7 +4823,8 @@ class PubServer(BaseHTTPRequestHandler):
if not dangerous_markup(about_str, if not dangerous_markup(about_str,
allow_local_network_access): allow_local_network_access):
try: try:
with open(about_filename, 'w+') as aboutfile: with open(about_filename, 'w+',
encoding='utf-8') as aboutfile:
aboutfile.write(about_str) aboutfile.write(about_str)
except OSError: except OSError:
print('EX: unable to write about ' + print('EX: unable to write about ' +
@ -4830,7 +4842,8 @@ class PubServer(BaseHTTPRequestHandler):
if not dangerous_markup(tos_str, if not dangerous_markup(tos_str,
allow_local_network_access): allow_local_network_access):
try: try:
with open(tos_filename, 'w+') as tosfile: with open(tos_filename, 'w+',
encoding='utf-8') as tosfile:
tosfile.write(tos_str) tosfile.write(tos_str)
except OSError: except OSError:
print('EX: unable to write TOS ' + tos_filename) print('EX: unable to write TOS ' + tos_filename)
@ -5024,7 +5037,8 @@ class PubServer(BaseHTTPRequestHandler):
newswire_str += '\n' newswire_str += '\n'
newswire_str += fields['newNewswireFeed'] + '\n' newswire_str += fields['newNewswireFeed'] + '\n'
try: try:
with open(newswire_filename, 'w+') as newsfile: with open(newswire_filename, 'w+',
encoding='utf-8') as newsfile:
newsfile.write(newswire_str) newsfile.write(newswire_str)
except OSError: except OSError:
print('EX: unable to write ' + newswire_filename) print('EX: unable to write ' + newswire_filename)
@ -5033,7 +5047,8 @@ class PubServer(BaseHTTPRequestHandler):
# the text area is empty but there is a new feed added # the text area is empty but there is a new feed added
newswire_str = fields['newNewswireFeed'] + '\n' newswire_str = fields['newNewswireFeed'] + '\n'
try: try:
with open(newswire_filename, 'w+') as newsfile: with open(newswire_filename, 'w+',
encoding='utf-8') as newsfile:
newsfile.write(newswire_str) newsfile.write(newswire_str)
except OSError: except OSError:
print('EX: unable to write ' + newswire_filename) print('EX: unable to write ' + newswire_filename)
@ -5052,7 +5067,8 @@ class PubServer(BaseHTTPRequestHandler):
'news@' + domain + '/filters.txt' 'news@' + domain + '/filters.txt'
if fields.get('filteredWordsNewswire'): if fields.get('filteredWordsNewswire'):
try: try:
with open(filter_newswire_filename, 'w+') as filterfile: with open(filter_newswire_filename, 'w+',
encoding='utf-8') as filterfile:
filterfile.write(fields['filteredWordsNewswire']) filterfile.write(fields['filteredWordsNewswire'])
except OSError: except OSError:
print('EX: unable to write ' + filter_newswire_filename) print('EX: unable to write ' + filter_newswire_filename)
@ -5069,7 +5085,8 @@ class PubServer(BaseHTTPRequestHandler):
base_dir + '/accounts/hashtagrules.txt' base_dir + '/accounts/hashtagrules.txt'
if fields.get('hashtagRulesList'): if fields.get('hashtagRulesList'):
try: try:
with open(hashtag_rules_filename, 'w+') as rulesfile: with open(hashtag_rules_filename, 'w+',
encoding='utf-8') as rulesfile:
rulesfile.write(fields['hashtagRulesList']) rulesfile.write(fields['hashtagRulesList'])
except OSError: except OSError:
print('EX: unable to write ' + hashtag_rules_filename) print('EX: unable to write ' + hashtag_rules_filename)
@ -5088,7 +5105,8 @@ class PubServer(BaseHTTPRequestHandler):
if not newswire_trusted.endswith('\n'): if not newswire_trusted.endswith('\n'):
newswire_trusted += '\n' newswire_trusted += '\n'
try: try:
with open(newswire_tusted_filename, 'w+') as trustfile: with open(newswire_tusted_filename, 'w+',
encoding='utf-8') as trustfile:
trustfile.write(newswire_trusted) trustfile.write(newswire_trusted)
except OSError: except OSError:
print('EX: unable to write ' + newswire_tusted_filename) print('EX: unable to write ' + newswire_tusted_filename)
@ -5188,7 +5206,8 @@ class PubServer(BaseHTTPRequestHandler):
# save citations dates, so that they can be added when # save citations dates, so that they can be added when
# reloading the newblog screen # reloading the newblog screen
try: try:
with open(citations_filename, 'w+') as citfile: with open(citations_filename, 'w+',
encoding='utf-8') as citfile:
citfile.write(citations_str) citfile.write(citations_str)
except OSError: except OSError:
print('EX: unable to write ' + citations_filename) print('EX: unable to write ' + citations_filename)
@ -6315,8 +6334,8 @@ class PubServer(BaseHTTPRequestHandler):
# if the list was given as comma separated # if the list was given as comma separated
mods = fields['moderators'].split(',') mods = fields['moderators'].split(',')
try: try:
with open(moderators_file, with open(moderators_file, 'w+',
'w+') as modfile: encoding='utf-8') as modfile:
for mod_nick in mods: for mod_nick in mods:
mod_nick = mod_nick.strip() mod_nick = mod_nick.strip()
mod_dir = base_dir + \ mod_dir = base_dir + \
@ -6343,8 +6362,8 @@ class PubServer(BaseHTTPRequestHandler):
# nicknames on separate lines # nicknames on separate lines
mods = fields['moderators'].split('\n') mods = fields['moderators'].split('\n')
try: try:
with open(moderators_file, with open(moderators_file, 'w+',
'w+') as modfile: encoding='utf-8') as modfile:
for mod_nick in mods: for mod_nick in mods:
mod_nick = mod_nick.strip() mod_nick = mod_nick.strip()
mod_dir = \ mod_dir = \
@ -6447,8 +6466,8 @@ class PubServer(BaseHTTPRequestHandler):
# if the list was given as comma separated # if the list was given as comma separated
eds = fields['counselors'].split(',') eds = fields['counselors'].split(',')
try: try:
with open(counselors_file, with open(counselors_file, 'w+',
'w+') as edfile: encoding='utf-8') as edfile:
for ed_nick in eds: for ed_nick in eds:
ed_nick = ed_nick.strip() ed_nick = ed_nick.strip()
ed_dir = base_dir + \ ed_dir = base_dir + \
@ -6475,8 +6494,8 @@ class PubServer(BaseHTTPRequestHandler):
# nicknames on separate lines # nicknames on separate lines
eds = fields['counselors'].split('\n') eds = fields['counselors'].split('\n')
try: try:
with open(counselors_file, with open(counselors_file, 'w+',
'w+') as edfile: encoding='utf-8') as edfile:
for ed_nick in eds: for ed_nick in eds:
ed_nick = ed_nick.strip() ed_nick = ed_nick.strip()
ed_dir = \ ed_dir = \
@ -6515,7 +6534,8 @@ class PubServer(BaseHTTPRequestHandler):
# if the list was given as comma separated # if the list was given as comma separated
eds = fields['artists'].split(',') eds = fields['artists'].split(',')
try: try:
with open(artists_file, 'w+') as edfil: with open(artists_file, 'w+',
encoding='utf-8') as edfil:
for ed_nick in eds: for ed_nick in eds:
ed_nick = ed_nick.strip() ed_nick = ed_nick.strip()
ed_dir = base_dir + \ ed_dir = base_dir + \
@ -6540,7 +6560,8 @@ class PubServer(BaseHTTPRequestHandler):
# nicknames on separate lines # nicknames on separate lines
eds = fields['artists'].split('\n') eds = fields['artists'].split('\n')
try: try:
with open(artists_file, 'w+') as edfil: with open(artists_file, 'w+',
encoding='utf-8') as edfil:
for ed_nick in eds: for ed_nick in eds:
ed_nick = ed_nick.strip() ed_nick = ed_nick.strip()
ed_dir = \ ed_dir = \
@ -6655,7 +6676,8 @@ class PubServer(BaseHTTPRequestHandler):
# initial default setting created via # initial default setting created via
# the welcome screen # the welcome screen
try: try:
with open(follow_dms_filename, 'w+') as ffile: with open(follow_dms_filename, 'w+',
encoding='utf-8') as ffile:
ffile.write('\n') ffile.write('\n')
except OSError: except OSError:
print('EX: unable to write follow DMs ' + print('EX: unable to write follow DMs ' +
@ -6667,8 +6689,8 @@ class PubServer(BaseHTTPRequestHandler):
if fields['followDMs'] == 'on': if fields['followDMs'] == 'on':
follow_dms_active = True follow_dms_active = True
try: try:
with open(follow_dms_filename, with open(follow_dms_filename, 'w+',
'w+') as ffile: encoding='utf-8') as ffile:
ffile.write('\n') ffile.write('\n')
except OSError: except OSError:
print('EX: unable to write follow DMs 2 ' + print('EX: unable to write follow DMs 2 ' +
@ -6691,8 +6713,8 @@ class PubServer(BaseHTTPRequestHandler):
if fields['removeTwitter'] == 'on': if fields['removeTwitter'] == 'on':
remove_twitter_active = True remove_twitter_active = True
try: try:
with open(remove_twitter_filename, with open(remove_twitter_filename, 'w+',
'w+') as rfile: encoding='utf-8') as rfile:
rfile.write('\n') rfile.write('\n')
except OSError: except OSError:
print('EX: unable to write remove twitter ' + print('EX: unable to write remove twitter ' +
@ -6718,7 +6740,8 @@ class PubServer(BaseHTTPRequestHandler):
if fields['hideLikeButton'] == 'on': if fields['hideLikeButton'] == 'on':
hide_like_button_active = True hide_like_button_active = True
try: try:
with open(hide_like_button_file, 'w+') as rfil: with open(hide_like_button_file, 'w+',
encoding='utf-8') as rfil:
rfil.write('\n') rfil.write('\n')
except OSError: except OSError:
print('EX: unable to write hide like ' + print('EX: unable to write hide like ' +
@ -6752,8 +6775,8 @@ class PubServer(BaseHTTPRequestHandler):
if fields['hideReactionButton'] == 'on': if fields['hideReactionButton'] == 'on':
hide_reaction_button_active = True hide_reaction_button_active = True
try: try:
with open(hide_reaction_button_file, with open(hide_reaction_button_file, 'w+',
'w+') as rfile: encoding='utf-8') as rfile:
rfile.write('\n') rfile.write('\n')
except OSError: except OSError:
print('EX: unable to write hide reaction ' + print('EX: unable to write hide reaction ' +
@ -6785,8 +6808,8 @@ class PubServer(BaseHTTPRequestHandler):
bold_reading = True bold_reading = True
self.server.bold_reading[nickname] = True self.server.bold_reading[nickname] = True
try: try:
with open(bold_reading_filename, with open(bold_reading_filename, 'w+',
'w+') as rfile: encoding='utf-8') as rfile:
rfile.write('\n') rfile.write('\n')
except OSError: except OSError:
print('EX: unable to write bold reading ' + print('EX: unable to write bold reading ' +
@ -6806,7 +6829,8 @@ class PubServer(BaseHTTPRequestHandler):
if on_final_welcome_screen: if on_final_welcome_screen:
# default setting from welcome screen # default setting from welcome screen
try: try:
with open(notify_likes_filename, 'w+') as rfile: with open(notify_likes_filename, 'w+',
encoding='utf-8') as rfile:
rfile.write('\n') rfile.write('\n')
except OSError: except OSError:
print('EX: unable to write notify likes ' + print('EX: unable to write notify likes ' +
@ -6819,8 +6843,8 @@ class PubServer(BaseHTTPRequestHandler):
not hide_like_button_active: not hide_like_button_active:
notify_likes_active = True notify_likes_active = True
try: try:
with open(notify_likes_filename, with open(notify_likes_filename, 'w+',
'w+') as rfile: encoding='utf-8') as rfile:
rfile.write('\n') rfile.write('\n')
except OSError: except OSError:
print('EX: unable to write notify likes ' + print('EX: unable to write notify likes ' +
@ -6841,7 +6865,8 @@ class PubServer(BaseHTTPRequestHandler):
# default setting from welcome screen # default setting from welcome screen
notify_react_filename = notify_reactions_filename notify_react_filename = notify_reactions_filename
try: try:
with open(notify_react_filename, 'w+') as rfile: with open(notify_react_filename, 'w+',
encoding='utf-8') as rfile:
rfile.write('\n') rfile.write('\n')
except OSError: except OSError:
print('EX: unable to write notify reactions ' + print('EX: unable to write notify reactions ' +
@ -6854,8 +6879,8 @@ class PubServer(BaseHTTPRequestHandler):
not hide_reaction_button_active: not hide_reaction_button_active:
notify_reactions_active = True notify_reactions_active = True
try: try:
with open(notify_reactions_filename, with open(notify_reactions_filename, 'w+',
'w+') as rfile: encoding='utf-8') as rfile:
rfile.write('\n') rfile.write('\n')
except OSError: except OSError:
print('EX: unable to write ' + print('EX: unable to write ' +
@ -6945,7 +6970,8 @@ class PubServer(BaseHTTPRequestHandler):
'/filters.txt' '/filters.txt'
if fields.get('filteredWords'): if fields.get('filteredWords'):
try: try:
with open(filter_filename, 'w+') as filterfile: with open(filter_filename, 'w+',
encoding='utf-8') as filterfile:
filterfile.write(fields['filteredWords']) filterfile.write(fields['filteredWords'])
except OSError: except OSError:
print('EX: unable to write filter ' + print('EX: unable to write filter ' +
@ -6965,7 +6991,8 @@ class PubServer(BaseHTTPRequestHandler):
'/filters_bio.txt' '/filters_bio.txt'
if fields.get('filteredWordsBio'): if fields.get('filteredWordsBio'):
try: try:
with open(filter_bio_filename, 'w+') as filterfile: with open(filter_bio_filename, 'w+',
encoding='utf-8') as filterfile:
filterfile.write(fields['filteredWordsBio']) filterfile.write(fields['filteredWordsBio'])
except OSError: except OSError:
print('EX: unable to write bio filter ' + print('EX: unable to write bio filter ' +
@ -6985,7 +7012,8 @@ class PubServer(BaseHTTPRequestHandler):
'/replacewords.txt' '/replacewords.txt'
if fields.get('switchwords'): if fields.get('switchwords'):
try: try:
with open(switch_filename, 'w+') as switchfile: with open(switch_filename, 'w+',
encoding='utf-8') as switchfile:
switchfile.write(fields['switchwords']) switchfile.write(fields['switchwords'])
except OSError: except OSError:
print('EX: unable to write switches ' + print('EX: unable to write switches ' +
@ -7005,7 +7033,8 @@ class PubServer(BaseHTTPRequestHandler):
'/autotags.txt' '/autotags.txt'
if fields.get('autoTags'): if fields.get('autoTags'):
try: try:
with open(auto_tags_filename, 'w+') as autofile: with open(auto_tags_filename, 'w+',
encoding='utf-8') as autofile:
autofile.write(fields['autoTags']) autofile.write(fields['autoTags'])
except OSError: except OSError:
print('EX: unable to write auto tags ' + print('EX: unable to write auto tags ' +
@ -7025,7 +7054,8 @@ class PubServer(BaseHTTPRequestHandler):
'/autocw.txt' '/autocw.txt'
if fields.get('autoCW'): if fields.get('autoCW'):
try: try:
with open(auto_cw_filename, 'w+') as auto_cw_file: with open(auto_cw_filename, 'w+',
encoding='utf-8') as auto_cw_file:
auto_cw_file.write(fields['autoCW']) auto_cw_file.write(fields['autoCW'])
except OSError: except OSError:
print('EX: unable to write auto CW ' + print('EX: unable to write auto CW ' +
@ -7045,7 +7075,8 @@ class PubServer(BaseHTTPRequestHandler):
'/blocking.txt' '/blocking.txt'
if fields.get('blocked'): if fields.get('blocked'):
try: try:
with open(blocked_filename, 'w+') as blockedfile: with open(blocked_filename, 'w+',
encoding='utf-8') as blockedfile:
blockedfile.write(fields['blocked']) blockedfile.write(fields['blocked'])
except OSError: except OSError:
print('EX: unable to write blocked accounts ' + print('EX: unable to write blocked accounts ' +
@ -7067,8 +7098,8 @@ class PubServer(BaseHTTPRequestHandler):
'/dmAllowedinstances.txt' '/dmAllowedinstances.txt'
if fields.get('dmAllowedInstances'): if fields.get('dmAllowedInstances'):
try: try:
with open(dm_allowed_instances_filename, with open(dm_allowed_instances_filename, 'w+',
'w+') as afile: encoding='utf-8') as afile:
afile.write(fields['dmAllowedInstances']) afile.write(fields['dmAllowedInstances'])
except OSError: except OSError:
print('EX: unable to write allowed DM instances ' + print('EX: unable to write allowed DM instances ' +
@ -7090,7 +7121,8 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('allowedInstances'): if fields.get('allowedInstances'):
inst_filename = allowed_instances_filename inst_filename = allowed_instances_filename
try: try:
with open(inst_filename, 'w+') as afile: with open(inst_filename, 'w+',
encoding='utf-8') as afile:
afile.write(fields['allowedInstances']) afile.write(fields['allowedInstances'])
except OSError: except OSError:
print('EX: unable to write allowed instances ' + print('EX: unable to write allowed instances ' +
@ -7173,8 +7205,8 @@ class PubServer(BaseHTTPRequestHandler):
if fields.get('ptInstances'): if fields.get('ptInstances'):
self.server.peertube_instances.clear() self.server.peertube_instances.clear()
try: try:
with open(peertube_instances_file, with open(peertube_instances_file, 'w+',
'w+') as afile: encoding='utf-8') as afile:
afile.write(fields['ptInstances']) afile.write(fields['ptInstances'])
except OSError: except OSError:
print('EX: unable to write peertube ' + print('EX: unable to write peertube ' +
@ -7205,7 +7237,8 @@ class PubServer(BaseHTTPRequestHandler):
'/gitprojects.txt' '/gitprojects.txt'
if fields.get('gitProjects'): if fields.get('gitProjects'):
try: try:
with open(git_projects_filename, 'w+') as afile: with open(git_projects_filename, 'w+',
encoding='utf-8') as afile:
afile.write(fields['gitProjects'].lower()) afile.write(fields['gitProjects'].lower())
except OSError: except OSError:
print('EX: unable to write git ' + print('EX: unable to write git ' +
@ -11133,7 +11166,7 @@ class PubServer(BaseHTTPRequestHandler):
return True return True
ssml_str = None ssml_str = None
try: try:
with open(ssml_filename, 'r') as fp_ssml: with open(ssml_filename, 'r', encoding='utf-8') as fp_ssml:
ssml_str = fp_ssml.read() ssml_str = fp_ssml.read()
except OSError: except OSError:
pass pass
@ -14337,12 +14370,12 @@ class PubServer(BaseHTTPRequestHandler):
""" """
image_extensions = get_image_extensions() image_extensions = get_image_extensions()
for ext in image_extensions: for ext in image_extensions:
for bg in ('follow', 'options', 'login', 'welcome'): for bg_im in ('follow', 'options', 'login', 'welcome'):
# follow screen background image # follow screen background image
if path.endswith('/' + bg + '-background.' + ext): if path.endswith('/' + bg_im + '-background.' + ext):
bg_filename = \ bg_filename = \
base_dir + '/accounts/' + \ base_dir + '/accounts/' + \
bg + '-background.' + ext bg_im + '-background.' + ext
if os.path.isfile(bg_filename): if os.path.isfile(bg_filename):
if self._etag_exists(bg_filename): if self._etag_exists(bg_filename):
# The file has not changed # The file has not changed
@ -17503,13 +17536,13 @@ class PubServer(BaseHTTPRequestHandler):
in_reply_to_url = self.path.split('?replyto=')[1] in_reply_to_url = self.path.split('?replyto=')[1]
if '?' in in_reply_to_url: if '?' in in_reply_to_url:
mentions_list = in_reply_to_url.split('?') mentions_list = in_reply_to_url.split('?')
for m in mentions_list: for ment in mentions_list:
if m.startswith('mention='): if ment.startswith('mention='):
reply_handle = m.replace('mention=', '') reply_handle = ment.replace('mention=', '')
if reply_handle not in reply_to_list: if reply_handle not in reply_to_list:
reply_to_list.append(reply_handle) reply_to_list.append(reply_handle)
if m.startswith('page='): if ment.startswith('page='):
reply_page_str = m.replace('page=', '') reply_page_str = ment.replace('page=', '')
if reply_page_str.isdigit(): if reply_page_str.isdigit():
reply_page_number = int(reply_page_str) reply_page_number = int(reply_page_str)
# if m.startswith('actor='): # if m.startswith('actor='):
@ -17524,13 +17557,13 @@ class PubServer(BaseHTTPRequestHandler):
in_reply_to_url = self.path.split('?replyunlisted=')[1] in_reply_to_url = self.path.split('?replyunlisted=')[1]
if '?' in in_reply_to_url: if '?' in in_reply_to_url:
mentions_list = in_reply_to_url.split('?') mentions_list = in_reply_to_url.split('?')
for m in mentions_list: for ment in mentions_list:
if m.startswith('mention='): if ment.startswith('mention='):
reply_handle = m.replace('mention=', '') reply_handle = ment.replace('mention=', '')
if reply_handle not in reply_to_list: if reply_handle not in reply_to_list:
reply_to_list.append(reply_handle) reply_to_list.append(reply_handle)
if m.startswith('page='): if ment.startswith('page='):
reply_page_str = m.replace('page=', '') reply_page_str = ment.replace('page=', '')
if reply_page_str.isdigit(): if reply_page_str.isdigit():
reply_page_number = int(reply_page_str) reply_page_number = int(reply_page_str)
in_reply_to_url = mentions_list[0] in_reply_to_url = mentions_list[0]
@ -18800,7 +18833,8 @@ class PubServer(BaseHTTPRequestHandler):
acct_dir(self.server.base_dir, acct_dir(self.server.base_dir,
nickname, self.server.domain) + '/.lastUsed' nickname, self.server.domain) + '/.lastUsed'
try: try:
with open(last_used_filename, 'w+') as lastfile: with open(last_used_filename, 'w+',
encoding='utf-8') as lastfile:
lastfile.write(str(int(time.time()))) lastfile.write(str(int(time.time())))
except OSError: except OSError:
print('EX: _receive_new_post_process unable to write ' + print('EX: _receive_new_post_process unable to write ' +

View File

@ -169,12 +169,14 @@ def _mark_post_as_read(actor: str, post_id: str, post_category: str) -> None:
read_posts_dir = home_dir + '/.config/epicyon/' + handle read_posts_dir = home_dir + '/.config/epicyon/' + handle
read_posts_filename = read_posts_dir + '/' + post_category + '.txt' read_posts_filename = read_posts_dir + '/' + post_category + '.txt'
if os.path.isfile(read_posts_filename): if os.path.isfile(read_posts_filename):
if post_id in open(read_posts_filename).read(): if post_id in open(read_posts_filename,
encoding='utf-8').read():
return return
try: try:
# prepend to read posts file # prepend to read posts file
post_id += '\n' post_id += '\n'
with open(read_posts_filename, 'r+') as read_file: with open(read_posts_filename, 'r+',
encoding='utf-8') as read_file:
content = read_file.read() content = read_file.read()
if post_id not in content: if post_id not in content:
read_file.seek(0, 0) read_file.seek(0, 0)
@ -182,7 +184,7 @@ def _mark_post_as_read(actor: str, post_id: str, post_category: str) -> None:
except Exception as ex: except Exception as ex:
print('EX: Failed to mark post as read' + str(ex)) print('EX: Failed to mark post as read' + str(ex))
else: else:
with open(read_posts_filename, 'w+') as read_file: with open(read_posts_filename, 'w+', encoding='utf-8') as read_file:
read_file.write(post_id + '\n') read_file.write(post_id + '\n')
@ -292,7 +294,7 @@ def _desktop_show_banner() -> None:
banner_filename = 'theme/' + banner_theme + '/banner.txt' banner_filename = 'theme/' + banner_theme + '/banner.txt'
if not os.path.isfile(banner_filename): if not os.path.isfile(banner_filename):
return return
with open(banner_filename, 'r') as banner_file: with open(banner_filename, 'r', encoding='utf-8') as banner_file:
banner = banner_file.read() banner = banner_file.read()
if banner: if banner:
print(banner + '\n') print(banner + '\n')
@ -1325,7 +1327,7 @@ def _desktop_show_follow_requests(follow_requests_json: {},
def _desktop_show_following(following_json: {}, translate: {}, def _desktop_show_following(following_json: {}, translate: {},
page_number: int, indent: str, page_number: int, indent: str,
followType='following') -> None: follow_type: str = 'following') -> None:
"""Shows a page of accounts followed """Shows a page of accounts followed
""" """
if not isinstance(following_json, dict): if not isinstance(following_json, dict):
@ -1335,9 +1337,9 @@ def _desktop_show_following(following_json: {}, translate: {},
if not following_json['orderedItems']: if not following_json['orderedItems']:
return return
print('') print('')
if followType == 'following': if follow_type == 'following':
print(indent + 'Following page ' + str(page_number)) print(indent + 'Following page ' + str(page_number))
elif followType == 'followers': elif follow_type == 'followers':
print(indent + 'Followers page ' + str(page_number)) print(indent + 'Followers page ' + str(page_number))
print('') print('')
for item in following_json['orderedItems']: for item in following_json['orderedItems']:

View File

@ -1006,7 +1006,7 @@ def _command_options() -> None:
__version__, argb.language, __version__, argb.language,
signing_priv_key_pem) signing_priv_key_pem)
try: try:
with open('socnet.dot', 'w+') as fp_soc: with open('socnet.dot', 'w+', encoding='utf-8') as fp_soc:
fp_soc.write(dot_graph) fp_soc.write(dot_graph)
print('Saved to socnet.dot') print('Saved to socnet.dot')
except OSError: except OSError:
@ -1453,7 +1453,8 @@ def _command_options() -> None:
approve_follows_filename = accounts_dir + '/followrequests.txt' approve_follows_filename = accounts_dir + '/followrequests.txt'
approve_ctr = 0 approve_ctr = 0
if os.path.isfile(approve_follows_filename): if os.path.isfile(approve_follows_filename):
with open(approve_follows_filename, 'r') as approvefile: with open(approve_follows_filename, 'r',
encoding='utf-8') as approvefile:
for approve in approvefile: for approve in approvefile:
print(approve.replace('\n', '').replace('\r', '')) print(approve.replace('\n', '').replace('\r', ''))
approve_ctr += 1 approve_ctr += 1

View File

@ -16,10 +16,11 @@ def add_filter(base_dir: str, nickname: str, domain: str, words: str) -> bool:
""" """
filters_filename = acct_dir(base_dir, nickname, domain) + '/filters.txt' filters_filename = acct_dir(base_dir, nickname, domain) + '/filters.txt'
if os.path.isfile(filters_filename): if os.path.isfile(filters_filename):
if words in open(filters_filename).read(): if words in open(filters_filename, encoding='utf-8').read():
return False return False
try: try:
with open(filters_filename, 'a+') as filters_file: with open(filters_filename, 'a+',
encoding='utf-8') as filters_file:
filters_file.write(words + '\n') filters_file.write(words + '\n')
except OSError: except OSError:
print('EX: unable to append filters ' + filters_filename) print('EX: unable to append filters ' + filters_filename)
@ -36,10 +37,10 @@ def add_global_filter(base_dir: str, words: str) -> bool:
return False return False
filters_filename = base_dir + '/accounts/filters.txt' filters_filename = base_dir + '/accounts/filters.txt'
if os.path.isfile(filters_filename): if os.path.isfile(filters_filename):
if words in open(filters_filename).read(): if words in open(filters_filename, encoding='utf-8').read():
return False return False
try: try:
with open(filters_filename, 'a+') as filters_file: with open(filters_filename, 'a+', encoding='utf-8') as filters_file:
filters_file.write(words + '\n') filters_file.write(words + '\n')
except OSError: except OSError:
print('EX: unable to append filters ' + filters_filename) print('EX: unable to append filters ' + filters_filename)
@ -53,12 +54,12 @@ def remove_filter(base_dir: str, nickname: str, domain: str,
filters_filename = acct_dir(base_dir, nickname, domain) + '/filters.txt' filters_filename = acct_dir(base_dir, nickname, domain) + '/filters.txt'
if not os.path.isfile(filters_filename): if not os.path.isfile(filters_filename):
return False return False
if words not in open(filters_filename).read(): if words not in open(filters_filename, encoding='utf-8').read():
return False return False
new_filters_filename = filters_filename + '.new' new_filters_filename = filters_filename + '.new'
try: try:
with open(filters_filename, 'r') as fp_filt: with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
with open(new_filters_filename, 'w+') as fpnew: with open(new_filters_filename, 'w+', encoding='utf-8') as fpnew:
for line in fp_filt: for line in fp_filt:
line = line.replace('\n', '') line = line.replace('\n', '')
if line != words: if line != words:
@ -78,12 +79,12 @@ def remove_global_filter(base_dir: str, words: str) -> bool:
filters_filename = base_dir + '/accounts/filters.txt' filters_filename = base_dir + '/accounts/filters.txt'
if not os.path.isfile(filters_filename): if not os.path.isfile(filters_filename):
return False return False
if words not in open(filters_filename).read(): if words not in open(filters_filename, encoding='utf-8').read():
return False return False
new_filters_filename = filters_filename + '.new' new_filters_filename = filters_filename + '.new'
try: try:
with open(filters_filename, 'r') as fp_filt: with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
with open(new_filters_filename, 'w+') as fpnew: with open(new_filters_filename, 'w+', encoding='utf-8') as fpnew:
for line in fp_filt: for line in fp_filt:
line = line.replace('\n', '') line = line.replace('\n', '')
if line != words: if line != words:
@ -118,7 +119,7 @@ def _is_filtered_base(filename: str, content: str) -> bool:
return False return False
try: try:
with open(filename, 'r') as fp_filt: with open(filename, 'r', encoding='utf-8') as fp_filt:
for line in fp_filt: for line in fp_filt:
filter_str = line.replace('\n', '').replace('\r', '') filter_str = line.replace('\n', '').replace('\r', '')
if not filter_str: if not filter_str:

View File

@ -56,7 +56,8 @@ def create_initial_last_seen(base_dir: str, http_prefix: str) -> None:
os.mkdir(last_seen_dir) os.mkdir(last_seen_dir)
following_handles = [] following_handles = []
try: try:
with open(following_filename, 'r') as fp_foll: with open(following_filename, 'r',
encoding='utf-8') as fp_foll:
following_handles = fp_foll.readlines() following_handles = fp_foll.readlines()
except OSError: except OSError:
print('EX: create_initial_last_seen ' + following_filename) print('EX: create_initial_last_seen ' + following_filename)
@ -75,7 +76,8 @@ def create_initial_last_seen(base_dir: str, http_prefix: str) -> None:
last_seen_dir + '/' + actor.replace('/', '#') + '.txt' last_seen_dir + '/' + actor.replace('/', '#') + '.txt'
if not os.path.isfile(last_seen_filename): if not os.path.isfile(last_seen_filename):
try: try:
with open(last_seen_filename, 'w+') as fp_last: with open(last_seen_filename, 'w+',
encoding='utf-8') as fp_last:
fp_last.write(str(100)) fp_last.write(str(100))
except OSError: except OSError:
print('EX: create_initial_last_seen 2 ' + print('EX: create_initial_last_seen 2 ' +
@ -92,7 +94,8 @@ def _pre_approved_follower(base_dir: str,
account_dir = base_dir + '/accounts/' + handle account_dir = base_dir + '/accounts/' + handle
approved_filename = account_dir + '/approved.txt' approved_filename = account_dir + '/approved.txt'
if os.path.isfile(approved_filename): if os.path.isfile(approved_filename):
if approve_handle in open(approved_filename).read(): if approve_handle in open(approved_filename,
encoding='utf-8').read():
return True return True
return False return False
@ -112,7 +115,8 @@ def _remove_from_follow_base(base_dir: str,
' to remove ' + handle + ' from') ' to remove ' + handle + ' from')
return return
accept_deny_actor = None accept_deny_actor = None
if accept_or_deny_handle not in open(approve_follows_filename).read(): if accept_or_deny_handle not in open(approve_follows_filename,
encoding='utf-8').read():
# is this stored in the file as an actor rather than a handle? # is this stored in the file as an actor rather than a handle?
accept_deny_nickname = accept_or_deny_handle.split('@')[0] accept_deny_nickname = accept_or_deny_handle.split('@')[0]
accept_deny_domain = accept_or_deny_handle.split('@')[1] accept_deny_domain = accept_or_deny_handle.split('@')[1]
@ -123,14 +127,17 @@ def _remove_from_follow_base(base_dir: str,
for users_name in users_paths: for users_name in users_paths:
accept_deny_actor = \ accept_deny_actor = \
'://' + accept_deny_domain + users_name + accept_deny_nickname '://' + accept_deny_domain + users_name + accept_deny_nickname
if accept_deny_actor in open(approve_follows_filename).read(): if accept_deny_actor in open(approve_follows_filename,
encoding='utf-8').read():
actor_found = True actor_found = True
break break
if not actor_found: if not actor_found:
return return
try: try:
with open(approve_follows_filename + '.new', 'w+') as approvefilenew: with open(approve_follows_filename + '.new', 'w+',
with open(approve_follows_filename, 'r') as approvefile: encoding='utf-8') as approvefilenew:
with open(approve_follows_filename, 'r',
encoding='utf-8') as approvefile:
if not accept_deny_actor: if not accept_deny_actor:
for approve_handle in approvefile: for approve_handle in approvefile:
accept_deny_handle = accept_or_deny_handle accept_deny_handle = accept_or_deny_handle
@ -179,7 +186,7 @@ def is_following_actor(base_dir: str,
return False return False
if actor.startswith('@'): if actor.startswith('@'):
actor = actor[1:] actor = actor[1:]
if actor.lower() in open(following_file).read().lower(): if actor.lower() in open(following_file, encoding='utf-8').read().lower():
return True return True
following_nickname = get_nickname_from_actor(actor) following_nickname = get_nickname_from_actor(actor)
if not following_nickname: if not following_nickname:
@ -189,7 +196,8 @@ def is_following_actor(base_dir: str,
following_handle = \ following_handle = \
get_full_domain(following_nickname + '@' + following_domain, get_full_domain(following_nickname + '@' + following_domain,
following_port) following_port)
if following_handle.lower() in open(following_file).read().lower(): if following_handle.lower() in open(following_file,
encoding='utf-8').read().lower():
return True return True
return False return False
@ -232,7 +240,7 @@ def get_follower_domains(base_dir: str, nickname: str, domain: str) -> []:
lines = [] lines = []
try: try:
with open(followers_file, 'r') as fp_foll: with open(followers_file, 'r', encoding='utf-8') as fp_foll:
lines = fp_foll.readlines() lines = fp_foll.readlines()
except OSError: except OSError:
print('EX: get_follower_domains ' + followers_file) print('EX: get_follower_domains ' + followers_file)
@ -269,7 +277,7 @@ def is_follower_of_person(base_dir: str, nickname: str, domain: str,
followers_str = '' followers_str = ''
try: try:
with open(followers_file, 'r') as fp_foll: with open(followers_file, 'r', encoding='utf-8') as fp_foll:
followers_str = fp_foll.read() followers_str = fp_foll.read()
except OSError: except OSError:
print('EX: is_follower_of_person ' + followers_file) print('EX: is_follower_of_person ' + followers_file)
@ -309,20 +317,21 @@ def unfollow_account(base_dir: str, nickname: str, domain: str,
print('DEBUG: follow file ' + filename + ' was not found') print('DEBUG: follow file ' + filename + ' was not found')
return False return False
handle_to_unfollow_lower = handle_to_unfollow.lower() handle_to_unfollow_lower = handle_to_unfollow.lower()
if handle_to_unfollow_lower not in open(filename).read().lower(): if handle_to_unfollow_lower not in open(filename,
encoding='utf-8').read().lower():
if debug: if debug:
print('DEBUG: handle to unfollow ' + handle_to_unfollow + print('DEBUG: handle to unfollow ' + handle_to_unfollow +
' is not in ' + filename) ' is not in ' + filename)
return return
lines = [] lines = []
try: try:
with open(filename, 'r') as fp_unfoll: with open(filename, 'r', encoding='utf-8') as fp_unfoll:
lines = fp_unfoll.readlines() lines = fp_unfoll.readlines()
except OSError: except OSError:
print('EX: unfollow_account ' + filename) print('EX: unfollow_account ' + filename)
if lines: if lines:
try: try:
with open(filename, 'w+') as fp_unfoll: with open(filename, 'w+', encoding='utf-8') as fp_unfoll:
for line in lines: for line in lines:
check_handle = line.strip("\n").strip("\r").lower() check_handle = line.strip("\n").strip("\r").lower()
if check_handle not in (handle_to_unfollow_lower, if check_handle not in (handle_to_unfollow_lower,
@ -336,15 +345,17 @@ def unfollow_account(base_dir: str, nickname: str, domain: str,
unfollowed_filename = base_dir + '/accounts/' + handle + '/unfollowed.txt' unfollowed_filename = base_dir + '/accounts/' + handle + '/unfollowed.txt'
if os.path.isfile(unfollowed_filename): if os.path.isfile(unfollowed_filename):
if handle_to_unfollow_lower not in \ if handle_to_unfollow_lower not in \
open(unfollowed_filename).read().lower(): open(unfollowed_filename, encoding='utf-8').read().lower():
try: try:
with open(unfollowed_filename, 'a+') as fp_unfoll: with open(unfollowed_filename, 'a+',
encoding='utf-8') as fp_unfoll:
fp_unfoll.write(handle_to_unfollow + '\n') fp_unfoll.write(handle_to_unfollow + '\n')
except OSError: except OSError:
print('EX: unable to append ' + unfollowed_filename) print('EX: unable to append ' + unfollowed_filename)
else: else:
try: try:
with open(unfollowed_filename, 'w+') as fp_unfoll: with open(unfollowed_filename, 'w+',
encoding='utf-8') as fp_unfoll:
fp_unfoll.write(handle_to_unfollow + '\n') fp_unfoll.write(handle_to_unfollow + '\n')
except OSError: except OSError:
print('EX: unable to write ' + unfollowed_filename) print('EX: unable to write ' + unfollowed_filename)
@ -400,7 +411,7 @@ def _get_no_of_follows(base_dir: str, nickname: str, domain: str,
ctr = 0 ctr = 0
lines = [] lines = []
try: try:
with open(filename, 'r') as fp_foll: with open(filename, 'r', encoding='utf-8') as fp_foll:
lines = fp_foll.readlines() lines = fp_foll.readlines()
except OSError: except OSError:
print('EX: _get_no_of_follows ' + filename) print('EX: _get_no_of_follows ' + filename)
@ -517,7 +528,7 @@ def get_following_feed(base_dir: str, domain: str, port: int, path: str,
total_ctr = 0 total_ctr = 0
lines = [] lines = []
try: try:
with open(filename, 'r') as fp_foll: with open(filename, 'r', encoding='utf-8') as fp_foll:
lines = fp_foll.readlines() lines = fp_foll.readlines()
except OSError: except OSError:
print('EX: get_following_feed ' + filename) print('EX: get_following_feed ' + filename)
@ -606,7 +617,8 @@ def no_of_follow_requests(base_dir: str,
ctr = 0 ctr = 0
lines = [] lines = []
try: try:
with open(approve_follows_filename, 'r') as fp_approve: with open(approve_follows_filename, 'r',
encoding='utf-8') as fp_approve:
lines = fp_approve.readlines() lines = fp_approve.readlines()
except OSError: except OSError:
print('EX: no_of_follow_requests ' + approve_follows_filename) print('EX: no_of_follow_requests ' + approve_follows_filename)
@ -650,7 +662,8 @@ def store_follow_request(base_dir: str,
followers_str = '' followers_str = ''
try: try:
with open(followers_filename, 'r') as fp_foll: with open(followers_filename, 'r',
encoding='utf-8') as fp_foll:
followers_str = fp_foll.read() followers_str = fp_foll.read()
except OSError: except OSError:
print('EX: store_follow_request ' + followers_filename) print('EX: store_follow_request ' + followers_filename)
@ -675,7 +688,8 @@ def store_follow_request(base_dir: str,
# should this follow be denied? # should this follow be denied?
deny_follows_filename = accounts_dir + '/followrejects.txt' deny_follows_filename = accounts_dir + '/followrejects.txt'
if os.path.isfile(deny_follows_filename): if os.path.isfile(deny_follows_filename):
if approve_handle in open(deny_follows_filename).read(): if approve_handle in open(deny_follows_filename,
encoding='utf-8').read():
remove_from_follow_requests(base_dir, nickname_to_follow, remove_from_follow_requests(base_dir, nickname_to_follow,
domain_to_follow, approve_handle, domain_to_follow, approve_handle,
debug) debug)
@ -694,9 +708,11 @@ def store_follow_request(base_dir: str,
approve_handle = '!' + approve_handle approve_handle = '!' + approve_handle
if os.path.isfile(approve_follows_filename): if os.path.isfile(approve_follows_filename):
if approve_handle not in open(approve_follows_filename).read(): if approve_handle not in open(approve_follows_filename,
encoding='utf-8').read():
try: try:
with open(approve_follows_filename, 'a+') as fp_approve: with open(approve_follows_filename, 'a+',
encoding='utf-8') as fp_approve:
fp_approve.write(approve_handle_stored + '\n') fp_approve.write(approve_handle_stored + '\n')
except OSError: except OSError:
print('EX: store_follow_request 2 ' + approve_follows_filename) print('EX: store_follow_request 2 ' + approve_follows_filename)
@ -706,7 +722,8 @@ def store_follow_request(base_dir: str,
' is already awaiting approval') ' is already awaiting approval')
else: else:
try: try:
with open(approve_follows_filename, 'w+') as fp_approve: with open(approve_follows_filename, 'w+',
encoding='utf-8') as fp_approve:
fp_approve.write(approve_handle_stored + '\n') fp_approve.write(approve_handle_stored + '\n')
except OSError: except OSError:
print('EX: store_follow_request 3 ' + approve_follows_filename) print('EX: store_follow_request 3 ' + approve_follows_filename)
@ -907,10 +924,12 @@ def send_follow_request(session, base_dir: str,
unfollowed_filename = \ unfollowed_filename = \
acct_dir(base_dir, nickname, domain) + '/unfollowed.txt' acct_dir(base_dir, nickname, domain) + '/unfollowed.txt'
if os.path.isfile(unfollowed_filename): if os.path.isfile(unfollowed_filename):
if follow_handle in open(unfollowed_filename).read(): if follow_handle in open(unfollowed_filename,
encoding='utf-8').read():
unfollowed_file = None unfollowed_file = None
try: try:
with open(unfollowed_filename, 'r') as fp_unfoll: with open(unfollowed_filename, 'r',
encoding='utf-8') as fp_unfoll:
unfollowed_file = fp_unfoll.read() unfollowed_file = fp_unfoll.read()
except OSError: except OSError:
print('EX: send_follow_request ' + unfollowed_filename) print('EX: send_follow_request ' + unfollowed_filename)
@ -918,7 +937,8 @@ def send_follow_request(session, base_dir: str,
unfollowed_file = \ unfollowed_file = \
unfollowed_file.replace(follow_handle + '\n', '') unfollowed_file.replace(follow_handle + '\n', '')
try: try:
with open(unfollowed_filename, 'w+') as fp_unfoll: with open(unfollowed_filename, 'w+',
encoding='utf-8') as fp_unfoll:
fp_unfoll.write(unfollowed_file) fp_unfoll.write(unfollowed_file)
except OSError: except OSError:
print('EX: unable to write ' + unfollowed_filename) print('EX: unable to write ' + unfollowed_filename)

View File

@ -48,17 +48,20 @@ def receiving_calendar_events(base_dir: str, nickname: str, domain: str,
# create a new calendar file from the following file # create a new calendar file from the following file
following_handles = None following_handles = None
try: try:
with open(following_filename, 'r') as following_file: with open(following_filename, 'r',
encoding='utf-8') as following_file:
following_handles = following_file.read() following_handles = following_file.read()
except OSError: except OSError:
print('EX: receiving_calendar_events ' + following_filename) print('EX: receiving_calendar_events ' + following_filename)
if following_handles: if following_handles:
try: try:
with open(calendar_filename, 'w+') as fp_cal: with open(calendar_filename, 'w+',
encoding='utf-8') as fp_cal:
fp_cal.write(following_handles) fp_cal.write(following_handles)
except OSError: except OSError:
print('EX: receiving_calendar_events 2 ' + calendar_filename) print('EX: receiving_calendar_events 2 ' + calendar_filename)
return handle + '\n' in open(calendar_filename).read() return handle + '\n' in open(calendar_filename,
encoding='utf-8').read()
def _receive_calendar_events(base_dir: str, nickname: str, domain: str, def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
@ -79,7 +82,8 @@ def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
handle = following_nickname + '@' + following_domain handle = following_nickname + '@' + following_domain
# check that you are following this handle # check that you are following this handle
if handle + '\n' not in open(following_filename).read(): if handle + '\n' not in open(following_filename,
encoding='utf-8').read():
print('WARN: ' + handle + ' is not in ' + following_filename) print('WARN: ' + handle + ' is not in ' + following_filename)
return return
@ -92,7 +96,8 @@ def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
if os.path.isfile(calendar_filename): if os.path.isfile(calendar_filename):
print('Calendar file exists') print('Calendar file exists')
try: try:
with open(calendar_filename, 'r') as calendar_file: with open(calendar_filename, 'r',
encoding='utf-8') as calendar_file:
following_handles = calendar_file.read() following_handles = calendar_file.read()
except OSError: except OSError:
print('EX: _receive_calendar_events ' + calendar_filename) print('EX: _receive_calendar_events ' + calendar_filename)
@ -101,13 +106,15 @@ def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
print('Creating calendar file ' + calendar_filename) print('Creating calendar file ' + calendar_filename)
following_handles = '' following_handles = ''
try: try:
with open(following_filename, 'r') as following_file: with open(following_filename, 'r',
encoding='utf-8') as following_file:
following_handles = following_file.read() following_handles = following_file.read()
except OSError: except OSError:
print('EX: _receive_calendar_events 2 ' + calendar_filename) print('EX: _receive_calendar_events 2 ' + calendar_filename)
if add: if add:
try: try:
with open(calendar_filename, 'w+') as fp_cal: with open(calendar_filename, 'w+',
encoding='utf-8') as fp_cal:
fp_cal.write(following_handles + handle + '\n') fp_cal.write(following_handles + handle + '\n')
except OSError: except OSError:
print('EX: unable to write ' + calendar_filename) print('EX: unable to write ' + calendar_filename)
@ -121,7 +128,8 @@ def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
# remove from calendar file # remove from calendar file
following_handles = following_handles.replace(handle + '\n', '') following_handles = following_handles.replace(handle + '\n', '')
try: try:
with open(calendar_filename, 'w+') as fp_cal: with open(calendar_filename, 'w+',
encoding='utf-8') as fp_cal:
fp_cal.write(following_handles) fp_cal.write(following_handles)
except OSError: except OSError:
print('EX: _receive_calendar_events 3 ' + calendar_filename) print('EX: _receive_calendar_events 3 ' + calendar_filename)
@ -132,7 +140,8 @@ def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
# append to the list of handles # append to the list of handles
following_handles += handle + '\n' following_handles += handle + '\n'
try: try:
with open(calendar_filename, 'w+') as fp_cal: with open(calendar_filename, 'w+',
encoding='utf-8') as fp_cal:
fp_cal.write(following_handles) fp_cal.write(following_handles)
except OSError: except OSError:
print('EX: _receive_calendar_events 4 ' + calendar_filename) print('EX: _receive_calendar_events 4 ' + calendar_filename)

7
git.py
View File

@ -38,7 +38,7 @@ def _get_git_project_name(base_dir: str, nickname: str, domain: str,
return None return None
subject_line_words = subject.lower().split(' ') subject_line_words = subject.lower().split(' ')
for word in subject_line_words: for word in subject_line_words:
if word in open(git_projects_filename).read(): if word in open(git_projects_filename, encoding='utf-8').read():
return word return word
return None return None
@ -208,11 +208,12 @@ def receive_git_patch(base_dir: str, nickname: str, domain: str,
_git_add_from_handle(patch_str, _git_add_from_handle(patch_str,
'@' + from_nickname + '@' + from_domain) '@' + from_nickname + '@' + from_domain)
try: try:
with open(patch_filename, 'w+') as patch_file: with open(patch_filename, 'w+', encoding='utf-8') as patch_file:
patch_file.write(patch_str) patch_file.write(patch_str)
patch_notify_filename = \ patch_notify_filename = \
acct_dir(base_dir, nickname, domain) + '/.newPatchContent' acct_dir(base_dir, nickname, domain) + '/.newPatchContent'
with open(patch_notify_filename, 'w+') as patch_file: with open(patch_notify_filename, 'w+',
encoding='utf-8') as patch_file:
patch_file.write(patch_str) patch_file.write(patch_str)
return True return True
except OSError as ex: except OSError as ex:

View File

@ -70,12 +70,15 @@ def _remove_event_from_timeline(event_id: str,
tl_events_filename: str) -> None: tl_events_filename: str) -> None:
"""Removes the given event Id from the timeline """Removes the given event Id from the timeline
""" """
if event_id + '\n' not in open(tl_events_filename).read(): if event_id + '\n' not in open(tl_events_filename,
encoding='utf-8').read():
return return
with open(tl_events_filename, 'r') as fp_tl: with open(tl_events_filename, 'r',
encoding='utf-8') as fp_tl:
events_timeline = fp_tl.read().replace(event_id + '\n', '') events_timeline = fp_tl.read().replace(event_id + '\n', '')
try: try:
with open(tl_events_filename, 'w+') as fp2: with open(tl_events_filename, 'w+',
encoding='utf-8') as fp2:
fp2.write(events_timeline) fp2.write(events_timeline)
except OSError: except OSError:
print('EX: ERROR: unable to save events timeline') print('EX: ERROR: unable to save events timeline')
@ -135,7 +138,8 @@ def save_event_post(base_dir: str, handle: str, post_id: str,
if os.path.isfile(tl_events_filename): if os.path.isfile(tl_events_filename):
_remove_event_from_timeline(event_id, tl_events_filename) _remove_event_from_timeline(event_id, tl_events_filename)
try: try:
with open(tl_events_filename, 'r+') as tl_events_file: with open(tl_events_filename, 'r+',
encoding='utf-8') as tl_events_file:
content = tl_events_file.read() content = tl_events_file.read()
if event_id + '\n' not in content: if event_id + '\n' not in content:
tl_events_file.seek(0, 0) tl_events_file.seek(0, 0)
@ -146,7 +150,8 @@ def save_event_post(base_dir: str, handle: str, post_id: str,
return False return False
else: else:
try: try:
with open(tl_events_filename, 'w+') as tl_events_file: with open(tl_events_filename, 'w+',
encoding='utf-8') as tl_events_file:
tl_events_file.write(event_id + '\n') tl_events_file.write(event_id + '\n')
except OSError: except OSError:
print('EX: unable to write ' + tl_events_filename) print('EX: unable to write ' + tl_events_filename)
@ -161,13 +166,14 @@ def save_event_post(base_dir: str, handle: str, post_id: str,
# Does this event post already exist within the calendar month? # Does this event post already exist within the calendar month?
if os.path.isfile(calendar_filename): if os.path.isfile(calendar_filename):
if post_id in open(calendar_filename).read(): if post_id in open(calendar_filename,
encoding='utf-8').read():
# Event post already exists # Event post already exists
return False return False
# append the post Id to the file for the calendar month # append the post Id to the file for the calendar month
try: try:
with open(calendar_filename, 'a+') as calendar_file: with open(calendar_filename, 'a+', encoding='utf-8') as calendar_file:
calendar_file.write(post_id + '\n') calendar_file.write(post_id + '\n')
except OSError: except OSError:
print('EX: unable to append ' + calendar_filename) print('EX: unable to append ' + calendar_filename)
@ -179,7 +185,7 @@ def save_event_post(base_dir: str, handle: str, post_id: str,
'/calendar?year=' + str(event_year) + '?month=' + \ '/calendar?year=' + str(event_year) + '?month=' + \
str(event_month_number) + '?day=' + str(event_day_of_month) str(event_month_number) + '?day=' + str(event_day_of_month)
try: try:
with open(cal_notify_filename, 'w+') as cal_file: with open(cal_notify_filename, 'w+', encoding='utf-8') as cal_file:
cal_file.write(notify_str) cal_file.write(notify_str)
except OSError: except OSError:
print('EX: unable to write ' + cal_notify_filename) print('EX: unable to write ' + cal_notify_filename)
@ -255,7 +261,7 @@ def get_todays_events(base_dir: str, nickname: str, domain: str,
calendar_post_ids = [] calendar_post_ids = []
recreate_events_file = False recreate_events_file = False
with open(calendar_filename, 'r') as events_file: with open(calendar_filename, 'r', encoding='utf-8') as events_file:
for post_id in events_file: for post_id in events_file:
post_id = post_id.replace('\n', '').replace('\r', '') post_id = post_id.replace('\n', '').replace('\r', '')
post_filename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
@ -321,7 +327,8 @@ def get_todays_events(base_dir: str, nickname: str, domain: str,
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreate_events_file: if recreate_events_file:
try: try:
with open(calendar_filename, 'w+') as calendar_file: with open(calendar_filename, 'w+',
encoding='utf-8') as calendar_file:
for post_id in calendar_post_ids: for post_id in calendar_post_ids:
calendar_file.write(post_id + '\n') calendar_file.write(post_id + '\n')
except OSError: except OSError:
@ -553,7 +560,7 @@ def day_events_check(base_dir: str, nickname: str, domain: str,
return False return False
events_exist = False events_exist = False
with open(calendar_filename, 'r') as events_file: with open(calendar_filename, 'r', encoding='utf-8') as events_file:
for post_id in events_file: for post_id in events_file:
post_id = post_id.replace('\n', '').replace('\r', '') post_id = post_id.replace('\n', '').replace('\r', '')
post_filename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
@ -649,7 +656,8 @@ def get_this_weeks_events(base_dir: str, nickname: str, domain: str) -> {}:
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreate_events_file: if recreate_events_file:
try: try:
with open(calendar_filename, 'w+') as calendar_file: with open(calendar_filename, 'w+',
encoding='utf-8') as calendar_file:
for post_id in calendar_post_ids: for post_id in calendar_post_ids:
calendar_file.write(post_id + '\n') calendar_file.write(post_id + '\n')
except OSError: except OSError:
@ -675,7 +683,7 @@ def get_calendar_events(base_dir: str, nickname: str, domain: str,
calendar_post_ids = [] calendar_post_ids = []
recreate_events_file = False recreate_events_file = False
with open(calendar_filename, 'r') as events_file: with open(calendar_filename, 'r', encoding='utf-8') as events_file:
for post_id in events_file: for post_id in events_file:
post_id = post_id.replace('\n', '').replace('\r', '') post_id = post_id.replace('\n', '').replace('\r', '')
post_filename = locate_post(base_dir, nickname, domain, post_id) post_filename = locate_post(base_dir, nickname, domain, post_id)
@ -730,7 +738,8 @@ def get_calendar_events(base_dir: str, nickname: str, domain: str,
# if some posts have been deleted then regenerate the calendar file # if some posts have been deleted then regenerate the calendar file
if recreate_events_file: if recreate_events_file:
try: try:
with open(calendar_filename, 'w+') as calendar_file: with open(calendar_filename, 'w+',
encoding='utf-8') as calendar_file:
for post_id in calendar_post_ids: for post_id in calendar_post_ids:
calendar_file.write(post_id + '\n') calendar_file.write(post_id + '\n')
except OSError: except OSError:
@ -751,15 +760,15 @@ def remove_calendar_event(base_dir: str, nickname: str, domain: str,
return return
if '/' in message_id: if '/' in message_id:
message_id = message_id.replace('/', '#') message_id = message_id.replace('/', '#')
if message_id not in open(calendar_filename).read(): if message_id not in open(calendar_filename, encoding='utf-8').read():
return return
lines = None lines = None
with open(calendar_filename, 'r') as fp_cal: with open(calendar_filename, 'r', encoding='utf-8') as fp_cal:
lines = fp_cal.readlines() lines = fp_cal.readlines()
if not lines: if not lines:
return return
try: try:
with open(calendar_filename, 'w+') as fp_cal: with open(calendar_filename, 'w+', encoding='utf-8') as fp_cal:
for line in lines: for line in lines:
if message_id not in line: if message_id not in line:
fp_cal.write(line) fp_cal.write(line)

View File

@ -245,7 +245,7 @@ def _store_last_post_id(base_dir: str, nickname: str, domain: str,
os.mkdir(lastpost_dir) os.mkdir(lastpost_dir)
actor_filename = lastpost_dir + '/' + actor.replace('/', '#') actor_filename = lastpost_dir + '/' + actor.replace('/', '#')
try: try:
with open(actor_filename, 'w+') as fp_actor: with open(actor_filename, 'w+', encoding='utf-8') as fp_actor:
fp_actor.write(post_id) fp_actor.write(post_id)
except OSError: except OSError:
print('EX: Unable to write last post id to ' + actor_filename) print('EX: Unable to write last post id to ' + actor_filename)
@ -287,7 +287,8 @@ def _update_cached_hashtag_swarm(base_dir: str, nickname: str, domain: str,
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: try:
with open(cached_hashtag_swarm_filename, 'w+') as fp_swarm: with open(cached_hashtag_swarm_filename, 'w+',
encoding='utf-8') as fp_swarm:
fp_swarm.write(new_swarm_str) fp_swarm.write(new_swarm_str)
return True return True
except OSError: except OSError:
@ -343,7 +344,7 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str,
hashtag_added = False hashtag_added = False
if not os.path.isfile(tags_filename): if not os.path.isfile(tags_filename):
try: try:
with open(tags_filename, 'w+') as tags_file: with open(tags_filename, 'w+', encoding='utf-8') as tags_file:
tags_file.write(tag_line) tags_file.write(tag_line)
hashtag_added = True hashtag_added = True
except OSError: except OSError:
@ -351,14 +352,15 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str,
else: else:
content = '' content = ''
try: try:
with open(tags_filename, 'r') as tags_file: with open(tags_filename, 'r', encoding='utf-8') as tags_file:
content = tags_file.read() content = tags_file.read()
except OSError: except OSError:
pass pass
if post_url not in content: if post_url not in content:
content = tag_line + content content = tag_line + content
try: try:
with open(tags_filename, 'w+') as tags_file: with open(tags_filename, 'w+',
encoding='utf-8') as tags_file:
tags_file.write(content) tags_file.write(content)
hashtag_added = True hashtag_added = True
except OSError as ex: except OSError as ex:
@ -446,7 +448,7 @@ def valid_inbox(base_dir: str, nickname: str, domain: str) -> bool:
if not os.path.isfile(filename): if not os.path.isfile(filename):
print('filename: ' + filename) print('filename: ' + filename)
return False return False
if 'postNickname' in open(filename).read(): if 'postNickname' in open(filename, encoding='utf-8').read():
print('queue file incorrectly saved to ' + filename) print('queue file incorrectly saved to ' + filename)
return False return False
break break
@ -2384,7 +2386,8 @@ def _receive_announce(recent_posts_cache: {},
theme_name, system_language, theme_name, system_language,
'inbox') 'inbox')
try: try:
with open(post_filename + '.tts', 'w+') as ttsfile: with open(post_filename + '.tts', 'w+',
encoding='utf-8') as ttsfile:
ttsfile.write('\n') ttsfile.write('\n')
except OSError: except OSError:
print('EX: unable to write recent post ' + print('EX: unable to write recent post ' +
@ -2551,18 +2554,22 @@ def populate_replies(base_dir: str, http_prefix: str, domain: str,
post_replies_filename = post_filename.replace('.json', '.replies') post_replies_filename = post_filename.replace('.json', '.replies')
message_id = remove_id_ending(message_json['id']) message_id = remove_id_ending(message_json['id'])
if os.path.isfile(post_replies_filename): if os.path.isfile(post_replies_filename):
num_lines = sum(1 for line in open(post_replies_filename)) num_lines = sum(1 for line in open(post_replies_filename,
encoding='utf-8'))
if num_lines > max_replies: if num_lines > max_replies:
return False return False
if message_id not in open(post_replies_filename).read(): if message_id not in open(post_replies_filename,
encoding='utf-8').read():
try: try:
with open(post_replies_filename, 'a+') as replies_file: with open(post_replies_filename, 'a+',
encoding='utf-8') as replies_file:
replies_file.write(message_id + '\n') replies_file.write(message_id + '\n')
except OSError: except OSError:
print('EX: unable to append ' + post_replies_filename) print('EX: unable to append ' + post_replies_filename)
else: else:
try: try:
with open(post_replies_filename, 'w+') as replies_file: with open(post_replies_filename, 'w+',
encoding='utf-8') as replies_file:
replies_file.write(message_id + '\n') replies_file.write(message_id + '\n')
except OSError: except OSError:
print('EX: unable to write ' + post_replies_filename) print('EX: unable to write ' + post_replies_filename)
@ -2775,7 +2782,7 @@ def _dm_notify(base_dir: str, handle: str, url: str) -> None:
dm_file = account_dir + '/.newDM' dm_file = account_dir + '/.newDM'
if not os.path.isfile(dm_file): if not os.path.isfile(dm_file):
try: try:
with open(dm_file, 'w+') as fp_dm: with open(dm_file, 'w+', encoding='utf-8') as fp_dm:
fp_dm.write(url) fp_dm.write(url)
except OSError: except OSError:
print('EX: unable to write ' + dm_file) print('EX: unable to write ' + dm_file)
@ -2886,19 +2893,19 @@ def _like_notify(base_dir: str, domain: str, onion_domain: str,
# was there a previous like notification? # was there a previous like notification?
if os.path.isfile(prev_like_file): if os.path.isfile(prev_like_file):
# is it the same as the current notification ? # is it the same as the current notification ?
with open(prev_like_file, 'r') as fp_like: with open(prev_like_file, 'r', encoding='utf-8') as fp_like:
prev_like_str = fp_like.read() prev_like_str = fp_like.read()
if prev_like_str == like_str: if prev_like_str == like_str:
return return
try: try:
with open(prev_like_file, 'w+') as fp_like: with open(prev_like_file, 'w+', encoding='utf-8') as fp_like:
fp_like.write(like_str) fp_like.write(like_str)
except OSError: except OSError:
print('EX: ERROR: unable to save previous like notification ' + print('EX: ERROR: unable to save previous like notification ' +
prev_like_file) prev_like_file)
try: try:
with open(like_file, 'w+') as fp_like: with open(like_file, 'w+', encoding='utf-8') as fp_like:
fp_like.write(like_str) fp_like.write(like_str)
except OSError: except OSError:
print('EX: ERROR: unable to write like notification file ' + print('EX: ERROR: unable to write like notification file ' +
@ -2931,7 +2938,7 @@ def _reaction_notify(base_dir: str, domain: str, onion_domain: str,
reaction_file = account_dir + '/.newReaction' reaction_file = account_dir + '/.newReaction'
if os.path.isfile(reaction_file): if os.path.isfile(reaction_file):
if '##sent##' not in open(reaction_file).read(): if '##sent##' not in open(reaction_file, encoding='utf-8').read():
return return
reaction_nickname = get_nickname_from_actor(actor) reaction_nickname = get_nickname_from_actor(actor)
@ -2950,19 +2957,19 @@ def _reaction_notify(base_dir: str, domain: str, onion_domain: str,
# was there a previous reaction notification? # was there a previous reaction notification?
if os.path.isfile(prev_reaction_file): if os.path.isfile(prev_reaction_file):
# is it the same as the current notification ? # is it the same as the current notification ?
with open(prev_reaction_file, 'r') as fp_react: with open(prev_reaction_file, 'r', encoding='utf-8') as fp_react:
prev_reaction_str = fp_react.read() prev_reaction_str = fp_react.read()
if prev_reaction_str == reaction_str: if prev_reaction_str == reaction_str:
return return
try: try:
with open(prev_reaction_file, 'w+') as fp_react: with open(prev_reaction_file, 'w+', encoding='utf-8') as fp_react:
fp_react.write(reaction_str) fp_react.write(reaction_str)
except OSError: except OSError:
print('EX: ERROR: unable to save previous reaction notification ' + print('EX: ERROR: unable to save previous reaction notification ' +
prev_reaction_file) prev_reaction_file)
try: try:
with open(reaction_file, 'w+') as fp_react: with open(reaction_file, 'w+', encoding='utf-8') as fp_react:
fp_react.write(reaction_str) fp_react.write(reaction_str)
except OSError: except OSError:
print('EX: ERROR: unable to write reaction notification file ' + print('EX: ERROR: unable to write reaction notification file ' +
@ -2980,12 +2987,12 @@ def _notify_post_arrival(base_dir: str, handle: str, url: str) -> None:
notify_file = account_dir + '/.newNotifiedPost' notify_file = account_dir + '/.newNotifiedPost'
if os.path.isfile(notify_file): if os.path.isfile(notify_file):
# check that the same notification is not repeatedly sent # check that the same notification is not repeatedly sent
with open(notify_file, 'r') as fp_notify: with open(notify_file, 'r', encoding='utf-8') as fp_notify:
existing_notification_message = fp_notify.read() existing_notification_message = fp_notify.read()
if url in existing_notification_message: if url in existing_notification_message:
return return
try: try:
with open(notify_file, 'w+') as fp_notify: with open(notify_file, 'w+', encoding='utf-8') as fp_notify:
fp_notify.write(url) fp_notify.write(url)
except OSError: except OSError:
print('EX: unable to write ' + notify_file) print('EX: unable to write ' + notify_file)
@ -3000,7 +3007,7 @@ def _reply_notify(base_dir: str, handle: str, url: str) -> None:
reply_file = account_dir + '/.newReply' reply_file = account_dir + '/.newReply'
if not os.path.isfile(reply_file): if not os.path.isfile(reply_file):
try: try:
with open(reply_file, 'w+') as fp_reply: with open(reply_file, 'w+', encoding='utf-8') as fp_reply:
fp_reply.write(url) fp_reply.write(url)
except OSError: except OSError:
print('EX: unable to write ' + reply_file) print('EX: unable to write ' + reply_file)
@ -3018,7 +3025,7 @@ def _git_patch_notify(base_dir: str, handle: str,
subject = subject.replace('[PATCH]', '').strip() subject = subject.replace('[PATCH]', '').strip()
handle = '@' + from_nickname + '@' + from_domain handle = '@' + from_nickname + '@' + from_domain
try: try:
with open(patch_file, 'w+') as fp_patch: with open(patch_file, 'w+', encoding='utf-8') as fp_patch:
fp_patch.write('git ' + handle + ' ' + subject) fp_patch.write('git ' + handle + ' ' + subject)
except OSError: except OSError:
print('EX: unable to write ' + patch_file) print('EX: unable to write ' + patch_file)
@ -3175,7 +3182,7 @@ def inbox_update_index(boxname: str, base_dir: str, handle: str,
written = False written = False
if os.path.isfile(index_filename): if os.path.isfile(index_filename):
try: try:
with open(index_filename, 'r+') as index_file: with open(index_filename, 'r+', encoding='utf-8') as index_file:
content = index_file.read() content = index_file.read()
if destination_filename + '\n' not in content: if destination_filename + '\n' not in content:
index_file.seek(0, 0) index_file.seek(0, 0)
@ -3186,7 +3193,7 @@ def inbox_update_index(boxname: str, base_dir: str, handle: str,
print('EX: Failed to write entry to index ' + str(ex)) print('EX: Failed to write entry to index ' + str(ex))
else: else:
try: try:
with open(index_filename, 'w+') as index_file: with open(index_filename, 'w+', encoding='utf-8') as index_file:
index_file.write(destination_filename + '\n') index_file.write(destination_filename + '\n')
written = True written = True
except OSError as ex: except OSError as ex:
@ -3218,13 +3225,15 @@ def _update_last_seen(base_dir: str, handle: str, actor: str) -> None:
days_since_epoch = (curr_time - datetime.datetime(1970, 1, 1)).days days_since_epoch = (curr_time - datetime.datetime(1970, 1, 1)).days
# has the value changed? # has the value changed?
if os.path.isfile(last_seen_filename): if os.path.isfile(last_seen_filename):
with open(last_seen_filename, 'r') as last_seen_file: with open(last_seen_filename, 'r',
encoding='utf-8') as last_seen_file:
days_since_epoch_file = last_seen_file.read() days_since_epoch_file = last_seen_file.read()
if int(days_since_epoch_file) == days_since_epoch: if int(days_since_epoch_file) == days_since_epoch:
# value hasn't changed, so we can save writing anything to file # value hasn't changed, so we can save writing anything to file
return return
try: try:
with open(last_seen_filename, 'w+') as last_seen_file: with open(last_seen_filename, 'w+',
encoding='utf-8') as last_seen_file:
last_seen_file.write(str(days_since_epoch)) last_seen_file.write(str(days_since_epoch))
except OSError: except OSError:
print('EX: unable to write ' + last_seen_filename) print('EX: unable to write ' + last_seen_filename)
@ -4142,7 +4151,8 @@ def _inbox_after_initial(server, inbox_start_time,
destination_filename_mitm = \ destination_filename_mitm = \
destination_filename.replace('.json', '') + '.mitm' destination_filename.replace('.json', '') + '.mitm'
try: try:
with open(destination_filename_mitm, 'w+') as mitm_file: with open(destination_filename_mitm, 'w+',
encoding='utf-8') as mitm_file:
mitm_file.write('\n') mitm_file.write('\n')
except OSError: except OSError:
print('EX: unable to write ' + destination_filename_mitm) print('EX: unable to write ' + destination_filename_mitm)
@ -4561,12 +4571,13 @@ def _check_json_signature(base_dir: str, queue_json: {}) -> (bool, bool):
already_unknown = False already_unknown = False
if os.path.isfile(unknown_contexts_file): if os.path.isfile(unknown_contexts_file):
if unknown_context in \ if unknown_context in \
open(unknown_contexts_file).read(): open(unknown_contexts_file, encoding='utf-8').read():
already_unknown = True already_unknown = True
if not already_unknown: if not already_unknown:
try: try:
with open(unknown_contexts_file, 'a+') as unknown_file: with open(unknown_contexts_file, 'a+',
encoding='utf-8') as unknown_file:
unknown_file.write(unknown_context + '\n') unknown_file.write(unknown_context + '\n')
except OSError: except OSError:
print('EX: unable to append ' + unknown_contexts_file) print('EX: unable to append ' + unknown_contexts_file)
@ -4579,7 +4590,7 @@ def _check_json_signature(base_dir: str, queue_json: {}) -> (bool, bool):
already_unknown = False already_unknown = False
if os.path.isfile(unknown_signatures_file): if os.path.isfile(unknown_signatures_file):
if jwebsig_type in \ if jwebsig_type in \
open(unknown_signatures_file).read(): open(unknown_signatures_file, encoding='utf-8').read():
already_unknown = True already_unknown = True
if not already_unknown: if not already_unknown:
@ -4818,7 +4829,8 @@ def _receive_follow_request(session, session_onion, session_i2p,
print('Group cannot follow a group') print('Group cannot follow a group')
return False return False
try: try:
with open(followers_filename, 'r+') as followers_file: with open(followers_filename, 'r+',
encoding='utf-8') as followers_file:
content = followers_file.read() content = followers_file.read()
if approve_handle + '\n' not in content: if approve_handle + '\n' not in content:
followers_file.seek(0, 0) followers_file.seek(0, 0)
@ -4834,7 +4846,8 @@ def _receive_follow_request(session, session_onion, session_i2p,
str(ex)) str(ex))
else: else:
try: try:
with open(followers_filename, 'w+') as followers_file: with open(followers_filename, 'w+',
encoding='utf-8') as followers_file:
followers_file.write(approve_handle + '\n') followers_file.write(approve_handle + '\n')
except OSError: except OSError:
print('EX: unable to write ' + followers_filename) print('EX: unable to write ' + followers_filename)

View File

@ -38,7 +38,8 @@ def manual_deny_follow_request(session, session_onion, session_i2p,
# has this handle already been rejected? # has this handle already been rejected?
rejected_follows_filename = accounts_dir + '/followrejects.txt' rejected_follows_filename = accounts_dir + '/followrejects.txt'
if os.path.isfile(rejected_follows_filename): if os.path.isfile(rejected_follows_filename):
if deny_handle in open(rejected_follows_filename).read(): if deny_handle in open(rejected_follows_filename,
encoding='utf-8').read():
remove_from_follow_requests(base_dir, nickname, domain, remove_from_follow_requests(base_dir, nickname, domain,
deny_handle, debug) deny_handle, debug)
print(deny_handle + print(deny_handle +
@ -49,7 +50,8 @@ def manual_deny_follow_request(session, session_onion, session_i2p,
# Store rejected follows # Store rejected follows
try: try:
with open(rejected_follows_filename, 'a+') as rejects_file: with open(rejected_follows_filename, 'a+',
encoding='utf-8') as rejects_file:
rejects_file.write(deny_handle + '\n') rejects_file.write(deny_handle + '\n')
except OSError: except OSError:
print('EX: unable to append ' + rejected_follows_filename) print('EX: unable to append ' + rejected_follows_filename)
@ -153,7 +155,7 @@ def manual_approve_follow_request(session, session_onion, session_i2p,
# is the handle in the requests file? # is the handle in the requests file?
approve_follows_str = '' approve_follows_str = ''
with open(approve_follows_filename, 'r') as fp_foll: with open(approve_follows_filename, 'r', encoding='utf-8') as fp_foll:
approve_follows_str = fp_foll.read() approve_follows_str = fp_foll.read()
exists = False exists = False
approve_handle_full = approve_handle approve_handle_full = approve_handle
@ -181,10 +183,12 @@ def manual_approve_follow_request(session, session_onion, session_i2p,
'" ' + approve_follows_filename) '" ' + approve_follows_filename)
return return
with open(approve_follows_filename + '.new', 'w+') as approvefilenew: with open(approve_follows_filename + '.new', 'w+',
encoding='utf-8') as approvefilenew:
update_approved_followers = False update_approved_followers = False
follow_activity_filename = None follow_activity_filename = None
with open(approve_follows_filename, 'r') as approvefile: with open(approve_follows_filename, 'r',
encoding='utf-8') as approvefile:
for handle_of_follow_requester in approvefile: for handle_of_follow_requester in approvefile:
# is this the approved follow? # is this the approved follow?
if handle_of_follow_requester.startswith(approve_handle_full): if handle_of_follow_requester.startswith(approve_handle_full):
@ -276,9 +280,11 @@ def manual_approve_follow_request(session, session_onion, session_i2p,
# update the followers # update the followers
print('Manual follow accept: updating ' + followers_filename) print('Manual follow accept: updating ' + followers_filename)
if os.path.isfile(followers_filename): if os.path.isfile(followers_filename):
if approve_handle_full not in open(followers_filename).read(): if approve_handle_full not in open(followers_filename,
encoding='utf-8').read():
try: try:
with open(followers_filename, 'r+') as followers_file: with open(followers_filename, 'r+',
encoding='utf-8') as followers_file:
content = followers_file.read() content = followers_file.read()
if approve_handle_full + '\n' not in content: if approve_handle_full + '\n' not in content:
followers_file.seek(0, 0) followers_file.seek(0, 0)
@ -294,14 +300,16 @@ def manual_approve_follow_request(session, session_onion, session_i2p,
print('Manual follow accept: first follower accepted for ' + print('Manual follow accept: first follower accepted for ' +
handle + ' is ' + approve_handle_full) handle + ' is ' + approve_handle_full)
try: try:
with open(followers_filename, 'w+') as followers_file: with open(followers_filename, 'w+',
encoding='utf-8') as followers_file:
followers_file.write(approve_handle_full + '\n') followers_file.write(approve_handle_full + '\n')
except OSError: except OSError:
print('EX: unable to write ' + followers_filename) print('EX: unable to write ' + followers_filename)
# only update the follow requests file if the follow is confirmed to be # only update the follow requests file if the follow is confirmed to be
# in followers.txt # in followers.txt
if approve_handle_full in open(followers_filename).read(): if approve_handle_full in open(followers_filename,
encoding='utf-8').read():
# mark this handle as approved for following # mark this handle as approved for following
_approve_follower_handle(account_dir, approve_handle) _approve_follower_handle(account_dir, approve_handle)
# update the follow requests with the handles not yet approved # update the follow requests with the handles not yet approved

View File

@ -533,7 +533,7 @@ 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+') as efile: with open(media_filename + '.etag', 'w+', encoding='utf-8') as efile:
efile.write(etag) efile.write(etag)
except OSError: except OSError:
print('EX: _update_etag unable to write ' + print('EX: _update_etag unable to write ' +

View File

@ -105,7 +105,7 @@ def meta_data_instance(show_accounts: bool,
rules_filename = \ rules_filename = \
base_dir + '/accounts/tos.md' base_dir + '/accounts/tos.md'
if os.path.isfile(rules_filename): if os.path.isfile(rules_filename):
with open(rules_filename, 'r') as fp_rules: with open(rules_filename, 'r', encoding='utf-8') as fp_rules:
rules_lines = fp_rules.readlines() rules_lines = fp_rules.readlines()
rule_ctr = 1 rule_ctr = 1
for line in rules_lines: for line in rules_lines:

View File

@ -34,7 +34,7 @@ def _move_following_handles_for_account(base_dir: str,
acct_dir(base_dir, nickname, domain) + '/following.txt' acct_dir(base_dir, nickname, domain) + '/following.txt'
if not os.path.isfile(following_filename): if not os.path.isfile(following_filename):
return ctr return ctr
with open(following_filename, 'r') as fp_foll: with open(following_filename, 'r', encoding='utf-8') as fp_foll:
following_handles = fp_foll.readlines() following_handles = fp_foll.readlines()
for follow_handle in following_handles: for follow_handle in following_handles:
follow_handle = follow_handle.strip("\n").strip("\r") follow_handle = follow_handle.strip("\n").strip("\r")
@ -129,7 +129,7 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
following_filename = \ following_filename = \
acct_dir(base_dir, nickname, domain) + '/following.txt' acct_dir(base_dir, nickname, domain) + '/following.txt'
if os.path.isfile(following_filename): if os.path.isfile(following_filename):
with open(following_filename, 'r') as foll1: with open(following_filename, 'r', encoding='utf-8') as foll1:
following_handles = foll1.readlines() following_handles = foll1.readlines()
moved_to_handle = moved_to_nickname + '@' + moved_to_domain_full moved_to_handle = moved_to_nickname + '@' + moved_to_domain_full
@ -139,7 +139,7 @@ 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+') as foll2: with open(following_filename, 'w+', encoding='utf-8') as 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:
@ -157,22 +157,24 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
# save the new handles to the refollow list # save the new handles to the refollow list
if os.path.isfile(refollow_filename): if os.path.isfile(refollow_filename):
with open(refollow_filename, 'a+') as refoll: with open(refollow_filename, 'a+',
encoding='utf-8') as refoll:
refoll.write(moved_to_handle + '\n') refoll.write(moved_to_handle + '\n')
else: else:
with open(refollow_filename, 'w+') as refoll: with open(refollow_filename, 'w+',
encoding='utf-8') as refoll:
refoll.write(moved_to_handle + '\n') refoll.write(moved_to_handle + '\n')
followers_filename = \ followers_filename = \
acct_dir(base_dir, nickname, domain) + '/followers.txt' acct_dir(base_dir, nickname, domain) + '/followers.txt'
if os.path.isfile(followers_filename): if os.path.isfile(followers_filename):
with open(followers_filename, 'r') as foll3: with open(followers_filename, 'r', encoding='utf-8') as foll3:
follower_handles = foll3.readlines() follower_handles = foll3.readlines()
handle_lower = handle.lower() handle_lower = handle.lower()
# remove followers who have moved # remove followers who have moved
with open(followers_filename, 'w+') as foll4: with open(followers_filename, 'w+', encoding='utf-8') as 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:

View File

@ -46,7 +46,7 @@ def _update_feeds_outbox_index(base_dir: str, domain: str,
index_filename = base_path + '/outbox.index' index_filename = base_path + '/outbox.index'
if os.path.isfile(index_filename): if os.path.isfile(index_filename):
if post_id not in open(index_filename).read(): if post_id not in open(index_filename, encoding='utf-8').read():
try: try:
with open(index_filename, 'r+') as feeds_file: with open(index_filename, 'r+') as feeds_file:
content = feeds_file.read() content = feeds_file.read()
@ -59,7 +59,7 @@ def _update_feeds_outbox_index(base_dir: str, domain: str,
index_filename + ' ' + str(ex)) index_filename + ' ' + str(ex))
else: else:
try: try:
with open(index_filename, 'w+') as feeds_file: with open(index_filename, 'w+', encoding='utf-8') as feeds_file:
feeds_file.write(post_id + '\n') feeds_file.write(post_id + '\n')
except OSError: except OSError:
print('EX: unable to write ' + index_filename) print('EX: unable to write ' + index_filename)
@ -70,7 +70,8 @@ def _save_arrived_time(base_dir: str, post_filename: str,
"""Saves the time when an rss post arrived to a file """Saves the time when an rss post arrived to a file
""" """
try: try:
with open(post_filename + '.arrived', 'w+') as arrived_file: with open(post_filename + '.arrived', 'w+',
encoding='utf-8') as arrived_file:
arrived_file.write(arrived) arrived_file.write(arrived)
except OSError: except OSError:
print('EX: unable to write ' + post_filename + '.arrived') print('EX: unable to write ' + post_filename + '.arrived')
@ -394,7 +395,7 @@ def _newswire_hashtag_processing(session, base_dir: str, post_json_object: {},
if not os.path.isfile(rules_filename): if not os.path.isfile(rules_filename):
return True return True
rules = [] rules = []
with open(rules_filename, 'r') as fp_rules: with open(rules_filename, 'r', encoding='utf-8') as fp_rules:
rules = fp_rules.readlines() rules = fp_rules.readlines()
domain_full = get_full_domain(domain, port) domain_full = get_full_domain(domain, port)
@ -466,7 +467,7 @@ def _create_news_mirror(base_dir: str, domain: str,
# no index for mirrors found # no index for mirrors found
return True return True
removals = [] removals = []
with open(mirror_index_filename, 'r') as index_file: with open(mirror_index_filename, 'r', encoding='utf-8') as index_file:
# remove the oldest directories # remove the oldest directories
ctr = 0 ctr = 0
while no_of_dirs > max_mirrored_articles: while no_of_dirs > max_mirrored_articles:
@ -489,13 +490,15 @@ def _create_news_mirror(base_dir: str, domain: str,
# remove the corresponding index entries # remove the corresponding index entries
if removals: if removals:
index_content = '' index_content = ''
with open(mirror_index_filename, 'r') as index_file: with open(mirror_index_filename, 'r',
encoding='utf-8') as index_file:
index_content = index_file.read() index_content = index_file.read()
for remove_post_id in removals: for remove_post_id in removals:
index_content = \ index_content = \
index_content.replace(remove_post_id + '\n', '') index_content.replace(remove_post_id + '\n', '')
try: try:
with open(mirror_index_filename, 'w+') as index_file: with open(mirror_index_filename, 'w+',
encoding='utf-8') as index_file:
index_file.write(index_content) index_file.write(index_content)
except OSError: except OSError:
print('EX: unable to write ' + mirror_index_filename) print('EX: unable to write ' + mirror_index_filename)
@ -524,13 +527,15 @@ def _create_news_mirror(base_dir: str, domain: str,
# append the post Id number to the index file # append the post Id number to the index file
if os.path.isfile(mirror_index_filename): if os.path.isfile(mirror_index_filename):
try: try:
with open(mirror_index_filename, 'a+') as index_file: with open(mirror_index_filename, 'a+',
encoding='utf-8') as index_file:
index_file.write(post_id_number + '\n') index_file.write(post_id_number + '\n')
except OSError: except OSError:
print('EX: unable to append ' + mirror_index_filename) print('EX: unable to append ' + mirror_index_filename)
else: else:
try: try:
with open(mirror_index_filename, 'w+') as index_file: with open(mirror_index_filename, 'w+',
encoding='utf-8') as index_file:
index_file.write(post_id_number + '\n') index_file.write(post_id_number + '\n')
except OSError: except OSError:
print('EX: unable to write ' + mirror_index_filename) print('EX: unable to write ' + mirror_index_filename)

View File

@ -375,7 +375,7 @@ def load_hashtag_categories(base_dir: str, language: str) -> None:
if not os.path.isfile(hashtag_categories_filename): if not os.path.isfile(hashtag_categories_filename):
return return
with open(hashtag_categories_filename, 'r') as fp_cat: with open(hashtag_categories_filename, 'r', encoding='utf-8') as fp_cat:
xml_str = fp_cat.read() xml_str = fp_cat.read()
_xml2str_to_hashtag_categories(base_dir, xml_str, 1024, True) _xml2str_to_hashtag_categories(base_dir, xml_str, 1024, True)

View File

@ -30,7 +30,8 @@ def _notify_on_post_arrival(base_dir: str, nickname: str, domain: str,
handle = following_nickname + '@' + following_domain handle = following_nickname + '@' + following_domain
# check that you are following this handle # check that you are following this handle
if handle + '\n' not in open(following_filename).read(): if handle + '\n' not in open(following_filename,
encoding='utf-8').read():
print('WARN: ' + handle + ' is not in ' + following_filename) print('WARN: ' + handle + ' is not in ' + following_filename)
return return
@ -42,16 +43,19 @@ def _notify_on_post_arrival(base_dir: str, nickname: str, domain: str,
following_handles = '' following_handles = ''
if os.path.isfile(notify_on_post_filename): if os.path.isfile(notify_on_post_filename):
print('notify file exists') print('notify file exists')
with open(notify_on_post_filename, 'r') as calendar_file: with open(notify_on_post_filename, 'r',
encoding='utf-8') as calendar_file:
following_handles = calendar_file.read() following_handles = calendar_file.read()
else: else:
# create a new notifyOnPost file from the following file # create a new notifyOnPost file from the following file
print('Creating notifyOnPost file ' + notify_on_post_filename) print('Creating notifyOnPost file ' + notify_on_post_filename)
following_handles = '' following_handles = ''
with open(following_filename, 'r') as following_file: with open(following_filename, 'r',
encoding='utf-8') as following_file:
following_handles = following_file.read() following_handles = following_file.read()
if add: if add:
with open(notify_on_post_filename, 'w+') as fp_notify: with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles + handle + '\n') fp_notify.write(following_handles + handle + '\n')
# already in the notifyOnPost file? # already in the notifyOnPost file?
@ -62,7 +66,8 @@ def _notify_on_post_arrival(base_dir: str, nickname: str, domain: str,
return return
# remove from calendar file # remove from calendar file
following_handles = following_handles.replace(handle + '\n', '') following_handles = following_handles.replace(handle + '\n', '')
with open(notify_on_post_filename, 'w+') as fp_notify: with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles) fp_notify.write(following_handles)
else: else:
print(handle + ' not in notifyOnPost.txt') print(handle + ' not in notifyOnPost.txt')
@ -70,7 +75,8 @@ def _notify_on_post_arrival(base_dir: str, nickname: str, domain: str,
if add: if add:
# append to the list of handles # append to the list of handles
following_handles += handle + '\n' following_handles += handle + '\n'
with open(notify_on_post_filename, 'w+') as fp_notify: with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write(following_handles) fp_notify.write(following_handles)
@ -104,6 +110,8 @@ def notify_when_person_posts(base_dir: str, nickname: str, domain: str,
handle = following_nickname + '@' + following_domain handle = following_nickname + '@' + following_domain
if not os.path.isfile(notify_on_post_filename): if not os.path.isfile(notify_on_post_filename):
# create a new notifyOnPost file # create a new notifyOnPost file
with open(notify_on_post_filename, 'w+') as fp_notify: with open(notify_on_post_filename, 'w+',
encoding='utf-8') as fp_notify:
fp_notify.write('') fp_notify.write('')
return handle + '\n' in open(notify_on_post_filename).read() return handle + '\n' in open(notify_on_post_filename,
encoding='utf-8').read()

View File

@ -354,7 +354,7 @@ def get_default_person_context() -> str:
def _create_person_base(base_dir: str, nickname: str, domain: str, port: int, def _create_person_base(base_dir: str, nickname: str, domain: str, port: int,
http_prefix: str, saveToFile: bool, http_prefix: str, save_to_file: bool,
manual_follower_approval: bool, manual_follower_approval: bool,
group_account: bool, group_account: bool,
password: str) -> (str, str, {}, {}): password: str) -> (str, str, {}, {}):
@ -365,7 +365,7 @@ def _create_person_base(base_dir: str, nickname: str, domain: str, port: int,
create_webfinger_endpoint(nickname, domain, port, create_webfinger_endpoint(nickname, domain, port,
http_prefix, public_key_pem, http_prefix, public_key_pem,
group_account) group_account)
if saveToFile: if save_to_file:
store_webfinger_endpoint(nickname, domain, port, store_webfinger_endpoint(nickname, domain, port,
base_dir, webfinger_endpoint) base_dir, webfinger_endpoint)
@ -485,7 +485,7 @@ def _create_person_base(base_dir: str, nickname: str, domain: str, port: int,
del new_person['following'] del new_person['following']
del new_person['attachment'] del new_person['attachment']
if saveToFile: if save_to_file:
# save person to file # save person to file
people_subdir = '/accounts' people_subdir = '/accounts'
if not os.path.isdir(base_dir + people_subdir): if not os.path.isdir(base_dir + people_subdir):
@ -569,14 +569,14 @@ def register_account(base_dir: str, http_prefix: str, domain: str, port: int,
def create_group(base_dir: str, nickname: str, domain: str, port: int, def create_group(base_dir: str, nickname: str, domain: str, port: int,
http_prefix: str, saveToFile: bool, http_prefix: str, save_to_file: bool,
password: str = None) -> (str, str, {}, {}): password: str = None) -> (str, str, {}, {}):
"""Returns a group """Returns a group
""" """
(private_key_pem, public_key_pem, (private_key_pem, public_key_pem,
new_person, webfinger_endpoint) = create_person(base_dir, nickname, new_person, webfinger_endpoint) = create_person(base_dir, nickname,
domain, port, domain, port,
http_prefix, saveToFile, http_prefix, save_to_file,
False, password, True) False, password, True)
return private_key_pem, public_key_pem, new_person, webfinger_endpoint return private_key_pem, public_key_pem, new_person, webfinger_endpoint
@ -621,7 +621,7 @@ def save_person_qrcode(base_dir: str,
def create_person(base_dir: str, nickname: str, domain: str, port: int, def create_person(base_dir: str, nickname: str, domain: str, port: int,
http_prefix: str, saveToFile: bool, http_prefix: str, save_to_file: bool,
manual_follower_approval: bool, manual_follower_approval: bool,
password: str, password: str,
group_account: bool = False) -> (str, str, {}, {}): group_account: bool = False) -> (str, str, {}, {}):
@ -650,7 +650,7 @@ def create_person(base_dir: str, nickname: str, domain: str, port: int,
new_person, webfinger_endpoint) = _create_person_base(base_dir, nickname, new_person, webfinger_endpoint) = _create_person_base(base_dir, nickname,
domain, port, domain, port,
http_prefix, http_prefix,
saveToFile, save_to_file,
manual_follower, manual_follower,
group_account, group_account,
password) password)
@ -672,7 +672,7 @@ 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+') as ffile: with open(follow_dms_filename, 'w+', encoding='utf-8') as ffile:
ffile.write('\n') ffile.write('\n')
except OSError: except OSError:
print('EX: unable to write ' + follow_dms_filename) print('EX: unable to write ' + follow_dms_filename)
@ -682,7 +682,7 @@ 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+') as nfile: with open(notify_likes_filename, 'w+', encoding='utf-8') as nfile:
nfile.write('\n') nfile.write('\n')
except OSError: except OSError:
print('EX: unable to write ' + notify_likes_filename) print('EX: unable to write ' + notify_likes_filename)
@ -692,7 +692,8 @@ def create_person(base_dir: str, nickname: str, domain: str, port: int,
notify_reactions_filename = \ notify_reactions_filename = \
acct_dir(base_dir, nickname, domain) + '/.notifyReactions' acct_dir(base_dir, nickname, domain) + '/.notifyReactions'
try: try:
with open(notify_reactions_filename, 'w+') as nfile: with open(notify_reactions_filename, 'w+',
encoding='utf-8') as nfile:
nfile.write('\n') nfile.write('\n')
except OSError: except OSError:
print('EX: unable to write ' + notify_reactions_filename) print('EX: unable to write ' + notify_reactions_filename)
@ -1050,8 +1051,8 @@ def person_box_json(recent_posts_cache: {},
def set_display_nickname(base_dir: str, nickname: str, domain: str, def set_display_nickname(base_dir: str, nickname: str, domain: str,
displayName: str) -> bool: display_name: str) -> bool:
if len(displayName) > 32: if len(display_name) > 32:
return False return False
handle = nickname + '@' + domain handle = nickname + '@' + domain
filename = base_dir + '/accounts/' + handle + '.json' filename = base_dir + '/accounts/' + handle + '.json'
@ -1061,7 +1062,7 @@ def set_display_nickname(base_dir: str, nickname: str, domain: str,
person_json = load_json(filename) person_json = load_json(filename)
if not person_json: if not person_json:
return False return False
person_json['name'] = displayName person_json['name'] = display_name
save_json(person_json, filename) save_json(person_json, filename)
return True return True
@ -1088,15 +1089,15 @@ def set_bio(base_dir: str, nickname: str, domain: str, bio: str) -> bool:
def reenable_account(base_dir: str, nickname: str) -> None: def reenable_account(base_dir: str, nickname: str) -> None:
"""Removes an account suspention """Removes an account suspension
""" """
suspended_filename = base_dir + '/accounts/suspended.txt' suspended_filename = base_dir + '/accounts/suspended.txt'
if os.path.isfile(suspended_filename): if os.path.isfile(suspended_filename):
lines = [] lines = []
with open(suspended_filename, 'r') as fp_sus: with open(suspended_filename, 'r', encoding='utf-8') as fp_sus:
lines = fp_sus.readlines() lines = fp_sus.readlines()
try: try:
with open(suspended_filename, 'w+') as fp_sus: with open(suspended_filename, 'w+', encoding='utf-8') as fp_sus:
for suspended in lines: for suspended in lines:
if suspended.strip('\n').strip('\r') != nickname: if suspended.strip('\n').strip('\r') != nickname:
fp_sus.write(suspended) fp_sus.write(suspended)
@ -1118,7 +1119,7 @@ def suspend_account(base_dir: str, nickname: str, domain: str) -> None:
# Don't suspend moderators # Don't suspend moderators
moderators_file = base_dir + '/accounts/moderators.txt' moderators_file = base_dir + '/accounts/moderators.txt'
if os.path.isfile(moderators_file): if os.path.isfile(moderators_file):
with open(moderators_file, 'r') as fp_mod: with open(moderators_file, 'r', encoding='utf-8') as fp_mod:
lines = fp_mod.readlines() lines = fp_mod.readlines()
for moderator in lines: for moderator in lines:
if moderator.strip('\n').strip('\r') == nickname: if moderator.strip('\n').strip('\r') == nickname:
@ -1139,19 +1140,19 @@ def suspend_account(base_dir: str, nickname: str, domain: str) -> None:
suspended_filename = base_dir + '/accounts/suspended.txt' suspended_filename = base_dir + '/accounts/suspended.txt'
if os.path.isfile(suspended_filename): if os.path.isfile(suspended_filename):
with open(suspended_filename, 'r') as fp_sus: with open(suspended_filename, 'r', encoding='utf-8') as fp_sus:
lines = fp_sus.readlines() lines = fp_sus.readlines()
for suspended in lines: for suspended in lines:
if suspended.strip('\n').strip('\r') == nickname: if suspended.strip('\n').strip('\r') == nickname:
return return
try: try:
with open(suspended_filename, 'a+') as fp_sus: with open(suspended_filename, 'a+', encoding='utf-8') as fp_sus:
fp_sus.write(nickname + '\n') fp_sus.write(nickname + '\n')
except OSError: except OSError:
print('EX: unable to append ' + suspended_filename) print('EX: unable to append ' + suspended_filename)
else: else:
try: try:
with open(suspended_filename, 'w+') as fp_sus: with open(suspended_filename, 'w+', encoding='utf-8') as fp_sus:
fp_sus.write(nickname + '\n') fp_sus.write(nickname + '\n')
except OSError: except OSError:
print('EX: unable to write ' + suspended_filename) print('EX: unable to write ' + suspended_filename)
@ -1176,7 +1177,7 @@ def can_remove_post(base_dir: str, nickname: str,
# is the post by a moderator? # is the post by a moderator?
moderators_file = base_dir + '/accounts/moderators.txt' moderators_file = base_dir + '/accounts/moderators.txt'
if os.path.isfile(moderators_file): if os.path.isfile(moderators_file):
with open(moderators_file, 'r') as fp_mod: with open(moderators_file, 'r', encoding='utf-8') as fp_mod:
lines = fp_mod.readlines() lines = fp_mod.readlines()
for moderator in lines: for moderator in lines:
if domain_full + '/users/' + \ if domain_full + '/users/' + \
@ -1206,13 +1207,13 @@ def _remove_tags_for_nickname(base_dir: str, nickname: str,
continue continue
if not os.path.isfile(tag_filename): if not os.path.isfile(tag_filename):
continue continue
if match_str not in open(tag_filename).read(): if match_str not in open(tag_filename, encoding='utf-8').read():
continue continue
lines = [] lines = []
with open(tag_filename, 'r') as fp_tag: with open(tag_filename, 'r', encoding='utf-8') as fp_tag:
lines = fp_tag.readlines() lines = fp_tag.readlines()
try: try:
with open(tag_filename, 'w+') as tag_file: with open(tag_filename, 'w+', encoding='utf-8') as tag_file:
for tagline in lines: for tagline in lines:
if match_str not in tagline: if match_str not in tagline:
tag_file.write(tagline) tag_file.write(tagline)
@ -1234,7 +1235,7 @@ def remove_account(base_dir: str, nickname: str,
# Don't remove moderators # Don't remove moderators
moderators_file = base_dir + '/accounts/moderators.txt' moderators_file = base_dir + '/accounts/moderators.txt'
if os.path.isfile(moderators_file): if os.path.isfile(moderators_file):
with open(moderators_file, 'r') as fp_mod: with open(moderators_file, 'r', encoding='utf-8') as fp_mod:
lines = fp_mod.readlines() lines = fp_mod.readlines()
for moderator in lines: for moderator in lines:
if moderator.strip('\n') == nickname: if moderator.strip('\n') == nickname:
@ -1357,11 +1358,12 @@ def is_person_snoozed(base_dir: str, nickname: str, domain: str,
snoozed_filename = acct_dir(base_dir, nickname, domain) + '/snoozed.txt' snoozed_filename = acct_dir(base_dir, nickname, domain) + '/snoozed.txt'
if not os.path.isfile(snoozed_filename): if not os.path.isfile(snoozed_filename):
return False return False
if snooze_actor + ' ' not in open(snoozed_filename).read(): if snooze_actor + ' ' not in open(snoozed_filename,
encoding='utf-8').read():
return False return False
# remove the snooze entry if it has timed out # remove the snooze entry if it has timed out
replace_str = None replace_str = None
with open(snoozed_filename, 'r') as snoozed_file: with open(snoozed_filename, 'r', encoding='utf-8') as snoozed_file:
for line in snoozed_file: for line in snoozed_file:
# is this the entry for the actor? # is this the entry for the actor?
if line.startswith(snooze_actor + ' '): if line.startswith(snooze_actor + ' '):
@ -1379,16 +1381,17 @@ def is_person_snoozed(base_dir: str, nickname: str, domain: str,
break break
if replace_str: if replace_str:
content = None content = None
with open(snoozed_filename, 'r') as snoozed_file: with open(snoozed_filename, 'r', encoding='utf-8') as snoozed_file:
content = snoozed_file.read().replace(replace_str, '') content = snoozed_file.read().replace(replace_str, '')
if content: if content:
try: try:
with open(snoozed_filename, 'w+') as snoozfile: with open(snoozed_filename, 'w+',
encoding='utf-8') as snoozfile:
snoozfile.write(content) snoozfile.write(content)
except OSError: except OSError:
print('EX: unable to write ' + snoozed_filename) print('EX: unable to write ' + snoozed_filename)
if snooze_actor + ' ' in open(snoozed_filename).read(): if snooze_actor + ' ' in open(snoozed_filename, encoding='utf-8').read():
return True return True
return False return False
@ -1403,10 +1406,11 @@ def person_snooze(base_dir: str, nickname: str, domain: str,
return return
snoozed_filename = account_dir + '/snoozed.txt' snoozed_filename = account_dir + '/snoozed.txt'
if os.path.isfile(snoozed_filename): if os.path.isfile(snoozed_filename):
if snooze_actor + ' ' in open(snoozed_filename).read(): if snooze_actor + ' ' in open(snoozed_filename,
encoding='utf-8').read():
return return
try: try:
with open(snoozed_filename, 'a+') as snoozed_file: with open(snoozed_filename, 'a+', encoding='utf-8') as snoozed_file:
snoozed_file.write(snooze_actor + ' ' + snoozed_file.write(snooze_actor + ' ' +
str(int(time.time())) + '\n') str(int(time.time())) + '\n')
except OSError: except OSError:
@ -1424,21 +1428,23 @@ def person_unsnooze(base_dir: str, nickname: str, domain: str,
snoozed_filename = account_dir + '/snoozed.txt' snoozed_filename = account_dir + '/snoozed.txt'
if not os.path.isfile(snoozed_filename): if not os.path.isfile(snoozed_filename):
return return
if snooze_actor + ' ' not in open(snoozed_filename).read(): if snooze_actor + ' ' not in open(snoozed_filename,
encoding='utf-8').read():
return return
replace_str = None replace_str = None
with open(snoozed_filename, 'r') as snoozed_file: with open(snoozed_filename, 'r', encoding='utf-8') as snoozed_file:
for line in snoozed_file: for line in snoozed_file:
if line.startswith(snooze_actor + ' '): if line.startswith(snooze_actor + ' '):
replace_str = line replace_str = line
break break
if replace_str: if replace_str:
content = None content = None
with open(snoozed_filename, 'r') as snoozed_file: with open(snoozed_filename, 'r', encoding='utf-8') as snoozed_file:
content = snoozed_file.read().replace(replace_str, '') content = snoozed_file.read().replace(replace_str, '')
if content: if content:
try: try:
with open(snoozed_filename, 'w+') as snoozfile: with open(snoozed_filename, 'w+',
encoding='utf-8') as snoozfile:
snoozfile.write(content) snoozfile.write(content)
except OSError: except OSError:
print('EX: unable to write ' + snoozed_filename) print('EX: unable to write ' + snoozed_filename)
@ -1457,7 +1463,7 @@ def set_person_notes(base_dir: str, nickname: str, domain: str,
os.mkdir(notes_dir) os.mkdir(notes_dir)
notes_filename = notes_dir + '/' + handle + '.txt' notes_filename = notes_dir + '/' + handle + '.txt'
try: try:
with open(notes_filename, 'w+') as notes_file: with open(notes_filename, 'w+', encoding='utf-8') as notes_file:
notes_file.write(notes) notes_file.write(notes)
except OSError: except OSError:
print('EX: unable to write ' + notes_filename) print('EX: unable to write ' + notes_filename)
@ -1703,7 +1709,7 @@ def get_person_avatar_url(base_dir: str, person_url: str, person_cache: {},
if ext != 'svg': if ext != 'svg':
return im_path return im_path
content = '' content = ''
with open(im_filename, 'r') as fp_im: with open(im_filename, 'r', encoding='utf-8') as fp_im:
content = fp_im.read() content = fp_im.read()
if not dangerous_svg(content, False): if not dangerous_svg(content, False):
return im_path return im_path

View File

@ -28,7 +28,7 @@ def set_pet_name(base_dir: str, nickname: str, domain: str,
# does this entry already exist? # does this entry already exist?
if os.path.isfile(petnames_filename): if os.path.isfile(petnames_filename):
with open(petnames_filename, 'r') as petnames_file: with open(petnames_filename, 'r', encoding='utf-8') as petnames_file:
petnames_str = petnames_file.read() petnames_str = petnames_file.read()
if entry in petnames_str: if entry in petnames_str:
return True return True
@ -42,7 +42,8 @@ def set_pet_name(base_dir: str, nickname: str, domain: str,
new_petnames_str += entry new_petnames_str += entry
# save the updated petnames file # save the updated petnames file
try: try:
with open(petnames_filename, 'w+') as petnames_file: with open(petnames_filename, 'w+',
encoding='utf-8') as petnames_file:
petnames_file.write(new_petnames_str) petnames_file.write(new_petnames_str)
except OSError: except OSError:
print('EX: unable to save ' + petnames_filename) print('EX: unable to save ' + petnames_filename)
@ -50,7 +51,8 @@ def set_pet_name(base_dir: str, nickname: str, domain: str,
return True return True
# entry does not exist in the petnames file # entry does not exist in the petnames file
try: try:
with open(petnames_filename, 'a+') as petnames_file: with open(petnames_filename, 'a+',
encoding='utf-8') as petnames_file:
petnames_file.write(entry) petnames_file.write(entry)
except OSError: except OSError:
print('EX: unable to append ' + petnames_filename) print('EX: unable to append ' + petnames_filename)
@ -59,7 +61,7 @@ def set_pet_name(base_dir: str, nickname: str, domain: str,
# first entry # first entry
try: try:
with open(petnames_filename, 'w+') as petnames_file: with open(petnames_filename, 'w+', encoding='utf-8') as petnames_file:
petnames_file.write(entry) petnames_file.write(entry)
except OSError: except OSError:
print('EX: unable to write ' + petnames_filename) print('EX: unable to write ' + petnames_filename)
@ -79,7 +81,7 @@ def get_pet_name(base_dir: str, nickname: str, domain: str,
if not os.path.isfile(petnames_filename): if not os.path.isfile(petnames_filename):
return '' return ''
with open(petnames_filename, 'r') as petnames_file: with open(petnames_filename, 'r', encoding='utf-8') as petnames_file:
petnames_str = petnames_file.read() petnames_str = petnames_file.read()
if ' ' + handle + '\n' in petnames_str: if ' ' + handle + '\n' in petnames_str:
petnames_list = petnames_str.split('\n') petnames_list = petnames_str.split('\n')
@ -106,7 +108,7 @@ def _get_pet_name_handle(base_dir: str, nickname: str, domain: str,
if not os.path.isfile(petnames_filename): if not os.path.isfile(petnames_filename):
return '' return ''
with open(petnames_filename, 'r') as petnames_file: with open(petnames_filename, 'r', encoding='utf-8') as petnames_file:
petnames_str = petnames_file.read() petnames_str = petnames_file.read()
if petname + ' ' in petnames_str: if petname + ' ' in petnames_str:
petnames_list = petnames_str.split('\n') petnames_list = petnames_str.split('\n')

View File

@ -110,7 +110,7 @@ def is_moderator(base_dir: str, nickname: str) -> bool:
return True return True
return False return False
with open(moderators_file, 'r') as fp_mod: with open(moderators_file, 'r', encoding='utf-8') as fp_mod:
lines = fp_mod.readlines() lines = fp_mod.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')
@ -134,7 +134,7 @@ def no_of_followers_on_domain(base_dir: str, handle: str,
return 0 return 0
ctr = 0 ctr = 0
with open(filename, 'r') as followers_file: with open(filename, 'r', encoding='utf-8') as followers_file:
for follower_handle in followers_file: for follower_handle in followers_file:
if '@' in follower_handle: if '@' in follower_handle:
follower_domain = follower_handle.split('@')[1] follower_domain = follower_handle.split('@')[1]
@ -154,7 +154,7 @@ def _get_local_private_key(base_dir: str, nickname: str, domain: str) -> str:
key_filename = base_dir + '/keys/private/' + handle.lower() + '.key' key_filename = base_dir + '/keys/private/' + handle.lower() + '.key'
if not os.path.isfile(key_filename): if not os.path.isfile(key_filename):
return None return None
with open(key_filename, 'r') as pem_file: with open(key_filename, 'r', encoding='utf-8') as pem_file:
return pem_file.read() return pem_file.read()
return None return None
@ -175,7 +175,7 @@ def _get_local_public_key(base_dir: str, nickname: str, domain: str) -> str:
key_filename = base_dir + '/keys/public/' + handle.lower() + '.key' key_filename = base_dir + '/keys/public/' + handle.lower() + '.key'
if not os.path.isfile(key_filename): if not os.path.isfile(key_filename):
return None return None
with open(key_filename, 'r') as pem_file: with open(key_filename, 'r', encoding='utf-8') as pem_file:
return pem_file.read() return pem_file.read()
return None return None
@ -961,16 +961,16 @@ def _update_hashtags_index(base_dir: str, tag: {}, new_post_id: str) -> None:
if not os.path.isfile(tags_filename): if not os.path.isfile(tags_filename):
# create a new tags index file # create a new tags index file
try: try:
with open(tags_filename, 'w+') as tags_file: with open(tags_filename, 'w+', encoding='utf-8') as tags_file:
tags_file.write(tagline) tags_file.write(tagline)
except OSError: except OSError:
print('EX: _update_hashtags_index unable to write tags file ' + print('EX: _update_hashtags_index unable to write tags file ' +
tags_filename) tags_filename)
else: else:
# prepend to tags index file # prepend to tags index file
if tagline not in open(tags_filename).read(): if tagline not in open(tags_filename, encoding='utf-8').read():
try: try:
with open(tags_filename, 'r+') as tags_file: with open(tags_filename, 'r+', encoding='utf-8') as tags_file:
content = tags_file.read() content = tags_file.read()
if tagline not in content: if tagline not in content:
tags_file.seek(0, 0) tags_file.seek(0, 0)
@ -990,9 +990,11 @@ def _add_schedule_post(base_dir: str, nickname: str, domain: str,
index_str = event_date_str + ' ' + post_id.replace('/', '#') index_str = event_date_str + ' ' + post_id.replace('/', '#')
if os.path.isfile(schedule_index_filename): if os.path.isfile(schedule_index_filename):
if index_str not in open(schedule_index_filename).read(): if index_str not in open(schedule_index_filename,
encoding='utf-8').read():
try: try:
with open(schedule_index_filename, 'r+') as schedule_file: with open(schedule_index_filename, 'r+',
encoding='utf-8') as schedule_file:
content = schedule_file.read() content = schedule_file.read()
if index_str + '\n' not in content: if index_str + '\n' not in content:
schedule_file.seek(0, 0) schedule_file.seek(0, 0)
@ -1003,7 +1005,8 @@ def _add_schedule_post(base_dir: str, nickname: str, domain: str,
schedule_index_filename + ' ' + str(ex)) schedule_index_filename + ' ' + str(ex))
else: else:
try: try:
with open(schedule_index_filename, 'w+') as schedule_file: with open(schedule_index_filename, 'w+',
encoding='utf-8') as schedule_file:
schedule_file.write(index_str + '\n') schedule_file.write(index_str + '\n')
except OSError as ex: except OSError as ex:
print('EX: Failed to write entry to scheduled posts index2 ' + print('EX: Failed to write entry to scheduled posts index2 ' +
@ -1029,7 +1032,7 @@ def _load_auto_cw(base_dir: str, nickname: str, domain: str) -> []:
if not os.path.isfile(filename): if not os.path.isfile(filename):
return [] return []
try: try:
with open(filename, 'r') as fp_auto: with open(filename, 'r', encoding='utf-8') as fp_auto:
return fp_auto.readlines() return fp_auto.readlines()
except OSError: except OSError:
print('EX: unable to load auto cw file ' + filename) print('EX: unable to load auto cw file ' + filename)
@ -1371,7 +1374,7 @@ def _create_post_mod_report(base_dir: str,
# save to index file # save to index file
moderation_index_file = base_dir + '/accounts/moderation.txt' moderation_index_file = base_dir + '/accounts/moderation.txt'
try: try:
with open(moderation_index_file, 'a+') as mod_file: with open(moderation_index_file, 'a+', encoding='utf-8') as mod_file:
mod_file.write(new_post_id + '\n') mod_file.write(new_post_id + '\n')
except OSError: except OSError:
print('EX: unable to write moderation index file ' + print('EX: unable to write moderation index file ' +
@ -1707,7 +1710,7 @@ def pin_post(base_dir: str, nickname: str, domain: str,
account_dir = acct_dir(base_dir, nickname, domain) account_dir = acct_dir(base_dir, nickname, domain)
pinned_filename = account_dir + '/pinToProfile.txt' pinned_filename = account_dir + '/pinToProfile.txt'
try: try:
with open(pinned_filename, 'w+') as pin_file: with open(pinned_filename, 'w+', encoding='utf-8') as pin_file:
pin_file.write(pinned_content) pin_file.write(pinned_content)
except OSError: except OSError:
print('EX: unable to write ' + pinned_filename) print('EX: unable to write ' + pinned_filename)
@ -1737,7 +1740,7 @@ def get_pinned_post_as_json(base_dir: str, http_prefix: str,
actor = local_actor_url(http_prefix, nickname, domain_full) actor = local_actor_url(http_prefix, nickname, domain_full)
if os.path.isfile(pinned_filename): if os.path.isfile(pinned_filename):
pinned_content = None pinned_content = None
with open(pinned_filename, 'r') as pin_file: with open(pinned_filename, 'r', encoding='utf-8') as pin_file:
pinned_content = pin_file.read() pinned_content = pin_file.read()
if pinned_content: if pinned_content:
pinned_post_json = { pinned_post_json = {
@ -1816,7 +1819,7 @@ def regenerate_index_for_box(base_dir: str,
result = '' result = ''
try: try:
with open(box_index_filename, 'w+') as fp_box: with open(box_index_filename, 'w+', encoding='utf-8') as fp_box:
for line in index_lines: for line in index_lines:
result += line + '\n' result += line + '\n'
fp_box.write(line + '\n') fp_box.write(line + '\n')
@ -1886,7 +1889,7 @@ def _append_citations_to_blog_post(base_dir: str,
if not os.path.isfile(citations_filename): if not os.path.isfile(citations_filename):
return return
citations_separator = '#####' citations_separator = '#####'
with open(citations_filename, 'r') as fp_cit: with open(citations_filename, 'r', encoding='utf-8') as fp_cit:
citations = fp_cit.readlines() citations = fp_cit.readlines()
for line in citations: for line in citations:
if citations_separator not in line: if citations_separator not in line:
@ -2212,7 +2215,7 @@ def create_report_post(base_dir: str,
moderators_list = [] moderators_list = []
moderators_file = base_dir + '/accounts/moderators.txt' moderators_file = base_dir + '/accounts/moderators.txt'
if os.path.isfile(moderators_file): if os.path.isfile(moderators_file):
with open(moderators_file, 'r') as fp_mod: with open(moderators_file, 'r', encoding='utf-8') as fp_mod:
for line in fp_mod: for line in fp_mod:
line = line.strip('\n').strip('\r') line = line.strip('\n').strip('\r')
if line.startswith('#'): if line.startswith('#'):
@ -2284,7 +2287,7 @@ def create_report_post(base_dir: str,
if os.path.isfile(new_report_file): if os.path.isfile(new_report_file):
continue continue
try: try:
with open(new_report_file, 'w+') as fp_report: with open(new_report_file, 'w+', encoding='utf-8') as fp_report:
fp_report.write(to_url + '/moderation') fp_report.write(to_url + '/moderation')
except OSError: except OSError:
print('EX: create_report_post unable to write ' + new_report_file) print('EX: create_report_post unable to write ' + new_report_file)
@ -2362,10 +2365,12 @@ def thread_send_post(session, post_json_str: str, federation_list: [],
# save the log file # save the log file
post_log_filename = base_dir + '/post.log' post_log_filename = base_dir + '/post.log'
if os.path.isfile(post_log_filename): if os.path.isfile(post_log_filename):
with open(post_log_filename, 'a+') as log_file: with open(post_log_filename, 'a+',
encoding='utf-8') as log_file:
log_file.write(log_str + '\n') log_file.write(log_str + '\n')
else: else:
with open(post_log_filename, 'w+') as log_file: with open(post_log_filename, 'w+',
encoding='utf-8') as log_file:
log_file.write(log_str + '\n') log_file.write(log_str + '\n')
if post_result: if post_result:
@ -2702,7 +2707,7 @@ def group_followers_by_domain(base_dir: str, nickname: str, domain: str) -> {}:
if not os.path.isfile(followers_filename): if not os.path.isfile(followers_filename):
return None return None
grouped = {} grouped = {}
with open(followers_filename, 'r') as foll_file: with open(followers_filename, 'r', encoding='utf-8') as foll_file:
for follower_handle in foll_file: for follower_handle in foll_file:
if '@' not in follower_handle: if '@' not in follower_handle:
continue continue
@ -3701,7 +3706,8 @@ def create_moderation(base_dir: str, nickname: str, domain: str, port: int,
if is_moderator(base_dir, nickname): if is_moderator(base_dir, nickname):
moderation_index_file = base_dir + '/accounts/moderation.txt' moderation_index_file = base_dir + '/accounts/moderation.txt'
if os.path.isfile(moderation_index_file): if os.path.isfile(moderation_index_file):
with open(moderation_index_file, 'r') as index_file: with open(moderation_index_file, 'r',
encoding='utf-8') as index_file:
lines = index_file.readlines() lines = index_file.readlines()
box_header['totalItems'] = len(lines) box_header['totalItems'] = len(lines)
if header_only: if header_only:
@ -3831,7 +3837,7 @@ def _add_post_to_timeline(file_path: str, boxname: str,
posts_in_box: [], box_actor: str) -> bool: posts_in_box: [], box_actor: str) -> bool:
""" Reads a post from file and decides whether it is valid """ Reads a post from file and decides whether it is valid
""" """
with open(file_path, 'r') as post_file: with open(file_path, 'r', encoding='utf-8') as post_file:
post_str = post_file.read() post_str = post_file.read()
if file_path.endswith('.json'): if file_path.endswith('.json'):
@ -4011,7 +4017,7 @@ def _create_box_indexed(recent_posts_cache: {},
total_posts_count = 0 total_posts_count = 0
posts_added_to_timeline = 0 posts_added_to_timeline = 0
if os.path.isfile(index_filename): if os.path.isfile(index_filename):
with open(index_filename, 'r') as index_file: with open(index_filename, 'r', encoding='utf-8') as index_file:
posts_added_to_timeline = 0 posts_added_to_timeline = 0
while posts_added_to_timeline < items_per_page: while posts_added_to_timeline < items_per_page:
post_filename = index_file.readline() post_filename = index_file.readline()
@ -4267,7 +4273,7 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
index_ctr = 0 index_ctr = 0
# get the existing index entries as a string # get the existing index entries as a string
new_index = '' new_index = ''
with open(index_filename, 'r') as index_file: with open(index_filename, 'r', encoding='utf-8') as index_file:
for post_id in index_file: for post_id in index_file:
new_index += post_id new_index += post_id
index_ctr += 1 index_ctr += 1
@ -4275,7 +4281,7 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
break break
# save the new index file # save the new index file
if len(new_index) > 0: if len(new_index) > 0:
with open(index_filename, 'w+') as index_file: with open(index_filename, 'w+', encoding='utf-8') as index_file:
index_file.write(new_index) index_file.write(new_index)
posts_in_box_dict = {} posts_in_box_dict = {}
@ -4288,7 +4294,7 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
# Time of file creation # Time of file creation
full_filename = os.path.join(box_dir, post_filename) full_filename = os.path.join(box_dir, post_filename)
if os.path.isfile(full_filename): if os.path.isfile(full_filename):
content = open(full_filename).read() content = open(full_filename, encoding='utf-8').read()
if '"published":' in content: if '"published":' in content:
published_str = content.split('"published":')[1] published_str = content.split('"published":')[1]
if '"' in published_str: if '"' in published_str:
@ -4617,7 +4623,7 @@ def get_public_post_domains_blocked(session, base_dir: str,
# read the blocked domains as a single string # read the blocked domains as a single string
blocked_str = '' blocked_str = ''
with open(blocking_filename, 'r') as fp_block: with open(blocking_filename, 'r', encoding='utf-8') as fp_block:
blocked_str = fp_block.read() blocked_str = fp_block.read()
blocked_domains = [] blocked_domains = []
@ -4669,7 +4675,8 @@ def check_domains(session, base_dir: str,
update_follower_warnings = False update_follower_warnings = False
follower_warning_str = '' follower_warning_str = ''
if os.path.isfile(follower_warning_filename): if os.path.isfile(follower_warning_filename):
with open(follower_warning_filename, 'r') as fp_warn: with open(follower_warning_filename, 'r',
encoding='utf-8') as fp_warn:
follower_warning_str = fp_warn.read() follower_warning_str = fp_warn.read()
if single_check: if single_check:
@ -4719,7 +4726,8 @@ def check_domains(session, base_dir: str,
update_follower_warnings = True update_follower_warnings = True
if update_follower_warnings and follower_warning_str: if update_follower_warnings and follower_warning_str:
with open(follower_warning_filename, 'w+') as fp_warn: with open(follower_warning_filename, 'w+',
encoding='utf-8') as fp_warn:
fp_warn.write(follower_warning_str) fp_warn.write(follower_warning_str)
if not single_check: if not single_check:
print(follower_warning_str) print(follower_warning_str)
@ -4731,7 +4739,7 @@ def populate_replies_json(base_dir: str, nickname: str, domain: str,
pub_str = 'https://www.w3.org/ns/activitystreams#Public' pub_str = 'https://www.w3.org/ns/activitystreams#Public'
# populate the items list with replies # populate the items list with replies
replies_boxes = ('outbox', 'inbox') replies_boxes = ('outbox', 'inbox')
with open(post_replies_filename, 'r') as replies_file: with open(post_replies_filename, 'r', encoding='utf-8') as replies_file:
for message_id in replies_file: for message_id in replies_file:
reply_found = False reply_found = False
# examine inbox and outbox # examine inbox and outbox
@ -4743,7 +4751,8 @@ def populate_replies_json(base_dir: str, nickname: str, domain: str,
message_id2.replace('/', '#') + '.json' message_id2.replace('/', '#') + '.json'
if os.path.isfile(search_filename): if os.path.isfile(search_filename):
if authorized or \ if authorized or \
pub_str in open(search_filename).read(): pub_str in open(search_filename,
encoding='utf-8').read():
post_json_object = load_json(search_filename) post_json_object = load_json(search_filename)
if post_json_object: if post_json_object:
if post_json_object['object'].get('cc'): if post_json_object['object'].get('cc'):
@ -4770,7 +4779,8 @@ def populate_replies_json(base_dir: str, nickname: str, domain: str,
message_id2.replace('/', '#') + '.json' message_id2.replace('/', '#') + '.json'
if os.path.isfile(search_filename): if os.path.isfile(search_filename):
if authorized or \ if authorized or \
pub_str in open(search_filename).read(): pub_str in open(search_filename,
encoding='utf-8').read():
# get the json of the reply and append it to # get the json of the reply and append it to
# the collection # the collection
post_json_object = load_json(search_filename) post_json_object = load_json(search_filename)
@ -4799,7 +4809,8 @@ def _reject_announce(announce_filename: str,
# reject the post referenced by the announce activity object # reject the post referenced by the announce activity object
if not os.path.isfile(announce_filename + '.reject'): if not os.path.isfile(announce_filename + '.reject'):
with open(announce_filename + '.reject', 'w+') as reject_announce_file: with open(announce_filename + '.reject', 'w+',
encoding='utf-8') as reject_announce_file:
reject_announce_file.write('\n') reject_announce_file.write('\n')
@ -5565,7 +5576,8 @@ def edited_post_filename(base_dir: str, nickname: str, domain: str,
post_id = remove_id_ending(post_json_object['object']['id']) post_id = remove_id_ending(post_json_object['object']['id'])
lastpost_id = None lastpost_id = None
try: try:
with open(actor_filename, 'r') as fp_actor: with open(actor_filename, 'r',
encoding='utf-8') as fp_actor:
lastpost_id = fp_actor.read() lastpost_id = fp_actor.read()
except OSError: except OSError:
print('EX: edited_post_filename unable to read ' + actor_filename) print('EX: edited_post_filename unable to read ' + actor_filename)

View File

@ -66,17 +66,20 @@ def question_update_votes(base_dir: str, nickname: str, domain: str,
if not os.path.isfile(voters_filename): if not os.path.isfile(voters_filename):
# create a new voters file # create a new voters file
try: try:
with open(voters_filename, 'w+') as voters_file: with open(voters_filename, 'w+',
encoding='utf-8') as voters_file:
voters_file.write(reply_json['actor'] + voters_file.write(reply_json['actor'] +
voters_file_separator + voters_file_separator +
found_answer + '\n') found_answer + '\n')
except OSError: except OSError:
print('EX: unable to write voters file ' + voters_filename) print('EX: unable to write voters file ' + voters_filename)
else: else:
if reply_json['actor'] not in open(voters_filename).read(): if reply_json['actor'] not in open(voters_filename,
encoding='utf-8').read():
# append to the voters file # append to the voters file
try: try:
with open(voters_filename, 'a+') as voters_file: with open(voters_filename, 'a+',
encoding='utf-8') as voters_file:
voters_file.write(reply_json['actor'] + voters_file.write(reply_json['actor'] +
voters_file_separator + voters_file_separator +
found_answer + '\n') found_answer + '\n')
@ -84,7 +87,8 @@ def question_update_votes(base_dir: str, nickname: str, domain: str,
print('EX: unable to append to voters file ' + voters_filename) print('EX: unable to append to voters file ' + voters_filename)
else: else:
# change an entry in the voters file # change an entry in the voters file
with open(voters_filename, 'r') as voters_file: with open(voters_filename, 'r',
encoding='utf-8') as voters_file:
lines = voters_file.readlines() lines = voters_file.readlines()
newlines = [] newlines = []
save_voters_file = False save_voters_file = False
@ -101,7 +105,8 @@ def question_update_votes(base_dir: str, nickname: str, domain: str,
newlines.append(vote_line) newlines.append(vote_line)
if save_voters_file: if save_voters_file:
try: try:
with open(voters_filename, 'w+') as voters_file: with open(voters_filename, 'w+',
encoding='utf-8') as voters_file:
for vote_line in newlines: for vote_line in newlines:
voters_file.write(vote_line) voters_file.write(vote_line)
except OSError: except OSError:
@ -115,7 +120,7 @@ def question_update_votes(base_dir: str, nickname: str, domain: str,
if not possible_answer.get('name'): if not possible_answer.get('name'):
continue continue
total_items = 0 total_items = 0
with open(voters_filename, 'r') as voters_file: with open(voters_filename, 'r', encoding='utf-8') as voters_file:
lines = voters_file.readlines() lines = voters_file.readlines()
for vote_line in lines: for vote_line in lines:
if vote_line.endswith(voters_file_separator + if vote_line.endswith(voters_file_separator +

View File

@ -452,7 +452,8 @@ def _update_common_reactions(base_dir: str, emoji_content: str) -> None:
common_reactions = None common_reactions = None
if os.path.isfile(common_reactions_filename): if os.path.isfile(common_reactions_filename):
try: try:
with open(common_reactions_filename, 'r') as fp_react: with open(common_reactions_filename, 'r',
encoding='utf-8') as fp_react:
common_reactions = fp_react.readlines() common_reactions = fp_react.readlines()
except OSError: except OSError:
print('EX: unable to load common reactions file') print('EX: unable to load common reactions file')
@ -476,7 +477,8 @@ def _update_common_reactions(base_dir: str, emoji_content: str) -> None:
new_common_reactions.append(str(1).zfill(16) + ' ' + emoji_content) new_common_reactions.append(str(1).zfill(16) + ' ' + emoji_content)
new_common_reactions.sort(reverse=True) new_common_reactions.sort(reverse=True)
try: try:
with open(common_reactions_filename, 'w+') as fp_react: with open(common_reactions_filename, 'w+',
encoding='utf-8') as fp_react:
for line in new_common_reactions: for line in new_common_reactions:
fp_react.write(line + '\n') fp_react.write(line + '\n')
except OSError: except OSError:
@ -485,7 +487,8 @@ def _update_common_reactions(base_dir: str, emoji_content: str) -> None:
else: else:
line = str(1).zfill(16) + ' ' + emoji_content + '\n' line = str(1).zfill(16) + ' ' + emoji_content + '\n'
try: try:
with open(common_reactions_filename, 'w+') as fp_react: with open(common_reactions_filename, 'w+',
encoding='utf-8') as fp_react:
fp_react.write(line) fp_react.write(line)
except OSError: except OSError:
print('EX: error writing common reactions 2') print('EX: error writing common reactions 2')

View File

@ -28,7 +28,8 @@ def _clear_role_status(base_dir: str, role: str) -> None:
if not filename.endswith(".json"): if not filename.endswith(".json"):
continue continue
filename = os.path.join(base_dir + '/accounts/', filename) filename = os.path.join(base_dir + '/accounts/', filename)
if '"' + role + '"' not in open(filename).read(): if '"' + role + '"' not in open(filename,
encoding='utf-8').read():
continue continue
actor_json = load_json(filename) actor_json = load_json(filename)
if not actor_json: if not actor_json:
@ -84,7 +85,7 @@ def _add_role(base_dir: str, nickname: str, domain: str,
lines = [] lines = []
try: try:
with open(role_file, 'r') as fp_role: with open(role_file, 'r', encoding='utf-8') as fp_role:
lines = fp_role.readlines() lines = fp_role.readlines()
except OSError: except OSError:
print('EX: _add_role, failed to read roles file ' + role_file) print('EX: _add_role, failed to read roles file ' + role_file)
@ -96,7 +97,7 @@ def _add_role(base_dir: str, nickname: str, domain: str,
lines.append(nickname) lines.append(nickname)
try: try:
with open(role_file, 'w+') as fp_role: with open(role_file, 'w+', encoding='utf-8') as fp_role:
for role_nickname in lines: for role_nickname in lines:
role_nickname = role_nickname.strip('\n').strip('\r') role_nickname = role_nickname.strip('\n').strip('\r')
if len(role_nickname) < 2: if len(role_nickname) < 2:
@ -108,7 +109,7 @@ def _add_role(base_dir: str, nickname: str, domain: str,
print('EX: _add_role, failed to write roles file1 ' + role_file) print('EX: _add_role, failed to write roles file1 ' + role_file)
else: else:
try: try:
with open(role_file, 'w+') as fp_role: with open(role_file, 'w+', encoding='utf-8') as fp_role:
account_dir = acct_dir(base_dir, nickname, domain) account_dir = acct_dir(base_dir, nickname, domain)
if os.path.isdir(account_dir): if os.path.isdir(account_dir):
fp_role.write(nickname + '\n') fp_role.write(nickname + '\n')
@ -125,13 +126,13 @@ def _remove_role(base_dir: str, nickname: str, role_filename: str) -> None:
return return
try: try:
with open(role_file, 'r') as fp_role: with open(role_file, 'r', encoding='utf-8') as fp_role:
lines = fp_role.readlines() lines = fp_role.readlines()
except OSError: except OSError:
print('EX: _remove_role, failed to read roles file ' + role_file) print('EX: _remove_role, failed to read roles file ' + role_file)
try: try:
with open(role_file, 'w+') as fp_role: with open(role_file, 'w+', encoding='utf-8') as fp_role:
for role_nickname in lines: for role_nickname in lines:
role_nickname = role_nickname.strip('\n').strip('\r') role_nickname = role_nickname.strip('\n').strip('\r')
if len(role_nickname) > 1 and role_nickname != nickname: if len(role_nickname) > 1 and role_nickname != nickname:

View File

@ -36,7 +36,7 @@ def _update_post_schedule(base_dir: str, handle: str, httpd,
index_lines = [] index_lines = []
delete_schedule_post = False delete_schedule_post = False
nickname = handle.split('@')[0] nickname = handle.split('@')[0]
with open(schedule_index_filename, 'r') as sched_file: with open(schedule_index_filename, 'r', encoding='utf-8') as sched_file:
for line in sched_file: for line in sched_file:
if ' ' not in line: if ' ' not in line:
continue continue
@ -163,7 +163,7 @@ def _update_post_schedule(base_dir: str, handle: str, httpd,
# write the new schedule index file # write the new schedule index file
schedule_index_file = \ schedule_index_file = \
base_dir + '/accounts/' + handle + '/schedule.index' base_dir + '/accounts/' + handle + '/schedule.index'
with open(schedule_index_file, 'w+') as schedule_file: with open(schedule_index_file, 'w+', encoding='utf-8') as schedule_file:
for line in index_lines: for line in index_lines:
schedule_file.write(line) schedule_file.write(line)

View File

@ -291,7 +291,7 @@ def _indicate_new_share_available(base_dir: str, http_prefix: str,
local_actor = \ local_actor = \
local_actor_url(http_prefix, account_nickname, domain_full) local_actor_url(http_prefix, account_nickname, domain_full)
try: try:
with open(new_share_file, 'w+') as fp_new: with open(new_share_file, 'w+', encoding='utf-8') as fp_new:
if shares_file_type == 'shares': if shares_file_type == 'shares':
fp_new.write(local_actor + '/tlshares') fp_new.write(local_actor + '/tlshares')
else: else:
@ -1639,7 +1639,7 @@ def _generate_next_shares_token_update(base_dir: str,
token_update_filename = token_update_dir + '/.tokenUpdate' token_update_filename = token_update_dir + '/.tokenUpdate'
next_update_sec = None next_update_sec = None
if os.path.isfile(token_update_filename): if os.path.isfile(token_update_filename):
with open(token_update_filename, 'r') as fp_tok: with open(token_update_filename, 'r', encoding='utf-8') as fp_tok:
next_update_str = fp_tok.read() next_update_str = fp_tok.read()
if next_update_str: if next_update_str:
if next_update_str.isdigit(): if next_update_str.isdigit():
@ -1658,7 +1658,7 @@ def _generate_next_shares_token_update(base_dir: str,
next_update_sec = curr_time + next_update_interval next_update_sec = curr_time + next_update_interval
updated = True updated = True
if updated: if updated:
with open(token_update_filename, 'w+') as fp_tok: with open(token_update_filename, 'w+', encoding='utf-8') as fp_tok:
fp_tok.write(str(next_update_sec)) fp_tok.write(str(next_update_sec))
@ -1683,7 +1683,7 @@ def _regenerate_shares_token(base_dir: str, domain_full: str,
if not os.path.isfile(token_update_filename): if not os.path.isfile(token_update_filename):
return return
next_update_sec = None next_update_sec = None
with open(token_update_filename, 'r') as fp_tok: with open(token_update_filename, 'r', encoding='utf-8') as fp_tok:
next_update_str = fp_tok.read() next_update_str = fp_tok.read()
if next_update_str: if next_update_str:
if next_update_str.isdigit(): if next_update_str.isdigit():

View File

@ -146,7 +146,7 @@ def _speaker_pronounce(base_dir: str, say_text: str, translate: {}) -> str:
")": "," ")": ","
} }
if os.path.isfile(pronounce_filename): if os.path.isfile(pronounce_filename):
with open(pronounce_filename, 'r') as fp_pro: with open(pronounce_filename, 'r', encoding='utf-8') as fp_pro:
pronounce_list = fp_pro.readlines() pronounce_list = fp_pro.readlines()
for conversion in pronounce_list: for conversion in pronounce_list:
separator = None separator = None
@ -507,7 +507,7 @@ def _post_to_speaker_json(base_dir: str, http_prefix: str,
accounts_dir = acct_dir(base_dir, nickname, domain_full) accounts_dir = acct_dir(base_dir, nickname, domain_full)
approve_follows_filename = accounts_dir + '/followrequests.txt' approve_follows_filename = accounts_dir + '/followrequests.txt'
if os.path.isfile(approve_follows_filename): if os.path.isfile(approve_follows_filename):
with open(approve_follows_filename, 'r') as fp_foll: with open(approve_follows_filename, 'r', encoding='utf-8') as fp_foll:
follows = fp_foll.readlines() follows = fp_foll.readlines()
if len(follows) > 0: if len(follows) > 0:
follow_requests_exist = True follow_requests_exist = True
@ -525,7 +525,7 @@ def _post_to_speaker_json(base_dir: str, http_prefix: str,
liked_by = '' liked_by = ''
like_filename = accounts_dir + '/.newLike' like_filename = accounts_dir + '/.newLike'
if os.path.isfile(like_filename): if os.path.isfile(like_filename):
with open(like_filename, 'r') as fp_like: with open(like_filename, 'r', encoding='utf-8') as fp_like:
liked_by = fp_like.read() liked_by = fp_like.read()
calendar_filename = accounts_dir + '/.newCalendar' calendar_filename = accounts_dir + '/.newCalendar'
post_cal = os.path.isfile(calendar_filename) post_cal = os.path.isfile(calendar_filename)
@ -586,7 +586,7 @@ def update_speaker(base_dir: str, http_prefix: str,
system_language, system_language,
gender, box_name) gender, box_name)
try: try:
with open(cached_ssml_filename, 'w+') as fp_ssml: with open(cached_ssml_filename, 'w+', encoding='utf-8') as fp_ssml:
fp_ssml.write(ssml_str) fp_ssml.write(ssml_str)
except OSError: except OSError:
print('EX: unable to write ssml ' + cached_ssml_filename) print('EX: unable to write ssml ' + cached_ssml_filename)

125
tests.py
View File

@ -1418,7 +1418,7 @@ def test_post_message_between_servers(base_dir: str) -> None:
bob_domain, None, None) bob_domain, None, None)
for _ in range(20): for _ in range(20):
if 'likes' in open(outbox_post_filename).read(): if 'likes' in open(outbox_post_filename, encoding='utf-8').read():
break break
time.sleep(1) time.sleep(1)
@ -1426,7 +1426,7 @@ def test_post_message_between_servers(base_dir: str) -> None:
if alice_post_json: if alice_post_json:
pprint(alice_post_json) pprint(alice_post_json)
assert 'likes' in open(outbox_post_filename).read() assert 'likes' in open(outbox_post_filename, encoding='utf-8').read()
print('\n\n*******************************************************') print('\n\n*******************************************************')
print("Bob reacts to Alice's post") print("Bob reacts to Alice's post")
@ -1441,7 +1441,7 @@ def test_post_message_between_servers(base_dir: str) -> None:
bob_domain, None, None) bob_domain, None, None)
for _ in range(20): for _ in range(20):
if 'reactions' in open(outbox_post_filename).read(): if 'reactions' in open(outbox_post_filename, encoding='utf-8').read():
break break
time.sleep(1) time.sleep(1)
@ -1450,7 +1450,7 @@ def test_post_message_between_servers(base_dir: str) -> None:
pprint(alice_post_json) pprint(alice_post_json)
# TODO: fix reactions unit test # TODO: fix reactions unit test
# assert 'reactions' in open(outbox_post_filename).read() # assert 'reactions' in open(outbox_post_filename, encoding='utf-8').read()
print('\n\n*******************************************************') print('\n\n*******************************************************')
print("Bob repeats Alice's post") print("Bob repeats Alice's post")
@ -1639,12 +1639,15 @@ def test_follow_between_servers(base_dir: str) -> None:
alice_domain, alice_port) alice_domain, alice_port)
assert 'alice@' + alice_domain in open(bob_dir + '/accounts/bob@' + assert 'alice@' + alice_domain in open(bob_dir + '/accounts/bob@' +
bob_domain + bob_domain +
'/followers.txt').read() '/followers.txt',
encoding='utf-8').read()
assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' + assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' +
alice_domain + '/following.txt').read() alice_domain + '/following.txt',
encoding='utf-8').read()
assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' + assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' +
alice_domain + alice_domain +
'/followingCalendar.txt').read() '/followingCalendar.txt',
encoding='utf-8').read()
assert not is_group_actor(alice_dir, bob_actor, alice_person_cache) assert not is_group_actor(alice_dir, bob_actor, alice_person_cache)
assert not is_group_account(alice_dir, 'alice', alice_domain) assert not is_group_account(alice_dir, 'alice', alice_domain)
@ -1860,12 +1863,15 @@ def test_shared_items_federation(base_dir: str) -> None:
alice_domain, alice_port) alice_domain, alice_port)
assert 'alice@' + alice_domain in open(bob_dir + '/accounts/bob@' + assert 'alice@' + alice_domain in open(bob_dir + '/accounts/bob@' +
bob_domain + bob_domain +
'/followers.txt').read() '/followers.txt',
encoding='utf-8').read()
assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' + assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' +
alice_domain + '/following.txt').read() alice_domain + '/following.txt',
encoding='utf-8').read()
assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' + assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' +
alice_domain + alice_domain +
'/followingCalendar.txt').read() '/followingCalendar.txt',
encoding='utf-8').read()
assert not is_group_actor(alice_dir, bob_actor, alice_person_cache) assert not is_group_actor(alice_dir, bob_actor, alice_person_cache)
assert not is_group_account(bob_dir, 'bob', bob_domain) assert not is_group_account(bob_dir, 'bob', bob_domain)
@ -2316,20 +2322,22 @@ def test_group_follow(base_dir: str) -> None:
assert valid_inbox(testgroup_dir, 'testgroup', testgroup_domain) assert valid_inbox(testgroup_dir, 'testgroup', testgroup_domain)
assert valid_inbox_filenames(testgroup_dir, 'testgroup', testgroup_domain, assert valid_inbox_filenames(testgroup_dir, 'testgroup', testgroup_domain,
alice_domain, alice_port) alice_domain, alice_port)
assert 'alice@' + alice_domain in open(testgroup_followers_filename).read() assert 'alice@' + alice_domain in open(testgroup_followers_filename,
encoding='utf-8').read()
assert '!alice@' + alice_domain not in \ assert '!alice@' + alice_domain not in \
open(testgroup_followers_filename).read() open(testgroup_followers_filename, encoding='utf-8').read()
testgroup_webfinger_filename = \ testgroup_webfinger_filename = \
testgroup_dir + '/wfendpoints/testgroup@' + \ testgroup_dir + '/wfendpoints/testgroup@' + \
testgroup_domain + ':' + str(testgroupPort) + '.json' testgroup_domain + ':' + str(testgroupPort) + '.json'
assert os.path.isfile(testgroup_webfinger_filename) assert os.path.isfile(testgroup_webfinger_filename)
assert 'acct:testgroup@' in open(testgroup_webfinger_filename).read() assert 'acct:testgroup@' in open(testgroup_webfinger_filename,
encoding='utf-8').read()
print('acct: exists within the webfinger endpoint for testgroup') print('acct: exists within the webfinger endpoint for testgroup')
testgroup_handle = 'testgroup@' + testgroup_domain testgroup_handle = 'testgroup@' + testgroup_domain
following_str = '' following_str = ''
with open(alice_following_filename, 'r') as fp_foll: with open(alice_following_filename, 'r', encoding='utf-8') as fp_foll:
following_str = fp_foll.read() following_str = fp_foll.read()
print('Alice following.txt:\n\n' + following_str) print('Alice following.txt:\n\n' + following_str)
if '!testgroup' not in following_str: if '!testgroup' not in following_str:
@ -2339,8 +2347,10 @@ def test_group_follow(base_dir: str) -> None:
assert not is_group_account(alice_dir, 'alice', alice_domain) assert not is_group_account(alice_dir, 'alice', alice_domain)
assert is_group_account(testgroup_dir, 'testgroup', testgroup_domain) assert is_group_account(testgroup_dir, 'testgroup', testgroup_domain)
assert '!testgroup' in following_str assert '!testgroup' in following_str
assert testgroup_handle in open(alice_following_filename).read() assert testgroup_handle in open(alice_following_filename,
assert testgroup_handle in open(alice_following_calendar_filename).read() encoding='utf-8').read()
assert testgroup_handle in open(alice_following_calendar_filename,
encoding='utf-8').read()
print('\n\n*********************************************************') print('\n\n*********************************************************')
print('Alice follows the test group') print('Alice follows the test group')
@ -2394,20 +2404,22 @@ def test_group_follow(base_dir: str) -> None:
assert valid_inbox(testgroup_dir, 'testgroup', testgroup_domain) assert valid_inbox(testgroup_dir, 'testgroup', testgroup_domain)
assert valid_inbox_filenames(testgroup_dir, 'testgroup', testgroup_domain, assert valid_inbox_filenames(testgroup_dir, 'testgroup', testgroup_domain,
bob_domain, bob_port) bob_domain, bob_port)
assert 'bob@' + bob_domain in open(testgroup_followers_filename).read() assert 'bob@' + bob_domain in open(testgroup_followers_filename,
encoding='utf-8').read()
assert '!bob@' + bob_domain not in \ assert '!bob@' + bob_domain not in \
open(testgroup_followers_filename).read() open(testgroup_followers_filename, encoding='utf-8').read()
testgroup_webfinger_filename = \ testgroup_webfinger_filename = \
testgroup_dir + '/wfendpoints/testgroup@' + \ testgroup_dir + '/wfendpoints/testgroup@' + \
testgroup_domain + ':' + str(testgroupPort) + '.json' testgroup_domain + ':' + str(testgroupPort) + '.json'
assert os.path.isfile(testgroup_webfinger_filename) assert os.path.isfile(testgroup_webfinger_filename)
assert 'acct:testgroup@' in open(testgroup_webfinger_filename).read() assert 'acct:testgroup@' in open(testgroup_webfinger_filename,
encoding='utf-8').read()
print('acct: exists within the webfinger endpoint for testgroup') print('acct: exists within the webfinger endpoint for testgroup')
testgroup_handle = 'testgroup@' + testgroup_domain testgroup_handle = 'testgroup@' + testgroup_domain
following_str = '' following_str = ''
with open(bob_following_filename, 'r') as fp_foll: with open(bob_following_filename, 'r', encoding='utf-8') as fp_foll:
following_str = fp_foll.read() following_str = fp_foll.read()
print('Bob following.txt:\n\n' + following_str) print('Bob following.txt:\n\n' + following_str)
if '!testgroup' not in following_str: if '!testgroup' not in following_str:
@ -2415,8 +2427,10 @@ def test_group_follow(base_dir: str) -> None:
testgroup_domain + ':' + str(testgroupPort)) testgroup_domain + ':' + str(testgroupPort))
assert is_group_actor(bob_dir, testgroup_actor, bob_person_cache) assert is_group_actor(bob_dir, testgroup_actor, bob_person_cache)
assert '!testgroup' in following_str assert '!testgroup' in following_str
assert testgroup_handle in open(bob_following_filename).read() assert testgroup_handle in open(bob_following_filename,
assert testgroup_handle in open(bob_following_calendar_filename).read() encoding='utf-8').read()
assert testgroup_handle in open(bob_following_calendar_filename,
encoding='utf-8').read()
print('Bob follows the test group') print('Bob follows the test group')
print('\n\n*********************************************************') print('\n\n*********************************************************')
@ -2734,7 +2748,8 @@ def _test_follows(base_dir: str) -> None:
federation_list, False, False) federation_list, False, False)
account_dir = acct_dir(base_dir, nickname, domain) account_dir = acct_dir(base_dir, nickname, domain)
with open(account_dir + '/following.txt', 'r') as fp_foll: with open(account_dir + '/following.txt', 'r',
encoding='utf-8') as fp_foll:
domain_found = False domain_found = False
for following_domain in fp_foll: for following_domain in fp_foll:
test_domain = following_domain.split('@')[1] test_domain = following_domain.split('@')[1]
@ -2772,7 +2787,8 @@ def _test_follows(base_dir: str) -> None:
federation_list, False, False) federation_list, False, False)
account_dir = acct_dir(base_dir, nickname, domain) account_dir = acct_dir(base_dir, nickname, domain)
with open(account_dir + '/followers.txt', 'r') as fp_foll: with open(account_dir + '/followers.txt', 'r',
encoding='utf-8') as fp_foll:
for follower_domain in fp_foll: for follower_domain in fp_foll:
test_domain = follower_domain.split('@')[1] test_domain = follower_domain.split('@')[1]
test_domain = test_domain.replace('\n', '').replace('\r', '') test_domain = test_domain.replace('\n', '').replace('\r', '')
@ -3030,7 +3046,7 @@ def test_client_to_server(base_dir: str):
print('Bob follows the calendar of Alice') print('Bob follows the calendar of Alice')
following_cal_path = \ following_cal_path = \
bob_dir + '/accounts/bob@' + bob_domain + '/followingCalendar.txt' bob_dir + '/accounts/bob@' + bob_domain + '/followingCalendar.txt'
with open(following_cal_path, 'w+') as fp_foll: with open(following_cal_path, 'w+', encoding='utf-8') as fp_foll:
fp_foll.write('alice@' + alice_domain + '\n') fp_foll.write('alice@' + alice_domain + '\n')
print('\n\n*******************************************************') print('\n\n*******************************************************')
@ -3172,11 +3188,13 @@ def test_client_to_server(base_dir: str):
for _ in range(10): for _ in range(10):
if os.path.isfile(bob_followers_filename): if os.path.isfile(bob_followers_filename):
if 'alice@' + alice_domain + ':' + str(alice_port) in \ if 'alice@' + alice_domain + ':' + str(alice_port) in \
open(bob_followers_filename).read(): open(bob_followers_filename,
encoding='utf-8').read():
if os.path.isfile(alice_following_filename) and \ if os.path.isfile(alice_following_filename) and \
os.path.isfile(alice_petnames_filename): os.path.isfile(alice_petnames_filename):
if 'bob@' + bob_domain + ':' + str(bob_port) in \ if 'bob@' + bob_domain + ':' + str(bob_port) in \
open(alice_following_filename).read(): open(alice_following_filename,
encoding='utf-8').read():
break break
time.sleep(1) time.sleep(1)
@ -3184,15 +3202,15 @@ def test_client_to_server(base_dir: str):
assert os.path.isfile(alice_following_filename) assert os.path.isfile(alice_following_filename)
assert os.path.isfile(alice_petnames_filename) assert os.path.isfile(alice_petnames_filename)
assert 'bob bob@' + bob_domain in \ assert 'bob bob@' + bob_domain in \
open(alice_petnames_filename).read() open(alice_petnames_filename, encoding='utf-8').read()
print('alice@' + alice_domain + ':' + str(alice_port) + ' in ' + print('alice@' + alice_domain + ':' + str(alice_port) + ' in ' +
bob_followers_filename) bob_followers_filename)
assert 'alice@' + alice_domain + ':' + str(alice_port) in \ assert 'alice@' + alice_domain + ':' + str(alice_port) in \
open(bob_followers_filename).read() open(bob_followers_filename, encoding='utf-8').read()
print('bob@' + bob_domain + ':' + str(bob_port) + ' in ' + print('bob@' + bob_domain + ':' + str(bob_port) + ' in ' +
alice_following_filename) alice_following_filename)
assert 'bob@' + bob_domain + ':' + str(bob_port) in \ assert 'bob@' + bob_domain + ':' + str(bob_port) in \
open(alice_following_filename).read() open(alice_following_filename, encoding='utf-8').read()
assert valid_inbox(bob_dir, 'bob', bob_domain) assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain, assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port) alice_domain, alice_port)
@ -3210,20 +3228,21 @@ def test_client_to_server(base_dir: str):
'/followers.txt'): '/followers.txt'):
if 'bob@' + bob_domain + ':' + str(bob_port) in \ if 'bob@' + bob_domain + ':' + str(bob_port) in \
open(alice_dir + '/accounts/alice@' + alice_domain + open(alice_dir + '/accounts/alice@' + alice_domain +
'/followers.txt').read(): '/followers.txt', encoding='utf-8').read():
if os.path.isfile(bob_dir + '/accounts/bob@' + bob_domain + if os.path.isfile(bob_dir + '/accounts/bob@' + bob_domain +
'/following.txt'): '/following.txt'):
alice_handle_str = \ alice_handle_str = \
'alice@' + alice_domain + ':' + str(alice_port) 'alice@' + alice_domain + ':' + str(alice_port)
if alice_handle_str in \ if alice_handle_str in \
open(bob_dir + '/accounts/bob@' + bob_domain + open(bob_dir + '/accounts/bob@' + bob_domain +
'/following.txt').read(): '/following.txt', encoding='utf-8').read():
if os.path.isfile(bob_dir + '/accounts/bob@' + if os.path.isfile(bob_dir + '/accounts/bob@' +
bob_domain + bob_domain +
'/followingCalendar.txt'): '/followingCalendar.txt'):
if alice_handle_str in \ if alice_handle_str in \
open(bob_dir + '/accounts/bob@' + bob_domain + open(bob_dir + '/accounts/bob@' + bob_domain +
'/followingCalendar.txt').read(): '/followingCalendar.txt',
encoding='utf-8').read():
break break
time.sleep(1) time.sleep(1)
@ -3233,9 +3252,10 @@ def test_client_to_server(base_dir: str):
'/following.txt') '/following.txt')
assert 'bob@' + bob_domain + ':' + str(bob_port) in \ assert 'bob@' + bob_domain + ':' + str(bob_port) in \
open(alice_dir + '/accounts/alice@' + alice_domain + open(alice_dir + '/accounts/alice@' + alice_domain +
'/followers.txt').read() '/followers.txt', encoding='utf-8').read()
assert 'alice@' + alice_domain + ':' + str(alice_port) in \ assert 'alice@' + alice_domain + ':' + str(alice_port) in \
open(bob_dir + '/accounts/bob@' + bob_domain + '/following.txt').read() open(bob_dir + '/accounts/bob@' + bob_domain + '/following.txt',
encoding='utf-8').read()
session_bob = create_session(proxy_type) session_bob = create_session(proxy_type)
password = 'bobpass' password = 'bobpass'
@ -3439,18 +3459,18 @@ def test_client_to_server(base_dir: str):
True, __version__, signing_priv_key_pem) True, __version__, signing_priv_key_pem)
for _ in range(10): for _ in range(10):
if 'alice@' + alice_domain + ':' + str(alice_port) not in \ if 'alice@' + alice_domain + ':' + str(alice_port) not in \
open(bob_followers_filename).read(): open(bob_followers_filename, encoding='utf-8').read():
if 'bob@' + bob_domain + ':' + str(bob_port) not in \ if 'bob@' + bob_domain + ':' + str(bob_port) not in \
open(alice_following_filename).read(): open(alice_following_filename, encoding='utf-8').read():
break break
time.sleep(1) time.sleep(1)
assert os.path.isfile(bob_followers_filename) assert os.path.isfile(bob_followers_filename)
assert os.path.isfile(alice_following_filename) assert os.path.isfile(alice_following_filename)
assert 'alice@' + alice_domain + ':' + str(alice_port) \ assert 'alice@' + alice_domain + ':' + str(alice_port) \
not in open(bob_followers_filename).read() not in open(bob_followers_filename, encoding='utf-8').read()
assert 'bob@' + bob_domain + ':' + str(bob_port) \ assert 'bob@' + bob_domain + ':' + str(bob_port) \
not in open(alice_following_filename).read() not in open(alice_following_filename, encoding='utf-8').read()
assert valid_inbox(bob_dir, 'bob', bob_domain) assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain, assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port) alice_domain, alice_port)
@ -4228,7 +4248,7 @@ 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') as file_source: with open(source_file, 'r', encoding='utf-8') as file_source:
source_str = file_source.read() source_str = file_source.read()
if not source_str: if not source_str:
continue continue
@ -4833,7 +4853,7 @@ def _diagram_groups(include_groups: [],
for group_name in include_groups: for group_name in include_groups:
filename += '_' + group_name.replace(' ', '-') filename += '_' + group_name.replace(' ', '-')
filename += '.dot' filename += '.dot'
with open(filename, 'w+') as fp_graph: with open(filename, 'w+', encoding='utf-8') as fp_graph:
fp_graph.write(call_graph_str) fp_graph.write(call_graph_str)
print('Graph saved to ' + filename) print('Graph saved to ' + filename)
print('Plot using: ' + print('Plot using: ' +
@ -4853,7 +4873,7 @@ 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') as file_source: with open(source_file, 'r', encoding='utf-8') as file_source:
source_str = file_source.read() source_str = file_source.read()
if not source_str: if not source_str:
continue continue
@ -4888,7 +4908,7 @@ 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') as file_source: with open(source_file, 'r', encoding='utf-8') as file_source:
source_str = file_source.read() source_str = file_source.read()
if not source_str: if not source_str:
continue continue
@ -4936,7 +4956,7 @@ 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') as file_source: with open(source_file, 'r', encoding='utf-8') as file_source:
source_str = file_source.read() source_str = file_source.read()
if not source_str: if not source_str:
continue continue
@ -4959,7 +4979,7 @@ 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') as file_source: with open(source_file, 'r', encoding='utf-8') as file_source:
source_str = file_source.read() source_str = file_source.read()
if not source_str: if not source_str:
continue continue
@ -4999,7 +5019,7 @@ 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') as file_source: with open(source_file, 'r', encoding='utf-8') as file_source:
source_str = file_source.read() source_str = file_source.read()
if not source_str: if not source_str:
return return
@ -5082,12 +5102,12 @@ def _test_thread_functions():
'functions': [] 'functions': []
} }
source_str = '' source_str = ''
with open(source_file, 'r') as fp_src: with open(source_file, 'r', encoding='utf-8') as fp_src:
source_str = fp_src.read() source_str = fp_src.read()
modules[mod_name]['source'] = source_str modules[mod_name]['source'] = source_str
if 'thread_with_trace(' in source_str: if 'thread_with_trace(' in source_str:
threads_called_in_modules.append(mod_name) threads_called_in_modules.append(mod_name)
with open(source_file, 'r') as fp_src: with open(source_file, 'r', encoding='utf-8') as fp_src:
lines = fp_src.readlines() lines = fp_src.readlines()
modules[mod_name]['lines'] = lines modules[mod_name]['lines'] = lines
@ -5197,10 +5217,10 @@ def _test_functions():
'functions': [] 'functions': []
} }
source_str = '' source_str = ''
with open(source_file, 'r') as fp_src: with open(source_file, 'r', encoding='utf-8') as fp_src:
source_str = fp_src.read() source_str = fp_src.read()
modules[mod_name]['source'] = source_str modules[mod_name]['source'] = source_str
with open(source_file, 'r') as fp_src: with open(source_file, 'r', encoding='utf-8') as fp_src:
lines = fp_src.readlines() lines = fp_src.readlines()
modules[mod_name]['lines'] = lines modules[mod_name]['lines'] = lines
line_count = 0 line_count = 0
@ -5313,7 +5333,8 @@ def _test_functions():
'sed -i "s|' + name + '|' + snake_case_name + '|g" *.py\n' 'sed -i "s|' + name + '|' + snake_case_name + '|g" *.py\n'
ctr += 1 ctr += 1
print(function_names_str + '\n') print(function_names_str + '\n')
with open('scripts/bad_function_names.sh', 'w+') as fn_file: with open('scripts/bad_function_names.sh', 'w+',
encoding='utf-8') as fn_file:
fn_file.write(function_names_sh) fn_file.write(function_names_sh)
assert False assert False
@ -6246,7 +6267,7 @@ 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+') as kmlfile: with open('unittest_decoy.kml', 'w+', encoding='utf-8') as kmlfile:
kmlfile.write(kml_str) kmlfile.write(kml_str)

View File

@ -41,7 +41,8 @@ def import_theme(base_dir: str, filename: str) -> bool:
' missing from imported theme') ' missing from imported theme')
return False return False
new_theme_name = None new_theme_name = None
with open(temp_theme_dir + '/name.txt', 'r') as fp_theme: with open(temp_theme_dir + '/name.txt', 'r',
encoding='utf-8') as fp_theme:
new_theme_name = fp_theme.read().replace('\n', '').replace('\r', '') new_theme_name = fp_theme.read().replace('\n', '').replace('\r', '')
if len(new_theme_name) > 20: if len(new_theme_name) > 20:
print('WARN: Imported theme name is too long') print('WARN: Imported theme name is too long')
@ -65,7 +66,7 @@ def import_theme(base_dir: str, filename: str) -> bool:
default_themes_filename = base_dir + '/defaultthemes.txt' default_themes_filename = base_dir + '/defaultthemes.txt'
if os.path.isfile(default_themes_filename): if os.path.isfile(default_themes_filename):
if new_theme_name.title() + '\n' in \ if new_theme_name.title() + '\n' in \
open(default_themes_filename).read(): open(default_themes_filename, encoding='utf-8').read():
new_theme_name = new_theme_name + '2' new_theme_name = new_theme_name + '2'
theme_dir = base_dir + '/theme/' + new_theme_name theme_dir = base_dir + '/theme/' + new_theme_name
@ -349,7 +350,7 @@ 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') as cssfile: with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read() css = cssfile.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':
@ -384,7 +385,7 @@ def _set_theme_from_dict(base_dir: str, name: str,
continue continue
css = set_css_param(css, param_name, param_value) css = set_css_param(css, param_name, param_value)
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
with open(filename, 'w+') as cssfile: with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css) cssfile.write(css)
screen_name = ( screen_name = (
@ -404,10 +405,10 @@ def _set_background_format(base_dir: str, name: str,
css_filename = base_dir + '/' + background_type + '.css' css_filename = base_dir + '/' + background_type + '.css'
if not os.path.isfile(css_filename): if not os.path.isfile(css_filename):
return return
with open(css_filename, 'r') as cssfile: with open(css_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read() css = cssfile.read()
css = css.replace('background.jpg', 'background.' + extension) css = css.replace('background.jpg', 'background.' + extension)
with open(css_filename, 'w+') as cssfile2: with open(css_filename, 'w+', encoding='utf-8') as cssfile2:
cssfile2.write(css) cssfile2.write(css)
@ -419,18 +420,18 @@ def enable_grayscale(base_dir: str) -> None:
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') as cssfile: with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read() css = cssfile.read()
if 'grayscale' not in css: if 'grayscale' not in css:
css = \ css = \
css.replace('body, html {', css.replace('body, html {',
'body, html {\n filter: grayscale(100%);') 'body, html {\n filter: grayscale(100%);')
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
with open(filename, 'w+') as cssfile: with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css) cssfile.write(css)
grayscale_filename = base_dir + '/accounts/.grayscale' grayscale_filename = base_dir + '/accounts/.grayscale'
if not os.path.isfile(grayscale_filename): if not os.path.isfile(grayscale_filename):
with open(grayscale_filename, 'w+') as grayfile: with open(grayscale_filename, 'w+', encoding='utf-8') as grayfile:
grayfile.write(' ') grayfile.write(' ')
@ -442,13 +443,13 @@ def disable_grayscale(base_dir: str) -> None:
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') as cssfile: with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read() css = cssfile.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+') as cssfile: with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css) cssfile.write(css)
grayscale_filename = base_dir + '/accounts/.grayscale' grayscale_filename = base_dir + '/accounts/.grayscale'
if os.path.isfile(grayscale_filename): if os.path.isfile(grayscale_filename):
@ -467,7 +468,7 @@ 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') as cssfile: with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read() css = cssfile.read()
css = \ css = \
set_css_param(css, "*src", set_css_param(css, "*src",
@ -475,7 +476,7 @@ def _set_dyslexic_font(base_dir: str) -> bool:
"') format('woff2')") "') format('woff2')")
css = set_css_param(css, "*font-family", "'OpenDyslexic'") css = set_css_param(css, "*font-family", "'OpenDyslexic'")
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
with open(filename, 'w+') as cssfile: with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css) cssfile.write(css)
return False return False
@ -504,7 +505,7 @@ 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') as cssfile: with open(template_filename, 'r', encoding='utf-8') as cssfile:
css = cssfile.read() css = cssfile.read()
css = \ css = \
set_css_param(css, "*src", set_css_param(css, "*src",
@ -515,7 +516,7 @@ def _set_custom_font(base_dir: str):
css = set_css_param(css, "*font-family", "'CustomFont'") css = set_css_param(css, "*font-family", "'CustomFont'")
css = set_css_param(css, "header-font", "'CustomFont'") css = set_css_param(css, "header-font", "'CustomFont'")
filename = base_dir + '/' + filename filename = base_dir + '/' + filename
with open(filename, 'w+') as cssfile: with open(filename, 'w+', encoding='utf-8') as cssfile:
cssfile.write(css) cssfile.write(css)
@ -630,7 +631,8 @@ def get_text_mode_banner(base_dir: str) -> str:
""" """
text_mode_banner_filename = base_dir + '/accounts/banner.txt' text_mode_banner_filename = base_dir + '/accounts/banner.txt'
if os.path.isfile(text_mode_banner_filename): if os.path.isfile(text_mode_banner_filename):
with open(text_mode_banner_filename, 'r') as fp_text: with open(text_mode_banner_filename, 'r',
encoding='utf-8') as fp_text:
banner_str = fp_text.read() banner_str = fp_text.read()
if banner_str: if banner_str:
return banner_str.replace('\n', '<br>') return banner_str.replace('\n', '<br>')
@ -644,7 +646,7 @@ def get_text_mode_logo(base_dir: str) -> str:
if not os.path.isfile(text_mode_logo_filename): if not os.path.isfile(text_mode_logo_filename):
text_mode_logo_filename = base_dir + '/img/logo.txt' text_mode_logo_filename = base_dir + '/img/logo.txt'
with open(text_mode_logo_filename, 'r') as fp_text: with open(text_mode_logo_filename, 'r', encoding='utf-8') as fp_text:
logo_str = fp_text.read() logo_str = fp_text.read()
if logo_str: if logo_str:
return logo_str.replace('\n', '<br>') return logo_str.replace('\n', '<br>')
@ -846,7 +848,7 @@ def _set_clear_cache_flag(base_dir: str) -> None:
if not os.path.isdir(base_dir + '/accounts'): if not os.path.isdir(base_dir + '/accounts'):
return return
flag_filename = base_dir + '/accounts/.clear_cache' flag_filename = base_dir + '/accounts/.clear_cache'
with open(flag_filename, 'w+') as fp_flag: with open(flag_filename, 'w+', encoding='utf-8') as fp_flag:
fp_flag.write('\n') fp_flag.write('\n')
@ -924,7 +926,7 @@ def update_default_themes_list(base_dir: str) -> None:
""" """
theme_names = get_themes_list(base_dir) theme_names = get_themes_list(base_dir)
default_themes_filename = base_dir + '/defaultthemes.txt' default_themes_filename = base_dir + '/defaultthemes.txt'
with open(default_themes_filename, 'w+') as fp_def: with open(default_themes_filename, 'w+', encoding='utf-8') as fp_def:
for name in theme_names: for name in theme_names:
fp_def.write(name + '\n') fp_def.write(name + '\n')
@ -938,7 +940,7 @@ def scan_themes_for_scripts(base_dir: str) -> bool:
continue continue
svg_filename = os.path.join(subdir, fname) svg_filename = os.path.join(subdir, fname)
content = '' content = ''
with open(svg_filename, 'r') as fp_svg: with open(svg_filename, 'r', encoding='utf-8') as fp_svg:
content = fp_svg.read() content = fp_svg.read()
svg_dangerous = dangerous_svg(content, False) svg_dangerous = dangerous_svg(content, False)
if svg_dangerous: if svg_dangerous:

View File

@ -156,7 +156,7 @@ def remove_dormant_threads(base_dir: str, threads_list: [], debug: bool,
if debug: if debug:
send_log_filename = base_dir + '/send.csv' send_log_filename = base_dir + '/send.csv'
try: try:
with open(send_log_filename, 'a+') as fp_log: with open(send_log_filename, 'a+', encoding='utf-8') as fp_log:
fp_log.write(curr_time.strftime("%Y-%m-%dT%H:%M:%SZ") + fp_log.write(curr_time.strftime("%Y-%m-%dT%H:%M:%SZ") +
',' + str(no_of_active_threads) + ',' + str(no_of_active_threads) +
',' + str(len(threads_list)) + '\n') ',' + str(len(threads_list)) + '\n')

104
utils.py
View File

@ -197,7 +197,8 @@ def refresh_newswire(base_dir: str):
refresh_newswire_filename = base_dir + '/accounts/.refresh_newswire' refresh_newswire_filename = base_dir + '/accounts/.refresh_newswire'
if os.path.isfile(refresh_newswire_filename): if os.path.isfile(refresh_newswire_filename):
return return
with open(refresh_newswire_filename, 'w+') as refresh_file: with open(refresh_newswire_filename, 'w+',
encoding='utf-8') as refresh_file:
refresh_file.write('\n') refresh_file.write('\n')
@ -328,7 +329,8 @@ def is_dormant(base_dir: str, nickname: str, domain: str, actor: str,
days_since_epoch_str = None days_since_epoch_str = None
try: try:
with open(last_seen_filename, 'r') as last_seen_file: with open(last_seen_filename, 'r',
encoding='utf-8') as last_seen_file:
days_since_epoch_str = last_seen_file.read() days_since_epoch_str = last_seen_file.read()
except OSError: except OSError:
print('EX: failed to read last seen ' + last_seen_filename) print('EX: failed to read last seen ' + last_seen_filename)
@ -358,7 +360,7 @@ def is_editor(base_dir: str, nickname: str) -> bool:
return True return True
return False return False
with open(editors_file, 'r') as editors: with open(editors_file, 'r', encoding='utf-8') as editors:
lines = editors.readlines() lines = 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')
@ -384,7 +386,7 @@ def is_artist(base_dir: str, nickname: str) -> bool:
return True return True
return False return False
with open(artists_file, 'r') as artists: with open(artists_file, 'r', encoding='utf-8') as artists:
lines = artists.readlines() lines = 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')
@ -601,7 +603,7 @@ def is_suspended(base_dir: str, nickname: str) -> bool:
suspended_filename = base_dir + '/accounts/suspended.txt' suspended_filename = base_dir + '/accounts/suspended.txt'
if os.path.isfile(suspended_filename): if os.path.isfile(suspended_filename):
with open(suspended_filename, 'r') as susp_file: with open(suspended_filename, 'r', encoding='utf-8') as susp_file:
lines = susp_file.readlines() lines = susp_file.readlines()
for suspended in lines: for suspended in lines:
if suspended.strip('\n').strip('\r') == nickname: if suspended.strip('\n').strip('\r') == nickname:
@ -619,7 +621,7 @@ def get_followers_list(base_dir: str,
if not os.path.isfile(filename): if not os.path.isfile(filename):
return [] return []
with open(filename, 'r') as foll_file: with open(filename, 'r', encoding='utf-8') as foll_file:
lines = foll_file.readlines() lines = foll_file.readlines()
for i, _ in enumerate(lines): for i, _ in enumerate(lines):
lines[i] = lines[i].strip() lines[i] = lines[i].strip()
@ -648,7 +650,7 @@ 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') as followingfile: with open(filename, 'r', encoding='utf-8') as followingfile:
for following_handle in followingfile: for following_handle in followingfile:
following_handle2 = following_handle.replace('\n', '') following_handle2 = following_handle.replace('\n', '')
following_handle2 = following_handle2.replace('\r', '') following_handle2 = following_handle2.replace('\r', '')
@ -724,7 +726,7 @@ def save_json(json_object: {}, filename: str) -> bool:
tries = 0 tries = 0
while tries < 5: while tries < 5:
try: try:
with open(filename, 'w+') as json_file: with open(filename, 'w+', encoding='utf-8') as json_file:
json_file.write(json.dumps(json_object)) json_file.write(json.dumps(json_object))
return True return True
except OSError: except OSError:
@ -743,7 +745,7 @@ def load_json(filename: str, delay_sec: int = 2, max_tries: int = 5) -> {}:
tries = 0 tries = 0
while tries < max_tries: while tries < max_tries:
try: try:
with open(filename, 'r') as json_file: with open(filename, 'r', encoding='utf-8') as json_file:
data = json_file.read() data = json_file.read()
json_object = json.loads(data) json_object = json.loads(data)
break break
@ -766,7 +768,7 @@ def load_json_onionify(filename: str, domain: str, onion_domain: str,
tries = 0 tries = 0
while tries < 5: while tries < 5:
try: try:
with open(filename, 'r') as json_file: with open(filename, 'r', encoding='utf-8') as json_file:
data = json_file.read() data = json_file.read()
if data: if data:
data = data.replace(domain, onion_domain) data = data.replace(domain, onion_domain)
@ -1262,11 +1264,11 @@ def _set_default_pet_name(base_dir: str, nickname: str, domain: str,
follow_nickname + '@' + follow_domain + '\n' follow_nickname + '@' + follow_domain + '\n'
if not os.path.isfile(petnames_filename): if not os.path.isfile(petnames_filename):
# if there is no existing petnames lookup file # if there is no existing petnames lookup file
with open(petnames_filename, 'w+') as petnames_file: with open(petnames_filename, 'w+', encoding='utf-8') as petnames_file:
petnames_file.write(petname_lookup_entry) petnames_file.write(petname_lookup_entry)
return return
with open(petnames_filename, 'r') as petnames_file: with open(petnames_filename, 'r', encoding='utf-8') as petnames_file:
petnames_str = petnames_file.read() petnames_str = petnames_file.read()
if petnames_str: if petnames_str:
petnames_list = petnames_str.split('\n') petnames_list = petnames_str.split('\n')
@ -1275,7 +1277,7 @@ def _set_default_pet_name(base_dir: str, nickname: str, domain: str,
# petname already exists # petname already exists
return return
# petname doesn't already exist # petname doesn't already exist
with open(petnames_filename, 'a+') as petnames_file: with open(petnames_filename, 'a+', encoding='utf-8') as petnames_file:
petnames_file.write(petname_lookup_entry) petnames_file.write(petname_lookup_entry)
@ -1318,15 +1320,18 @@ def follow_person(base_dir: str, nickname: str, domain: str,
# was this person previously unfollowed? # was this person previously unfollowed?
unfollowed_filename = base_dir + '/accounts/' + handle + '/unfollowed.txt' unfollowed_filename = base_dir + '/accounts/' + handle + '/unfollowed.txt'
if os.path.isfile(unfollowed_filename): if os.path.isfile(unfollowed_filename):
if handle_to_follow in open(unfollowed_filename).read(): if handle_to_follow in open(unfollowed_filename,
encoding='utf-8').read():
# remove them from the unfollowed file # remove them from the unfollowed file
new_lines = '' new_lines = ''
with open(unfollowed_filename, 'r') as unfoll_file: with open(unfollowed_filename, 'r',
encoding='utf-8') as unfoll_file:
lines = unfoll_file.readlines() lines = unfoll_file.readlines()
for line in lines: for line in lines:
if handle_to_follow not in line: if handle_to_follow not in line:
new_lines += line new_lines += line
with open(unfollowed_filename, 'w+') as unfoll_file: with open(unfollowed_filename, 'w+',
encoding='utf-8') as unfoll_file:
unfoll_file.write(new_lines) unfoll_file.write(new_lines)
if not os.path.isdir(base_dir + '/accounts'): if not os.path.isdir(base_dir + '/accounts'):
@ -1336,13 +1341,13 @@ def follow_person(base_dir: str, nickname: str, domain: str,
handle_to_follow = '!' + handle_to_follow handle_to_follow = '!' + handle_to_follow
filename = base_dir + '/accounts/' + handle + '/' + follow_file filename = base_dir + '/accounts/' + handle + '/' + follow_file
if os.path.isfile(filename): if os.path.isfile(filename):
if handle_to_follow in open(filename).read(): if handle_to_follow in open(filename, encoding='utf-8').read():
if debug: if debug:
print('DEBUG: follow already exists') print('DEBUG: follow already exists')
return True return True
# prepend to follow file # prepend to follow file
try: try:
with open(filename, 'r+') as foll_file: with open(filename, 'r+', encoding='utf-8') as foll_file:
content = foll_file.read() content = foll_file.read()
if handle_to_follow + '\n' not in content: if handle_to_follow + '\n' not in content:
foll_file.seek(0, 0) foll_file.seek(0, 0)
@ -1358,7 +1363,7 @@ def follow_person(base_dir: str, nickname: str, domain: str,
' creating new following file to follow ' + ' creating new following file to follow ' +
handle_to_follow + handle_to_follow +
', filename is ' + filename) ', filename is ' + filename)
with open(filename, 'w+') as foll_file: with open(filename, 'w+', encoding='utf-8') as foll_file:
foll_file.write(handle_to_follow + '\n') foll_file.write(handle_to_follow + '\n')
if follow_file.endswith('following.txt'): if follow_file.endswith('following.txt'):
@ -1430,7 +1435,7 @@ def locate_news_arrival(base_dir: str, domain: str,
account_dir = base_dir + '/accounts/news@' + domain + '/' account_dir = base_dir + '/accounts/news@' + domain + '/'
post_filename = account_dir + 'outbox/' + post_url post_filename = account_dir + 'outbox/' + post_url
if os.path.isfile(post_filename): if os.path.isfile(post_filename):
with open(post_filename, 'r') as arrival_file: with open(post_filename, 'r', encoding='utf-8') as arrival_file:
arrival = arrival_file.read() arrival = arrival_file.read()
if arrival: if arrival:
arrival_date = \ arrival_date = \
@ -1537,7 +1542,8 @@ def get_reply_interval_hours(base_dir: str, nickname: str, domain: str,
reply_interval_filename = \ reply_interval_filename = \
acct_dir(base_dir, nickname, domain) + '/.reply_interval_hours' acct_dir(base_dir, nickname, domain) + '/.reply_interval_hours'
if os.path.isfile(reply_interval_filename): if os.path.isfile(reply_interval_filename):
with open(reply_interval_filename, 'r') as interval_file: with open(reply_interval_filename, 'r',
encoding='utf-8') as interval_file:
hours_str = interval_file.read() hours_str = interval_file.read()
if hours_str.isdigit(): if hours_str.isdigit():
return int(hours_str) return int(hours_str)
@ -1553,7 +1559,8 @@ def set_reply_interval_hours(base_dir: str, nickname: str, domain: str,
reply_interval_filename = \ reply_interval_filename = \
acct_dir(base_dir, nickname, domain) + '/.reply_interval_hours' acct_dir(base_dir, nickname, domain) + '/.reply_interval_hours'
try: try:
with open(reply_interval_filename, 'w+') as interval_file: with open(reply_interval_filename, 'w+',
encoding='utf-8') as interval_file:
interval_file.write(str(reply_interval_hours)) interval_file.write(str(reply_interval_hours))
return True return True
except OSError: except OSError:
@ -1641,10 +1648,12 @@ def remove_moderation_post_from_index(base_dir: str, post_url: str,
if not os.path.isfile(moderation_index_file): if not os.path.isfile(moderation_index_file):
return return
post_id = remove_id_ending(post_url) post_id = remove_id_ending(post_url)
if post_id in open(moderation_index_file).read(): if post_id in open(moderation_index_file, encoding='utf-8').read():
with open(moderation_index_file, 'r') as file1: with open(moderation_index_file, 'r',
encoding='utf-8') as file1:
lines = file1.readlines() lines = file1.readlines()
with open(moderation_index_file, 'w+') as file2: with open(moderation_index_file, 'w+',
encoding='utf-8') as file2:
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) file2.write(line)
@ -1670,7 +1679,7 @@ def _is_reply_to_blog_post(base_dir: str, nickname: str, domain: str,
return False return False
post_id = remove_id_ending(post_json_object['object']['inReplyTo']) post_id = remove_id_ending(post_json_object['object']['inReplyTo'])
post_id = post_id.replace('/', '#') post_id = post_id.replace('/', '#')
if post_id in open(blogs_index_filename).read(): if post_id in open(blogs_index_filename, encoding='utf-8').read():
return True return True
return False return False
@ -1686,7 +1695,7 @@ def _delete_post_remove_replies(base_dir: str, nickname: str, domain: str,
return return
if debug: if debug:
print('DEBUG: removing replies to ' + post_filename) print('DEBUG: removing replies to ' + post_filename)
with open(replies_filename, 'r') as replies_file: with open(replies_filename, 'r', encoding='utf-8') as replies_file:
for reply_id in replies_file: for reply_id in replies_file:
reply_file = locate_post(base_dir, nickname, domain, reply_id) reply_file = locate_post(base_dir, nickname, domain, reply_id)
if not reply_file: if not reply_file:
@ -1711,7 +1720,8 @@ def _is_bookmarked(base_dir: str, nickname: str, domain: str,
acct_dir(base_dir, nickname, domain) + '/bookmarks.index' acct_dir(base_dir, nickname, domain) + '/bookmarks.index'
if os.path.isfile(bookmarks_index_filename): if os.path.isfile(bookmarks_index_filename):
bookmark_index = post_filename.split('/')[-1] + '\n' bookmark_index = post_filename.split('/')[-1] + '\n'
if bookmark_index in open(bookmarks_index_filename).read(): if bookmark_index in open(bookmarks_index_filename,
encoding='utf-8').read():
return True return True
return False return False
@ -1815,7 +1825,7 @@ def _delete_hashtags_on_post(base_dir: str, post_json_object: {}) -> None:
continue continue
# remove post_id from the tag index file # remove post_id from the tag index file
lines = None lines = None
with open(tag_index_filename, 'r') as index_file: with open(tag_index_filename, 'r', encoding='utf-8') as index_file:
lines = index_file.readlines() lines = index_file.readlines()
if not lines: if not lines:
continue continue
@ -1834,7 +1844,8 @@ def _delete_hashtags_on_post(base_dir: str, post_json_object: {}) -> None:
'unable to delete tag index ' + str(tag_index_filename)) 'unable to delete tag index ' + str(tag_index_filename))
else: else:
# write the new hashtag index without the given post in it # write the new hashtag index without the given post in it
with open(tag_index_filename, 'w+') as index_file: with open(tag_index_filename, 'w+',
encoding='utf-8') as index_file:
index_file.write(newlines) index_file.write(newlines)
@ -1857,13 +1868,13 @@ def _delete_conversation_post(base_dir: str, nickname: str, domain: str,
if not os.path.isfile(conversation_filename): if not os.path.isfile(conversation_filename):
return False return False
conversation_str = '' conversation_str = ''
with open(conversation_filename, 'r') as conv_file: with open(conversation_filename, 'r', encoding='utf-8') as conv_file:
conversation_str = conv_file.read() conversation_str = conv_file.read()
if post_id + '\n' not in conversation_str: if post_id + '\n' not in conversation_str:
return False return False
conversation_str = conversation_str.replace(post_id + '\n', '') conversation_str = conversation_str.replace(post_id + '\n', '')
if conversation_str: if conversation_str:
with open(conversation_filename, 'w+') as conv_file: with open(conversation_filename, 'w+', encoding='utf-8') as conv_file:
conv_file.write(conversation_str) conv_file.write(conversation_str)
else: else:
if os.path.isfile(conversation_filename + '.muted'): if os.path.isfile(conversation_filename + '.muted'):
@ -2175,7 +2186,8 @@ def no_of_active_accounts_monthly(base_dir: str, months: int) -> bool:
base_dir + '/accounts/' + account + '/.lastUsed' base_dir + '/accounts/' + account + '/.lastUsed'
if not os.path.isfile(last_used_filename): if not os.path.isfile(last_used_filename):
continue continue
with open(last_used_filename, 'r') as last_used_file: with open(last_used_filename, 'r',
encoding='utf-8') as last_used_file:
last_used = last_used_file.read() last_used = last_used_file.read()
if last_used.isdigit(): if last_used.isdigit():
time_diff = (curr_time - int(last_used)) time_diff = (curr_time - int(last_used))
@ -2334,7 +2346,7 @@ def get_css(base_dir: str, css_filename: str, css_cache: {}) -> str:
# file hasn't changed, so return the version in the cache # file hasn't changed, so return the version in the cache
return css_cache[css_filename][1] return css_cache[css_filename][1]
with open(css_filename, 'r') as fp_css: with open(css_filename, 'r', encoding='utf-8') as fp_css:
css = fp_css.read() css = fp_css.read()
if css_cache.get(css_filename): if css_cache.get(css_filename):
# alter the cache contents # alter the cache contents
@ -2394,7 +2406,7 @@ def _search_virtual_box_posts(base_dir: str, nickname: str, domain: str,
search_words = [search_str] search_words = [search_str]
res = [] res = []
with open(index_filename, 'r') as index_file: with open(index_filename, 'r', encoding='utf-8') as index_file:
post_filename = 'start' post_filename = 'start'
while post_filename: while post_filename:
post_filename = index_file.readline() post_filename = index_file.readline()
@ -2405,7 +2417,7 @@ def _search_virtual_box_posts(base_dir: str, nickname: str, domain: str,
post_filename = path + '/' + post_filename.strip() post_filename = path + '/' + post_filename.strip()
if not os.path.isfile(post_filename): if not os.path.isfile(post_filename):
continue continue
with open(post_filename, 'r') as post_file: with open(post_filename, 'r', encoding='utf-8') as post_file:
data = post_file.read().lower() data = post_file.read().lower()
not_found = False not_found = False
@ -2449,7 +2461,7 @@ def search_box_posts(base_dir: str, nickname: str, domain: str,
for root, _, fnames in os.walk(path): for root, _, fnames in os.walk(path):
for fname in fnames: for fname in fnames:
file_path = os.path.join(root, fname) file_path = os.path.join(root, fname)
with open(file_path, 'r') as post_file: with open(file_path, 'r', encoding='utf-8') as post_file:
data = post_file.read().lower() data = post_file.read().lower()
not_found = False not_found = False
@ -2892,7 +2904,8 @@ def reject_post_id(base_dir: str, nickname: str, domain: str,
if recent_posts_cache['html'].get(post_url): if recent_posts_cache['html'].get(post_url):
del recent_posts_cache['html'][post_url] del recent_posts_cache['html'][post_url]
with open(post_filename + '.reject', 'w+') as reject_file: with open(post_filename + '.reject', 'w+',
encoding='utf-8') as reject_file:
reject_file.write('\n') reject_file.write('\n')
@ -3011,7 +3024,8 @@ def dm_allowed_from_domain(base_dir: str,
acct_dir(base_dir, nickname, domain) + '/dmAllowedInstances.txt' acct_dir(base_dir, nickname, domain) + '/dmAllowedInstances.txt'
if not os.path.isfile(dm_allowed_instances_file): if not os.path.isfile(dm_allowed_instances_file):
return False return False
if sending_actor_domain + '\n' in open(dm_allowed_instances_file).read(): if sending_actor_domain + '\n' in open(dm_allowed_instances_file,
encoding='utf-8').read():
return True return True
return False return False
@ -3325,7 +3339,8 @@ def is_group_actor(base_dir: str, actor: str, person_cache: {},
if debug: if debug:
print('Cached actor file not found ' + cached_actor_filename) print('Cached actor file not found ' + cached_actor_filename)
return False return False
if '"type": "Group"' in open(cached_actor_filename).read(): if '"type": "Group"' in open(cached_actor_filename,
encoding='utf-8').read():
if debug: if debug:
print('Group type found in ' + cached_actor_filename) print('Group type found in ' + cached_actor_filename)
return True return True
@ -3338,7 +3353,8 @@ def is_group_account(base_dir: str, nickname: str, domain: str) -> bool:
account_filename = acct_dir(base_dir, nickname, domain) + '.json' account_filename = acct_dir(base_dir, nickname, domain) + '.json'
if not os.path.isfile(account_filename): if not os.path.isfile(account_filename):
return False return False
if '"type": "Group"' in open(account_filename).read(): if '"type": "Group"' in open(account_filename,
encoding='utf-8').read():
return True return True
return False return False
@ -3596,7 +3612,7 @@ def load_account_timezones(base_dir: str) -> {}:
if not os.path.isfile(tz_filename): if not os.path.isfile(tz_filename):
continue continue
timezone = None timezone = None
with open(tz_filename, 'r') as fp_timezone: with open(tz_filename, 'r', encoding='utf-8') as fp_timezone:
timezone = fp_timezone.read().strip() timezone = fp_timezone.read().strip()
if timezone: if timezone:
nickname = acct.split('@')[0] nickname = acct.split('@')[0]
@ -3632,7 +3648,7 @@ def get_account_timezone(base_dir: str, nickname: str, domain: str) -> str:
if not os.path.isfile(tz_filename): if not os.path.isfile(tz_filename):
return None return None
timezone = None timezone = None
with open(tz_filename, 'r') as fp_timezone: with open(tz_filename, 'r', encoding='utf-8') as fp_timezone:
timezone = fp_timezone.read().strip() timezone = fp_timezone.read().strip()
return timezone return timezone
@ -3644,7 +3660,7 @@ def set_account_timezone(base_dir: str, nickname: str, domain: str,
tz_filename = \ tz_filename = \
base_dir + '/accounts/' + nickname + '@' + domain + '/timezone.txt' base_dir + '/accounts/' + nickname + '@' + domain + '/timezone.txt'
timezone = timezone.strip() timezone = timezone.strip()
with open(tz_filename, 'w+') as fp_timezone: with open(tz_filename, 'w+', encoding='utf-8') as fp_timezone:
fp_timezone.write(timezone) fp_timezone.write(timezone)

View File

@ -32,7 +32,8 @@ def html_about(css_cache: {}, 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(base_dir + '/accounts/about.md'): if os.path.isfile(base_dir + '/accounts/about.md'):
with open(base_dir + '/accounts/about.md', 'r') as fp_about: with open(base_dir + '/accounts/about.md', 'r',
encoding='utf-8') as fp_about:
about_text = markdown_to_html(fp_about.read()) about_text = markdown_to_html(fp_about.read())
about_form = '' about_form = ''

View File

@ -218,7 +218,7 @@ def get_left_column_content(base_dir: str, nickname: str, domain_full: str,
links_file_contains_entries = False links_file_contains_entries = False
links_list = None links_list = None
if os.path.isfile(links_filename): if os.path.isfile(links_filename):
with open(links_filename, 'r') as fp_links: with open(links_filename, 'r', encoding='utf-8') as fp_links:
links_list = fp_links.readlines() links_list = fp_links.readlines()
if not front_page: if not front_page:

View File

@ -372,7 +372,7 @@ def html_citations(base_dir: str, nickname: str, domain: str,
citations_selected = [] citations_selected = []
if os.path.isfile(citations_filename): if os.path.isfile(citations_filename):
citations_separator = '#####' citations_separator = '#####'
with open(citations_filename, 'r') as fp_cit: with open(citations_filename, 'r', encoding='utf-8') as fp_cit:
citations = fp_cit.readlines() citations = fp_cit.readlines()
for line in citations: for line in citations:
if citations_separator not in line: if citations_separator not in line:
@ -611,7 +611,7 @@ def html_edit_newswire(css_cache: {}, translate: {}, base_dir: str, path: str,
newswire_filename = base_dir + '/accounts/newswire.txt' newswire_filename = base_dir + '/accounts/newswire.txt'
newswire_str = '' newswire_str = ''
if os.path.isfile(newswire_filename): if os.path.isfile(newswire_filename):
with open(newswire_filename, 'r') as fp_news: with open(newswire_filename, 'r', encoding='utf-8') as fp_news:
newswire_str = fp_news.read() newswire_str = fp_news.read()
edit_newswire_form += \ edit_newswire_form += \
@ -633,7 +633,7 @@ def html_edit_newswire(css_cache: {}, translate: {}, base_dir: str, path: str,
filter_filename = \ filter_filename = \
base_dir + '/accounts/news@' + domain + '/filters.txt' base_dir + '/accounts/news@' + domain + '/filters.txt'
if os.path.isfile(filter_filename): if os.path.isfile(filter_filename):
with open(filter_filename, 'r') as filterfile: with open(filter_filename, 'r', encoding='utf-8') as filterfile:
filter_str = filterfile.read() filter_str = filterfile.read()
edit_newswire_form += \ edit_newswire_form += \
@ -649,7 +649,7 @@ def html_edit_newswire(css_cache: {}, translate: {}, base_dir: str, path: str,
hashtag_rules_filename = \ hashtag_rules_filename = \
base_dir + '/accounts/hashtagrules.txt' base_dir + '/accounts/hashtagrules.txt'
if os.path.isfile(hashtag_rules_filename): if os.path.isfile(hashtag_rules_filename):
with open(hashtag_rules_filename, 'r') as rulesfile: with open(hashtag_rules_filename, 'r', encoding='utf-8') as rulesfile:
hashtag_rules_str = rulesfile.read() hashtag_rules_str = rulesfile.read()
edit_newswire_form += \ edit_newswire_form += \

View File

@ -42,7 +42,8 @@ def _html_following_data_list(base_dir: str, nickname: str,
acct_dir(base_dir, nickname, domain) + '/following.txt' acct_dir(base_dir, nickname, domain) + '/following.txt'
msg = None msg = None
if os.path.isfile(following_filename): if os.path.isfile(following_filename):
with open(following_filename, 'r') as following_file: with open(following_filename, 'r',
encoding='utf-8') as following_file:
msg = following_file.read() msg = following_file.read()
# 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
@ -53,7 +54,8 @@ def _html_following_data_list(base_dir: str, nickname: str,
acct_dir(base_dir, nickname, domain) + '/petnames.txt' acct_dir(base_dir, nickname, domain) + '/petnames.txt'
if os.path.isfile(petnames_filename): if os.path.isfile(petnames_filename):
following_list = [] following_list = []
with open(petnames_filename, 'r') as petnames_file: with open(petnames_filename, 'r',
encoding='utf-8') as petnames_file:
pet_str = petnames_file.read() pet_str = petnames_file.read()
# extract each petname and append it # extract each petname and append it
petnames_list = pet_str.split('\n') petnames_list = pet_str.split('\n')
@ -322,7 +324,8 @@ def html_new_post(css_cache: {}, media_instance: bool, translate: {},
# custom report header with any additional instructions # custom report header with any additional instructions
if os.path.isfile(base_dir + '/accounts/report.txt'): if os.path.isfile(base_dir + '/accounts/report.txt'):
with open(base_dir + '/accounts/report.txt', 'r') as file: with open(base_dir + '/accounts/report.txt', 'r',
encoding='utf-8') as file:
custom_report_text = file.read() custom_report_text = file.read()
if '</p>' not in custom_report_text: if '</p>' not in custom_report_text:
custom_report_text = \ custom_report_text = \
@ -359,7 +362,8 @@ def html_new_post(css_cache: {}, media_instance: bool, translate: {},
'</h1>\n' '</h1>\n'
if os.path.isfile(base_dir + '/accounts/newpost.txt'): if os.path.isfile(base_dir + '/accounts/newpost.txt'):
with open(base_dir + '/accounts/newpost.txt', 'r') as file: with open(base_dir + '/accounts/newpost.txt', 'r',
encoding='utf-8') as file:
new_post_text = \ new_post_text = \
'<p>' + file.read() + '</p>\n' '<p>' + file.read() + '</p>\n'
@ -617,7 +621,7 @@ def html_new_post(css_cache: {}, media_instance: bool, translate: {},
translate['Citations'] + ':</label></p>\n' translate['Citations'] + ':</label></p>\n'
citations_str += ' <ul>\n' citations_str += ' <ul>\n'
citations_separator = '#####' citations_separator = '#####'
with open(citations_filename, 'r') as cit_file: with open(citations_filename, 'r', encoding='utf-8') as cit_file:
citations = cit_file.readlines() citations = cit_file.readlines()
for line in citations: for line in citations:
if citations_separator not in line: if citations_separator not in line:

View File

@ -79,7 +79,8 @@ def html_hash_tag_swarm(base_dir: str, actor: str, translate: {}) -> str:
blocked_str = '' blocked_str = ''
global_blocking_filename = base_dir + '/accounts/blocking.txt' global_blocking_filename = base_dir + '/accounts/blocking.txt'
if os.path.isfile(global_blocking_filename): if os.path.isfile(global_blocking_filename):
with open(global_blocking_filename, 'r') as fp_block: with open(global_blocking_filename, 'r',
encoding='utf-8') as fp_block:
blocked_str = fp_block.read() blocked_str = fp_block.read()
for _, _, files in os.walk(base_dir + '/tags'): for _, _, files in os.walk(base_dir + '/tags'):
@ -112,13 +113,13 @@ def html_hash_tag_swarm(base_dir: str, actor: str, translate: {}) -> str:
continue continue
if '#' + hash_tag_name + '\n' in blocked_str: if '#' + hash_tag_name + '\n' in blocked_str:
continue continue
with open(tags_filename, 'r') as fp_tags: with open(tags_filename, 'r', encoding='utf-8') as fp_tags:
# only read one line, which saves time and memory # only read one line, which saves time and memory
last_tag = fp_tags.readline() last_tag = fp_tags.readline()
if not last_tag.startswith(days_since_epoch_str): if not last_tag.startswith(days_since_epoch_str):
if not last_tag.startswith(days_since_epoch_str2): if not last_tag.startswith(days_since_epoch_str2):
continue continue
with open(tags_filename, 'r') as fp_tags: with open(tags_filename, 'r', encoding='utf-8') as fp_tags:
while True: while True:
line = fp_tags.readline() line = fp_tags.readline()
if not line: if not line:

View File

@ -113,7 +113,8 @@ def html_login(css_cache: {}, translate: {},
'</p>' '</p>'
if os.path.isfile(base_dir + '/accounts/login.txt'): if os.path.isfile(base_dir + '/accounts/login.txt'):
# custom login message # custom login message
with open(base_dir + '/accounts/login.txt', 'r') as file: with open(base_dir + '/accounts/login.txt', 'r',
encoding='utf-8') as file:
login_text = '<p class="login-text">' + file.read() + '</p>' login_text = '<p class="login-text">' + file.read() + '</p>'
css_filename = base_dir + '/epicyon-login.css' css_filename = base_dir + '/epicyon-login.css'

View File

@ -17,7 +17,8 @@ def load_peertube_instances(base_dir: str, peertube_instances: []) -> None:
peertube_list = None peertube_list = None
peertube_instances_filename = base_dir + '/accounts/peertube.txt' peertube_instances_filename = base_dir + '/accounts/peertube.txt'
if os.path.isfile(peertube_instances_filename): if os.path.isfile(peertube_instances_filename):
with open(peertube_instances_filename, 'r') as fp_inst: with open(peertube_instances_filename, 'r',
encoding='utf-8') as fp_inst:
peertube_str = fp_inst.read() peertube_str = fp_inst.read()
if peertube_str: if peertube_str:
peertube_str = peertube_str.replace('\r', '') peertube_str = peertube_str.replace('\r', '')

View File

@ -40,7 +40,7 @@ def set_minimal(base_dir: str, domain: str, nickname: str,
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: try:
with open(minimal_filename, 'w+') as fp_min: with open(minimal_filename, 'w+', encoding='utf-8') as fp_min:
fp_min.write('\n') fp_min.write('\n')
except OSError: except OSError:
print('EX: unable to write minimal ' + minimal_filename) print('EX: unable to write minimal ' + minimal_filename)

View File

@ -394,7 +394,7 @@ def html_moderation_info(translate: {}, base_dir: str,
suspended_filename = base_dir + '/accounts/suspended.txt' suspended_filename = base_dir + '/accounts/suspended.txt'
if os.path.isfile(suspended_filename): if os.path.isfile(suspended_filename):
with open(suspended_filename, 'r') as fp_sus: with open(suspended_filename, 'r', encoding='utf-8') as fp_sus:
suspended_str = fp_sus.read() suspended_str = fp_sus.read()
info_form += '<div class="container">\n' info_form += '<div class="container">\n'
info_form += ' <br><b>' + \ info_form += ' <br><b>' + \
@ -410,7 +410,7 @@ def html_moderation_info(translate: {}, base_dir: str,
blocking_filename = base_dir + '/accounts/blocking.txt' blocking_filename = base_dir + '/accounts/blocking.txt'
if os.path.isfile(blocking_filename): if os.path.isfile(blocking_filename):
with open(blocking_filename, 'r') as fp_block: with open(blocking_filename, 'r', encoding='utf-8') as fp_block:
blocked_lines = fp_block.readlines() blocked_lines = fp_block.readlines()
blocked_str = '' blocked_str = ''
if blocked_lines: if blocked_lines:
@ -436,7 +436,7 @@ def html_moderation_info(translate: {}, base_dir: str,
filters_filename = base_dir + '/accounts/filters.txt' filters_filename = base_dir + '/accounts/filters.txt'
if os.path.isfile(filters_filename): if os.path.isfile(filters_filename):
with open(filters_filename, 'r') as fp_filt: with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
filtered_str = fp_filt.read() filtered_str = fp_filt.read()
info_form += '<div class="container">\n' info_form += '<div class="container">\n'
info_form += \ info_form += \

View File

@ -444,7 +444,8 @@ def html_person_options(default_timeline: str,
acct_dir(base_dir, nickname, domain) + \ acct_dir(base_dir, nickname, domain) + \
'/notes/' + handle + '.txt' '/notes/' + handle + '.txt'
if os.path.isfile(person_notes_filename): if os.path.isfile(person_notes_filename):
with open(person_notes_filename, 'r') as fp_notes: with open(person_notes_filename, 'r',
encoding='utf-8') as fp_notes:
person_notes = fp_notes.read() person_notes = fp_notes.read()
options_str += \ options_str += \

View File

@ -302,7 +302,7 @@ def _save_individual_post_as_html_to_cache(base_dir: str,
os.mkdir(html_post_cache_dir) os.mkdir(html_post_cache_dir)
try: try:
with open(cached_post_filename, 'w+') as fp_cache: with open(cached_post_filename, 'w+', encoding='utf-8') as fp_cache:
fp_cache.write(post_html) fp_cache.write(post_html)
return True return True
except Exception as ex: except Exception as ex:
@ -1529,7 +1529,8 @@ def _substitute_onion_domains(base_dir: str, content: str) -> str:
if os.path.isfile(onion_domains_filename): if os.path.isfile(onion_domains_filename):
onion_domains_list = [] onion_domains_list = []
try: try:
with open(onion_domains_filename, 'r') as fp_onions: with open(onion_domains_filename, 'r',
encoding='utf-8') as fp_onions:
onion_domains_list = fp_onions.readlines() onion_domains_list = fp_onions.readlines()
except OSError: except OSError:
print('EX: unable to load onion domains file ' + print('EX: unable to load onion domains file ' +
@ -1808,7 +1809,8 @@ def individual_post_as_html(signing_priv_key_pem: str,
translate, post_json_object['actor'], translate, post_json_object['actor'],
theme_name, system_language, theme_name, system_language,
box_name) box_name)
with open(announce_filename + '.tts', 'w+') as ttsfile: with open(announce_filename + '.tts', 'w+',
encoding='utf-8') as ttsfile:
ttsfile.write('\n') ttsfile.write('\n')
is_announced = True is_announced = True

View File

@ -780,7 +780,8 @@ def html_profile(signing_priv_key_pem: str,
follow_requests_filename = \ follow_requests_filename = \
acct_dir(base_dir, nickname, domain) + '/followrequests.txt' acct_dir(base_dir, nickname, domain) + '/followrequests.txt'
if os.path.isfile(follow_requests_filename): if os.path.isfile(follow_requests_filename):
with open(follow_requests_filename, 'r') as foll_file: with open(follow_requests_filename, 'r',
encoding='utf-8') as foll_file:
for line in foll_file: for line in foll_file:
if len(line) > 0: if len(line) > 0:
follow_approvals = True follow_approvals = True
@ -792,7 +793,8 @@ def html_profile(signing_priv_key_pem: str,
if follow_approvals: if follow_approvals:
curr_follower_domains = \ curr_follower_domains = \
get_follower_domains(base_dir, nickname, domain) get_follower_domains(base_dir, nickname, domain)
with open(follow_requests_filename, 'r') as req_file: with open(follow_requests_filename, 'r',
encoding='utf-8') as req_file:
for follower_handle in req_file: for follower_handle in req_file:
if len(follower_handle) > 0: if len(follower_handle) > 0:
follower_handle = follower_handle.replace('\n', '') follower_handle = follower_handle.replace('\n', '')
@ -890,7 +892,7 @@ 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):
with open(pinned_filename, 'r') as pin_file: with open(pinned_filename, 'r', encoding='utf-8') as pin_file:
pinned_content = pin_file.read() pinned_content = pin_file.read()
profile_header_str = \ profile_header_str = \
@ -1476,7 +1478,7 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
moderators = '' moderators = ''
moderators_file = base_dir + '/accounts/moderators.txt' moderators_file = base_dir + '/accounts/moderators.txt'
if os.path.isfile(moderators_file): if os.path.isfile(moderators_file):
with open(moderators_file, 'r') as mod_file: with open(moderators_file, 'r', encoding='utf-8') as mod_file:
moderators = mod_file.read() moderators = mod_file.read()
# site moderators # site moderators
role_assign_str = \ role_assign_str = \
@ -1494,7 +1496,7 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
editors = '' editors = ''
editors_file = base_dir + '/accounts/editors.txt' editors_file = base_dir + '/accounts/editors.txt'
if os.path.isfile(editors_file): if os.path.isfile(editors_file):
with open(editors_file, 'r') as edit_file: with open(editors_file, 'r', encoding='utf-8') as edit_file:
editors = edit_file.read() editors = edit_file.read()
role_assign_str += \ role_assign_str += \
' <b><label class="labels">' + \ ' <b><label class="labels">' + \
@ -1509,7 +1511,7 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
counselors = '' counselors = ''
counselors_file = base_dir + '/accounts/counselors.txt' counselors_file = base_dir + '/accounts/counselors.txt'
if os.path.isfile(counselors_file): if os.path.isfile(counselors_file):
with open(counselors_file, 'r') as co_file: with open(counselors_file, 'r', encoding='utf-8') as co_file:
counselors = co_file.read() counselors = co_file.read()
role_assign_str += \ role_assign_str += \
edit_text_area(translate['Counselors'], 'counselors', counselors, edit_text_area(translate['Counselors'], 'counselors', counselors,
@ -1519,7 +1521,7 @@ def _html_edit_profile_instance(base_dir: str, translate: {},
artists = '' artists = ''
artists_file = base_dir + '/accounts/artists.txt' artists_file = base_dir + '/accounts/artists.txt'
if os.path.isfile(artists_file): if os.path.isfile(artists_file):
with open(artists_file, 'r') as art_file: with open(artists_file, 'r', encoding='utf-8') as art_file:
artists = art_file.read() artists = art_file.read()
role_assign_str += \ role_assign_str += \
edit_text_area(translate['Artists'], 'artists', artists, edit_text_area(translate['Artists'], 'artists', artists,
@ -1633,7 +1635,7 @@ 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):
with open(git_projects_filename, 'r') as git_file: with open(git_projects_filename, 'r', encoding='utf-8') as git_file:
git_projects_str = git_file.read() git_projects_str = git_file.read()
edit_profile_form = begin_edit_section(translate['Git Projects']) edit_profile_form = begin_edit_section(translate['Git Projects'])
@ -1678,56 +1680,58 @@ 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):
with open(filter_filename, 'r') as filterfile: with open(filter_filename, 'r', encoding='utf-8') as filterfile:
filter_str = filterfile.read() filter_str = filterfile.read()
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):
with open(filter_bio_filename, 'r') as filterfile: with open(filter_bio_filename, 'r', encoding='utf-8') as filterfile:
filter_bio_str = filterfile.read() filter_bio_str = filterfile.read()
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):
with open(switch_filename, 'r') as switchfile: with open(switch_filename, 'r', encoding='utf-8') as switchfile:
switch_str = switchfile.read() switch_str = switchfile.read()
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):
with open(auto_tags_filename, 'r') as auto_file: with open(auto_tags_filename, 'r', encoding='utf-8') as auto_file:
auto_tags = auto_file.read() auto_tags = auto_file.read()
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):
with open(auto_cw_filename, 'r') as cw_file: with open(auto_cw_filename, 'r', encoding='utf-8') as cw_file:
auto_cw = cw_file.read() auto_cw = cw_file.read()
blocked_str = '' blocked_str = ''
blocked_filename = \ blocked_filename = \
acct_dir(base_dir, nickname, domain) + '/blocking.txt' acct_dir(base_dir, nickname, domain) + '/blocking.txt'
if os.path.isfile(blocked_filename): if os.path.isfile(blocked_filename):
with open(blocked_filename, 'r') as blockedfile: with open(blocked_filename, 'r', encoding='utf-8') as blockedfile:
blocked_str = blockedfile.read() blocked_str = blockedfile.read()
dm_allowed_instances_str = '' dm_allowed_instances_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):
with open(dm_allowed_instances_filename, 'r') as dm_file: with open(dm_allowed_instances_filename, 'r',
encoding='utf-8') as dm_file:
dm_allowed_instances_str = dm_file.read() dm_allowed_instances_str = dm_file.read()
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):
with open(allowed_instances_filename, 'r') as allow_file: with open(allowed_instances_filename, 'r',
encoding='utf-8') as allow_file:
allowed_instances_str = allow_file.read() allowed_instances_str = allow_file.read()
edit_profile_form = begin_edit_section(translate['Filtering and Blocking']) edit_profile_form = begin_edit_section(translate['Filtering and Blocking'])
@ -1748,13 +1752,13 @@ 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):
with open(city_filename, 'r') as city_file: with open(city_filename, 'r', encoding='utf-8') as city_file:
city = city_file.read().replace('\n', '') city = city_file.read().replace('\n', '')
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'
cities = [] cities = []
with open(locations_filename, 'r') as loc_file: with open(locations_filename, 'r', encoding='utf-8') as loc_file:
cities = loc_file.readlines() cities = loc_file.readlines()
cities.sort() cities.sort()
edit_profile_form += ' <select id="cityDropdown" ' + \ edit_profile_form += ' <select id="cityDropdown" ' + \

View File

@ -34,7 +34,7 @@ def insert_question(base_dir: str, translate: {},
show_question_results = False show_question_results = False
if os.path.isfile(votes_filename): if os.path.isfile(votes_filename):
if message_id in open(votes_filename).read(): if message_id in open(votes_filename, encoding='utf-8').read():
show_question_results = True show_question_results = True
if not show_question_results: if not show_question_results:

View File

@ -442,7 +442,8 @@ def html_search(translate: {}, base_dir: str, path: str, domain: str,
swarm_str = '' swarm_str = ''
if os.path.isfile(cached_hashtag_swarm_filename): if os.path.isfile(cached_hashtag_swarm_filename):
try: try:
with open(cached_hashtag_swarm_filename, 'r') as fp_swarm: with open(cached_hashtag_swarm_filename, 'r',
encoding='utf-8') as fp_swarm:
swarm_str = fp_swarm.read() swarm_str = fp_swarm.read()
except OSError: except OSError:
print('EX: html_search unable to read cached hashtag swarm ' + print('EX: html_search unable to read cached hashtag swarm ' +
@ -451,7 +452,8 @@ def html_search(translate: {}, base_dir: str, path: str, domain: 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: try:
with open(cached_hashtag_swarm_filename, 'w+') as fp_hash: with open(cached_hashtag_swarm_filename, 'w+',
encoding='utf-8') as fp_hash:
fp_hash.write(swarm_str) fp_hash.write(swarm_str)
except OSError: except OSError:
print('EX: html_search unable to save cached hashtag swarm ' + print('EX: html_search unable to save cached hashtag swarm ' +
@ -757,7 +759,7 @@ def html_hashtag_search(nickname: str, domain: str, port: int,
nickname = None nickname = None
# read the index # read the index
with open(hashtag_index_file, 'r') as fp_hash: with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines() lines = fp_hash.readlines()
# read the css # read the css
@ -947,7 +949,7 @@ def rss_hashtag_search(nickname: str, domain: str, port: int,
# read the index # read the index
lines = [] lines = []
with open(hashtag_index_file, 'r') as fp_hash: with open(hashtag_index_file, 'r', encoding='utf-8') as fp_hash:
lines = fp_hash.readlines() lines = fp_hash.readlines()
if not lines: if not lines:
return None return None

View File

@ -86,7 +86,7 @@ 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'
with open(help_filename, 'r') as help_file: with open(help_filename, 'r', encoding='utf-8') as help_file:
help_text = help_file.read() help_text = help_file.read()
if dangerous_markup(help_text, False): if dangerous_markup(help_text, False):
return '' return ''
@ -512,7 +512,7 @@ def html_timeline(css_cache: {}, 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'
with open(calendar_file, 'r') as calfile: with open(calendar_file, 'r', encoding='utf-8') as calfile:
calendar_path = calfile.read().replace('##sent##', '') calendar_path = calfile.read().replace('##sent##', '')
calendar_path = calendar_path.replace('\n', '').replace('\r', '') calendar_path = calendar_path.replace('\n', '').replace('\r', '')
if '/calendar' not in calendar_path: if '/calendar' not in calendar_path:
@ -670,7 +670,8 @@ def html_timeline(css_cache: {}, default_timeline: str,
follow_requests_filename = \ follow_requests_filename = \
acct_dir(base_dir, nickname, domain) + '/followrequests.txt' acct_dir(base_dir, nickname, domain) + '/followrequests.txt'
if os.path.isfile(follow_requests_filename): if os.path.isfile(follow_requests_filename):
with open(follow_requests_filename, 'r') as foll_file: with open(follow_requests_filename, 'r',
encoding='utf-8') as foll_file:
for line in foll_file: for line in foll_file:
if len(line) > 0: if len(line) > 0:
# show follow approvals icon # show follow approvals icon

View File

@ -32,7 +32,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(base_dir + '/accounts/tos.md'): if os.path.isfile(base_dir + '/accounts/tos.md'):
with open(base_dir + '/accounts/tos.md', 'r') as file: with open(base_dir + '/accounts/tos.md', 'r',
encoding='utf-8') as file:
tos_text = markdown_to_html(file.read()) tos_text = markdown_to_html(file.read())
tos_form = '' tos_form = ''

View File

@ -45,7 +45,7 @@ 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
""" """
with open(following_filename, 'r') as following_file: with open(following_filename, 'r', encoding='utf-8') as following_file:
msg = following_file.read() msg = following_file.read()
following_list = msg.split('\n') following_list = msg.split('\n')
following_list.sort() following_list.sort()
@ -362,7 +362,7 @@ def scheduled_posts_exist(base_dir: str, nickname: str, domain: str) -> bool:
acct_dir(base_dir, nickname, domain) + '/schedule.index' acct_dir(base_dir, nickname, domain) + '/schedule.index'
if not os.path.isfile(schedule_index_filename): if not os.path.isfile(schedule_index_filename):
return False return False
if '#users#' in open(schedule_index_filename).read(): if '#users#' in open(schedule_index_filename, encoding='utf-8').read():
return True return True
return False return False
@ -578,7 +578,7 @@ def get_pwa_theme_colors(css_filename: str) -> (str, str):
return pwa_theme_color, pwa_theme_background_color return pwa_theme_color, pwa_theme_background_color
css_str = '' css_str = ''
with open(css_filename, 'r') as fp_css: with open(css_filename, 'r', encoding='utf-8') as fp_css:
css_str = fp_css.read() css_str = fp_css.read()
pwa_theme_color = \ pwa_theme_color = \
@ -963,7 +963,7 @@ 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') as file: with open(cached_post_filename, 'r', encoding='utf-8') as file:
post_html = file.read() post_html = file.read()
break break
except Exception as ex: except Exception as ex:
@ -1731,7 +1731,7 @@ def html_common_emoji(base_dir: str, no_of_emoji: int) -> str:
return '' return ''
common_emoji = None common_emoji = None
try: try:
with open(common_emoji_filename, 'r') as fp_emoji: with open(common_emoji_filename, 'r', encoding='utf-8') as fp_emoji:
common_emoji = fp_emoji.readlines() common_emoji = fp_emoji.readlines()
except OSError: except OSError:
print('EX: html_common_emoji unable to load file') print('EX: html_common_emoji unable to load file')

View File

@ -36,7 +36,7 @@ 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'
with open(complete_filename, 'w+') as fp_comp: with open(complete_filename, 'w+', encoding='utf-8') as fp_comp:
fp_comp.write('\n') fp_comp.write('\n')
@ -77,7 +77,7 @@ 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):
with open(welcome_filename, 'r') as fp_wel: with open(welcome_filename, 'r', encoding='utf-8') as fp_wel:
welcome_text = fp_wel.read() welcome_text = fp_wel.read()
welcome_text = welcome_text.replace('INSTANCE', instance_title) welcome_text = welcome_text.replace('INSTANCE', instance_title)
welcome_text = markdown_to_html(remove_html(welcome_text)) welcome_text = markdown_to_html(remove_html(welcome_text))

View File

@ -50,7 +50,7 @@ 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):
with open(final_filename, 'r') as final_file: with open(final_filename, 'r', encoding='utf-8') as final_file:
final_text = final_file.read() final_text = final_file.read()
final_text = final_text.replace('INSTANCE', instance_title) final_text = final_text.replace('INSTANCE', instance_title)
final_text = markdown_to_html(remove_html(final_text)) final_text = markdown_to_html(remove_html(final_text))

View File

@ -57,7 +57,7 @@ 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):
with open(profile_filename, 'r') as fp_pro: with open(profile_filename, 'r', encoding='utf-8') as fp_pro:
profile_text = fp_pro.read() profile_text = fp_pro.read()
profile_text = profile_text.replace('INSTANCE', instance_title) profile_text = profile_text.replace('INSTANCE', instance_title)
profile_text = markdown_to_html(remove_html(profile_text)) profile_text = markdown_to_html(remove_html(profile_text))