2020-06-29 15:07:22 +00:00
|
|
|
__filename__ = "petnames.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2021-01-26 10:07:42 +00:00
|
|
|
__version__ = "1.2.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-25 16:17:53 +00:00
|
|
|
def setPetName(base_dir: str, nickname: str, domain: str,
|
2020-06-29 15:07:22 +00:00
|
|
|
handle: str, petname: str) -> bool:
|
|
|
|
"""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:]
|
2021-12-26 12:02:29 +00:00
|
|
|
petnamesFilename = 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?
|
|
|
|
if os.path.isfile(petnamesFilename):
|
2021-06-21 22:52:04 +00:00
|
|
|
with open(petnamesFilename, 'r') as petnamesFile:
|
|
|
|
petnamesStr = petnamesFile.read()
|
2020-11-23 15:07:55 +00:00
|
|
|
if entry in petnamesStr:
|
2020-06-29 15:07:22 +00:00
|
|
|
return True
|
|
|
|
if ' ' + handle + '\n' in petnamesStr:
|
|
|
|
petnamesList = petnamesStr.split('\n')
|
|
|
|
newPetnamesStr = ''
|
|
|
|
for pet in petnamesList:
|
|
|
|
if not pet.endswith(' ' + handle):
|
|
|
|
newPetnamesStr += pet + '\n'
|
|
|
|
else:
|
|
|
|
newPetnamesStr += entry
|
|
|
|
# save the updated petnames file
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(petnamesFilename, 'w+') as petnamesFile:
|
|
|
|
petnamesFile.write(newPetnamesStr)
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to save ' + petnamesFilename)
|
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:
|
|
|
|
with open(petnamesFilename, 'a+') as petnamesFile:
|
|
|
|
petnamesFile.write(entry)
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to append ' + petnamesFilename)
|
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:
|
|
|
|
with open(petnamesFilename, 'w+') as petnamesFile:
|
|
|
|
petnamesFile.write(entry)
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + petnamesFilename)
|
2021-11-25 21:18:53 +00:00
|
|
|
return False
|
2020-06-29 15:07:22 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-12-25 16:17:53 +00:00
|
|
|
def getPetName(base_dir: str, nickname: str, domain: str,
|
2020-06-29 15:07:22 +00:00
|
|
|
handle: str) -> str:
|
|
|
|
"""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:]
|
2021-12-26 12:02:29 +00:00
|
|
|
petnamesFilename = acct_dir(base_dir, nickname, domain) + '/petnames.txt'
|
2020-06-29 15:07:22 +00:00
|
|
|
|
|
|
|
if not os.path.isfile(petnamesFilename):
|
2020-06-29 16:25:28 +00:00
|
|
|
return ''
|
2021-06-21 22:52:04 +00:00
|
|
|
with open(petnamesFilename, 'r') as petnamesFile:
|
|
|
|
petnamesStr = petnamesFile.read()
|
2020-06-29 15:07:22 +00:00
|
|
|
if ' ' + handle + '\n' in petnamesStr:
|
|
|
|
petnamesList = petnamesStr.split('\n')
|
|
|
|
for pet in petnamesList:
|
|
|
|
if pet.endswith(' ' + handle):
|
|
|
|
return pet.replace(' ' + handle, '').strip()
|
2021-06-30 13:01:51 +00:00
|
|
|
elif ' ' + handle.lower() + '\n' in petnamesStr.lower():
|
|
|
|
petnamesList = petnamesStr.split('\n')
|
|
|
|
handle = handle.lower()
|
|
|
|
for pet in petnamesList:
|
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-25 16:17:53 +00:00
|
|
|
def _getPetNameHandle(base_dir: str, nickname: str, domain: str,
|
2020-12-22 18:06:23 +00:00
|
|
|
petname: str) -> str:
|
2020-06-29 19:15:51 +00:00
|
|
|
"""Given a petname returns the handle
|
|
|
|
"""
|
|
|
|
if petname.startswith('@'):
|
|
|
|
petname = petname[1:]
|
2021-12-26 12:02:29 +00:00
|
|
|
petnamesFilename = acct_dir(base_dir, nickname, domain) + '/petnames.txt'
|
2020-06-29 19:15:51 +00:00
|
|
|
|
|
|
|
if not os.path.isfile(petnamesFilename):
|
|
|
|
return ''
|
2021-06-21 22:52:04 +00:00
|
|
|
with open(petnamesFilename, 'r') as petnamesFile:
|
|
|
|
petnamesStr = petnamesFile.read()
|
2020-06-29 19:15:51 +00:00
|
|
|
if petname + ' ' in petnamesStr:
|
|
|
|
petnamesList = petnamesStr.split('\n')
|
|
|
|
for pet in petnamesList:
|
|
|
|
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-25 16:17:53 +00:00
|
|
|
def resolvePetnames(base_dir: str, nickname: str, domain: str,
|
2020-06-29 19:15:51 +00:00
|
|
|
content: str) -> str:
|
|
|
|
"""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-25 16:17:53 +00:00
|
|
|
handle = _getPetNameHandle(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
|