Replace open with text in file function

merge-requests/25/head
Bob Mottram 2022-06-10 14:29:51 +01:00
parent f0f612fba1
commit e1ac8f1ea1
6 changed files with 17 additions and 13 deletions

View File

@ -8,6 +8,7 @@ __status__ = "Production"
__module_group__ = "ActivityPub"
import os
from utils import text_in_file
from utils import has_object_string_object
from utils import has_users_path
from utils import get_full_domain
@ -164,8 +165,8 @@ def _accept_follow(base_dir: str, message_json: {},
unfollowed_filename = \
acct_dir(base_dir, nickname, accepted_domain_full) + '/unfollowed.txt'
if os.path.isfile(unfollowed_filename):
if followed_nickname + '@' + followed_domain_full in \
open(unfollowed_filename, encoding='utf-8').read():
if text_in_file(followed_nickname + '@' + followed_domain_full,
unfollowed_filename):
if debug:
print('DEBUG: follow accept arrived for ' +
nickname + '@' + accepted_domain_full +

View File

@ -60,8 +60,7 @@ def add_global_block(base_dir: str,
block_hashtag = block_nickname
# is the hashtag already blocked?
if os.path.isfile(blocking_filename):
if block_hashtag + '\n' in \
open(blocking_filename, encoding='utf-8').read():
if text_in_file(block_hashtag + '\n', blocking_filename):
return False
# block a hashtag
try:

View File

@ -340,8 +340,8 @@ def unfollow_account(base_dir: str, nickname: str, domain: str,
# later arrives then it can be ignored
unfollowed_filename = base_dir + '/accounts/' + handle + '/unfollowed.txt'
if os.path.isfile(unfollowed_filename):
if handle_to_unfollow_lower not in \
open(unfollowed_filename, encoding='utf-8').read().lower():
if not text_in_file(handle_to_unfollow_lower,
unfollowed_filename, False):
try:
with open(unfollowed_filename, 'a+',
encoding='utf-8') as fp_unfoll:

View File

@ -4569,8 +4569,7 @@ def _check_json_signature(base_dir: str, queue_json: {}) -> (bool, bool):
already_unknown = False
if os.path.isfile(unknown_contexts_file):
if unknown_context in \
open(unknown_contexts_file, encoding='utf-8').read():
if text_in_file(unknown_context, unknown_contexts_file):
already_unknown = True
if not already_unknown:
@ -4588,8 +4587,7 @@ def _check_json_signature(base_dir: str, queue_json: {}) -> (bool, bool):
already_unknown = False
if os.path.isfile(unknown_signatures_file):
if jwebsig_type in \
open(unknown_signatures_file, encoding='utf-8').read():
if text_in_file(jwebsig_type, unknown_signatures_file):
already_unknown = True
if not already_unknown:

View File

@ -4294,7 +4294,12 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
# Time of file creation
full_filename = os.path.join(box_dir, post_filename)
if os.path.isfile(full_filename):
content = open(full_filename, encoding='utf-8').read()
content = ''
try:
with open(full_filename, 'r', encoding='utf-8') as fp_content:
content = fp_content.read()
except OSError:
print('EX: unable to open content ' + full_filename)
if '"published":' in content:
published_str = content.split('"published":')[1]
if '"' in published_str:

View File

@ -17,6 +17,7 @@ from utils import acct_dir
from utils import dangerous_svg
from utils import local_actor_url
from utils import remove_html
from utils import text_in_file
from shutil import copyfile
from shutil import make_archive
from shutil import unpack_archive
@ -65,8 +66,8 @@ def import_theme(base_dir: str, filename: str) -> bool:
# if the theme name in the default themes list?
default_themes_filename = base_dir + '/defaultthemes.txt'
if os.path.isfile(default_themes_filename):
if new_theme_name.title() + '\n' in \
open(default_themes_filename, encoding='utf-8').read():
test_str = new_theme_name.title() + '\n'
if text_in_file(test_str, default_themes_filename):
new_theme_name = new_theme_name + '2'
theme_dir = base_dir + '/theme/' + new_theme_name