__init__.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import json
  2. import sys
  3. import os
  4. from pyramid.paster import get_appsettings
  5. from waitress import serve
  6. from wsgidav.wsgidav_app import DEFAULT_CONFIG
  7. from wsgidav.xml_tools import useLxml
  8. from wsgidav.wsgidav_app import WsgiDAVApp
  9. from tracim_backend import CFG
  10. from tracim_backend.lib.utils.utils import DEFAULT_TRACIM_CONFIG_FILE, \
  11. DEFAULT_WEBDAV_CONFIG_FILE
  12. from tracim_backend.lib.webdav.dav_provider import Provider
  13. from tracim_backend.lib.webdav.authentification import TracimDomainController
  14. from wsgidav.dir_browser import WsgiDavDirBrowser
  15. from wsgidav.http_authenticator import HTTPAuthenticator
  16. from wsgidav.error_printer import ErrorPrinter
  17. from tracim_backend.lib.webdav.middlewares import TracimWsgiDavDebugFilter, \
  18. TracimEnforceHTTPS, TracimEnv, TracimUserSession
  19. from inspect import isfunction
  20. import traceback
  21. from tracim_backend.models import get_engine, get_session_factory
  22. class WebdavAppFactory(object):
  23. def __init__(self,
  24. tracim_config_file_path: str = None,
  25. ):
  26. self.config = self._initConfig(
  27. tracim_config_file_path
  28. )
  29. def _initConfig(self,
  30. tracim_config_file_path: str = None
  31. ):
  32. """Setup configuration dictionary from default,
  33. command line and configuration file."""
  34. if not tracim_config_file_path:
  35. tracim_config_file_path = DEFAULT_TRACIM_CONFIG_FILE
  36. # Set config defaults
  37. config = DEFAULT_CONFIG.copy()
  38. temp_verbose = config["verbose"]
  39. # Get pyramid Env
  40. tracim_config_file_path = os.path.abspath(tracim_config_file_path)
  41. config['tracim_config'] = tracim_config_file_path
  42. settings = self._get_tracim_settings(config)
  43. app_config = CFG(settings)
  44. default_config_file = os.path.abspath(settings['wsgidav.config_path'])
  45. webdav_config_file = self._readConfigFile(
  46. default_config_file,
  47. temp_verbose
  48. )
  49. # Configuration file overrides defaults
  50. config.update(webdav_config_file)
  51. if not useLxml and config["verbose"] >= 1:
  52. print(
  53. "WARNING: Could not import lxml: using xml instead (slower). "
  54. "consider installing lxml from http://codespeak.net/lxml/."
  55. )
  56. config['middleware_stack'] = [
  57. TracimEnforceHTTPS,
  58. WsgiDavDirBrowser,
  59. TracimUserSession,
  60. HTTPAuthenticator,
  61. ErrorPrinter,
  62. TracimWsgiDavDebugFilter,
  63. TracimEnv,
  64. ]
  65. config['provider_mapping'] = {
  66. config['root_path']: Provider(
  67. # TODO: Test to Re enabme archived and deleted
  68. show_archived=False, # config['show_archived'],
  69. show_deleted=False, # config['show_deleted'],
  70. show_history=False, # config['show_history'],
  71. app_config=app_config,
  72. )
  73. }
  74. config['domaincontroller'] = TracimDomainController(
  75. presetdomain=None,
  76. presetserver=None,
  77. app_config=app_config,
  78. )
  79. return config
  80. def _get_tracim_settings(
  81. self,
  82. default_config,
  83. ):
  84. """
  85. Get tracim settings
  86. """
  87. global_conf = get_appsettings(default_config['tracim_config']).global_conf
  88. local_conf = get_appsettings(default_config['tracim_config'], 'tracim_web') # nopep8
  89. settings = global_conf
  90. settings.update(local_conf)
  91. return settings
  92. # INFO - G.M - 13-04-2018 - Copy from
  93. # wsgidav.server.run_server._readConfigFile
  94. def _readConfigFile(self, config_file, verbose):
  95. """Read configuration file options into a dictionary."""
  96. if not os.path.exists(config_file):
  97. raise RuntimeError("Couldn't open configuration file '%s'." % config_file)
  98. if config_file.endswith(".json"):
  99. with open(config_file, mode="r", encoding="utf-8") as json_file:
  100. return json.load(json_file)
  101. try:
  102. import imp
  103. conf = {}
  104. configmodule = imp.load_source("configuration_module", config_file)
  105. for k, v in vars(configmodule).items():
  106. if k.startswith("__"):
  107. continue
  108. elif isfunction(v):
  109. continue
  110. conf[k] = v
  111. except Exception as e:
  112. # if verbose >= 1:
  113. # traceback.print_exc()
  114. exceptioninfo = traceback.format_exception_only(sys.exc_type, sys.exc_value)
  115. exceptiontext = ""
  116. for einfo in exceptioninfo:
  117. exceptiontext += einfo + "\n"
  118. # raise RuntimeError("Failed to read configuration file: " + config_file + "\nDue to "
  119. # + exceptiontext)
  120. print("Failed to read configuration file: " + config_file +
  121. "\nDue to " + exceptiontext, file=sys.stderr)
  122. raise
  123. return conf
  124. def get_wsgi_app(self):
  125. return WsgiDAVApp(self.config)
  126. if __name__ == '__main__':
  127. app_factory = WebdavAppFactory()
  128. app = app_factory.get_wsgi_app()
  129. serve(app)