__init__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import logging
  2. from os import environ, path
  3. from dotenv import load_dotenv
  4. env = load_dotenv()
  5. APP_ROOT = path.dirname(path.realpath(__file__))
  6. """
  7. SQLite database path.
  8. This DB stores all the runtime transliteration data.
  9. """
  10. DB_PATH = environ.get(
  11. "DB_PATH", path.join(APP_ROOT, "data", "scriptshifter.db"))
  12. """
  13. SMTP server for sending email. For a dummy server that just echoes the
  14. messages, run: `python -m smtpd -n -c DebuggingServer localhost:1025`
  15. and set SMTP_HOST to "localhost".
  16. The default is None which causes the feedback form to be disabled.
  17. """
  18. SMTP_HOST = environ.get("TXL_SMTP_HOST")
  19. """
  20. Folder path for the serialized feedback form.
  21. The feedback message shall be written to this folder as a text file for further
  22. processing in case a SMTP server is not available. The file name will have
  23. a unique random name.
  24. This only takes effect if SMTP_HOST is not set.
  25. """
  26. FEEDBACK_PATH = environ.get("TXL_FEEDBACK_PATH")
  27. with open(path.join(path.dirname(APP_ROOT), "VERSION")) as fh:
  28. version_info = fh.readlines()
  29. GIT_TAG = version_info[0].strip()
  30. GIT_COMMIT = version_info[1].strip()
  31. logging.basicConfig(
  32. # filename=environ.get("TXL_LOGFILE", "/dev/stdout"),
  33. level=environ.get("TXL_LOGLEVEL", logging.WARN))
  34. logger = logging.getLogger(__name__)
  35. if not env:
  36. logger.warn("No .env file found. Assuming env was passed externally.")
  37. if SMTP_HOST or FEEDBACK_PATH:
  38. EMAIL_FROM = environ["TXL_EMAIL_FROM"]
  39. EMAIL_TO = environ["TXL_EMAIL_TO"]
  40. try:
  41. SMTP_PORT = int(environ.get("TXL_SMTP_PORT", "1025"))
  42. except ValueError:
  43. raise SystemError("TXL_SMTP_PORT env var is not an integer.")
  44. if not SMTP_HOST:
  45. if FEEDBACK_PATH:
  46. logger.info(
  47. "No SMTP host defined. Feedback messages will be written "
  48. f"to files under {FEEDBACK_PATH}.")
  49. else:
  50. logger.warn(
  51. "No SMTP host or feedback message folder defined. "
  52. "Feedback form won't be available.")
  53. SMTP_PORT = EMAIL_FROM = EMAIL_TO = None