diff --git a/blocking.py b/blocking.py index 370a0e62d..91f47afba 100644 --- a/blocking.py +++ b/blocking.py @@ -1615,15 +1615,13 @@ def set_broch_mode(base_dir: str, domain_full: str, enabled: bool) -> None: break # write the allow file - try: - with open(allow_filename, 'w+', - encoding='utf-8') as fp_allow: - fp_allow.write(domain_full + '\n') - for allowed in allowed_domains: - fp_allow.write(allowed + '\n') - print('Broch mode enabled') - except OSError as ex: - print('EX: Broch mode not enabled due to file write ' + str(ex)) + text = domain_full + '\n' + for allowed in allowed_domains: + text += allowed + '\n' + if save_string(text, allow_filename, + 'EX: Broch mode not enabled due to file write [ex]'): + print('Broch mode enabled') + else: return set_config_param(base_dir, "brochMode", enabled) diff --git a/blog.py b/blog.py index 637b839c6..5430d9ab9 100644 --- a/blog.py +++ b/blog.py @@ -48,6 +48,7 @@ from newswire import rss2footer from cache import get_person_from_cache from flags import is_image_file from data import load_string +from data import save_string from data import load_list @@ -106,15 +107,14 @@ def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {}, if lines and removals: print('Rewriting ' + post_filename + ' to remove ' + str(len(removals)) + ' entries') - try: - with open(post_filename, 'w+', encoding='utf-8') as fp_post: - for reply_post_id in lines: - reply_post_id = remove_eol(reply_post_id) - if reply_post_id not in removals: - fp_post.write(reply_post_id + '\n') - except OSError as ex: - print('EX: unable to remove replies from post ' + - post_filename + ' ' + str(ex)) + text: str = '' + for reply_post_id in lines: + reply_post_id = remove_eol(reply_post_id) + if reply_post_id not in removals: + text += reply_post_id + '\n' + save_string(text, post_filename, + 'EX: unable to remove replies from post ' + + post_filename + ' [ex]') return replies diff --git a/categories.py b/categories.py index 27ce8d9fa..e2212ef35 100644 --- a/categories.py +++ b/categories.py @@ -34,15 +34,10 @@ def get_hashtag_category(base_dir: str, hashtag: str) -> str: if not os.path.isfile(category_filename): return '' - category_str = None - try: - with open(category_filename, 'r', encoding='utf-8') as fp_category: - category_str = fp_category.read() - except OSError: - print('EX: unable to read category ' + category_filename) - except UnicodeEncodeError as ex: - print('EX: unable to read category unicode ' + category_filename + - ' ' + str(ex)) + category_str: str = \ + load_string(category_filename, + 'EX: unable to read category ' + + category_filename + ' [ex]') if category_str: return category_str return '' @@ -74,7 +69,7 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None: cities_str = \ load_string(cities_filename, 'EX: unable to load cities file ' + - cities_filename) + cities_filename + ' [ex]') if cities_str: cities = cities_str.split('\n') if not cities: @@ -137,17 +132,10 @@ def get_hashtag_categories(base_dir: str, if len(hashtag) > MAX_TAG_LENGTH: continue - category_str = None - try: - with open(category_filename, 'r', - encoding='utf-8') as fp_category: - category_str = fp_category.read() - except OSError: - print('EX: get_hashtag_categories ' + category_filename) - except UnicodeEncodeError as ex: - print('EX: get_hashtag_categories unicode ' + - category_filename + ' ' + str(ex)) - + category_str: str = \ + load_string(category_filename, + 'EX: get_hashtag_categories ' + + category_filename + ' [ex]') if not category_str: continue @@ -263,16 +251,10 @@ def set_hashtag_category(base_dir: str, hashtag: str, category: str, return False category_written: bool = False - try: - with open(category_filename, 'w+', encoding='utf-8') as fp_category: - fp_category.write(category) - category_written = True - except OSError as ex: - print('EX: unable to write category ' + category_filename + - ' ' + str(ex)) - except UnicodeEncodeError as ex: - print('EX: unable to write category unicode ' + category_filename + - ' ' + str(ex)) + if save_string(category, category_filename, + 'EX: unable to write category ' + category_filename + + ' [ex]'): + category_written = True if category_written: if update: diff --git a/data.py b/data.py index ff383d73b..28fe47c30 100644 --- a/data.py +++ b/data.py @@ -20,6 +20,10 @@ def _store_base(text: str, filename: str, exception_text: str, 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 @@ -34,6 +38,10 @@ def load_string(filename: str, exception_text: str) -> str: 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 None @@ -62,6 +70,10 @@ def load_line(filename: str, exception_text: str) -> str: 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 None diff --git a/epicyon.py b/epicyon.py index 8b71266db..d84ceed24 100644 --- a/epicyon.py +++ b/epicyon.py @@ -136,6 +136,7 @@ from webapp_post import get_instance_software from siteactive import site_is_active from siteactive import is_online from data import save_string +from data import load_list def str2bool(value_str) -> bool: @@ -1976,16 +1977,15 @@ def _command_options() -> None: approve_follows_filename = accounts_dir + '/followrequests.txt' approve_ctr: int = 0 if os.path.isfile(approve_follows_filename): - try: - with open(approve_follows_filename, 'r', - encoding='utf-8') as fp_approve: - for approve in fp_approve: - approve1 = remove_eol(approve) - print(approve1) - approve_ctr += 1 - except OSError: - print('EX: unable to read follow approvals file ' + - approve_follows_filename) + approve_follows_list: list[str] = \ + load_list(approve_follows_filename, + 'EX: unable to read follow approvals file ' + + approve_follows_filename) + if approve_follows_list is not None: + for approve in approve_follows_list: + approve1 = remove_eol(approve) + print(approve1) + approve_ctr += 1 if approve_ctr == 0: print('There are no follow requests pending approval.') sys.exit()