mirror of https://gitlab.com/bashrc2/epicyon
Replace file operations with functions
parent
c10291fa12
commit
5818c78c4e
39
migrate.py
39
migrate.py
|
|
@ -21,6 +21,8 @@ from posts import get_user_url
|
|||
from follow import unfollow_account
|
||||
from person import get_actor_json
|
||||
from data import load_list
|
||||
from data import save_string
|
||||
from data import append_string
|
||||
|
||||
|
||||
def _move_following_handles_for_account(base_dir: str,
|
||||
|
|
@ -168,11 +170,11 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
|
|||
acct_dir(base_dir, nickname, domain) + '/refollow.txt'
|
||||
|
||||
# unfollow the old handle
|
||||
with open(following_filename, 'w+', encoding='utf-8') as fp_foll2:
|
||||
text = ''
|
||||
for follow_handle in following_handles:
|
||||
if follow_handle.strip("\n").strip("\r").lower() != \
|
||||
handle_lower:
|
||||
fp_foll2.write(follow_handle)
|
||||
text += follow_handle
|
||||
continue
|
||||
handle_nickname = handle.split('@')[0]
|
||||
handle_domain = handle.split('@')[1]
|
||||
|
|
@ -186,22 +188,17 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
|
|||
|
||||
# save the new handles to the refollow list
|
||||
if os.path.isfile(refollow_filename):
|
||||
try:
|
||||
with open(refollow_filename, 'a+',
|
||||
encoding='utf-8') as fp_refoll:
|
||||
fp_refoll.write(moved_to_handle + '\n')
|
||||
except OSError:
|
||||
print('EX: ' +
|
||||
append_string(moved_to_handle + '\n', refollow_filename,
|
||||
'EX: ' +
|
||||
'_update_moved_handle unable to append ' +
|
||||
refollow_filename)
|
||||
else:
|
||||
try:
|
||||
with open(refollow_filename, 'w+',
|
||||
encoding='utf-8') as fp_refoll:
|
||||
fp_refoll.write(moved_to_handle + '\n')
|
||||
except OSError:
|
||||
print('EX: _update_moved_handle unable to write ' +
|
||||
save_string(moved_to_handle + '\n', refollow_filename,
|
||||
'EX: _update_moved_handle unable to write ' +
|
||||
refollow_filename)
|
||||
save_string(text, following_filename,
|
||||
'EX: _update_moved_handle unable to save ' +
|
||||
following_filename)
|
||||
|
||||
followers_filename = \
|
||||
acct_dir(base_dir, nickname, domain) + '/followers.txt'
|
||||
|
|
@ -216,18 +213,16 @@ def _update_moved_handle(base_dir: str, nickname: str, domain: str,
|
|||
handle_lower = handle.lower()
|
||||
|
||||
# remove followers who have moved
|
||||
try:
|
||||
with open(followers_filename, 'w+', encoding='utf-8') as fp_foll4:
|
||||
text = ''
|
||||
for follower_handle in follower_handles:
|
||||
if follower_handle.strip("\n").strip("\r").lower() != \
|
||||
handle_lower:
|
||||
fp_foll4.write(follower_handle)
|
||||
if follower_handle.strip("\n").strip("\r").lower() != handle_lower:
|
||||
text += follower_handle
|
||||
else:
|
||||
ctr += 1
|
||||
print('Removed follower who has moved ' + handle)
|
||||
except OSError:
|
||||
print('EX: _update_moved_handle unable to remove moved follower ' +
|
||||
handle)
|
||||
save_string(text, followers_filename,
|
||||
'EX: _update_moved_handle ' +
|
||||
'unable to remove moved follower ' + handle)
|
||||
|
||||
return ctr
|
||||
|
||||
|
|
|
|||
|
|
@ -43,6 +43,9 @@ from threads import thread_with_trace
|
|||
from webapp_hashtagswarm import store_hash_tags
|
||||
from cache import clear_from_post_caches
|
||||
from data import load_list
|
||||
from data import load_string
|
||||
from data import save_string
|
||||
from data import append_string
|
||||
|
||||
|
||||
def _update_feeds_outbox_index(base_dir: str, domain: str,
|
||||
|
|
@ -67,23 +70,16 @@ def _update_feeds_outbox_index(base_dir: str, domain: str,
|
|||
index_filename + ' ' + str(ex))
|
||||
return
|
||||
|
||||
try:
|
||||
with open(index_filename, 'w+', encoding='utf-8') as fp_feeds:
|
||||
fp_feeds.write(post_id + '\n')
|
||||
except OSError:
|
||||
print('EX: _update_feeds_outbox_index unable to write ' +
|
||||
save_string(post_id + '\n', index_filename,
|
||||
'EX: _update_feeds_outbox_index unable to write ' +
|
||||
index_filename)
|
||||
|
||||
|
||||
def _save_arrived_time(post_filename: str, arrived: str) -> None:
|
||||
"""Saves the time when an rss post arrived to a file
|
||||
"""
|
||||
try:
|
||||
with open(post_filename + '.arrived', 'w+',
|
||||
encoding='utf-8') as fp_arrived:
|
||||
fp_arrived.write(arrived)
|
||||
except OSError:
|
||||
print('EX: _save_arrived_time unable to write ' +
|
||||
save_string(arrived, post_filename + '.arrived',
|
||||
'EX: _save_arrived_time unable to write ' +
|
||||
post_filename + '.arrived')
|
||||
|
||||
|
||||
|
|
@ -501,23 +497,16 @@ def _create_news_mirror(base_dir: str, domain: str,
|
|||
|
||||
# remove the corresponding index entries
|
||||
if removals:
|
||||
index_content = ''
|
||||
try:
|
||||
with open(mirror_index_filename, 'r',
|
||||
encoding='utf-8') as fp_index:
|
||||
index_content = fp_index.read()
|
||||
index_content: str = \
|
||||
load_string(mirror_index_filename,
|
||||
'EX: _create_news_mirror unable to read ' +
|
||||
mirror_index_filename)
|
||||
if index_content is not None:
|
||||
for remove_post_id in removals:
|
||||
index_content = \
|
||||
index_content.replace(remove_post_id + '\n', '')
|
||||
except OSError:
|
||||
print('EX: _create_news_mirror unable to read ' +
|
||||
mirror_index_filename)
|
||||
try:
|
||||
with open(mirror_index_filename, 'w+',
|
||||
encoding='utf-8') as fp_index:
|
||||
fp_index.write(index_content)
|
||||
except OSError:
|
||||
print('EX: _create_news_mirror unable to write ' +
|
||||
save_string(index_content, mirror_index_filename,
|
||||
'EX: _create_news_mirror unable to write ' +
|
||||
mirror_index_filename)
|
||||
|
||||
mirror_article_dir = mirror_dir + '/' + post_id_number
|
||||
|
|
@ -543,20 +532,12 @@ def _create_news_mirror(base_dir: str, domain: str,
|
|||
|
||||
# append the post Id number to the index file
|
||||
if os.path.isfile(mirror_index_filename):
|
||||
try:
|
||||
with open(mirror_index_filename, 'a+',
|
||||
encoding='utf-8') as fp_index:
|
||||
fp_index.write(post_id_number + '\n')
|
||||
except OSError:
|
||||
print('EX: _create_news_mirror unable to append ' +
|
||||
append_string(post_id_number + '\n', mirror_index_filename,
|
||||
'EX: _create_news_mirror unable to append ' +
|
||||
mirror_index_filename)
|
||||
else:
|
||||
try:
|
||||
with open(mirror_index_filename, 'w+',
|
||||
encoding='utf-8') as fp_index:
|
||||
fp_index.write(post_id_number + '\n')
|
||||
except OSError:
|
||||
print('EX: _create_news_mirror unable to write ' +
|
||||
save_string(post_id_number + '\n', mirror_index_filename,
|
||||
'EX: _create_news_mirror unable to write ' +
|
||||
mirror_index_filename)
|
||||
|
||||
return True
|
||||
|
|
|
|||
47
person.py
47
person.py
|
|
@ -1327,14 +1327,13 @@ def reenable_account(base_dir: str, nickname: str, domain: str) -> None:
|
|||
suspended_filename)
|
||||
if lines is None:
|
||||
return
|
||||
try:
|
||||
with open(suspended_filename, 'w+', encoding='utf-8') as fp_sus:
|
||||
text = ''
|
||||
for suspended in lines:
|
||||
if suspended.strip('\n').strip('\r') != nickname:
|
||||
fp_sus.write(suspended)
|
||||
except OSError as ex:
|
||||
print('EX: reenable_account unable to save ' +
|
||||
suspended_filename + ' ' + str(ex))
|
||||
text += suspended
|
||||
save_string(text, suspended_filename,
|
||||
'EX: reenable_account unable to save ' +
|
||||
suspended_filename + ' [ex]')
|
||||
account_dir = acct_dir(base_dir, nickname, domain)
|
||||
_unsuspend_media_for_account(base_dir, account_dir)
|
||||
|
||||
|
|
@ -1482,13 +1481,12 @@ def _remove_tags_for_nickname(base_dir: str, nickname: str,
|
|||
tag_filename)
|
||||
if lines is None:
|
||||
continue
|
||||
try:
|
||||
with open(tag_filename, 'w+', encoding='utf-8') as fp_tag:
|
||||
text = ''
|
||||
for tagline in lines:
|
||||
if match_str not in tagline:
|
||||
fp_tag.write(tagline)
|
||||
except OSError:
|
||||
print('EX: _remove_tags_for_nickname unable to write ' +
|
||||
text += tagline
|
||||
save_string(text, tag_filename,
|
||||
'EX: _remove_tags_for_nickname unable to write ' +
|
||||
tag_filename)
|
||||
|
||||
|
||||
|
|
@ -1665,11 +1663,15 @@ def is_person_snoozed(base_dir: str, nickname: str, domain: str,
|
|||
return False
|
||||
# remove the snooze entry if it has timed out
|
||||
replace_str = None
|
||||
try:
|
||||
with open(snoozed_filename, 'r', encoding='utf-8') as fp_snoozed:
|
||||
for line in fp_snoozed:
|
||||
|
||||
snoozed_list: list[str] = \
|
||||
load_list(snoozed_filename,
|
||||
'EX: is_person_snoozed unable to read ' + snoozed_filename)
|
||||
if snoozed_list is not None:
|
||||
for line in snoozed_list:
|
||||
# is this the entry for the actor?
|
||||
if line.startswith(snooze_actor + ' '):
|
||||
if not line.startswith(snooze_actor + ' '):
|
||||
continue
|
||||
snoozed_time_str1 = line.split(' ')[1]
|
||||
snoozed_time_str = remove_eol(snoozed_time_str1)
|
||||
# is there a time appended?
|
||||
|
|
@ -1682,8 +1684,7 @@ def is_person_snoozed(base_dir: str, nickname: str, domain: str,
|
|||
else:
|
||||
replace_str = line
|
||||
break
|
||||
except OSError:
|
||||
print('EX: is_person_snoozed unable to read ' + snoozed_filename)
|
||||
|
||||
if replace_str:
|
||||
content = load_string(snoozed_filename,
|
||||
'EX: is_person_snoozed unable to read 2 ' +
|
||||
|
|
@ -1731,14 +1732,16 @@ def person_unsnooze(base_dir: str, nickname: str, domain: str,
|
|||
if not text_in_file(snooze_actor + ' ', snoozed_filename):
|
||||
return
|
||||
replace_str = None
|
||||
try:
|
||||
with open(snoozed_filename, 'r', encoding='utf-8') as fp_snoozed:
|
||||
for line in fp_snoozed:
|
||||
|
||||
snoozed_list: list[str] = \
|
||||
load_list(snoozed_filename,
|
||||
'EX: person_unsnooze unable to read ' + snoozed_filename)
|
||||
if snoozed_list is not None:
|
||||
for line in snoozed_list:
|
||||
if line.startswith(snooze_actor + ' '):
|
||||
replace_str = line
|
||||
break
|
||||
except OSError:
|
||||
print('EX: person_unsnooze unable to read ' + snoozed_filename)
|
||||
|
||||
if replace_str:
|
||||
content = load_string(snoozed_filename,
|
||||
'EX: person_unsnooze unable to read 2 ' +
|
||||
|
|
|
|||
Loading…
Reference in New Issue