mirror of https://gitlab.com/bashrc2/epicyon
Replace file operations with functions
parent
be4a809f42
commit
8f83a31588
35
blog.py
35
blog.py
|
|
@ -47,6 +47,7 @@ from newswire import rss2header
|
||||||
from newswire import rss2footer
|
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
|
||||||
|
|
||||||
|
|
||||||
def _no_of_blog_replies(base_dir: str, http_prefix: str, translate: {},
|
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/' + \
|
'/postcache/' + \
|
||||||
post_id.replace('/', '#') + '.html'
|
post_id.replace('/', '#') + '.html'
|
||||||
if os.path.isfile(post_filename):
|
if os.path.isfile(post_filename):
|
||||||
try:
|
blog_text = load_string(post_filename,
|
||||||
with open(post_filename, 'r',
|
'EX: unable to read blog 3 ' +
|
||||||
encoding='utf-8') as fp_post:
|
post_filename)
|
||||||
return fp_post.read() + '\n'
|
if blog_text is not None:
|
||||||
except OSError:
|
return blog_text + '\n'
|
||||||
print('EX: unable to read blog 3 ' + post_filename)
|
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
lines: list[str] = []
|
lines: list[str] = []
|
||||||
|
|
@ -177,11 +177,11 @@ def _get_blog_replies(base_dir: str, http_prefix: str, translate: {},
|
||||||
reply_post_id.replace('/', '#') + '.html'
|
reply_post_id.replace('/', '#') + '.html'
|
||||||
if not os.path.isfile(post_filename):
|
if not os.path.isfile(post_filename):
|
||||||
continue
|
continue
|
||||||
try:
|
reply_text = load_string(post_filename,
|
||||||
with open(post_filename, 'r', encoding='utf-8') as fp_post:
|
'EX: unable to read blog replies ' +
|
||||||
replies_str += fp_post.read() + '\n'
|
post_filename)
|
||||||
except OSError:
|
if reply_text:
|
||||||
print('EX: unable to read blog replies ' + post_filename)
|
replies_str += reply_text + '\n'
|
||||||
rply = _get_blog_replies(base_dir, http_prefix, translate,
|
rply = _get_blog_replies(base_dir, http_prefix, translate,
|
||||||
nickname, domain, domain_full,
|
nickname, domain, domain_full,
|
||||||
reply_post_id, depth+1)
|
reply_post_id, depth+1)
|
||||||
|
|
@ -934,13 +934,12 @@ def html_edit_blog(media_instance: bool, translate: {},
|
||||||
# load blog template if it exists
|
# load blog template if it exists
|
||||||
dir_str = data_dir(base_dir)
|
dir_str = data_dir(base_dir)
|
||||||
if os.path.isfile(dir_str + '/newblog.txt'):
|
if os.path.isfile(dir_str + '/newblog.txt'):
|
||||||
try:
|
edit_blog_text_str = \
|
||||||
with open(dir_str + '/newblog.txt', 'r',
|
load_string(dir_str + '/newblog.txt',
|
||||||
encoding='utf-8') as fp_blog:
|
'EX: html_edit_blog unable to read ' +
|
||||||
edit_blog_text: str = '<p>' + fp_blog.read() + '</p>'
|
dir_str + '/newblog.txt')
|
||||||
except OSError:
|
if edit_blog_text_str:
|
||||||
print('EX: html_edit_blog unable to read ' +
|
edit_blog_text: str = '<p>' + edit_blog_text_str + '</p>'
|
||||||
dir_str + '/newblog.txt')
|
|
||||||
|
|
||||||
css_filename = base_dir + '/epicyon-profile.css'
|
css_filename = base_dir + '/epicyon-profile.css'
|
||||||
if os.path.isfile(base_dir + '/epicyon.css'):
|
if os.path.isfile(base_dir + '/epicyon.css'):
|
||||||
|
|
|
||||||
35
bookmarks.py
35
bookmarks.py
|
|
@ -35,6 +35,8 @@ from utils import remove_html
|
||||||
from utils import get_actor_from_post
|
from utils import get_actor_from_post
|
||||||
from posts import get_person_box
|
from posts import get_person_box
|
||||||
from session import post_json
|
from session import post_json
|
||||||
|
from data import load_string
|
||||||
|
from data import save_string
|
||||||
|
|
||||||
|
|
||||||
def undo_bookmarks_collection_entry(recent_posts_cache: {},
|
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):
|
if not text_in_file(bookmark_index, bookmarks_index_filename):
|
||||||
return
|
return
|
||||||
index_str: str = ''
|
index_str: str = ''
|
||||||
try:
|
index_str2 = \
|
||||||
with open(bookmarks_index_filename, 'r',
|
load_string(bookmarks_index_filename,
|
||||||
encoding='utf-8') as fp_index:
|
'EX: undo_bookmarks_collection_entry unable to read ' +
|
||||||
index_str = fp_index.read().replace(bookmark_index + '\n', '')
|
bookmarks_index_filename)
|
||||||
except OSError:
|
if index_str2:
|
||||||
print('EX: undo_bookmarks_collection_entry unable to read ' +
|
index_str = index_str2.replace(bookmark_index + '\n', '')
|
||||||
bookmarks_index_filename)
|
|
||||||
if index_str:
|
if index_str:
|
||||||
try:
|
save_string(index_str, bookmarks_index_filename,
|
||||||
with open(bookmarks_index_filename, 'w+',
|
'EX: unable to write bookmarks index ' +
|
||||||
encoding='utf-8') as fp_bmi:
|
bookmarks_index_filename)
|
||||||
fp_bmi.write(index_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write bookmarks index ' +
|
|
||||||
bookmarks_index_filename)
|
|
||||||
if not post_json_object.get('type'):
|
if not post_json_object.get('type'):
|
||||||
return
|
return
|
||||||
if post_json_object['type'] != 'Create':
|
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 ' +
|
print('WARN: Failed to write entry to bookmarks index ' +
|
||||||
bookmarks_index_filename + ' ' + str(ex))
|
bookmarks_index_filename + ' ' + str(ex))
|
||||||
else:
|
else:
|
||||||
try:
|
save_string(bookmark_index + '\n', bookmarks_index_filename,
|
||||||
with open(bookmarks_index_filename, 'w+',
|
'EX: unable to write bookmarks index ' +
|
||||||
encoding='utf-8') as fp_bm:
|
bookmarks_index_filename)
|
||||||
fp_bm.write(bookmark_index + '\n')
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write bookmarks index ' +
|
|
||||||
bookmarks_index_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def bookmark_post(recent_posts_cache: {},
|
def bookmark_post(recent_posts_cache: {},
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ from timeFunctions import date_epoch
|
||||||
from utils import data_dir
|
from utils import data_dir
|
||||||
from utils import replace_strings
|
from utils import replace_strings
|
||||||
from utils import get_invalid_characters
|
from utils import get_invalid_characters
|
||||||
|
from data import load_string
|
||||||
|
from data import save_string
|
||||||
|
|
||||||
MAX_TAG_LENGTH = 42
|
MAX_TAG_LENGTH = 42
|
||||||
|
|
||||||
|
|
@ -69,11 +71,12 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None:
|
||||||
if not os.path.isfile(cities_filename):
|
if not os.path.isfile(cities_filename):
|
||||||
continue
|
continue
|
||||||
cities: list[str] = []
|
cities: list[str] = []
|
||||||
try:
|
cities_str = \
|
||||||
with open(cities_filename, 'r', encoding='utf-8') as fp_cities:
|
load_string(cities_filename,
|
||||||
cities = fp_cities.read().split('\n')
|
'EX: unable to load cities file ' +
|
||||||
except OSError:
|
cities_filename)
|
||||||
print('EX: unable to load cities file ' + cities_filename)
|
if cities_str:
|
||||||
|
cities = cities_str.split('\n')
|
||||||
if not cities:
|
if not cities:
|
||||||
continue
|
continue
|
||||||
for hashtag in cities:
|
for hashtag in cities:
|
||||||
|
|
@ -83,13 +86,9 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None:
|
||||||
hashtag2 = replace_strings(hashtag, replacements2)
|
hashtag2 = replace_strings(hashtag, replacements2)
|
||||||
city_filename = base_dir + '/tags/' + hashtag2 + '.category'
|
city_filename = base_dir + '/tags/' + hashtag2 + '.category'
|
||||||
if not os.path.isfile(city_filename):
|
if not os.path.isfile(city_filename):
|
||||||
try:
|
save_string(category_str, city_filename,
|
||||||
with open(city_filename, 'w+',
|
'EX: unable to write city category ' +
|
||||||
encoding='utf-8') as fp_city:
|
city_filename)
|
||||||
fp_city.write(category_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write city category ' +
|
|
||||||
city_filename)
|
|
||||||
if '-' in hashtag:
|
if '-' in hashtag:
|
||||||
section = hashtag.split('-')
|
section = hashtag.split('-')
|
||||||
new_hashtag: str = ''
|
new_hashtag: str = ''
|
||||||
|
|
@ -99,13 +98,9 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None:
|
||||||
city_filename = \
|
city_filename = \
|
||||||
base_dir + '/tags/' + hashtag2 + '.category'
|
base_dir + '/tags/' + hashtag2 + '.category'
|
||||||
if not os.path.isfile(city_filename):
|
if not os.path.isfile(city_filename):
|
||||||
try:
|
save_string(category_str, city_filename,
|
||||||
with open(city_filename, 'w+',
|
'EX: unable to write city category2 ' +
|
||||||
encoding='utf-8') as fp_city:
|
city_filename)
|
||||||
fp_city.write(category_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write city category2 ' +
|
|
||||||
city_filename)
|
|
||||||
if ' ' in hashtag:
|
if ' ' in hashtag:
|
||||||
section = hashtag.split(' ')
|
section = hashtag.split(' ')
|
||||||
new_hashtag: str = ''
|
new_hashtag: str = ''
|
||||||
|
|
@ -115,13 +110,9 @@ def load_city_hashtags(base_dir: str, translate: {}) -> None:
|
||||||
city_filename = \
|
city_filename = \
|
||||||
base_dir + '/tags/' + hashtag2 + '.category'
|
base_dir + '/tags/' + hashtag2 + '.category'
|
||||||
if not os.path.isfile(city_filename):
|
if not os.path.isfile(city_filename):
|
||||||
try:
|
save_string(category_str, city_filename,
|
||||||
with open(city_filename, 'w+',
|
'EX: unable to write city category3 ' +
|
||||||
encoding='utf-8') as fp_city:
|
city_filename)
|
||||||
fp_city.write(category_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write city category3 ' +
|
|
||||||
city_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def get_hashtag_categories(base_dir: str,
|
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'
|
category_list_str += category_str + '\n'
|
||||||
|
|
||||||
# save a list of available categories for quick lookup
|
# save a list of available categories for quick lookup
|
||||||
try:
|
save_string(category_list_str, category_list_filename,
|
||||||
with open(category_list_filename, 'w+',
|
'EX: unable to write category ' +
|
||||||
encoding='utf-8') as fp_category:
|
category_list_filename)
|
||||||
fp_category.write(category_list_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write category ' + category_list_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def _valid_hashtag_category(category: str) -> bool:
|
def _valid_hashtag_category(category: str) -> bool:
|
||||||
|
|
|
||||||
33
city.py
33
city.py
|
|
@ -19,6 +19,7 @@ import math
|
||||||
from random import randint
|
from random import randint
|
||||||
from utils import acct_dir
|
from utils import acct_dir
|
||||||
from utils import remove_eol
|
from utils import remove_eol
|
||||||
|
from data import load_string
|
||||||
|
|
||||||
# states which the simulated city dweller can be in
|
# states which the simulated city dweller can be in
|
||||||
PERSON_SLEEP = 0
|
PERSON_SLEEP = 0
|
||||||
|
|
@ -230,11 +231,11 @@ def spoof_geolocation(base_dir: str,
|
||||||
default_latdirection, default_longdirection,
|
default_latdirection, default_longdirection,
|
||||||
"", "", 0)
|
"", "", 0)
|
||||||
cities: list[str] = []
|
cities: list[str] = []
|
||||||
try:
|
cities_str = load_string(locations_filename,
|
||||||
with open(locations_filename, 'r', encoding='utf-8') as fp_loc:
|
'EX: unable to read locations ' +
|
||||||
cities = fp_loc.readlines()
|
locations_filename)
|
||||||
except OSError:
|
if cities_str:
|
||||||
print('EX: unable to read locations ' + locations_filename)
|
cities = cities_str.split('\n')
|
||||||
|
|
||||||
nogo = []
|
nogo = []
|
||||||
if nogo_list:
|
if nogo_list:
|
||||||
|
|
@ -242,11 +243,12 @@ def spoof_geolocation(base_dir: str,
|
||||||
else:
|
else:
|
||||||
if os.path.isfile(nogo_filename):
|
if os.path.isfile(nogo_filename):
|
||||||
nogo_list: list[str] = []
|
nogo_list: list[str] = []
|
||||||
try:
|
nogo_list_str = \
|
||||||
with open(nogo_filename, 'r', encoding='utf-8') as fp_nogo:
|
load_string(nogo_filename,
|
||||||
nogo_list = fp_nogo.readlines()
|
'EX: spoof_geolocation unable to read ' +
|
||||||
except OSError:
|
nogo_filename)
|
||||||
print('EX: spoof_geolocation unable to read ' + nogo_filename)
|
if nogo_list_str:
|
||||||
|
nogo_list = nogo_list_str.split('\n')
|
||||||
for line in nogo_list:
|
for line in nogo_list:
|
||||||
if line.startswith(city + ':'):
|
if line.startswith(city + ':'):
|
||||||
polygon = parse_nogo_string(line)
|
polygon = parse_nogo_string(line)
|
||||||
|
|
@ -338,12 +340,11 @@ def get_spoofed_city(city: str, base_dir: str,
|
||||||
city: str = ''
|
city: str = ''
|
||||||
city_filename = acct_dir(base_dir, nickname, domain) + '/city.txt'
|
city_filename = acct_dir(base_dir, nickname, domain) + '/city.txt'
|
||||||
if os.path.isfile(city_filename):
|
if os.path.isfile(city_filename):
|
||||||
try:
|
city1 = load_string(city_filename,
|
||||||
with open(city_filename, 'r', encoding='utf-8') as fp_city:
|
'EX: get_spoofed_city unable to read ' +
|
||||||
city1 = fp_city.read()
|
city_filename)
|
||||||
city = remove_eol(city1)
|
if city1:
|
||||||
except OSError:
|
city = remove_eol(city1)
|
||||||
print('EX: get_spoofed_city unable to read ' + city_filename)
|
|
||||||
return city
|
return city
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue