config.py 15KB

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