Moved conversion from domain to username into filter with shorts table
parent
c7f5382c71
commit
45645de557
@ -0,0 +1,25 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
|
||||
NOT_ALLOWED_CHARS = re.compile(r'[^A-Za-z0-9-]+')
|
||||
DOMAIN_SHORTS = Path(__file__).parent / '..' / 'public_keys/domain_shorts'
|
||||
|
||||
def rreplace(text, to_replace, replacement, count=1):
|
||||
return replacement.join(text.rsplit(to_replace, count))
|
||||
|
||||
def domain_to_username(domain):
|
||||
with DOMAIN_SHORTS.open() as f:
|
||||
for l in f:
|
||||
long_domain, _, short_domain = l.strip().partition(' ')
|
||||
if domain.endswith(long_domain):
|
||||
domain = rreplace(domain, long_domain, short_domain)
|
||||
break
|
||||
return NOT_ALLOWED_CHARS.sub('-', domain)
|
||||
|
||||
class FilterModule(object):
|
||||
def filters(self):
|
||||
return {'domain_to_username': domain_to_username}
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(domain_to_username(sys.argv[1]))
|
Loading…
Reference in New Issue