diff --git a/blog.py b/blog.py index 8387010be..6e49f1030 100644 --- a/blog.py +++ b/blog.py @@ -47,6 +47,7 @@ from newswire import rss2header from newswire import rss2footer from cache import get_person_from_cache from flags import is_image_file +from data import load_string def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {}, @@ -148,12 +149,11 @@ def _get_blog_replies(base_dir: str, http_prefix: str, translate: {}, '/postcache/' + \ post_id.replace('/', '#') + '.html' if os.path.isfile(post_filename): - try: - with open(post_filename, 'r', - encoding='utf-8') as fp_post: - return fp_post.read() + '\n' - except OSError: - print('EX: unable to read blog 3 ' + post_filename) + blog_text = load_string(post_filename, + 'EX: unable to read blog 3 ' + + post_filename) + if blog_text is not None: + return blog_text + '\n' return '' lines: list[str] = [] @@ -177,11 +177,11 @@ def _get_blog_replies(base_dir: str, http_prefix: str, translate: {}, reply_post_id.replace('/', '#') + '.html' if not os.path.isfile(post_filename): continue - try: - with open(post_filename, 'r', encoding='utf-8') as fp_post: - replies_str += fp_post.read() + '\n' - except OSError: - print('EX: unable to read blog replies ' + post_filename) + reply_text = load_string(post_filename, + 'EX: unable to read blog replies ' + + post_filename) + if reply_text: + replies_str += reply_text + '\n' rply = _get_blog_replies(base_dir, http_prefix, translate, nickname, domain, domain_full, reply_post_id, depth+1) @@ -934,13 +934,12 @@ def html_edit_blog(media_instance: bool, translate: {}, # load blog template if it exists dir_str = data_dir(base_dir) if os.path.isfile(dir_str + '/newblog.txt'): - try: - with open(dir_str + '/newblog.txt', 'r', - encoding='utf-8') as fp_blog: - edit_blog_text: str = '

' + fp_blog.read() + '

' - except OSError: - print('EX: html_edit_blog unable to read ' + - dir_str + '/newblog.txt') + edit_blog_text_str = \ + load_string(dir_str + '/newblog.txt', + 'EX: html_edit_blog unable to read ' + + dir_str + '/newblog.txt') + if edit_blog_text_str: + edit_blog_text: str = '

' + edit_blog_text_str + '

' css_filename = base_dir + '/epicyon-profile.css' if os.path.isfile(base_dir + '/epicyon.css'): diff --git a/bookmarks.py b/bookmarks.py index 0faa5a4a9..1c97f12cc 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -35,6 +35,8 @@ from utils import remove_html from utils import get_actor_from_post from posts import get_person_box from session import post_json +from data import load_string +from data import save_string def undo_bookmarks_collection_entry(recent_posts_cache: {}, @@ -79,21 +81,16 @@ def undo_bookmarks_collection_entry(recent_posts_cache: {}, if not text_in_file(bookmark_index, bookmarks_index_filename): return index_str: str = '' - try: - with open(bookmarks_index_filename, 'r', - encoding='utf-8') as fp_index: - index_str = fp_index.read().replace(bookmark_index + '\n', '') - except OSError: - print('EX: undo_bookmarks_collection_entry unable to read ' + - bookmarks_index_filename) + index_str2 = \ + load_string(bookmarks_index_filename, + 'EX: undo_bookmarks_collection_entry unable to read ' + + bookmarks_index_filename) + if index_str2: + index_str = index_str2.replace(bookmark_index + '\n', '') if index_str: - try: - with open(bookmarks_index_filename, 'w+', - encoding='utf-8') as fp_bmi: - fp_bmi.write(index_str) - except OSError: - print('EX: unable to write bookmarks index ' + - bookmarks_index_filename) + save_string(index_str, bookmarks_index_filename, + 'EX: unable to write bookmarks index ' + + bookmarks_index_filename) if not post_json_object.get('type'): return if post_json_object['type'] != 'Create': @@ -269,13 +266,9 @@ def update_bookmarks_collection(recent_posts_cache: {}, print('WARN: Failed to write entry to bookmarks index ' + bookmarks_index_filename + ' ' + str(ex)) else: - try: - with open(bookmarks_index_filename, 'w+', - encoding='utf-8') as fp_bm: - fp_bm.write(bookmark_index + '\n') - except OSError: - print('EX: unable to write bookmarks index ' + - bookmarks_index_filename) + save_string(bookmark_index + '\n', bookmarks_index_filename, + 'EX: unable to write bookmarks index ' + + bookmarks_index_filename) def bookmark_post(recent_posts_cache: {}, diff --git a/categories.py b/categories.py index c4b2d8867..8ad2278bb 100644 --- a/categories.py +++ b/categories.py @@ -14,6 +14,8 @@ from timeFunctions import date_epoch from utils import data_dir from utils import replace_strings from utils import get_invalid_characters +from data import load_string +from data import save_string MAX_TAG_LENGTH = 42 @@ -69,11 +71,12 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None: if not os.path.isfile(cities_filename): continue cities: list[str] = [] - try: - with open(cities_filename, 'r', encoding='utf-8') as fp_cities: - cities = fp_cities.read().split('\n') - except OSError: - print('EX: unable to load cities file ' + cities_filename) + cities_str = \ + load_string(cities_filename, + 'EX: unable to load cities file ' + + cities_filename) + if cities_str: + cities = cities_str.split('\n') if not cities: continue for hashtag in cities: @@ -83,13 +86,9 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None: hashtag2 = replace_strings(hashtag, replacements2) city_filename = base_dir + '/tags/' + hashtag2 + '.category' if not os.path.isfile(city_filename): - try: - with open(city_filename, 'w+', - encoding='utf-8') as fp_city: - fp_city.write(category_str) - except OSError: - print('EX: unable to write city category ' + - city_filename) + save_string(category_str, city_filename, + 'EX: unable to write city category ' + + city_filename) if '-' in hashtag: section = hashtag.split('-') new_hashtag: str = '' @@ -99,13 +98,9 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None: city_filename = \ base_dir + '/tags/' + hashtag2 + '.category' if not os.path.isfile(city_filename): - try: - with open(city_filename, 'w+', - encoding='utf-8') as fp_city: - fp_city.write(category_str) - except OSError: - print('EX: unable to write city category2 ' + - city_filename) + save_string(category_str, city_filename, + 'EX: unable to write city category2 ' + + city_filename) if ' ' in hashtag: section = hashtag.split(' ') new_hashtag: str = '' @@ -115,13 +110,9 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None: city_filename = \ base_dir + '/tags/' + hashtag2 + '.category' if not os.path.isfile(city_filename): - try: - with open(city_filename, 'w+', - encoding='utf-8') as fp_city: - fp_city.write(category_str) - except OSError: - print('EX: unable to write city category3 ' + - city_filename) + save_string(category_str, city_filename, + 'EX: unable to write city category3 ' + + city_filename) def get_hashtag_categories(base_dir: str, @@ -213,12 +204,9 @@ def update_hashtag_categories(base_dir: str) -> None: category_list_str += category_str + '\n' # save a list of available categories for quick lookup - try: - with open(category_list_filename, 'w+', - encoding='utf-8') as fp_category: - fp_category.write(category_list_str) - except OSError: - print('EX: unable to write category ' + category_list_filename) + save_string(category_list_str, category_list_filename, + 'EX: unable to write category ' + + category_list_filename) def _valid_hashtag_category(category: str) -> bool: diff --git a/city.py b/city.py index f61e8603e..a854e97d5 100644 --- a/city.py +++ b/city.py @@ -19,6 +19,7 @@ import math from random import randint from utils import acct_dir from utils import remove_eol +from data import load_string # states which the simulated city dweller can be in PERSON_SLEEP = 0 @@ -230,11 +231,11 @@ def spoof_geolocation(base_dir: str, default_latdirection, default_longdirection, "", "", 0) cities: list[str] = [] - try: - with open(locations_filename, 'r', encoding='utf-8') as fp_loc: - cities = fp_loc.readlines() - except OSError: - print('EX: unable to read locations ' + locations_filename) + cities_str = load_string(locations_filename, + 'EX: unable to read locations ' + + locations_filename) + if cities_str: + cities = cities_str.split('\n') nogo = [] if nogo_list: @@ -242,11 +243,12 @@ def spoof_geolocation(base_dir: str, else: if os.path.isfile(nogo_filename): nogo_list: list[str] = [] - try: - with open(nogo_filename, 'r', encoding='utf-8') as fp_nogo: - nogo_list = fp_nogo.readlines() - except OSError: - print('EX: spoof_geolocation unable to read ' + nogo_filename) + nogo_list_str = \ + load_string(nogo_filename, + 'EX: spoof_geolocation unable to read ' + + nogo_filename) + if nogo_list_str: + nogo_list = nogo_list_str.split('\n') for line in nogo_list: if line.startswith(city + ':'): polygon = parse_nogo_string(line) @@ -338,12 +340,11 @@ def get_spoofed_city(city: str, base_dir: str, city: str = '' city_filename = acct_dir(base_dir, nickname, domain) + '/city.txt' if os.path.isfile(city_filename): - try: - with open(city_filename, 'r', encoding='utf-8') as fp_city: - city1 = fp_city.read() - city = remove_eol(city1) - except OSError: - print('EX: get_spoofed_city unable to read ' + city_filename) + city1 = load_string(city_filename, + 'EX: get_spoofed_city unable to read ' + + city_filename) + if city1: + city = remove_eol(city1) return city