Replace file operations with function

main
bashrc 2026-04-29 12:41:15 +01:00
parent 784808961a
commit 27ce1a8772
1 changed files with 47 additions and 47 deletions

View File

@ -120,9 +120,11 @@ 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', encoding='utf-8') as fp_post: data = load_string(post_filename,
data = fp_post.read().lower() 'EX: _search_virtual_box_posts ' +
'unable to read ' + post_filename)
if data is not None:
data = data.lower()
not_found: bool = False not_found: bool = False
for keyword in search_words: for keyword in search_words:
if keyword not in data: if keyword not in data:
@ -180,54 +182,52 @@ 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)
try: data = load_string(file_path,
with open(file_path, 'r', encoding='utf-8') as fp_post: 'EX: search_box_posts unable to read ' +
data = fp_post.read() file_path + ' [ex]')
data_lower = data.lower() if data is not None:
data_lower = data.lower()
not_found: bool = False not_found: bool = False
for keyword in search_words: for keyword in search_words:
if keyword not in data_lower: if keyword not in data_lower:
not_found = True not_found = True
break break
if not_found: if not_found:
continue
# if this is not an outbox/bookmarks search then is the
# post marked as being searchable?
# https://codeberg.org/fediverse/fep/
# src/branch/main/fep/268d/fep-268d.md
if check_searchable_by:
if '"searchableBy":' not in data:
continue continue
searchable_by = \
# if this is not an outbox/bookmarks search then is the data.split('"searchableBy":')[1].strip()
# post marked as being searchable? if searchable_by.startswith('['):
# https://codeberg.org/fediverse/fep/ searchable_by = searchable_by.split(']')[0]
# src/branch/main/fep/268d/fep-268d.md if '"' in searchable_by:
if check_searchable_by: searchable_by = searchable_by.split('"')[1]
if '"searchableBy":' not in data: elif "'" in searchable_by:
continue searchable_by = searchable_by.split("'")[1]
searchable_by = \ else:
data.split('"searchableBy":')[1].strip() continue
if searchable_by.startswith('['): if '#Public' not in searchable_by:
searchable_by = searchable_by.split(']')[0] if '/followers' in searchable_by and \
if '"' in searchable_by: following_list:
searchable_by = searchable_by.split('"')[1] if not _actor_in_searchable_by(searchable_by,
elif "'" in searchable_by: following_list):
searchable_by = searchable_by.split("'")[1] continue
elif '/mutuals' in searchable_by and mutuals_list:
if not _actor_in_searchable_by(searchable_by,
mutuals_list):
continue
else: else:
continue continue
if '#Public' not in searchable_by:
if '/followers' in searchable_by and \
following_list:
if not _actor_in_searchable_by(searchable_by,
following_list):
continue
elif '/mutuals' in searchable_by and mutuals_list:
if not _actor_in_searchable_by(searchable_by,
mutuals_list):
continue
else:
continue
res.append(file_path) res.append(file_path)
if len(res) >= max_results: if len(res) >= max_results:
return res return res
except OSError as exc:
print('EX: search_box_posts unable to read ' +
file_path + ' ' + str(exc))
break break
return res return res