epicyon/data.py

166 lines
4.9 KiB
Python
Raw Normal View History

__filename__ = "data.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.7.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Core"
2026-05-01 22:15:26 +00:00
import os
def _store_base(text: str, filename: str, exception_text: str,
mode: str) -> bool:
"""Saves a string to file
"""
try:
with open(filename, mode, encoding='utf-8') as fp:
fp.write(text)
return True
2026-04-25 16:37:13 +00:00
except OSError as exc:
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
def load_string(filename: str, exception_text: str) -> str:
"""Loads a string from file
"""
try:
with open(filename, 'r', encoding='utf-8') as fp:
2026-04-28 16:07:48 +00:00
text: str = fp.read()
return text
2026-04-25 16:37:13 +00:00
except OSError as exc:
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
def load_binary(filename: str, exception_text: str) -> str:
"""Loads a binary from file
"""
try:
with open(filename, 'rb') as fp:
binary = fp.read()
return binary
except OSError as exc:
if '[ex]' in exception_text:
exception_text = exception_text.replace('[ex]', str(exc))
print(exception_text)
return None
2026-04-27 13:25:46 +00:00
def load_line(filename: str, exception_text: str) -> str:
"""Loads a line of text from file
"""
try:
with open(filename, 'r', encoding='utf-8') as fp:
2026-04-28 16:07:48 +00:00
text: str = fp.readline()
2026-04-27 13:25:46 +00:00
return text
except OSError as exc:
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)
2026-04-27 13:25:46 +00:00
return None
2026-04-26 14:32:54 +00:00
def load_list(filename: str, exception_text: str) -> str:
"""Loads a list from file
This is used to replace readlines
"""
lines: list[str] = []
2026-04-28 16:07:48 +00:00
lines_str: str = load_string(filename, exception_text)
2026-04-27 17:34:33 +00:00
if lines_str is None:
return None
2026-04-26 14:32:54 +00:00
if lines_str:
lines2 = lines_str.split('\n')
for line in lines2:
if not line:
continue
lines.append(line + '\n')
return lines
def save_string(text: str, filename: str, exception_text: str) -> bool:
"""Saves a string to file
"""
return _store_base(text, filename, exception_text, 'w+')
2026-04-28 14:36:12 +00:00
def save_flag_file(filename: str, exception_text: str) -> bool:
"""Saves a flag file
This creates a file containing a single character, and the presence
or absence of the file is used to implement the checkbox profile
options
"""
return _store_base('\n', filename, exception_text, 'w+')
def save_binary(text: str, filename: str, exception_text: str) -> bool:
"""Saves a binary to file
"""
try:
with open(filename, 'wb') as fp:
fp.write(text)
return True
except OSError as exc:
if '[ex]' in exception_text:
exception_text = exception_text.replace('[ex]', str(exc))
print(exception_text)
return False
def append_string(text: str, filename: str, exception_text: str) -> bool:
"""Appends a string to file
"""
return _store_base(text, filename, exception_text, 'a+')
def prepend_string(text: str, filename: str, exception_text: str) -> bool:
"""Prepends a string to a file
"""
try:
with open(filename, 'r+', encoding='utf-8') as fp:
content: str = fp.read()
if text + '\n' not in content:
fp.seek(0, 0)
fp.write(text + '\n' + content)
return True
except OSError as exc:
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
2026-05-01 22:15:26 +00:00
def remove_file(filename: str, exception_text: str) -> bool:
"""Deletes a file
"""
try:
os.remove(filename)
return True
except OSError as exc:
if '[ex]' in exception_text:
exception_text = exception_text.replace('[ex]', str(exc))
print(exception_text)
return False