config.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from subprocess import check_output
  2. import yaml
  3. import os
  4. import errno
  5. import logging, logging.handlers
  6. currentdir = os.path.dirname(os.path.abspath(__file__))
  7. # Load must be called
  8. config = {}
  9. def load(filename: str):
  10. global config
  11. if not os.path.isfile(filename):
  12. raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
  13. with open(filename, "r") as fp:
  14. config = yaml.safe_load(fp)
  15. if "debug" in config and config["debug"]:
  16. level = logging.DEBUG
  17. else:
  18. level = logging.INFO
  19. root = logging.getLogger(None)
  20. ch = logging.StreamHandler()
  21. ch.setLevel(level)
  22. formatter = logging.Formatter(
  23. "%(asctime)s - %(filename)s (%(lineno)d) - %(name)s - %(levelname)s - %(message)s"
  24. )
  25. ch.setFormatter(formatter)
  26. root.addHandler(ch)
  27. if "logfile" in config and config["logfile"]:
  28. fhlog = logging.handlers.TimedRotatingFileHandler(
  29. filename=os.path.join(currentdir, config["logfile"]),
  30. when="midnight",
  31. backupCount=7,
  32. )
  33. fhlog.setFormatter(formatter)
  34. fhlog.setLevel(level)
  35. root.addHandler(fhlog)
  36. root.setLevel(level)
  37. # print("config:", config)
  38. # with open('logging.config', 'r') as fp:
  39. # LOGGING_CONFIG = yaml.safe_load(fp)
  40. # Extract the version information from git.
  41. # The match gives us only tags starting with v[0-9]* Using anything else trips up on double digits.
  42. version = check_output(
  43. [
  44. "git",
  45. "describe",
  46. "--abbrev=8",
  47. "--long",
  48. "--tags",
  49. "--dirty",
  50. "--always",
  51. "--match",
  52. "v[0-9]*",
  53. ],
  54. universal_newlines=True,
  55. ).strip()