config.py 16KB

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