config.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. from paste.deploy.converters import asbool
  2. from urllib.parse import urlparse
  3. from tracim.logger import logger
  4. from depot.manager import DepotManager
  5. from pyramid.request import Request
  6. class RequestWithCFG(Request):
  7. def config(self):
  8. cfg = CFG(self.registry.settings)
  9. cfg.configure_filedepot()
  10. return cfg
  11. class CFG(object):
  12. """Object used for easy access to config file parameters."""
  13. def __setattr__(self, key, value):
  14. """
  15. Log-ready setter.
  16. Logs all configuration parameters except password.
  17. :param key:
  18. :param value:
  19. :return:
  20. """
  21. if 'PASSWORD' not in key and \
  22. ('URL' not in key or type(value) == str) and \
  23. 'CONTENT' not in key:
  24. # We do not show PASSWORD for security reason
  25. # we do not show URL because At the time of configuration setup,
  26. # it can't be evaluated
  27. # We do not show CONTENT in order not to pollute log files
  28. logger.info(self, 'CONFIG: [ {} | {} ]'.format(key, value))
  29. else:
  30. logger.info(self, 'CONFIG: [ {} | <value not shown> ]'.format(key))
  31. self.__dict__[key] = value
  32. def __init__(self, settings):
  33. """Parse configuration file."""
  34. ###
  35. # General
  36. ###
  37. mandatory_msg = \
  38. 'ERROR: {} configuration is mandatory. Set it before continuing.'
  39. self.DEPOT_STORAGE_DIR = settings.get(
  40. 'depot_storage_dir',
  41. )
  42. if not self.DEPOT_STORAGE_DIR:
  43. raise Exception(
  44. mandatory_msg.format('depot_storage_dir')
  45. )
  46. self.DEPOT_STORAGE_NAME = settings.get(
  47. 'depot_storage_name',
  48. )
  49. if not self.DEPOT_STORAGE_NAME:
  50. raise Exception(
  51. mandatory_msg.format('depot_storage_name')
  52. )
  53. self.PREVIEW_CACHE_DIR = settings.get(
  54. 'preview_cache_dir',
  55. )
  56. if not self.PREVIEW_CACHE_DIR:
  57. raise Exception(
  58. 'ERROR: preview_cache_dir configuration is mandatory. '
  59. 'Set it before continuing.'
  60. )
  61. self.DATA_UPDATE_ALLOWED_DURATION = int(settings.get(
  62. 'content.update.allowed.duration',
  63. 0,
  64. ))
  65. self.WEBSITE_TITLE = settings.get(
  66. 'website.title',
  67. 'TRACIM',
  68. )
  69. self.WEBSITE_BASE_URL = settings.get(
  70. 'website.base_url',
  71. '',
  72. )
  73. # TODO - G.M - 26-03-2018 - [Cleanup] These params seems deprecated for tracimv2, # nopep8
  74. # Verify this
  75. #
  76. # self.WEBSITE_HOME_TITLE_COLOR = settings.get(
  77. # 'website.title.color',
  78. # '#555',
  79. # )
  80. # self.WEBSITE_HOME_IMAGE_PATH = settings.get(
  81. # '/assets/img/home_illustration.jpg',
  82. # )
  83. # self.WEBSITE_HOME_BACKGROUND_IMAGE_PATH = settings.get(
  84. # '/assets/img/bg.jpg',
  85. # )
  86. #
  87. self.WEBSITE_SERVER_NAME = settings.get(
  88. 'website.server_name',
  89. None,
  90. )
  91. if not self.WEBSITE_SERVER_NAME:
  92. self.WEBSITE_SERVER_NAME = urlparse(self.WEBSITE_BASE_URL).hostname
  93. logger.warning(
  94. self,
  95. 'NOTE: Generated website.server_name parameter from '
  96. 'website.base_url parameter -> {0}'
  97. .format(self.WEBSITE_SERVER_NAME)
  98. )
  99. self.WEBSITE_HOME_TAG_LINE = settings.get(
  100. 'website.home.tag_line',
  101. '',
  102. )
  103. self.WEBSITE_SUBTITLE = settings.get(
  104. 'website.home.subtitle',
  105. '',
  106. )
  107. self.WEBSITE_HOME_BELOW_LOGIN_FORM = settings.get(
  108. 'website.home.below_login_form',
  109. '',
  110. )
  111. self.WEBSITE_TREEVIEW_CONTENT = settings.get(
  112. 'website.treeview.content',
  113. )
  114. self.USER_AUTH_TOKEN_VALIDITY = int(settings.get(
  115. 'user.auth_token.validity',
  116. '604800',
  117. ))
  118. # TODO - G.M - 27-03-2018 - [Email] Restore email config
  119. ###
  120. # EMAIL related stuff (notification, reply)
  121. ###
  122. #
  123. # self.EMAIL_NOTIFICATION_NOTIFIED_EVENTS = [
  124. # # ActionDescription.COMMENT,
  125. # # ActionDescription.CREATION,
  126. # # ActionDescription.EDITION,
  127. # # ActionDescription.REVISION,
  128. # # ActionDescription.STATUS_UPDATE
  129. # ]
  130. #
  131. # self.EMAIL_NOTIFICATION_NOTIFIED_CONTENTS = [
  132. # # ContentType.Page,
  133. # # ContentType.Thread,
  134. # # ContentType.File,
  135. # # ContentType.Comment,
  136. # # ContentType.Folder -- Folder is skipped
  137. # ]
  138. # if settings.get('email.notification.from'):
  139. # raise Exception(
  140. # 'email.notification.from configuration is deprecated. '
  141. # 'Use instead email.notification.from.email and '
  142. # 'email.notification.from.default_label.'
  143. # )
  144. #
  145. # self.EMAIL_NOTIFICATION_FROM_EMAIL = settings.get(
  146. # 'email.notification.from.email',
  147. # )
  148. # self.EMAIL_NOTIFICATION_FROM_DEFAULT_LABEL = settings.get(
  149. # 'email.notification.from.default_label'
  150. # )
  151. # self.EMAIL_NOTIFICATION_REPLY_TO_EMAIL = settings.get(
  152. # 'email.notification.reply_to.email',
  153. # )
  154. # self.EMAIL_NOTIFICATION_REFERENCES_EMAIL = settings.get(
  155. # 'email.notification.references.email'
  156. # )
  157. # self.EMAIL_NOTIFICATION_CONTENT_UPDATE_TEMPLATE_HTML = settings.get(
  158. # 'email.notification.content_update.template.html',
  159. # )
  160. # self.EMAIL_NOTIFICATION_CONTENT_UPDATE_TEMPLATE_TEXT = settings.get(
  161. # 'email.notification.content_update.template.text',
  162. # )
  163. # self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_HTML = settings.get(
  164. # 'email.notification.created_account.template.html',
  165. # './tracim/templates/mail/created_account_body_html.mak',
  166. # )
  167. # self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_TEXT = settings.get(
  168. # 'email.notification.created_account.template.text',
  169. # './tracim/templates/mail/created_account_body_text.mak',
  170. # )
  171. # self.EMAIL_NOTIFICATION_CONTENT_UPDATE_SUBJECT = settings.get(
  172. # 'email.notification.content_update.subject',
  173. # )
  174. # self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_SUBJECT = settings.get(
  175. # 'email.notification.created_account.subject',
  176. # '[{website_title}] Created account',
  177. # )
  178. # self.EMAIL_NOTIFICATION_PROCESSING_MODE = settings.get(
  179. # 'email.notification.processing_mode',
  180. # )
  181. #
  182. # self.EMAIL_NOTIFICATION_ACTIVATED = asbool(settings.get(
  183. # 'email.notification.activated',
  184. # ))
  185. # self.EMAIL_NOTIFICATION_SMTP_SERVER = settings.get(
  186. # 'email.notification.smtp.server',
  187. # )
  188. # self.EMAIL_NOTIFICATION_SMTP_PORT = settings.get(
  189. # 'email.notification.smtp.port',
  190. # )
  191. # self.EMAIL_NOTIFICATION_SMTP_USER = settings.get(
  192. # 'email.notification.smtp.user',
  193. # )
  194. # self.EMAIL_NOTIFICATION_SMTP_PASSWORD = settings.get(
  195. # 'email.notification.smtp.password',
  196. # )
  197. # self.EMAIL_NOTIFICATION_LOG_FILE_PATH = settings.get(
  198. # 'email.notification.log_file_path',
  199. # None,
  200. # )
  201. #
  202. # self.EMAIL_REPLY_ACTIVATED = asbool(settings.get(
  203. # 'email.reply.activated',
  204. # False,
  205. # ))
  206. #
  207. # self.EMAIL_REPLY_IMAP_SERVER = settings.get(
  208. # 'email.reply.imap.server',
  209. # )
  210. # self.EMAIL_REPLY_IMAP_PORT = settings.get(
  211. # 'email.reply.imap.port',
  212. # )
  213. # self.EMAIL_REPLY_IMAP_USER = settings.get(
  214. # 'email.reply.imap.user',
  215. # )
  216. # self.EMAIL_REPLY_IMAP_PASSWORD = settings.get(
  217. # 'email.reply.imap.password',
  218. # )
  219. # self.EMAIL_REPLY_IMAP_FOLDER = settings.get(
  220. # 'email.reply.imap.folder',
  221. # )
  222. # self.EMAIL_REPLY_CHECK_HEARTBEAT = int(settings.get(
  223. # 'email.reply.check.heartbeat',
  224. # 60,
  225. # ))
  226. # self.EMAIL_REPLY_TOKEN = settings.get(
  227. # 'email.reply.token',
  228. # )
  229. # self.EMAIL_REPLY_IMAP_USE_SSL = asbool(settings.get(
  230. # 'email.reply.imap.use_ssl',
  231. # ))
  232. # self.EMAIL_REPLY_IMAP_USE_IDLE = asbool(settings.get(
  233. # 'email.reply.imap.use_idle',
  234. # True,
  235. # ))
  236. # self.EMAIL_REPLY_CONNECTION_MAX_LIFETIME = int(settings.get(
  237. # 'email.reply.connection.max_lifetime',
  238. # 600, # 10 minutes
  239. # ))
  240. # self.EMAIL_REPLY_USE_HTML_PARSING = asbool(settings.get(
  241. # 'email.reply.use_html_parsing',
  242. # True,
  243. # ))
  244. # self.EMAIL_REPLY_USE_TXT_PARSING = asbool(settings.get(
  245. # 'email.reply.use_txt_parsing',
  246. # True,
  247. # ))
  248. # self.EMAIL_REPLY_LOCKFILE_PATH = settings.get(
  249. # 'email.reply.lockfile_path',
  250. # ''
  251. # )
  252. # if not self.EMAIL_REPLY_LOCKFILE_PATH and self.EMAIL_REPLY_ACTIVATED:
  253. # raise Exception(
  254. # mandatory_msg.format('email.reply.lockfile_path')
  255. # )
  256. #
  257. # self.EMAIL_PROCESSING_MODE = settings.get(
  258. # 'email.processing_mode',
  259. # 'sync',
  260. # ).upper()
  261. #
  262. # if self.EMAIL_PROCESSING_MODE not in (
  263. # self.CST.ASYNC,
  264. # self.CST.SYNC,
  265. # ):
  266. # raise Exception(
  267. # 'email.processing_mode '
  268. # 'can ''be "{}" or "{}", not "{}"'.format(
  269. # self.CST.ASYNC,
  270. # self.CST.SYNC,
  271. # self.EMAIL_PROCESSING_MODE,
  272. # )
  273. # )
  274. #
  275. # self.EMAIL_SENDER_REDIS_HOST = settings.get(
  276. # 'email.async.redis.host',
  277. # 'localhost',
  278. # )
  279. # self.EMAIL_SENDER_REDIS_PORT = int(settings.get(
  280. # 'email.async.redis.port',
  281. # 6379,
  282. # ))
  283. # self.EMAIL_SENDER_REDIS_DB = int(settings.get(
  284. # 'email.async.redis.db',
  285. # 0,
  286. # ))
  287. ###
  288. # WSGIDAV (Webdav server)
  289. ###
  290. # TODO - G.M - 27-03-2018 - [WebDav] Restore wsgidav config
  291. #self.WSGIDAV_CONFIG_PATH = settings.get(
  292. # 'wsgidav.config_path',
  293. # 'wsgidav.conf',
  294. #)
  295. # TODO: Convert to importlib
  296. # http://stackoverflow.com/questions/41063938/use-importlib-instead-imp-for-non-py-file
  297. #self.wsgidav_config = imp.load_source(
  298. # 'wsgidav_config',
  299. # self.WSGIDAV_CONFIG_PATH,
  300. #)
  301. # self.WSGIDAV_PORT = self.wsgidav_config.port
  302. # self.WSGIDAV_CLIENT_BASE_URL = settings.get(
  303. # 'wsgidav.client.base_url',
  304. # None,
  305. # )
  306. #
  307. # if not self.WSGIDAV_CLIENT_BASE_URL:
  308. # self.WSGIDAV_CLIENT_BASE_URL = \
  309. # '{0}:{1}'.format(
  310. # self.WEBSITE_SERVER_NAME,
  311. # self.WSGIDAV_PORT,
  312. # )
  313. # logger.warning(self,
  314. # 'NOTE: Generated wsgidav.client.base_url parameter with '
  315. # 'followings parameters: website.server_name and '
  316. # 'wsgidav.conf port'.format(
  317. # self.WSGIDAV_CLIENT_BASE_URL,
  318. # )
  319. # )
  320. #
  321. # if not self.WSGIDAV_CLIENT_BASE_URL.endswith('/'):
  322. # self.WSGIDAV_CLIENT_BASE_URL += '/'
  323. # TODO - G.M - 27-03-2018 - [Caldav] Restore radicale config
  324. ###
  325. # RADICALE (Caldav server)
  326. ###
  327. # self.RADICALE_SERVER_HOST = settings.get(
  328. # 'radicale.server.host',
  329. # '127.0.0.1',
  330. # )
  331. # self.RADICALE_SERVER_PORT = int(settings.get(
  332. # 'radicale.server.port',
  333. # 5232,
  334. # ))
  335. # # Note: Other parameters needed to work in SSL (cert file, etc)
  336. # self.RADICALE_SERVER_SSL = asbool(settings.get(
  337. # 'radicale.server.ssl',
  338. # False,
  339. # ))
  340. # self.RADICALE_SERVER_FILE_SYSTEM_FOLDER = settings.get(
  341. # 'radicale.server.filesystem.folder',
  342. # )
  343. # if not self.RADICALE_SERVER_FILE_SYSTEM_FOLDER:
  344. # raise Exception(
  345. # mandatory_msg.format('radicale.server.filesystem.folder')
  346. # )
  347. # self.RADICALE_SERVER_ALLOW_ORIGIN = settings.get(
  348. # 'radicale.server.allow_origin',
  349. # None,
  350. # )
  351. # if not self.RADICALE_SERVER_ALLOW_ORIGIN:
  352. # self.RADICALE_SERVER_ALLOW_ORIGIN = self.WEBSITE_BASE_URL
  353. # logger.warning(self,
  354. # 'NOTE: Generated radicale.server.allow_origin parameter with '
  355. # 'followings parameters: website.base_url ({0})'
  356. # .format(self.WEBSITE_BASE_URL)
  357. # )
  358. #
  359. # self.RADICALE_SERVER_REALM_MESSAGE = settings.get(
  360. # 'radicale.server.realm_message',
  361. # 'Tracim Calendar - Password Required',
  362. # )
  363. #
  364. # self.RADICALE_CLIENT_BASE_URL_HOST = settings.get(
  365. # 'radicale.client.base_url.host',
  366. # 'http://{}:{}'.format(
  367. # self.RADICALE_SERVER_HOST,
  368. # self.RADICALE_SERVER_PORT,
  369. # ),
  370. # )
  371. #
  372. # self.RADICALE_CLIENT_BASE_URL_PREFIX = settings.get(
  373. # 'radicale.client.base_url.prefix',
  374. # '/',
  375. # )
  376. # # Ensure finished by '/'
  377. # if '/' != self.RADICALE_CLIENT_BASE_URL_PREFIX[-1]:
  378. # self.RADICALE_CLIENT_BASE_URL_PREFIX += '/'
  379. # if '/' != self.RADICALE_CLIENT_BASE_URL_PREFIX[0]:
  380. # self.RADICALE_CLIENT_BASE_URL_PREFIX \
  381. # = '/' + self.RADICALE_CLIENT_BASE_URL_PREFIX
  382. #
  383. # if not self.RADICALE_CLIENT_BASE_URL_HOST:
  384. # logger.warning(self,
  385. # 'Generated radicale.client.base_url.host parameter with '
  386. # 'followings parameters: website.server_name -> {}'
  387. # .format(self.WEBSITE_SERVER_NAME)
  388. # )
  389. # self.RADICALE_CLIENT_BASE_URL_HOST = self.WEBSITE_SERVER_NAME
  390. #
  391. # self.RADICALE_CLIENT_BASE_URL_TEMPLATE = '{}{}'.format(
  392. # self.RADICALE_CLIENT_BASE_URL_HOST,
  393. # self.RADICALE_CLIENT_BASE_URL_PREFIX,
  394. # )
  395. def configure_filedepot(self):
  396. depot_storage_name = self.DEPOT_STORAGE_NAME
  397. depot_storage_path = self.DEPOT_STORAGE_DIR
  398. depot_storage_settings = {'depot.storage_path': depot_storage_path}
  399. DepotManager.configure(
  400. depot_storage_name,
  401. depot_storage_settings,
  402. )
  403. class CST(object):
  404. ASYNC = 'ASYNC'
  405. SYNC = 'SYNC'
  406. TREEVIEW_FOLDERS = 'folders'
  407. TREEVIEW_ALL = 'all'