From f0f612fba188d851e01cf15a30c5a40c4afd43db Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 10 Jun 2022 14:01:39 +0100 Subject: [PATCH] Replacing open statements --- daemon.py | 3 +- desktop_client.py | 6 +- epicyon.py | 3 +- filters.py | 4 +- follow.py | 19 +++--- git.py | 3 +- happening.py | 3 +- inbox.py | 2 +- manualapprove.py | 6 +- notifyOnPost.py | 3 +- person.py | 5 +- posts.py | 6 +- tests.py | 143 +++++++++++++++++++++------------------------ utils.py | 27 ++++----- webapp_question.py | 3 +- webapp_utils.py | 3 +- 16 files changed, 110 insertions(+), 129 deletions(-) diff --git a/daemon.py b/daemon.py index da56b2bec..3fa423167 100644 --- a/daemon.py +++ b/daemon.py @@ -251,6 +251,7 @@ from languages import set_actor_languages from languages import get_understood_languages from like import update_likes_collection from reaction import update_reaction_collection +from utils import text_in_file from utils import is_onion_request from utils import is_i2p_request from utils import get_account_timezone @@ -510,7 +511,7 @@ class PubServer(BaseHTTPRequestHandler): if os.path.isfile(votes_filename): # have we already voted on this? - if message_id in open(votes_filename, encoding='utf-8').read(): + if text_in_file(message_id, votes_filename): print('Already voted on message ' + message_id) return diff --git a/desktop_client.py b/desktop_client.py index 9407a72e1..2de71cf0a 100644 --- a/desktop_client.py +++ b/desktop_client.py @@ -16,6 +16,7 @@ import webbrowser import urllib.parse from pathlib import Path from random import randint +from utils import text_in_file from utils import disallow_announce from utils import disallow_reply from utils import get_base_content_from_post @@ -169,8 +170,7 @@ def _mark_post_as_read(actor: str, post_id: str, post_category: str) -> None: read_posts_dir = home_dir + '/.config/epicyon/' + handle read_posts_filename = read_posts_dir + '/' + post_category + '.txt' if os.path.isfile(read_posts_filename): - if post_id in open(read_posts_filename, - encoding='utf-8').read(): + if text_in_file(post_id, read_posts_filename): return try: # prepend to read posts file @@ -201,7 +201,7 @@ def _has_read_post(actor: str, post_id: str, post_category: str) -> bool: read_posts_dir = home_dir + '/.config/epicyon/' + handle read_posts_filename = read_posts_dir + '/' + post_category + '.txt' if os.path.isfile(read_posts_filename): - if post_id in open(read_posts_filename).read(): + if text_in_file(post_id, read_posts_filename): return True return False diff --git a/epicyon.py b/epicyon.py index cb5b1786f..d9b13445f 100644 --- a/epicyon.py +++ b/epicyon.py @@ -68,6 +68,7 @@ from tests import test_update_actor from tests import run_all_tests from auth import store_basic_credentials from auth import create_password +from utils import text_in_file from utils import remove_domain_port from utils import get_port_from_domain from utils import has_users_path @@ -2748,7 +2749,7 @@ def _command_options() -> None: sys.exit() password_file = base_dir + '/accounts/passwords' if os.path.isfile(password_file): - if nickname + ':' in open(password_file).read(): + if text_in_file(nickname + ':', password_file): store_basic_credentials(base_dir, nickname, new_password) print('Password for ' + nickname + ' was changed') else: diff --git a/filters.py b/filters.py index 226aff75d..14c95b2bd 100644 --- a/filters.py +++ b/filters.py @@ -17,7 +17,7 @@ def add_filter(base_dir: str, nickname: str, domain: str, words: str) -> bool: """ filters_filename = acct_dir(base_dir, nickname, domain) + '/filters.txt' if os.path.isfile(filters_filename): - if words in open(filters_filename, encoding='utf-8').read(): + if text_in_file(words, filters_filename): return False try: with open(filters_filename, 'a+', @@ -38,7 +38,7 @@ def add_global_filter(base_dir: str, words: str) -> bool: return False filters_filename = base_dir + '/accounts/filters.txt' if os.path.isfile(filters_filename): - if words in open(filters_filename, encoding='utf-8').read(): + if text_in_file(words, filters_filename): return False try: with open(filters_filename, 'a+', encoding='utf-8') as filters_file: diff --git a/follow.py b/follow.py index 0d2f89c07..e67b3e30c 100644 --- a/follow.py +++ b/follow.py @@ -95,8 +95,7 @@ def _pre_approved_follower(base_dir: str, account_dir = base_dir + '/accounts/' + handle approved_filename = account_dir + '/approved.txt' if os.path.isfile(approved_filename): - if approve_handle in open(approved_filename, - encoding='utf-8').read(): + if text_in_file(approve_handle, approved_filename): return True return False @@ -127,8 +126,7 @@ def _remove_from_follow_base(base_dir: str, for users_name in users_paths: accept_deny_actor = \ '://' + accept_deny_domain + users_name + accept_deny_nickname - if accept_deny_actor in open(approve_follows_filename, - encoding='utf-8').read(): + if text_in_file(accept_deny_actor, approve_follows_filename): actor_found = True break if not actor_found: @@ -186,7 +184,7 @@ def is_following_actor(base_dir: str, return False if actor.startswith('@'): actor = actor[1:] - if actor.lower() in open(following_file, encoding='utf-8').read().lower(): + if text_in_file(actor, following_file, False): return True following_nickname = get_nickname_from_actor(actor) if not following_nickname: @@ -196,8 +194,7 @@ def is_following_actor(base_dir: str, following_handle = \ get_full_domain(following_nickname + '@' + following_domain, following_port) - if following_handle.lower() in open(following_file, - encoding='utf-8').read().lower(): + if text_in_file(following_handle, following_file, False): return True return False @@ -687,8 +684,7 @@ def store_follow_request(base_dir: str, # should this follow be denied? deny_follows_filename = accounts_dir + '/followrejects.txt' if os.path.isfile(deny_follows_filename): - if approve_handle in open(deny_follows_filename, - encoding='utf-8').read(): + if text_in_file(approve_handle, deny_follows_filename): remove_from_follow_requests(base_dir, nickname_to_follow, domain_to_follow, approve_handle, debug) @@ -922,8 +918,7 @@ def send_follow_request(session, base_dir: str, unfollowed_filename = \ acct_dir(base_dir, nickname, domain) + '/unfollowed.txt' if os.path.isfile(unfollowed_filename): - if follow_handle in open(unfollowed_filename, - encoding='utf-8').read(): + if text_in_file(follow_handle, unfollowed_filename): unfollowed_file = None try: with open(unfollowed_filename, 'r', @@ -1402,7 +1397,7 @@ def get_followers_of_actor(base_dir: str, actor: str, debug: bool) -> {}: if debug: print('DEBUG: checking if ' + actor_handle + ' in ' + following_filename) - if actor_handle in open(following_filename).read(): + if text_in_file(actor_handle, following_filename): if debug: print('DEBUG: ' + account + ' follows ' + actor_handle) diff --git a/git.py b/git.py index e0d51faa4..3aed91254 100644 --- a/git.py +++ b/git.py @@ -11,6 +11,7 @@ import os import html from utils import acct_dir from utils import has_object_string_type +from utils import text_in_file def _git_format_content(content: str) -> str: @@ -38,7 +39,7 @@ def _get_git_project_name(base_dir: str, nickname: str, domain: str, return None subject_line_words = subject.lower().split(' ') for word in subject_line_words: - if word in open(git_projects_filename, encoding='utf-8').read(): + if text_in_file(word, git_projects_filename): return word return None diff --git a/happening.py b/happening.py index c96f16e21..ee2fae65f 100644 --- a/happening.py +++ b/happening.py @@ -166,8 +166,7 @@ def save_event_post(base_dir: str, handle: str, post_id: str, # Does this event post already exist within the calendar month? if os.path.isfile(calendar_filename): - if post_id in open(calendar_filename, - encoding='utf-8').read(): + if text_in_file(post_id, calendar_filename): # Event post already exists return False diff --git a/inbox.py b/inbox.py index a7bc55008..23ca785ee 100644 --- a/inbox.py +++ b/inbox.py @@ -449,7 +449,7 @@ def valid_inbox(base_dir: str, nickname: str, domain: str) -> bool: if not os.path.isfile(filename): print('filename: ' + filename) return False - if 'postNickname' in open(filename, encoding='utf-8').read(): + if text_in_file('postNickname', filename): print('queue file incorrectly saved to ' + filename) return False break diff --git a/manualapprove.py b/manualapprove.py index e78df3a7d..7977415ef 100644 --- a/manualapprove.py +++ b/manualapprove.py @@ -39,8 +39,7 @@ def manual_deny_follow_request(session, session_onion, session_i2p, # has this handle already been rejected? rejected_follows_filename = accounts_dir + '/followrejects.txt' if os.path.isfile(rejected_follows_filename): - if deny_handle in open(rejected_follows_filename, - encoding='utf-8').read(): + if text_in_file(deny_handle, rejected_follows_filename): remove_from_follow_requests(base_dir, nickname, domain, deny_handle, debug) print(deny_handle + @@ -308,8 +307,7 @@ def manual_approve_follow_request(session, session_onion, session_i2p, # only update the follow requests file if the follow is confirmed to be # in followers.txt - if approve_handle_full in open(followers_filename, - encoding='utf-8').read(): + if text_in_file(approve_handle_full, followers_filename): # mark this handle as approved for following _approve_follower_handle(account_dir, approve_handle) # update the follow requests with the handles not yet approved diff --git a/notifyOnPost.py b/notifyOnPost.py index 2a2013a7f..f98811eb3 100644 --- a/notifyOnPost.py +++ b/notifyOnPost.py @@ -113,5 +113,4 @@ def notify_when_person_posts(base_dir: str, nickname: str, domain: str, with open(notify_on_post_filename, 'w+', encoding='utf-8') as fp_notify: fp_notify.write('') - return handle + '\n' in open(notify_on_post_filename, - encoding='utf-8').read() + return text_in_file(handle + '\n', notify_on_post_filename) diff --git a/person.py b/person.py index 25b975aa9..5afced652 100644 --- a/person.py +++ b/person.py @@ -1391,7 +1391,7 @@ def is_person_snoozed(base_dir: str, nickname: str, domain: str, except OSError: print('EX: unable to write ' + snoozed_filename) - if snooze_actor + ' ' in open(snoozed_filename, encoding='utf-8').read(): + if text_in_file(snooze_actor + ' ', snoozed_filename): return True return False @@ -1406,8 +1406,7 @@ def person_snooze(base_dir: str, nickname: str, domain: str, return snoozed_filename = account_dir + '/snoozed.txt' if os.path.isfile(snoozed_filename): - if snooze_actor + ' ' in open(snoozed_filename, - encoding='utf-8').read(): + if text_in_file(snooze_actor + ' ', snoozed_filename): return try: with open(snoozed_filename, 'a+', encoding='utf-8') as snoozed_file: diff --git a/posts.py b/posts.py index 03a5ed389..bc7decdb4 100644 --- a/posts.py +++ b/posts.py @@ -4751,8 +4751,7 @@ def populate_replies_json(base_dir: str, nickname: str, domain: str, message_id2.replace('/', '#') + '.json' if os.path.isfile(search_filename): if authorized or \ - pub_str in open(search_filename, - encoding='utf-8').read(): + text_in_file(pub_str, search_filename): post_json_object = load_json(search_filename) if post_json_object: if post_json_object['object'].get('cc'): @@ -4779,8 +4778,7 @@ def populate_replies_json(base_dir: str, nickname: str, domain: str, message_id2.replace('/', '#') + '.json' if os.path.isfile(search_filename): if authorized or \ - pub_str in open(search_filename, - encoding='utf-8').read(): + text_in_file(pub_str, search_filename): # get the json of the reply and append it to # the collection post_json_object = load_json(search_filename) diff --git a/tests.py b/tests.py index 996ac489d..30e31e1fc 100644 --- a/tests.py +++ b/tests.py @@ -1419,7 +1419,7 @@ def test_post_message_between_servers(base_dir: str) -> None: bob_domain, None, None) for _ in range(20): - if 'likes' in open(outbox_post_filename, encoding='utf-8').read(): + if text_in_file('likes', outbox_post_filename): break time.sleep(1) @@ -1427,7 +1427,7 @@ def test_post_message_between_servers(base_dir: str) -> None: if alice_post_json: pprint(alice_post_json) - assert 'likes' in open(outbox_post_filename, encoding='utf-8').read() + assert text_in_file('likes', outbox_post_filename) print('\n\n*******************************************************') print("Bob reacts to Alice's post") @@ -1442,7 +1442,7 @@ def test_post_message_between_servers(base_dir: str) -> None: bob_domain, None, None) for _ in range(20): - if 'reactions' in open(outbox_post_filename, encoding='utf-8').read(): + if text_in_file('reactions', outbox_post_filename): break time.sleep(1) @@ -1451,7 +1451,7 @@ def test_post_message_between_servers(base_dir: str) -> None: pprint(alice_post_json) # TODO: fix reactions unit test -# assert 'reactions' in open(outbox_post_filename, encoding='utf-8').read() +# assert text_in_file('reactions', outbox_post_filename) print('\n\n*******************************************************') print("Bob repeats Alice's post") @@ -1638,17 +1638,14 @@ def test_follow_between_servers(base_dir: str) -> None: assert valid_inbox(bob_dir, 'bob', bob_domain) assert valid_inbox_filenames(bob_dir, 'bob', bob_domain, alice_domain, alice_port) - assert 'alice@' + alice_domain in open(bob_dir + '/accounts/bob@' + - bob_domain + - '/followers.txt', - encoding='utf-8').read() - assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' + - alice_domain + '/following.txt', - encoding='utf-8').read() - assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' + - alice_domain + - '/followingCalendar.txt', - encoding='utf-8').read() + assert text_in_file('alice@' + alice_domain, bob_dir + '/accounts/bob@' + + bob_domain + '/followers.txt') + assert text_in_file('bob@' + bob_domain, + alice_dir + '/accounts/alice@' + + alice_domain + '/following.txt') + assert text_in_file('bob@' + bob_domain, + alice_dir + '/accounts/alice@' + + alice_domain + '/followingCalendar.txt') assert not is_group_actor(alice_dir, bob_actor, alice_person_cache) assert not is_group_account(alice_dir, 'alice', alice_domain) @@ -1862,17 +1859,15 @@ def test_shared_items_federation(base_dir: str) -> None: assert valid_inbox(bob_dir, 'bob', bob_domain) assert valid_inbox_filenames(bob_dir, 'bob', bob_domain, alice_domain, alice_port) - assert 'alice@' + alice_domain in open(bob_dir + '/accounts/bob@' + - bob_domain + - '/followers.txt', - encoding='utf-8').read() - assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' + - alice_domain + '/following.txt', - encoding='utf-8').read() - assert 'bob@' + bob_domain in open(alice_dir + '/accounts/alice@' + - alice_domain + - '/followingCalendar.txt', - encoding='utf-8').read() + assert text_in_file('alice@' + alice_domain, + bob_dir + '/accounts/bob@' + + bob_domain + '/followers.txt') + assert text_in_file('bob@' + bob_domain, + alice_dir + '/accounts/alice@' + + alice_domain + '/following.txt') + assert text_in_file('bob@' + bob_domain, + alice_dir + '/accounts/alice@' + + alice_domain + '/followingCalendar.txt') assert not is_group_actor(alice_dir, bob_actor, alice_person_cache) assert not is_group_account(bob_dir, 'bob', bob_domain) @@ -2323,17 +2318,15 @@ def test_group_follow(base_dir: str) -> None: assert valid_inbox(testgroup_dir, 'testgroup', testgroup_domain) assert valid_inbox_filenames(testgroup_dir, 'testgroup', testgroup_domain, alice_domain, alice_port) - assert 'alice@' + alice_domain in open(testgroup_followers_filename, - encoding='utf-8').read() - assert '!alice@' + alice_domain not in \ - open(testgroup_followers_filename, encoding='utf-8').read() + assert text_in_file('alice@' + alice_domain, testgroup_followers_filename) + assert not text_in_file('!alice@' + alice_domain, + testgroup_followers_filename) testgroup_webfinger_filename = \ testgroup_dir + '/wfendpoints/testgroup@' + \ testgroup_domain + ':' + str(testgroupPort) + '.json' assert os.path.isfile(testgroup_webfinger_filename) - assert 'acct:testgroup@' in open(testgroup_webfinger_filename, - encoding='utf-8').read() + assert text_in_file('acct:testgroup@', testgroup_webfinger_filename) print('acct: exists within the webfinger endpoint for testgroup') testgroup_handle = 'testgroup@' + testgroup_domain @@ -2348,10 +2341,8 @@ def test_group_follow(base_dir: str) -> None: assert not is_group_account(alice_dir, 'alice', alice_domain) assert is_group_account(testgroup_dir, 'testgroup', testgroup_domain) assert '!testgroup' in following_str - assert testgroup_handle in open(alice_following_filename, - encoding='utf-8').read() - assert testgroup_handle in open(alice_following_calendar_filename, - encoding='utf-8').read() + assert text_in_file(testgroup_handle, alice_following_filename) + assert text_in_file(testgroup_handle, alice_following_calendar_filename) print('\n\n*********************************************************') print('Alice follows the test group') @@ -2405,17 +2396,15 @@ def test_group_follow(base_dir: str) -> None: assert valid_inbox(testgroup_dir, 'testgroup', testgroup_domain) assert valid_inbox_filenames(testgroup_dir, 'testgroup', testgroup_domain, bob_domain, bob_port) - assert 'bob@' + bob_domain in open(testgroup_followers_filename, - encoding='utf-8').read() - assert '!bob@' + bob_domain not in \ - open(testgroup_followers_filename, encoding='utf-8').read() + assert text_in_file('bob@' + bob_domain, testgroup_followers_filename) + assert not text_in_file('!bob@' + bob_domain, + testgroup_followers_filename) testgroup_webfinger_filename = \ testgroup_dir + '/wfendpoints/testgroup@' + \ testgroup_domain + ':' + str(testgroupPort) + '.json' assert os.path.isfile(testgroup_webfinger_filename) - assert 'acct:testgroup@' in open(testgroup_webfinger_filename, - encoding='utf-8').read() + assert text_in_file('acct:testgroup@', testgroup_webfinger_filename) print('acct: exists within the webfinger endpoint for testgroup') testgroup_handle = 'testgroup@' + testgroup_domain @@ -2428,10 +2417,8 @@ def test_group_follow(base_dir: str) -> None: testgroup_domain + ':' + str(testgroupPort)) assert is_group_actor(bob_dir, testgroup_actor, bob_person_cache) assert '!testgroup' in following_str - assert testgroup_handle in open(bob_following_filename, - encoding='utf-8').read() - assert testgroup_handle in open(bob_following_calendar_filename, - encoding='utf-8').read() + assert text_in_file(testgroup_handle, bob_following_filename) + assert text_in_file(testgroup_handle, bob_following_calendar_filename) print('Bob follows the test group') print('\n\n*********************************************************') @@ -3188,30 +3175,27 @@ def test_client_to_server(base_dir: str): bob_dir + '/accounts/bob@' + bob_domain + '/followers.txt' for _ in range(10): if os.path.isfile(bob_followers_filename): - if 'alice@' + alice_domain + ':' + str(alice_port) in \ - open(bob_followers_filename, - encoding='utf-8').read(): + test_str = 'alice@' + alice_domain + ':' + str(alice_port) + if text_in_file(test_str, bob_followers_filename): if os.path.isfile(alice_following_filename) and \ os.path.isfile(alice_petnames_filename): - if 'bob@' + bob_domain + ':' + str(bob_port) in \ - open(alice_following_filename, - encoding='utf-8').read(): + test_str = 'bob@' + bob_domain + ':' + str(bob_port) + if text_in_file(test_str, alice_following_filename): break time.sleep(1) assert os.path.isfile(bob_followers_filename) assert os.path.isfile(alice_following_filename) assert os.path.isfile(alice_petnames_filename) - assert 'bob bob@' + bob_domain in \ - open(alice_petnames_filename, encoding='utf-8').read() + assert text_in_file('bob bob@' + bob_domain, alice_petnames_filename) print('alice@' + alice_domain + ':' + str(alice_port) + ' in ' + bob_followers_filename) - assert 'alice@' + alice_domain + ':' + str(alice_port) in \ - open(bob_followers_filename, encoding='utf-8').read() + test_str = 'alice@' + alice_domain + ':' + str(alice_port) + assert text_in_file(test_str, bob_followers_filename) print('bob@' + bob_domain + ':' + str(bob_port) + ' in ' + alice_following_filename) - assert 'bob@' + bob_domain + ':' + str(bob_port) in \ - open(alice_following_filename, encoding='utf-8').read() + test_str = 'bob@' + bob_domain + ':' + str(bob_port) + assert text_in_file(test_str, alice_following_filename) assert valid_inbox(bob_dir, 'bob', bob_domain) assert valid_inbox_filenames(bob_dir, 'bob', bob_domain, alice_domain, alice_port) @@ -3227,23 +3211,25 @@ def test_client_to_server(base_dir: str): for _ in range(10): if os.path.isfile(alice_dir + '/accounts/alice@' + alice_domain + '/followers.txt'): - if 'bob@' + bob_domain + ':' + str(bob_port) in \ - open(alice_dir + '/accounts/alice@' + alice_domain + - '/followers.txt', encoding='utf-8').read(): + test_str = 'bob@' + bob_domain + ':' + str(bob_port) + test_filename = \ + alice_dir + '/accounts/alice@' + \ + alice_domain + '/followers.txt' + if text_in_file(test_str, test_filename): if os.path.isfile(bob_dir + '/accounts/bob@' + bob_domain + '/following.txt'): alice_handle_str = \ 'alice@' + alice_domain + ':' + str(alice_port) - if alice_handle_str in \ - open(bob_dir + '/accounts/bob@' + bob_domain + - '/following.txt', encoding='utf-8').read(): + if text_in_file(alice_handle_str, + bob_dir + '/accounts/bob@' + bob_domain + + '/following.txt'): if os.path.isfile(bob_dir + '/accounts/bob@' + bob_domain + '/followingCalendar.txt'): - if alice_handle_str in \ - open(bob_dir + '/accounts/bob@' + bob_domain + - '/followingCalendar.txt', - encoding='utf-8').read(): + if text_in_file(alice_handle_str, + bob_dir + '/accounts/bob@' + + bob_domain + + '/followingCalendar.txt'): break time.sleep(1) @@ -3251,12 +3237,13 @@ def test_client_to_server(base_dir: str): '/followers.txt') assert os.path.isfile(bob_dir + '/accounts/bob@' + bob_domain + '/following.txt') - assert 'bob@' + bob_domain + ':' + str(bob_port) in \ - open(alice_dir + '/accounts/alice@' + alice_domain + - '/followers.txt', encoding='utf-8').read() - assert 'alice@' + alice_domain + ':' + str(alice_port) in \ - open(bob_dir + '/accounts/bob@' + bob_domain + '/following.txt', - encoding='utf-8').read() + test_str = 'bob@' + bob_domain + ':' + str(bob_port) + assert text_in_file(test_str, alice_dir + '/accounts/alice@' + + alice_domain + '/followers.txt') + test_str = 'alice@' + alice_domain + ':' + str(alice_port) + assert text_in_file(test_str, + bob_dir + '/accounts/bob@' + + bob_domain + '/following.txt') session_bob = create_session(proxy_type) password = 'bobpass' @@ -3459,10 +3446,10 @@ def test_client_to_server(base_dir: str): cached_webfingers, person_cache, True, __version__, signing_priv_key_pem) for _ in range(10): - if 'alice@' + alice_domain + ':' + str(alice_port) not in \ - open(bob_followers_filename, encoding='utf-8').read(): - if 'bob@' + bob_domain + ':' + str(bob_port) not in \ - open(alice_following_filename, encoding='utf-8').read(): + test_str = 'alice@' + alice_domain + ':' + str(alice_port) + if not text_in_file(test_str, bob_followers_filename): + test_str = 'bob@' + bob_domain + ':' + str(bob_port) + if not text_in_file(test_str, alice_following_filename): break time.sleep(1) diff --git a/utils.py b/utils.py index d4ab98c05..88c1f4665 100644 --- a/utils.py +++ b/utils.py @@ -40,7 +40,8 @@ INVALID_CHARACTERS = ( ) -def text_in_file(text: str, filename: str, case_sensitive: bool = True) -> bool: +def text_in_file(text: str, filename: str, + case_sensitive: bool = True) -> bool: """is the given text in the given file? """ if not case_sensitive: @@ -127,32 +128,32 @@ def has_object_dict(post_json_object: {}) -> bool: def get_content_from_post(post_json_object: {}, system_language: str, languages_understood: [], - contentType: str = "content") -> str: + content_type: str = "content") -> str: """Returns the content from the post in the given language including searching for a matching entry within contentMap """ this_post_json = post_json_object if has_object_dict(post_json_object): this_post_json = post_json_object['object'] - if not this_post_json.get(contentType): + if not this_post_json.get(content_type): return '' content = '' - mapDict = contentType + 'Map' - if this_post_json.get(mapDict): - if isinstance(this_post_json[mapDict], dict): - if this_post_json[mapDict].get(system_language): - sys_lang = this_post_json[mapDict][system_language] + map_dict = content_type + 'Map' + if this_post_json.get(map_dict): + if isinstance(this_post_json[map_dict], dict): + if this_post_json[map_dict].get(system_language): + sys_lang = this_post_json[map_dict][system_language] if isinstance(sys_lang, str): - return this_post_json[mapDict][system_language] + return this_post_json[map_dict][system_language] else: # is there a contentMap/summaryMap entry for one of # the understood languages? for lang in languages_understood: - if this_post_json[mapDict].get(lang): - return this_post_json[mapDict][lang] + if this_post_json[map_dict].get(lang): + return this_post_json[map_dict][lang] else: - if isinstance(this_post_json[contentType], str): - content = this_post_json[contentType] + if isinstance(this_post_json[content_type], str): + content = this_post_json[content_type] return content diff --git a/webapp_question.py b/webapp_question.py index e8f636d52..a01b2f2a3 100644 --- a/webapp_question.py +++ b/webapp_question.py @@ -11,6 +11,7 @@ import os from question import is_question from utils import remove_id_ending from utils import acct_dir +from utils import text_in_file def insert_question(base_dir: str, translate: {}, @@ -34,7 +35,7 @@ def insert_question(base_dir: str, translate: {}, show_question_results = False if os.path.isfile(votes_filename): - if message_id in open(votes_filename, encoding='utf-8').read(): + if text_in_file(message_id, votes_filename): show_question_results = True if not show_question_results: diff --git a/webapp_utils.py b/webapp_utils.py index b48d07ec8..60f5e7f5c 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -26,6 +26,7 @@ from utils import get_audio_extensions from utils import get_video_extensions from utils import get_image_extensions from utils import local_actor_url +from utils import text_in_file from cache import store_person_in_cache from content import add_html_tags from content import replace_emoji_from_tags @@ -362,7 +363,7 @@ def scheduled_posts_exist(base_dir: str, nickname: str, domain: str) -> bool: acct_dir(base_dir, nickname, domain) + '/schedule.index' if not os.path.isfile(schedule_index_filename): return False - if '#users#' in open(schedule_index_filename, encoding='utf-8').read(): + if text_in_file('#users#', schedule_index_filename): return True return False