__init__.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Radicale WSGI application.
  21. Can be used with an external WSGI server or the built-in server.
  22. """
  23. import os
  24. import pkg_resources
  25. import threading
  26. from radicale import config, log
  27. from radicale.app import Application
  28. VERSION = pkg_resources.get_distribution("radicale").version
  29. _application = None
  30. _application_config_path = None
  31. _application_lock = threading.Lock()
  32. def _init_application(config_path, wsgi_errors):
  33. global _application, _application_config_path
  34. with _application_lock:
  35. if _application is not None:
  36. return
  37. log.setup()
  38. with log.register_stream(wsgi_errors):
  39. _application_config_path = config_path
  40. configuration = config.load([config_path] if config_path else [],
  41. ignore_missing_paths=False)
  42. log.set_level(configuration.get("logging", "level"))
  43. _application = Application(configuration)
  44. def application(environ, start_response):
  45. config_path = environ.get("RADICALE_CONFIG",
  46. os.environ.get("RADICALE_CONFIG"))
  47. if _application is None:
  48. _init_application(config_path, environ["wsgi.errors"])
  49. if _application_config_path != config_path:
  50. raise ValueError("RADICALE_CONFIG must not change: %s != %s" %
  51. (repr(config_path), repr(_application_config_path)))
  52. return _application(environ, start_response)