config.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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["ROUTE_PREFIX"] = ""
  19. conf["BOOTSTRAP_SERVE_LOCAL"] = True
  20. conf["SECRET_KEY"] = "Please replace me!"
  21. conf["MPD_WAIT_START"] = True
  22. conf["MPD_WAIT_START_RETRYSECS"] = 5
  23. conf["CHECK_SECS"] = 20 # period for checking playlist length
  24. conf["EVENT_TICK_SECS"] = 30 # period for scheduling events
  25. conf["DEBUG"] = False
  26. conf["LOG_CONFIG"] = False
  27. conf["TMPDIR"] = os.getenv("TMPDIR", "/tmp/")
  28. conf["FILE_PATH_SUGGESTION"] = () # tuple of paths
  29. conf["UI_CALENDAR_FREQUENCY_THRESHOLD"] = 4 * 60 * 60 # 4 hours
  30. conf["UI_CALENDAR_DATE_FMT"] = "medium"
  31. conf["EVENT_FILTERS"] = []
  32. conf["HTTP_PORT"] = 5000
  33. conf["HOME_URL"] = "/db/calendar"
  34. conf.update(from_envvars(prefix=prefix))
  35. return conf
  36. def from_envvars(prefix=None, envvars=None, as_json=True):
  37. """Load environment variables in a dictionary
  38. Values are parsed as JSON. If parsing fails with a ValueError,
  39. values are instead used as verbatim strings.
  40. :param prefix: If ``None`` is passed as envvars, all variables from
  41. ``environ`` starting with this prefix are imported. The
  42. prefix is stripped upon import.
  43. :param envvars: A dictionary of mappings of environment-variable-names
  44. to Flask configuration names. If a list is passed
  45. instead, names are mapped 1:1. If ``None``, see prefix
  46. argument.
  47. :param as_json: If False, values will not be parsed as JSON first.
  48. """
  49. conf = {}
  50. if prefix is None and envvars is None:
  51. raise RuntimeError("Must either give prefix or envvars argument")
  52. # if it's a list, convert to dict
  53. if isinstance(envvars, list):
  54. envvars = {k: None for k in envvars}
  55. if not envvars:
  56. envvars = {
  57. k: k[len(prefix) :] for k in os.environ.keys() if k.startswith(prefix)
  58. }
  59. for env_name, name in envvars.items():
  60. if name is None:
  61. name = env_name
  62. if env_name not in os.environ:
  63. continue
  64. if as_json:
  65. try:
  66. conf[name] = json.loads(os.environ[env_name])
  67. except ValueError:
  68. conf[name] = os.environ[env_name]
  69. else:
  70. conf[name] = os.environ[env_name]
  71. return conf