__init__.py 4.9KB

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