globals.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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`
  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 import env
  28. >>> from lakesuperior.app_globals import AppGlobals
  29. >>> my_config = {'name': 'value', '...': '...'}
  30. >>> env.app_globals = AppGlobals(my_config)
  31. """
  32. def __init__(self, config):
  33. """
  34. Generate global variables from configuration.
  35. """
  36. from lakesuperior.messaging.messenger import Messenger
  37. app_conf = config['application']
  38. # Initialize RDF layout.
  39. rdfly_mod_name = app_conf['store']['ldp_rs']['layout']
  40. rdfly_mod = import_module('lakesuperior.store.ldp_rs.{}'.format(
  41. rdfly_mod_name))
  42. rdfly_cls = getattr(rdfly_mod, self.camelcase(rdfly_mod_name))
  43. #logger.info('RDF layout: {}'.format(rdfly_mod_name))
  44. # Initialize file layout.
  45. nonrdfly_mod_name = app_conf['store']['ldp_nr']['layout']
  46. nonrdfly_mod = import_module('lakesuperior.store.ldp_nr.{}'.format(
  47. nonrdfly_mod_name))
  48. nonrdfly_cls = getattr(nonrdfly_mod, self.camelcase(nonrdfly_mod_name))
  49. #logger.info('Non-RDF layout: {}'.format(nonrdfly_mod_name))
  50. # Set up messaging.
  51. self._messenger = Messenger(app_conf['messaging'])
  52. # Exposed globals.
  53. self._config = config
  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 config(self):
  59. """
  60. Global configuration.
  61. This is a collection of all configuration options **except** for the
  62. WSGI configuration which is initialized at a different time and is
  63. stored under :data:`lakesuperior.env.wsgi_options`.
  64. *TODO:* Update class reference when interface will be separated from
  65. implementation.
  66. """
  67. return self._config
  68. @property
  69. def rdfly(self):
  70. """
  71. Current RDF layout.
  72. This is an instance of
  73. :class:`~lakesuperior.store.ldp_rs.rsrc_centric_layout.RsrcCentricLayout`.
  74. *TODO:* Update class reference when interface will be separated from
  75. implementation.
  76. """
  77. return self._rdfly
  78. @property
  79. def rdf_store(self):
  80. """
  81. Current RDF low-level store.
  82. This is an instance of
  83. :class:`~lakesuperior.store.ldp_rs.lmdb_store.LmdbStore`.
  84. """
  85. return self._rdfly.store
  86. @property
  87. def nonrdfly(self):
  88. return self._nonrdfly
  89. """
  90. Current non-RDF (binary contents) layout.
  91. This is an instance of
  92. :class:`~lakesuperior.store.ldp_nr.base_non_rdf_layout.BaseNonRdfLayout`.
  93. """
  94. @property
  95. def messenger(self):
  96. """
  97. Current message handler.
  98. This is an instance of
  99. :class:`~lakesuperior.messaging.messenger.Messenger`.
  100. """
  101. return self._messenger
  102. @property
  103. def changelog(self):
  104. return self._changelog
  105. def camelcase(self, word):
  106. """
  107. Convert a string with underscores to a camel-cased one.
  108. Ripped from https://stackoverflow.com/a/6425628
  109. """
  110. return ''.join(x.capitalize() or '_' for x in word.split('_'))