Replace file operations with functions

main
bashrc 2026-04-29 17:42:08 +01:00
parent 946c584b98
commit edd183ed2c
2 changed files with 105 additions and 91 deletions

View File

@ -17,6 +17,8 @@ from unicodetext import standardize_text
from unicodetext import remove_inverted_text
from unicodetext import remove_square_capitals
from data import append_string
from data import save_string
from data import load_list
def add_filter(base_dir: str, nickname: str, domain: str, words: str) -> bool:
@ -61,17 +63,23 @@ def remove_filter(base_dir: str, nickname: str, domain: str,
if not text_in_file(words, filters_filename):
return False
new_filters_filename = filters_filename + '.new'
try:
with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
with open(new_filters_filename, 'w+', encoding='utf-8') as fp_new:
for line in fp_filt:
filters_list: list[str] = \
load_list(filters_filename,
'EX: unable to remove filter ' +
filters_filename + ' 1 [ex]')
if filters_list is None:
return False
text: str = ''
for line in filters_list:
line = remove_eol(line)
if line != words:
fp_new.write(line + '\n')
except OSError as ex:
print('EX: unable to remove filter ' +
filters_filename + ' ' + str(ex))
return False
text += line + '\n'
save_string(text, new_filters_filename,
'EX: unable to remove filter ' +
filters_filename + ' 2 [ex]')
if os.path.isfile(new_filters_filename):
try:
os.rename(new_filters_filename, filters_filename)
@ -91,17 +99,23 @@ def remove_global_filter(base_dir: str, words: str) -> bool:
if not text_in_file(words, filters_filename):
return False
new_filters_filename = filters_filename + '.new'
try:
with open(filters_filename, 'r', encoding='utf-8') as fp_filt:
with open(new_filters_filename, 'w+', encoding='utf-8') as fp_new:
for line in fp_filt:
global_list: list[str] = \
load_list(filters_filename,
'EX: unable to remove global filter ' +
filters_filename + ' [ex]')
if global_list is None:
return False
text: str = ''
for line in global_list:
line = remove_eol(line)
if line != words:
fp_new.write(line + '\n')
except OSError as ex:
print('EX: unable to remove global filter ' +
filters_filename + ' ' + str(ex))
return False
text += line + '\n'
save_string(text, new_filters_filename,
'EX: unable to remove global filter ' +
filters_filename + ' 2 [ex]')
if os.path.isfile(new_filters_filename):
try:
os.rename(new_filters_filename, filters_filename)
@ -152,9 +166,11 @@ def _is_filtered_base(filename: str, content: str,
# convert any fancy characters to ordinary ones
content = standardize_text(content)
try:
with open(filename, 'r', encoding='utf-8') as fp_filt:
for line in fp_filt:
filtered_list: list[str] = \
load_list(filename,
'EX: _is_filtered_base ' + filename + ' [ex]')
if filtered_list is not None:
for line in filtered_list:
filter_str = remove_eol(line)
if not filter_str:
continue
@ -169,8 +185,6 @@ def _is_filtered_base(filename: str, content: str,
if not filtered_match(filter_wrd, content):
return False
return True
except OSError as ex:
print('EX: _is_filtered_base ' + filename + ' ' + str(ex))
return False

View File

@ -144,23 +144,25 @@ def _remove_from_follow_base(base_dir: str,
actor_found = True
if not actor_found:
return
try:
with open(approve_follows_filename + '.new', 'w+',
encoding='utf-8') as fp_approve_new:
with open(approve_follows_filename, 'r',
encoding='utf-8') as fp_approve:
text: str = ''
approve_follows_list: list[str] = \
load_list(approve_follows_filename,
'EX: _remove_from_follow_base ' +
approve_follows_filename + ' 2 [ex]')
if approve_follows_list is not None:
if not accept_deny_actor:
for approve_handle in fp_approve:
for approve_handle in approve_follows_list:
accept_deny_handle = accept_or_deny_handle
if not approve_handle.startswith(accept_deny_handle):
fp_approve_new.write(approve_handle)
text += approve_handle
else:
for approve_handle in fp_approve:
for approve_handle in approve_follows_list:
if accept_deny_actor not in approve_handle:
fp_approve_new.write(approve_handle)
except OSError as ex:
print('EX: _remove_from_follow_base ' +
approve_follows_filename + ' ' + str(ex))
text += approve_handle
save_string(text, approve_follows_filename + '.new',
'EX: _remove_from_follow_base ' +
approve_follows_filename + ' 1 [ex]')
try:
os.rename(approve_follows_filename + '.new',
@ -337,16 +339,15 @@ def unfollow_account(base_dir: str, nickname: str, domain: str,
return False
if lines:
try:
with open(filename, 'w+', encoding='utf-8') as fp_unfoll:
text: str = ''
for line in lines:
check_handle = line.strip("\n").strip("\r").lower()
if check_handle not in (handle_to_unfollow_lower,
'!' + handle_to_unfollow_lower):
fp_unfoll.write(line)
except OSError as ex:
print('EX: unfollow_account unable to write ' +
filename + ' ' + str(ex))
text += line
save_string(text, filename,
'EX: unfollow_account unable to write ' +
filename + ' [ex]')
# write to an unfollowed file so that if a follow accept
# later arrives then it can be ignored
@ -1593,10 +1594,12 @@ def pending_followers_timeline_json(actor: str, base_dir: str,
follow_requests_filename = \
acct_dir(base_dir, nickname, domain) + '/followrequests.txt'
if os.path.isfile(follow_requests_filename):
try:
with open(follow_requests_filename, 'r',
encoding='utf-8') as fp_req:
for follower_handle in fp_req:
follow_requests_list: list[str] = \
load_list(follow_requests_filename,
'EX: unable to read follow requests ' +
follow_requests_filename + ' [ex]')
if follow_requests_list is not None:
for follower_handle in follow_requests_list:
if len(follower_handle) == 0:
continue
follower_handle = remove_eol(follower_handle)
@ -1616,7 +1619,4 @@ def pending_followers_timeline_json(actor: str, base_dir: str,
if not follow_json:
continue
result_json['orderedItems'].append(follow_json)
except OSError as exc:
print('EX: unable to read follow requests ' +
follow_requests_filename + ' ' + str(exc))
return result_json