config.py 15KB

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