config.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. """
  3. Taken from flask-appconfig
  4. """
  5. import json
  6. import os
  7. from xdg import BaseDirectory
  8. def get_conf(prefix="LARIGIRA_"):
  9. """This is where everyone should get configuration from"""
  10. conf_dir = BaseDirectory.save_config_path("larigira")
  11. conf = {}
  12. conf["CONTINOUS_AUDIOSPEC"] = dict(kind="mpd", howmany=1)
  13. conf["MPD_HOST"] = os.getenv("MPD_HOST", "localhost")
  14. conf["MPD_PORT"] = int(os.getenv("MPD_PORT", "6600"))
  15. conf["CACHING_TIME"] = 10
  16. conf["DB_URI"] = os.path.join(conf_dir, "db.json")
  17. conf["SCRIPTS_PATH"] = os.path.join(conf_dir, "scripts")
  18. conf["EXTRA_STATIC_PATH"] = os.path.join(conf_dir, "extra")
  19. conf["EXTRA_MENU_LINKS"] = []
  20. conf["ROUTE_PREFIX"] = ""
  21. conf["BOOTSTRAP_SERVE_LOCAL"] = True
  22. conf["SECRET_KEY"] = "Please replace me!"
  23. conf["MPD_WAIT_START"] = True
  24. conf["MPD_WAIT_START_RETRYSECS"] = 5
  25. conf["CHECK_SECS"] = 20 # period for checking playlist length
  26. conf["EVENT_TICK_SECS"] = 30 # period for scheduling events
  27. conf["DEBUG"] = False
  28. conf["LOG_CONFIG"] = False
  29. conf["TMPDIR"] = os.getenv("TMPDIR", "/tmp/")
  30. conf["FILE_PATH_SUGGESTION"] = () # tuple of paths
  31. conf["UI_CALENDAR_FREQUENCY_THRESHOLD"] = 4 * 60 * 60 # 4 hours
  32. conf["UI_CALENDAR_DATE_FMT"] = "medium"
  33. conf["EVENT_FILTERS"] = []
  34. conf["HTTP_PORT"] = 5000
  35. conf["HOME_URL"] = "/db/calendar"
  36. conf.update(from_envvars(prefix=prefix))
  37. return conf
  38. def from_envvars(prefix=None, envvars=None, as_json=True):
  39. """Load environment variables in a dictionary
  40. Values are parsed as JSON. If parsing fails with a ValueError,
  41. values are instead used as verbatim strings.
  42. :param prefix: If ``None`` is passed as envvars, all variables from
  43. ``environ`` starting with this prefix are imported. The
  44. prefix is stripped upon import.
  45. :param envvars: A dictionary of mappings of environment-variable-names
  46. to Flask configuration names. If a list is passed
  47. instead, names are mapped 1:1. If ``None``, see prefix
  48. argument.
  49. :param as_json: If False, values will not be parsed as JSON first.
  50. """
  51. conf = {}
  52. if prefix is None and envvars is None:
  53. raise RuntimeError("Must either give prefix or envvars argument")
  54. # if it's a list, convert to dict
  55. if isinstance(envvars, list):
  56. envvars = {k: None for k in envvars}
  57. if not envvars:
  58. envvars = {
  59. k: k[len(prefix) :] for k in os.environ.keys() if k.startswith(prefix)
  60. }
  61. for env_name, name in envvars.items():
  62. if name is None:
  63. name = env_name
  64. if env_name not in os.environ:
  65. continue
  66. if as_json:
  67. try:
  68. conf[name] = json.loads(os.environ[env_name])
  69. except ValueError:
  70. conf[name] = os.environ[env_name]
  71. else:
  72. conf[name] = os.environ[env_name]
  73. return conf