epicyon/petnames.py

142 lines
5.0 KiB
Python
Raw Normal View History

2020-06-29 15:07:22 +00:00
__filename__ = "petnames.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2023-01-21 23:03:30 +00:00
__version__ = "1.4.0"
2020-06-29 15:07:22 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-06-29 15:07:22 +00:00
__status__ = "Production"
2021-06-26 11:16:41 +00:00
__module_group__ = "Core"
2020-06-29 15:07:22 +00:00
import os
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2020-06-29 15:07:22 +00:00
2021-12-29 21:55:09 +00:00
def set_pet_name(base_dir: str, nickname: str, domain: str,
handle: str, petname: str) -> bool:
2020-06-29 15:07:22 +00:00
"""Adds a new petname
"""
if '@' not in handle:
return False
2020-06-29 15:23:52 +00:00
if ' ' in petname:
petname = petname.replace(' ', '_')
2020-06-29 15:07:22 +00:00
if handle.startswith('@'):
handle = handle[1:]
if petname.startswith('@'):
petname = petname[1:]
2022-01-03 15:06:00 +00:00
petnames_filename = acct_dir(base_dir, nickname, domain) + '/petnames.txt'
2020-06-29 15:07:22 +00:00
entry = petname + ' ' + handle + '\n'
# does this entry already exist?
2022-01-03 15:06:00 +00:00
if os.path.isfile(petnames_filename):
2022-06-09 14:46:30 +00:00
with open(petnames_filename, 'r', encoding='utf-8') as petnames_file:
2022-01-03 15:06:00 +00:00
petnames_str = petnames_file.read()
if entry in petnames_str:
2020-06-29 15:07:22 +00:00
return True
2022-01-03 15:06:00 +00:00
if ' ' + handle + '\n' in petnames_str:
petnames_list = petnames_str.split('\n')
new_petnames_str = ''
for pet in petnames_list:
2020-06-29 15:07:22 +00:00
if not pet.endswith(' ' + handle):
2022-01-03 15:06:00 +00:00
new_petnames_str += pet + '\n'
2020-06-29 15:07:22 +00:00
else:
2022-01-03 15:06:00 +00:00
new_petnames_str += entry
2020-06-29 15:07:22 +00:00
# save the updated petnames file
2021-11-25 21:18:53 +00:00
try:
2022-06-09 14:46:30 +00:00
with open(petnames_filename, 'w+',
encoding='utf-8') as petnames_file:
2022-01-03 15:06:00 +00:00
petnames_file.write(new_petnames_str)
2021-11-25 21:18:53 +00:00
except OSError:
2022-01-03 15:06:00 +00:00
print('EX: unable to save ' + petnames_filename)
2021-11-25 21:18:53 +00:00
return False
2020-06-29 15:07:22 +00:00
return True
# entry does not exist in the petnames file
2021-11-25 21:18:53 +00:00
try:
2022-06-09 14:46:30 +00:00
with open(petnames_filename, 'a+',
encoding='utf-8') as petnames_file:
2022-01-03 15:06:00 +00:00
petnames_file.write(entry)
2021-11-25 21:18:53 +00:00
except OSError:
2022-01-03 15:06:00 +00:00
print('EX: unable to append ' + petnames_filename)
2021-11-25 21:18:53 +00:00
return False
2020-06-29 15:07:22 +00:00
return True
# first entry
2021-11-25 21:18:53 +00:00
try:
2022-06-09 14:46:30 +00:00
with open(petnames_filename, 'w+', encoding='utf-8') as petnames_file:
2022-01-03 15:06:00 +00:00
petnames_file.write(entry)
2021-11-25 21:18:53 +00:00
except OSError:
2022-01-03 15:06:00 +00:00
print('EX: unable to write ' + petnames_filename)
2021-11-25 21:18:53 +00:00
return False
2020-06-29 15:07:22 +00:00
return True
2021-12-29 21:55:09 +00:00
def get_pet_name(base_dir: str, nickname: str, domain: str,
handle: str) -> str:
2020-06-29 15:07:22 +00:00
"""Given a handle returns the petname
"""
if '@' not in handle:
2020-06-29 16:25:28 +00:00
return ''
2020-06-29 15:07:22 +00:00
if handle.startswith('@'):
handle = handle[1:]
2022-01-03 15:06:00 +00:00
petnames_filename = acct_dir(base_dir, nickname, domain) + '/petnames.txt'
2020-06-29 15:07:22 +00:00
2022-01-03 15:06:00 +00:00
if not os.path.isfile(petnames_filename):
2020-06-29 16:25:28 +00:00
return ''
2022-06-09 14:46:30 +00:00
with open(petnames_filename, 'r', encoding='utf-8') as petnames_file:
2022-01-03 15:06:00 +00:00
petnames_str = petnames_file.read()
if ' ' + handle + '\n' in petnames_str:
petnames_list = petnames_str.split('\n')
for pet in petnames_list:
2020-06-29 15:07:22 +00:00
if pet.endswith(' ' + handle):
return pet.replace(' ' + handle, '').strip()
2022-01-03 15:06:00 +00:00
elif ' ' + handle.lower() + '\n' in petnames_str.lower():
petnames_list = petnames_str.split('\n')
2021-06-30 13:01:51 +00:00
handle = handle.lower()
2022-01-03 15:06:00 +00:00
for pet in petnames_list:
2021-06-30 13:06:48 +00:00
if pet.lower().endswith(' ' + handle):
handle2 = pet.split(' ')[-1]
return pet.replace(' ' + handle2, '').strip()
2020-06-29 16:25:28 +00:00
return ''
2020-06-29 19:15:51 +00:00
2021-12-29 21:55:09 +00:00
def _get_pet_name_handle(base_dir: str, nickname: str, domain: str,
petname: str) -> str:
2020-06-29 19:15:51 +00:00
"""Given a petname returns the handle
"""
if petname.startswith('@'):
petname = petname[1:]
2022-01-03 15:06:00 +00:00
petnames_filename = acct_dir(base_dir, nickname, domain) + '/petnames.txt'
2020-06-29 19:15:51 +00:00
2022-01-03 15:06:00 +00:00
if not os.path.isfile(petnames_filename):
2020-06-29 19:15:51 +00:00
return ''
2022-06-09 14:46:30 +00:00
with open(petnames_filename, 'r', encoding='utf-8') as petnames_file:
2022-01-03 15:06:00 +00:00
petnames_str = petnames_file.read()
if petname + ' ' in petnames_str:
petnames_list = petnames_str.split('\n')
for pet in petnames_list:
2020-06-29 19:15:51 +00:00
if pet.startswith(petname + ' '):
2020-06-29 19:35:14 +00:00
handle = pet.replace(petname + ' ', '').strip()
return handle
2020-06-29 19:15:51 +00:00
return ''
2021-12-29 21:55:09 +00:00
def resolve_petnames(base_dir: str, nickname: str, domain: str,
content: str) -> str:
2020-06-29 19:15:51 +00:00
"""Replaces petnames with their full handles
"""
if not content:
return content
if ' ' not in content:
return content
words = content.strip().split(' ')
for wrd in words:
# check initial words beginning with @
if not wrd.startswith('@'):
break
# does a petname handle exist for this?
2021-12-29 21:55:09 +00:00
handle = _get_pet_name_handle(base_dir, nickname, domain, wrd)
2020-06-29 19:15:51 +00:00
if not handle:
continue
# replace the petname with the handle
2020-06-29 19:39:15 +00:00
content = content.replace(wrd + ' ', '@' + handle + ' ')
2020-06-29 19:15:51 +00:00
return content