globals.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. self._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._changelog = deque()
  40. @property
  41. def rdfly(self):
  42. return self._rdfly
  43. @property
  44. def rdf_store(self):
  45. return self._rdfly.store
  46. @property
  47. def nonrdfly(self):
  48. return self._nonrdfly
  49. @property
  50. def messenger(self):
  51. return self._messenger
  52. @property
  53. def changelog(self):
  54. return self._changelog
  55. def camelcase(self, word):
  56. '''
  57. Convert a string with underscores to a camel-cased one.
  58. Ripped from https://stackoverflow.com/a/6425628
  59. '''
  60. return ''.join(x.capitalize() or '_' for x in word.split('_'))