globals.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import logging
  2. from collections import deque
  3. from importlib import import_module
  4. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  5. '''
  6. Constants used in messaging to identify an event type.
  7. '''
  8. RES_CREATED = '_create_'
  9. RES_DELETED = '_delete_'
  10. RES_UPDATED = '_update_'
  11. ROOT_UID = '/'
  12. ROOT_RSRC_URI = nsc['fcres'][ROOT_UID]
  13. class AppGlobals:
  14. '''
  15. Application Globals.
  16. This class sets up all connections and exposes them across the application
  17. outside of the Flask app context.
  18. '''
  19. def __init__(self, conf):
  20. from lakesuperior.messaging.messenger import Messenger
  21. app_conf = conf['application']
  22. # Initialize RDF layout.
  23. rdfly_mod_name = app_conf['store']['ldp_rs']['layout']
  24. rdfly_mod = import_module('lakesuperior.store.ldp_rs.{}'.format(
  25. rdfly_mod_name))
  26. rdfly_cls = getattr(rdfly_mod, self.camelcase(rdfly_mod_name))
  27. #logger.info('RDF layout: {}'.format(rdfly_mod_name))
  28. # Initialize file layout.
  29. nonrdfly_mod_name = app_conf['store']['ldp_nr']['layout']
  30. nonrdfly_mod = import_module('lakesuperior.store.ldp_nr.{}'.format(
  31. nonrdfly_mod_name))
  32. nonrdfly_cls = getattr(nonrdfly_mod, self.camelcase(nonrdfly_mod_name))
  33. #logger.info('Non-RDF layout: {}'.format(nonrdfly_mod_name))
  34. # Set up messaging.
  35. messenger = Messenger(app_conf['messaging'])
  36. # Exposed globals.
  37. self._rdfly = rdfly_cls(app_conf['store']['ldp_rs'])
  38. self._nonrdfly = nonrdfly_cls(app_conf['store']['ldp_nr'])
  39. self._messenger = messenger
  40. self._changelog = deque()
  41. @property
  42. def rdfly(self):
  43. return self._rdfly
  44. @property
  45. def rdf_store(self):
  46. return self._rdfly.store
  47. @property
  48. def nonrdfly(self):
  49. return self._nonrdfly
  50. @property
  51. def messenger(self):
  52. return self._messenger
  53. @property
  54. def changelog(self):
  55. return self._changelog
  56. def camelcase(self, word):
  57. '''
  58. Convert a string with underscores to a camel-cased one.
  59. Ripped from https://stackoverflow.com/a/6425628
  60. '''
  61. return ''.join(x.capitalize() or '_' for x in word.split('_'))