epicyon/petnames.py

128 lines
4.3 KiB
Python
Raw Normal View History

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-07-13 21:59:53 +00:00
from utils import acctDir
2020-06-29 15:07:22 +00:00
def setPetName(baseDir: str, nickname: str, domain: str,
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-07-13 21:59:53 +00:00
petnamesFilename = acctDir(baseDir, 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):
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
with open(petnamesFilename, 'w+') as petnamesFile:
petnamesFile.write(newPetnamesStr)
2020-06-29 15:07:22 +00:00
return True
# entry does not exist in the petnames file
with open(petnamesFilename, 'a+') as petnamesFile:
petnamesFile.write(entry)
2020-06-29 15:07:22 +00:00
return True
# first entry
with open(petnamesFilename, 'w+') as petnamesFile:
petnamesFile.write(entry)
2020-06-29 15:07:22 +00:00
return True
def getPetName(baseDir: str, nickname: str, domain: str,
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-07-13 21:59:53 +00:00
petnamesFilename = acctDir(baseDir, 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 ''
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
def _getPetNameHandle(baseDir: 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:]
2021-07-13 21:59:53 +00:00
petnamesFilename = acctDir(baseDir, nickname, domain) + '/petnames.txt'
2020-06-29 19:15:51 +00:00
if not os.path.isfile(petnamesFilename):
return ''
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 ''
def resolvePetnames(baseDir: str, nickname: str, domain: str,
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?
handle = _getPetNameHandle(baseDir, 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