__init__.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 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. global_conf = get_appsettings(config['tracim_config']).global_conf
  43. local_conf = get_appsettings(config['tracim_config'], 'tracim_web')
  44. settings = global_conf
  45. settings.update(local_conf)
  46. app_config = CFG(settings)
  47. default_config_file = os.path.abspath(settings['wsgidav.config_path'])
  48. webdav_config_file = self._readConfigFile(
  49. default_config_file,
  50. temp_verbose
  51. )
  52. # Configuration file overrides defaults
  53. config.update(webdav_config_file)
  54. if not useLxml and config["verbose"] >= 1:
  55. print(
  56. "WARNING: Could not import lxml: using xml instead (slower). "
  57. "consider installing lxml from http://codespeak.net/lxml/."
  58. )
  59. config['middleware_stack'] = [
  60. TracimEnforceHTTPS,
  61. WsgiDavDirBrowser,
  62. TracimUserSession,
  63. HTTPAuthenticator,
  64. ErrorPrinter,
  65. TracimWsgiDavDebugFilter,
  66. TracimEnv,
  67. ]
  68. config['provider_mapping'] = {
  69. config['root_path']: Provider(
  70. # TODO: Test to Re enabme archived and deleted
  71. show_archived=False, # config['show_archived'],
  72. show_deleted=False, # config['show_deleted'],
  73. show_history=False, # config['show_history'],
  74. app_config=app_config,
  75. )
  76. }
  77. config['domaincontroller'] = TracimDomainController(
  78. presetdomain=None,
  79. presetserver=None,
  80. app_config = app_config,
  81. )
  82. return config
  83. # INFO - G.M - 13-04-2018 - Copy from
  84. # wsgidav.server.run_server._readConfigFile
  85. def _readConfigFile(self, config_file, verbose):
  86. """Read configuration file options into a dictionary."""
  87. if not os.path.exists(config_file):
  88. raise RuntimeError("Couldn't open configuration file '%s'." % config_file)
  89. if config_file.endswith(".json"):
  90. with open(config_file, mode="r", encoding="utf-8") as json_file:
  91. return json.load(json_file)
  92. try:
  93. import imp
  94. conf = {}
  95. configmodule = imp.load_source("configuration_module", config_file)
  96. for k, v in vars(configmodule).items():
  97. if k.startswith("__"):
  98. continue
  99. elif isfunction(v):
  100. continue
  101. conf[k] = v
  102. except Exception as e:
  103. # if verbose >= 1:
  104. # traceback.print_exc()
  105. exceptioninfo = traceback.format_exception_only(sys.exc_type, sys.exc_value)
  106. exceptiontext = ""
  107. for einfo in exceptioninfo:
  108. exceptiontext += einfo + "\n"
  109. # raise RuntimeError("Failed to read configuration file: " + config_file + "\nDue to "
  110. # + exceptiontext)
  111. print("Failed to read configuration file: " + config_file +
  112. "\nDue to " + exceptiontext, file=sys.stderr)
  113. raise
  114. return conf
  115. def get_wsgi_app(self):
  116. return WsgiDAVApp(self.config)
  117. if __name__ == '__main__':
  118. app_factory = WebdavAppFactory()
  119. app = app_factory.get_wsgi_app()
  120. serve(app)