mail.py 993 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/python3
  2. # Style Guide for Python Code https://www.python.org/dev/peps/pep-0008/
  3. # Docstring Conventions https://www.python.org/dev/peps/pep-0257/
  4. import re
  5. class Aliases:
  6. def __init__(self, filename, domain):
  7. self.filename = filename
  8. self.aliases = dict()
  9. self.domain = domain.lower()
  10. with open(self.filename, "r") as f:
  11. for line in f:
  12. match = re.search('^([^#:]+):([^#:]+)(?:#.*|)$', line)
  13. if match:
  14. alias = match.group(1).strip() + '@' + self.domain
  15. to = match.group(2).strip()
  16. if '@' in to:
  17. self.aliases[alias] = to
  18. else:
  19. self.aliases[alias] = to + '@' + self.domain
  20. def get_alias(self, key):
  21. return self.aliases[key.lower()]
  22. def get_aliases(self):
  23. return self.aliases
  24. def __contains__(self, key):
  25. return key.lower() in self.aliases
  26. def __getitem__(self, key):
  27. return self.aliases[key.lower()]
  28. def items(self):
  29. return self.aliases.items()