diff --git a/blog.py b/blog.py index 6e49f1030..8387010be 100644 --- a/blog.py +++ b/blog.py @@ -47,7 +47,6 @@ 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: {}, @@ -149,11 +148,12 @@ def _get_blog_replies(base_dir: str, http_prefix: str, translate: {}, '/postcache/' + \ post_id.replace('/', '#') + '.html' if os.path.isfile(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' + 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) 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 - reply_text = load_string(post_filename, - 'EX: unable to read blog replies ' + - post_filename) - if reply_text: - replies_str += reply_text + '\n' + 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) rply = _get_blog_replies(base_dir, http_prefix, translate, nickname, domain, domain_full, reply_post_id, depth+1) @@ -934,12 +934,13 @@ 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'): - 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 + '

' + 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') css_filename = base_dir + '/epicyon-profile.css' if os.path.isfile(base_dir + '/epicyon.css'): diff --git a/bookmarks.py b/bookmarks.py index 1c97f12cc..0faa5a4a9 100644 --- a/bookmarks.py +++ b/bookmarks.py @@ -35,8 +35,6 @@ 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: {}, @@ -81,16 +79,21 @@ def undo_bookmarks_collection_entry(recent_posts_cache: {}, if not text_in_file(bookmark_index, bookmarks_index_filename): return index_str: str = '' - 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', '') + 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) if index_str: - save_string(index_str, bookmarks_index_filename, - 'EX: unable to write bookmarks index ' + - bookmarks_index_filename) + 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) if not post_json_object.get('type'): return if post_json_object['type'] != 'Create': @@ -266,9 +269,13 @@ def update_bookmarks_collection(recent_posts_cache: {}, print('WARN: Failed to write entry to bookmarks index ' + bookmarks_index_filename + ' ' + str(ex)) else: - save_string(bookmark_index + '\n', bookmarks_index_filename, - 'EX: unable to write bookmarks index ' + - bookmarks_index_filename) + 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) def bookmark_post(recent_posts_cache: {}, diff --git a/categories.py b/categories.py index 8ad2278bb..c4b2d8867 100644 --- a/categories.py +++ b/categories.py @@ -14,8 +14,6 @@ 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 @@ -71,12 +69,11 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None: if not os.path.isfile(cities_filename): continue cities: list[str] = [] - cities_str = \ - load_string(cities_filename, - 'EX: unable to load cities file ' + - cities_filename) - if cities_str: - cities = cities_str.split('\n') + 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) if not cities: continue for hashtag in cities: @@ -86,9 +83,13 @@ 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): - save_string(category_str, city_filename, - 'EX: unable to write city category ' + - 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) if '-' in hashtag: section = hashtag.split('-') new_hashtag: str = '' @@ -98,9 +99,13 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None: city_filename = \ base_dir + '/tags/' + hashtag2 + '.category' if not os.path.isfile(city_filename): - save_string(category_str, city_filename, - 'EX: unable to write city category2 ' + - 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) if ' ' in hashtag: section = hashtag.split(' ') new_hashtag: str = '' @@ -110,9 +115,13 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None: city_filename = \ base_dir + '/tags/' + hashtag2 + '.category' if not os.path.isfile(city_filename): - save_string(category_str, city_filename, - 'EX: unable to write city category3 ' + - 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) def get_hashtag_categories(base_dir: str, @@ -204,9 +213,12 @@ def update_hashtag_categories(base_dir: str) -> None: category_list_str += category_str + '\n' # save a list of available categories for quick lookup - save_string(category_list_str, category_list_filename, - 'EX: unable to write category ' + - category_list_filename) + 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) def _valid_hashtag_category(category: str) -> bool: diff --git a/city.py b/city.py index a854e97d5..f61e8603e 100644 --- a/city.py +++ b/city.py @@ -19,7 +19,6 @@ 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 @@ -231,11 +230,11 @@ def spoof_geolocation(base_dir: str, default_latdirection, default_longdirection, "", "", 0) cities: list[str] = [] - cities_str = load_string(locations_filename, - 'EX: unable to read locations ' + - locations_filename) - if cities_str: - cities = cities_str.split('\n') + 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) nogo = [] if nogo_list: @@ -243,12 +242,11 @@ def spoof_geolocation(base_dir: str, else: if os.path.isfile(nogo_filename): nogo_list: list[str] = [] - 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') + 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) for line in nogo_list: if line.startswith(city + ':'): polygon = parse_nogo_string(line) @@ -340,11 +338,12 @@ 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): - city1 = load_string(city_filename, - 'EX: get_spoofed_city unable to read ' + - city_filename) - if city1: - city = remove_eol(city1) + 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) return city