globals.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. RES_CREATED = '_create_'
  6. """A resource was created."""
  7. RES_DELETED = '_delete_'
  8. """A resource was deleted."""
  9. RES_UPDATED = '_update_'
  10. """A resource was updated."""
  11. ROOT_UID = '/'
  12. """Root node UID."""
  13. ROOT_RSRC_URI = nsc['fcres'][ROOT_UID]
  14. """Internal URI of root resource."""
  15. class AppGlobals:
  16. """
  17. Application Globals.
  18. This class is instantiated and used as a carrier for all connections and
  19. various global variables outside of the Flask app context.
  20. The variables are set on initialization by passing a configuration dict.
  21. Usually this is done when starting an application. The instance with the
  22. loaded variables is then assigned to the :data:`lakesuperior.env.env`
  23. global variable.
  24. You can either load the default configuration::
  25. >>>from lakesuperior import env_setup
  26. Or set up an environment with a custom configuration::
  27. >>>from lakesuperior.env import env
  28. >>>from lakesuperior.app_globals import AppGlobals
  29. >>>my_config = {'name': 'value', '...': '...'}
  30. >>>env.config = my_config
  31. >>>env.app_globals = AppGlobals(my_config)
  32. """
  33. def __init__(self, conf):
  34. """
  35. Generate global variables from configuration.
  36. """
  37. from lakesuperior.messaging.messenger import Messenger
  38. app_conf = conf['application']
  39. # Initialize RDF layout.
  40. rdfly_mod_name = app_conf['store']['ldp_rs']['layout']
  41. rdfly_mod = import_module('lakesuperior.store.ldp_rs.{}'.format(
  42. rdfly_mod_name))
  43. rdfly_cls = getattr(rdfly_mod, self.camelcase(rdfly_mod_name))
  44. #logger.info('RDF layout: {}'.format(rdfly_mod_name))
  45. # Initialize file layout.
  46. nonrdfly_mod_name = app_conf['store']['ldp_nr']['layout']
  47. nonrdfly_mod = import_module('lakesuperior.store.ldp_nr.{}'.format(
  48. nonrdfly_mod_name))
  49. nonrdfly_cls = getattr(nonrdfly_mod, self.camelcase(nonrdfly_mod_name))
  50. #logger.info('Non-RDF layout: {}'.format(nonrdfly_mod_name))
  51. # Set up messaging.
  52. self._messenger = Messenger(app_conf['messaging'])
  53. # Exposed globals.
  54. self._rdfly = rdfly_cls(app_conf['store']['ldp_rs'])
  55. self._nonrdfly = nonrdfly_cls(app_conf['store']['ldp_nr'])
  56. self._changelog = deque()
  57. @property
  58. def rdfly(self):
  59. """
  60. Current RDF layout.
  61. This is an instance of
  62. :class:`~lakesuperior.store.ldp_rs.rsrc_centric_layout.RsrcCentricLayout`.
  63. *TODO:* Update class reference when interface will be separated from
  64. implementation.
  65. """
  66. return self._rdfly
  67. @property
  68. def rdf_store(self):
  69. """
  70. Current RDF low-level store.
  71. This is an instance of
  72. :class:`~lakesuperior.store.ldp_rs.lmdb_store.LmdbStore`.
  73. """
  74. return self._rdfly.store
  75. @property
  76. def nonrdfly(self):
  77. return self._nonrdfly
  78. """
  79. Current non-RDF (binary contents) layout.
  80. This is an instance of
  81. :class:`~lakesuperior.store.ldp_nr.base_non_rdf_layout.BaseNonRdfLayout`.
  82. """
  83. @property
  84. def messenger(self):
  85. """
  86. Current message handler.
  87. This is an instance of
  88. :class:`~lakesuperior.messaging.messenger.Messenger`.
  89. """
  90. return self._messenger
  91. @property
  92. def changelog(self):
  93. return self._changelog
  94. def camelcase(self, word):
  95. """
  96. Convert a string with underscores to a camel-cased one.
  97. Ripped from https://stackoverflow.com/a/6425628
  98. """
  99. return ''.join(x.capitalize() or '_' for x in word.split('_'))