Include unicode exceptions during load and save

main
bashrc 2026-04-30 10:40:05 +01:00
parent edd183ed2c
commit 6a42cbca51
5 changed files with 51 additions and 59 deletions

View File

@ -1615,15 +1615,13 @@ def set_broch_mode(base_dir: str, domain_full: str, enabled: bool) -> None:
break break
# write the allow file # write the allow file
try: text = domain_full + '\n'
with open(allow_filename, 'w+',
encoding='utf-8') as fp_allow:
fp_allow.write(domain_full + '\n')
for allowed in allowed_domains: for allowed in allowed_domains:
fp_allow.write(allowed + '\n') text += allowed + '\n'
if save_string(text, allow_filename,
'EX: Broch mode not enabled due to file write [ex]'):
print('Broch mode enabled') print('Broch mode enabled')
except OSError as ex: else:
print('EX: Broch mode not enabled due to file write ' + str(ex))
return return
set_config_param(base_dir, "brochMode", enabled) set_config_param(base_dir, "brochMode", enabled)

12
blog.py
View File

@ -48,6 +48,7 @@ from newswire import rss2footer
from cache import get_person_from_cache from cache import get_person_from_cache
from flags import is_image_file from flags import is_image_file
from data import load_string from data import load_string
from data import save_string
from data import load_list 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: if lines and removals:
print('Rewriting ' + post_filename + ' to remove ' + print('Rewriting ' + post_filename + ' to remove ' +
str(len(removals)) + ' entries') str(len(removals)) + ' entries')
try: text: str = ''
with open(post_filename, 'w+', encoding='utf-8') as fp_post:
for reply_post_id in lines: for reply_post_id in lines:
reply_post_id = remove_eol(reply_post_id) reply_post_id = remove_eol(reply_post_id)
if reply_post_id not in removals: if reply_post_id not in removals:
fp_post.write(reply_post_id + '\n') text += reply_post_id + '\n'
except OSError as ex: save_string(text, post_filename,
print('EX: unable to remove replies from post ' + 'EX: unable to remove replies from post ' +
post_filename + ' ' + str(ex)) post_filename + ' [ex]')
return replies return replies

View File

@ -34,15 +34,10 @@ def get_hashtag_category(base_dir: str, hashtag: str) -> str:
if not os.path.isfile(category_filename): if not os.path.isfile(category_filename):
return '' return ''
category_str = None category_str: str = \
try: load_string(category_filename,
with open(category_filename, 'r', encoding='utf-8') as fp_category: 'EX: unable to read category ' +
category_str = fp_category.read() category_filename + ' [ex]')
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))
if category_str: if category_str:
return category_str return category_str
return '' return ''
@ -74,7 +69,7 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None:
cities_str = \ cities_str = \
load_string(cities_filename, load_string(cities_filename,
'EX: unable to load cities file ' + 'EX: unable to load cities file ' +
cities_filename) cities_filename + ' [ex]')
if cities_str: if cities_str:
cities = cities_str.split('\n') cities = cities_str.split('\n')
if not cities: if not cities:
@ -137,17 +132,10 @@ def get_hashtag_categories(base_dir: str,
if len(hashtag) > MAX_TAG_LENGTH: if len(hashtag) > MAX_TAG_LENGTH:
continue continue
category_str = None category_str: str = \
try: load_string(category_filename,
with open(category_filename, 'r', 'EX: get_hashtag_categories ' +
encoding='utf-8') as fp_category: category_filename + ' [ex]')
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))
if not category_str: if not category_str:
continue continue
@ -263,16 +251,10 @@ def set_hashtag_category(base_dir: str, hashtag: str, category: str,
return False return False
category_written: bool = False category_written: bool = False
try: if save_string(category, category_filename,
with open(category_filename, 'w+', encoding='utf-8') as fp_category: 'EX: unable to write category ' + category_filename +
fp_category.write(category) ' [ex]'):
category_written = True 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 category_written: if category_written:
if update: if update:

12
data.py
View File

@ -20,6 +20,10 @@ def _store_base(text: str, filename: str, exception_text: str,
if '[ex]' in exception_text: if '[ex]' in exception_text:
exception_text = exception_text.replace('[ex]', str(exc)) exception_text = exception_text.replace('[ex]', str(exc))
print(exception_text) 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 return False
@ -34,6 +38,10 @@ def load_string(filename: str, exception_text: str) -> str:
if '[ex]' in exception_text: if '[ex]' in exception_text:
exception_text = exception_text.replace('[ex]', str(exc)) exception_text = exception_text.replace('[ex]', str(exc))
print(exception_text) 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 return None
@ -62,6 +70,10 @@ def load_line(filename: str, exception_text: str) -> str:
if '[ex]' in exception_text: if '[ex]' in exception_text:
exception_text = exception_text.replace('[ex]', str(exc)) exception_text = exception_text.replace('[ex]', str(exc))
print(exception_text) 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 return None

View File

@ -136,6 +136,7 @@ from webapp_post import get_instance_software
from siteactive import site_is_active from siteactive import site_is_active
from siteactive import is_online from siteactive import is_online
from data import save_string from data import save_string
from data import load_list
def str2bool(value_str) -> bool: def str2bool(value_str) -> bool:
@ -1976,16 +1977,15 @@ def _command_options() -> None:
approve_follows_filename = accounts_dir + '/followrequests.txt' approve_follows_filename = accounts_dir + '/followrequests.txt'
approve_ctr: int = 0 approve_ctr: int = 0
if os.path.isfile(approve_follows_filename): if os.path.isfile(approve_follows_filename):
try: approve_follows_list: list[str] = \
with open(approve_follows_filename, 'r', load_list(approve_follows_filename,
encoding='utf-8') as fp_approve: 'EX: unable to read follow approvals file ' +
for approve in fp_approve: approve_follows_filename)
if approve_follows_list is not None:
for approve in approve_follows_list:
approve1 = remove_eol(approve) approve1 = remove_eol(approve)
print(approve1) print(approve1)
approve_ctr += 1 approve_ctr += 1
except OSError:
print('EX: unable to read follow approvals file ' +
approve_follows_filename)
if approve_ctr == 0: if approve_ctr == 0:
print('There are no follow requests pending approval.') print('There are no follow requests pending approval.')
sys.exit() sys.exit()