__init__.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. settings = get_appsettings(config['tracim_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. # INFO - G.M - 13-04-2018 - Copy from
  81. # wsgidav.server.run_server._readConfigFile
  82. def _readConfigFile(self, config_file, verbose):
  83. """Read configuration file options into a dictionary."""
  84. if not os.path.exists(config_file):
  85. raise RuntimeError("Couldn't open configuration file '%s'." % config_file)
  86. if config_file.endswith(".json"):
  87. with open(config_file, mode="r", encoding="utf-8") as json_file:
  88. return json.load(json_file)
  89. try:
  90. import imp
  91. conf = {}
  92. configmodule = imp.load_source("configuration_module", config_file)
  93. for k, v in vars(configmodule).items():
  94. if k.startswith("__"):
  95. continue
  96. elif isfunction(v):
  97. continue
  98. conf[k] = v
  99. except Exception as e:
  100. # if verbose >= 1:
  101. # traceback.print_exc()
  102. exceptioninfo = traceback.format_exception_only(sys.exc_type, sys.exc_value)
  103. exceptiontext = ""
  104. for einfo in exceptioninfo:
  105. exceptiontext += einfo + "\n"
  106. # raise RuntimeError("Failed to read configuration file: " + config_file + "\nDue to "
  107. # + exceptiontext)
  108. print("Failed to read configuration file: " + config_file +
  109. "\nDue to " + exceptiontext, file=sys.stderr)
  110. raise
  111. return conf
  112. def get_wsgi_app(self):
  113. return WsgiDAVApp(self.config)
  114. if __name__ == '__main__':
  115. app_factory = WebdavAppFactory()
  116. app = app_factory.get_wsgi_app()
  117. serve(app)