mirror of https://gitlab.com/bashrc2/epicyon
Replace file operations with functions
parent
f2606ef419
commit
9ae07571a7
116
content.py
116
content.py
|
|
@ -51,6 +51,9 @@ from utils import is_yggdrasil_address
|
||||||
from formats import get_image_extensions
|
from formats import get_image_extensions
|
||||||
from petnames import get_pet_name
|
from petnames import get_pet_name
|
||||||
from session import download_image
|
from session import download_image
|
||||||
|
from data import load_string
|
||||||
|
from data import save_string
|
||||||
|
from data import append_string
|
||||||
|
|
||||||
MUSIC_SITES = ('soundcloud.com', 'bandcamp.com', 'resonate.coop')
|
MUSIC_SITES = ('soundcloud.com', 'bandcamp.com', 'resonate.coop')
|
||||||
|
|
||||||
|
|
@ -276,12 +279,11 @@ def dangerous_css(filename: str, allow_local_network_access: bool) -> bool:
|
||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
content = None
|
content = load_string(filename,
|
||||||
try:
|
'EX: unable to read css file ' + filename)
|
||||||
with open(filename, 'r', encoding='utf-8') as fp_css:
|
if content is None:
|
||||||
content = fp_css.read().lower()
|
content = ''
|
||||||
except OSError:
|
content = content.lower()
|
||||||
print('EX: unable to read css file ' + filename)
|
|
||||||
|
|
||||||
if not content:
|
if not content:
|
||||||
return False
|
return False
|
||||||
|
|
@ -330,12 +332,11 @@ def switch_words(base_dir: str, nickname: str, domain: str, content: str,
|
||||||
acct_dir(base_dir, nickname, domain) + '/replacewords.txt'
|
acct_dir(base_dir, nickname, domain) + '/replacewords.txt'
|
||||||
if not os.path.isfile(switch_words_filename):
|
if not os.path.isfile(switch_words_filename):
|
||||||
return content
|
return content
|
||||||
try:
|
rules_str = load_string(switch_words_filename,
|
||||||
with open(switch_words_filename, 'r',
|
'EX: unable to read switches ' +
|
||||||
encoding='utf-8') as fp_words:
|
switch_words_filename)
|
||||||
rules = fp_words.readlines()
|
if rules_str:
|
||||||
except OSError:
|
rules = rules_str.split('\n')
|
||||||
print('EX: unable to read switches ' + switch_words_filename)
|
|
||||||
|
|
||||||
for line in rules:
|
for line in rules:
|
||||||
replace_str = remove_eol(line)
|
replace_str = remove_eol(line)
|
||||||
|
|
@ -426,12 +427,10 @@ def _update_common_emoji(base_dir: str, emoji_content: str) -> None:
|
||||||
common_emoji_filename = data_dir(base_dir) + '/common_emoji.txt'
|
common_emoji_filename = data_dir(base_dir) + '/common_emoji.txt'
|
||||||
common_emoji = None
|
common_emoji = None
|
||||||
if os.path.isfile(common_emoji_filename):
|
if os.path.isfile(common_emoji_filename):
|
||||||
try:
|
common_emoji_str = load_string(common_emoji_filename,
|
||||||
with open(common_emoji_filename, 'r',
|
'EX: unable to load common emoji file')
|
||||||
encoding='utf-8') as fp_emoji:
|
if common_emoji_str:
|
||||||
common_emoji = fp_emoji.readlines()
|
common_emoji = common_emoji_str.split('\n')
|
||||||
except OSError:
|
|
||||||
print('EX: unable to load common emoji file')
|
|
||||||
if common_emoji:
|
if common_emoji:
|
||||||
new_common_emoji: list[str] = []
|
new_common_emoji: list[str] = []
|
||||||
emoji_found = False
|
emoji_found = False
|
||||||
|
|
@ -452,22 +451,17 @@ def _update_common_emoji(base_dir: str, emoji_content: str) -> None:
|
||||||
if not emoji_found:
|
if not emoji_found:
|
||||||
new_common_emoji.append(str(1).zfill(16) + ' ' + emoji_content)
|
new_common_emoji.append(str(1).zfill(16) + ' ' + emoji_content)
|
||||||
new_common_emoji.sort(reverse=True)
|
new_common_emoji.sort(reverse=True)
|
||||||
try:
|
|
||||||
with open(common_emoji_filename, 'w+',
|
text = ''
|
||||||
encoding='utf-8') as fp_emoji:
|
for line in new_common_emoji:
|
||||||
for line in new_common_emoji:
|
text += line + '\n'
|
||||||
fp_emoji.write(line + '\n')
|
if not save_string(text, common_emoji_filename,
|
||||||
except OSError:
|
'EX: error writing common emoji 1'):
|
||||||
print('EX: error writing common emoji 1')
|
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
line = str(1).zfill(16) + ' ' + emoji_content + '\n'
|
line = str(1).zfill(16) + ' ' + emoji_content + '\n'
|
||||||
try:
|
if not save_string(line, common_emoji_filename,
|
||||||
with open(common_emoji_filename, 'w+',
|
'EX: error writing common emoji 2'):
|
||||||
encoding='utf-8') as fp_emoji:
|
|
||||||
fp_emoji.write(line)
|
|
||||||
except OSError:
|
|
||||||
print('EX: error writing common emoji 2')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1274,11 +1268,10 @@ def _load_auto_tags(base_dir: str, nickname: str, domain: str) -> []:
|
||||||
filename = acct_dir(base_dir, nickname, domain) + '/autotags.txt'
|
filename = acct_dir(base_dir, nickname, domain) + '/autotags.txt'
|
||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
return []
|
return []
|
||||||
try:
|
fp_tags_str = load_string(filename,
|
||||||
with open(filename, 'r', encoding='utf-8') as fp_tags:
|
'EX: unable to read auto tags ' + filename)
|
||||||
return fp_tags.readlines()
|
if fp_tags_str:
|
||||||
except OSError:
|
return fp_tags_str.split('\n')
|
||||||
print('EX: unable to read auto tags ' + filename)
|
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1419,12 +1412,13 @@ def load_dogwhistles(filename: str) -> {}:
|
||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
return {}
|
return {}
|
||||||
dogwhistle_lines: list[str] = []
|
dogwhistle_lines: list[str] = []
|
||||||
try:
|
dogwhistle_lines_str = \
|
||||||
with open(filename, 'r', encoding='utf-8') as fp_dogwhistles:
|
load_string(filename,
|
||||||
dogwhistle_lines = fp_dogwhistles.readlines()
|
'EX: unable to load dogwhistles from ' + filename)
|
||||||
except OSError:
|
if dogwhistle_lines_str is None:
|
||||||
print('EX: unable to load dogwhistles from ' + filename)
|
|
||||||
return {}
|
return {}
|
||||||
|
if dogwhistle_lines_str:
|
||||||
|
dogwhistle_lines = dogwhistle_lines_str.split('\n')
|
||||||
separators = ('->', '=>', ',', ';', '|', '=')
|
separators = ('->', '=>', ',', ';', '|', '=')
|
||||||
dogwhistles = {}
|
dogwhistles = {}
|
||||||
for line in dogwhistle_lines:
|
for line in dogwhistle_lines:
|
||||||
|
|
@ -1502,13 +1496,11 @@ def add_html_tags(base_dir: str, http_prefix: str,
|
||||||
if '@' in words:
|
if '@' in words:
|
||||||
if os.path.isfile(following_filename):
|
if os.path.isfile(following_filename):
|
||||||
following: list[str] = []
|
following: list[str] = []
|
||||||
try:
|
following_str = load_string(following_filename,
|
||||||
with open(following_filename, 'r',
|
'EX: add_html_tags unable to read ' +
|
||||||
encoding='utf-8') as fp_foll:
|
following_filename)
|
||||||
following = fp_foll.readlines()
|
if following_str:
|
||||||
except OSError:
|
following = following_str.split('\n')
|
||||||
print('EX: add_html_tags unable to read ' +
|
|
||||||
following_filename)
|
|
||||||
for handle in following:
|
for handle in following:
|
||||||
pet = get_pet_name(base_dir, nickname, domain, handle)
|
pet = get_pet_name(base_dir, nickname, domain, handle)
|
||||||
if pet:
|
if pet:
|
||||||
|
|
@ -2145,8 +2137,10 @@ def import_emoji(base_dir: str, import_filename: str, session) -> None:
|
||||||
return
|
return
|
||||||
emoji_dict = load_json(base_dir + '/emoji/default_emoji.json')
|
emoji_dict = load_json(base_dir + '/emoji/default_emoji.json')
|
||||||
added = 0
|
added = 0
|
||||||
with open(import_filename, "r", encoding='utf-8') as fp_emoji:
|
lines_str = load_string(import_filename,
|
||||||
lines = fp_emoji.readlines()
|
'EX: import_emoji failed ' + import_filename)
|
||||||
|
if lines_str:
|
||||||
|
lines = lines_str.split('\n')
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if ', ' not in line:
|
if ', ' not in line:
|
||||||
continue
|
continue
|
||||||
|
|
@ -2290,12 +2284,12 @@ def remove_script(content: str, log_filename: str,
|
||||||
write_type: str = 'a+'
|
write_type: str = 'a+'
|
||||||
if os.path.isfile(log_filename):
|
if os.path.isfile(log_filename):
|
||||||
write_type = 'w+'
|
write_type = 'w+'
|
||||||
try:
|
if write_type == 'a+':
|
||||||
with open(log_filename, write_type,
|
append_string(log_str, log_filename,
|
||||||
encoding='utf-8') as fp_log:
|
'EX: cannot append to svg script log')
|
||||||
fp_log.write(log_str)
|
else:
|
||||||
except OSError:
|
save_string(log_str, log_filename,
|
||||||
print('EX: cannot append to svg script log')
|
'EX: cannot save to svg script log')
|
||||||
content = content.replace(text, '')
|
content = content.replace(text, '')
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
@ -2448,11 +2442,11 @@ def _load_auto_cw(base_dir: str, nickname: str, domain: str) -> []:
|
||||||
auto_cw_filename = acct_dir(base_dir, nickname, domain) + '/autocw.txt'
|
auto_cw_filename = acct_dir(base_dir, nickname, domain) + '/autocw.txt'
|
||||||
if not os.path.isfile(auto_cw_filename):
|
if not os.path.isfile(auto_cw_filename):
|
||||||
return []
|
return []
|
||||||
try:
|
fp_auto_str = load_string(auto_cw_filename,
|
||||||
with open(auto_cw_filename, 'r', encoding='utf-8') as fp_auto:
|
'EX: unable to load auto cw file ' +
|
||||||
return fp_auto.read().split('\n')
|
auto_cw_filename)
|
||||||
except OSError:
|
if fp_auto_str:
|
||||||
print('EX: unable to load auto cw file ' + auto_cw_filename)
|
return fp_auto_str.split('\n')
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue