config.py 3.3 KB

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