diff --git a/bookmarks.py b/bookmarks.py index cb9ad3944..43d4e7d54 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -37,6 +37,7 @@ from posts import get_person_box from session import post_json from data import load_string from data import save_string +from data import prepend_string def undo_bookmarks_collection_entry(recent_posts_cache: {}, @@ -252,19 +253,12 @@ def update_bookmarks_collection(recent_posts_cache: {}, bookmark_index = post_filename.split('/')[-1] if os.path.isfile(bookmarks_index_filename): if not text_in_file(bookmark_index, bookmarks_index_filename): - try: - with open(bookmarks_index_filename, 'r+', - encoding='utf-8') as fp_bmi: - content = fp_bmi.read() - if bookmark_index + '\n' not in content: - fp_bmi.seek(0, 0) - fp_bmi.write(bookmark_index + '\n' + content) - if debug: - print('DEBUG: bookmark added to index') - except OSError as ex: + if prepend_string(bookmark_index, bookmarks_index_filename, + 'EX: ' + + 'Failed to prepend entry to bookmarks index ' + + bookmarks_index_filename + ' [ex]'): if debug: - print('WARN: Failed to write entry to bookmarks index ' + - bookmarks_index_filename + ' ' + str(ex)) + print('DEBUG: bookmark added to index') else: save_string(bookmark_index + '\n', bookmarks_index_filename, 'EX: unable to write bookmarks index ' + diff --git a/data.py b/data.py index 28fe47c30..ad9f7a115 100644 --- a/data.py +++ b/data.py @@ -127,3 +127,24 @@ def append_string(text: str, filename: str, exception_text: str) -> bool: """Appends a string to file """ return _store_base(text, filename, exception_text, 'a+') + + +def prepend_string(text: str, filename: str, exception_text: str) -> bool: + """Prepends a string to a file + """ + try: + with open(filename, 'r+', encoding='utf-8') as fp: + content: str = fp.read() + if text + '\n' not in content: + fp.seek(0, 0) + fp.write(text + '\n' + content) + return True + except OSError as exc: + if '[ex]' in exception_text: + exception_text = exception_text.replace('[ex]', str(exc)) + print(exception_text) + except UnicodeEncodeError as exc: + if '[ex]' in exception_text: + exception_text = exception_text.replace('[ex]', str(exc)) + print(exception_text) + return False diff --git a/desktop_client.py b/desktop_client.py index 097f64e6c..075eb6ba1 100644 --- a/desktop_client.py +++ b/desktop_client.py @@ -74,6 +74,7 @@ from person import get_actor_json from cache import get_person_from_cache from data import save_string from data import load_string +from data import prepend_string def _desktop_help() -> None: @@ -188,17 +189,8 @@ def _mark_post_as_read(actor: str, post_id: str, post_category: str) -> None: if os.path.isfile(read_posts_filename): if text_in_file(post_id, read_posts_filename): return - try: - # prepend to read posts file - post_id += '\n' - with open(read_posts_filename, 'r+', - encoding='utf-8') as fp_read: - content = fp_read.read() - if post_id not in content: - fp_read.seek(0, 0) - fp_read.write(post_id + content) - except OSError as ex: - print('EX: Failed to mark post as read 1 ' + str(ex)) + prepend_string(post_id, read_posts_filename, + 'EX: Failed to prepend post as read 1 [ex]') else: save_string(post_id + '\n', read_posts_filename, 'EX: Failed to mark post as read 2 [ex]') diff --git a/happening.py b/happening.py index bbe2ab096..8cc8e7b89 100644 --- a/happening.py +++ b/happening.py @@ -40,6 +40,7 @@ from data import load_list from data import load_string from data import save_string from data import append_string +from data import prepend_string def _strings_are_digits(strings_list: []) -> bool: @@ -159,16 +160,10 @@ def save_event_post(base_dir: str, handle: str, post_id: str, if os.path.isfile(tl_events_filename): _remove_event_from_timeline(event_id, tl_events_filename) - try: - with open(tl_events_filename, 'r+', - encoding='utf-8') as fp_tl_events: - content = fp_tl_events.read() - if event_id + '\n' not in content: - fp_tl_events.seek(0, 0) - fp_tl_events.write(event_id + '\n' + content) - except OSError as ex: - print('EX: Failed to write entry to events file ' + - tl_events_filename + ' ' + str(ex)) + ex_str: str = \ + 'EX: Failed to prepend entry to events file ' + \ + tl_events_filename + ' [ex]' + if not prepend_string(event_id, tl_events_filename, ex_str): return False else: save_string(event_id + '\n', tl_events_filename, diff --git a/inbox.py b/inbox.py index 3322d8bd7..24ec81ca8 100644 --- a/inbox.py +++ b/inbox.py @@ -142,6 +142,7 @@ from data import save_string from data import save_flag_file from data import load_string from data import append_string +from data import prepend_string def _store_last_post_id(base_dir: str, nickname: str, domain: str, @@ -940,16 +941,9 @@ def update_edited_post(base_dir: str, index_filename = \ acct_dir(base_dir, nickname, domain) + '/' + box_name + '.index' if not text_in_file(id_str, index_filename): - try: - with open(index_filename, 'r+', - encoding='utf-8') as fp_index: - content = fp_index.read() - if id_str + '\n' not in content: - fp_index.seek(0, 0) - fp_index.write(id_str + '\n' + content) - except OSError as ex: - print('WARN: Failed to write index after edit ' + - index_filename + ' ' + str(ex)) + prepend_string(id_str, index_filename, + 'WARN: Failed to prepend index after edit ' + + index_filename + ' [ex]') def populate_replies(base_dir: str, http_prefix: str, domain: str, @@ -3347,22 +3341,19 @@ def _receive_follow_request(session, session_onion, session_i2p, is_group_account(base_dir, nickname, domain): print('Group cannot follow a group') return True - try: - with open(followers_filename, 'r+', - encoding='utf-8') as fp_followers: - content = fp_followers.read() - if approve_handle + '\n' not in content: - fp_followers.seek(0, 0) - if not group_account: - fp_followers.write(approve_handle + - '\n' + content) - else: - fp_followers.write('!' + approve_handle + - '\n' + content) - except OSError as ex: - print('WARN: ' + - 'Failed to write entry to followers file ' + - str(ex)) + + if not group_account: + ex_str: str = \ + 'EX: Failed to prepend entry to followers file' + \ + ' [ex]' + prepend_string(approve_handle, followers_filename, + ex_str) + else: + ex_str: str = \ + 'EX: Failed to prepend group to followers file' + \ + ' [ex]' + prepend_string('!' + approve_handle, + followers_filename, ex_str) else: save_string(approve_handle + '\n', followers_filename, 'EX: _receive_follow_request unable to write ' + diff --git a/inbox_receive.py b/inbox_receive.py index 0a8a17c4d..1c5b5cf72 100644 --- a/inbox_receive.py +++ b/inbox_receive.py @@ -89,6 +89,7 @@ from webapp_hashtagswarm import store_hash_tags from data import save_string from data import save_flag_file from data import append_string +from data import prepend_string from data import load_string @@ -112,16 +113,9 @@ def inbox_update_index(boxname: str, base_dir: str, handle: str, written: bool = False if os.path.isfile(index_filename): - try: - with open(index_filename, 'r+', encoding='utf-8') as fp_index: - content = fp_index.read() - if destination_filename + '\n' not in content: - fp_index.seek(0, 0) - fp_index.write(destination_filename + '\n' + content) - written = True - return True - except OSError as ex: - print('EX: Failed to write entry to index ' + str(ex)) + if prepend_string(destination_filename, index_filename, + 'EX: Failed to prepend entry to index [ex]'): + written = True else: if save_string(destination_filename + '\n', index_filename, diff --git a/manualapprove.py b/manualapprove.py index b1686b319..5cd305728 100644 --- a/manualapprove.py +++ b/manualapprove.py @@ -27,6 +27,7 @@ from session import create_session from data import save_string from data import load_string from data import append_string +from data import prepend_string from data import load_list @@ -345,17 +346,10 @@ def manual_approve_follow_request(session, session_onion, session_i2p, print('Manual follow accept: updating ' + followers_filename) if os.path.isfile(followers_filename): if not text_in_file(approve_handle_full, followers_filename): - try: - with open(followers_filename, 'r+', - encoding='utf-8') as fp_followers: - content = fp_followers.read() - if approve_handle_full + '\n' not in content: - fp_followers.seek(0, 0) - fp_followers.write(approve_handle_full + '\n' + - content) - except OSError as ex: - print('WARN: Manual follow accept. ' + - 'Failed to write entry to followers file ' + str(ex)) + prepend_string(approve_handle_full, followers_filename, + 'EX: Manual follow accept. ' + + 'Failed to prepend entry to followers file ' + + '[ex]') else: print('WARN: Manual follow accept: ' + approve_handle_full + ' already exists in ' + followers_filename) diff --git a/newsdaemon.py b/newsdaemon.py index c1cfd0f79..98080c9cf 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -46,6 +46,7 @@ from data import load_list from data import load_string from data import save_string from data import append_string +from data import prepend_string def _update_feeds_outbox_index(base_dir: str, domain: str, @@ -57,17 +58,11 @@ def _update_feeds_outbox_index(base_dir: str, domain: str, if os.path.isfile(index_filename): if not text_in_file(post_id, index_filename): - try: - with open(index_filename, 'r+', - encoding='utf-8') as fp_feeds: - content = fp_feeds.read() - if post_id + '\n' not in content: - fp_feeds.seek(0, 0) - fp_feeds.write(post_id + '\n' + content) - print('DEBUG: feeds post added to index') - except OSError as ex: - print('EX: Failed to write entry to feeds posts index ' + - index_filename + ' ' + str(ex)) + if prepend_string(post_id, index_filename, + 'EX: ' + + 'Failed to prepend entry to feeds posts index ' + + index_filename + ' [ex]'): + print('DEBUG: feeds post added to index') return save_string(post_id + '\n', index_filename, diff --git a/posts.py b/posts.py index ba9f4c06f..38f015335 100644 --- a/posts.py +++ b/posts.py @@ -149,6 +149,7 @@ from data import load_string from data import save_string from data import save_flag_file from data import append_string +from data import prepend_string def convert_post_content_to_html(message_json: {}) -> None: @@ -1104,16 +1105,10 @@ def _update_hashtags_index(base_dir: str, tag: {}, new_post_id: str, days_since_epoch = days_diff.days tag_line = \ str(days_since_epoch) + ' ' + nickname + ' ' + \ - new_post_id + '\n' - try: - with open(tags_filename, 'r+', encoding='utf-8') as fp_tags: - content = fp_tags.read() - if tag_line not in content: - fp_tags.seek(0, 0) - fp_tags.write(tag_line + content) - except OSError as ex: - print('EX: Failed to write entry to tags file ' + - tags_filename + ' ' + str(ex)) + new_post_id + prepend_string(tag_line, tags_filename, + 'EX: Failed to prepend entry to tags file ' + + tags_filename + ' [ex]') def _add_schedule_post(base_dir: str, nickname: str, domain: str, @@ -1127,17 +1122,12 @@ def _add_schedule_post(base_dir: str, nickname: str, domain: str, index_str = event_date_str + ' ' + post_id.replace('/', '#') if os.path.isfile(schedule_index_filename): if not text_in_file(index_str, schedule_index_filename): - try: - with open(schedule_index_filename, 'r+', - encoding='utf-8') as fp_schedule: - content = fp_schedule.read() - if index_str + '\n' not in content: - fp_schedule.seek(0, 0) - fp_schedule.write(index_str + '\n' + content) - print('DEBUG: scheduled post added to index') - except OSError as ex: - print('EX: Failed to write entry to scheduled posts index ' + - schedule_index_filename + ' ' + str(ex)) + ex_str: str = \ + 'EX: Failed to prepend entry to scheduled posts index ' + \ + schedule_index_filename + ' [ex]' + if prepend_string(index_str, schedule_index_filename, + ex_str): + print('DEBUG: scheduled post added to index') return save_string(index_str + '\n', schedule_index_filename, diff --git a/reading.py b/reading.py index bdaef7a95..215402088 100644 --- a/reading.py +++ b/reading.py @@ -24,6 +24,7 @@ from timeFunctions import date_epoch from timeFunctions import date_from_string_format from data import save_string from data import load_string +from data import prepend_string def get_book_link_from_content(content: str) -> str: @@ -396,18 +397,12 @@ def _update_recent_books_list(base_dir: str, book_id: str, """ recent_books_filename = data_dir(base_dir) + '/recent_books.txt' if os.path.isfile(recent_books_filename): - try: - with open(recent_books_filename, 'r+', - encoding='utf-8') as fp_recent: - content = fp_recent.read() - if book_id + '\n' not in content: - fp_recent.seek(0, 0) - fp_recent.write(book_id + '\n' + content) - if debug: - print('DEBUG: recent book added') - except OSError as ex: - print('WARN: Failed to write entry to recent books ' + - recent_books_filename + ' ' + str(ex)) + ex_str: str = \ + 'EX: Failed to prepend entry to recent books ' + \ + recent_books_filename + ' [ex]' + if prepend_string(book_id, recent_books_filename, ex_str): + if debug: + print('DEBUG: recent book added') else: save_string(book_id + '\n', recent_books_filename, 'EX: unable to write recent books ' +