globals.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import logging
  2. from collections import deque
  3. from importlib import import_module
  4. from os import path
  5. from lakesuperior.dictionaries.namespaces import ns_collection as nsc
  6. RES_CREATED = '_create_'
  7. """A resource was created."""
  8. RES_DELETED = '_delete_'
  9. """A resource was deleted."""
  10. RES_UPDATED = '_update_'
  11. """A resource was updated."""
  12. ROOT_UID = '/'
  13. """Root node UID."""
  14. ROOT_RSRC_URI = nsc['fcres'][ROOT_UID]
  15. """Internal URI of root resource."""
  16. class AppGlobals:
  17. """
  18. Application Globals.
  19. This class is instantiated and used as a carrier for all connections and
  20. various global variables outside of the Flask app context.
  21. The variables are set on initialization by passing a configuration dict.
  22. Usually this is done when starting an application. The instance with the
  23. loaded variables is then assigned to the :data:`lakesuperior.env`
  24. global variable.
  25. You can either load the default configuration::
  26. >>>from lakesuperior import env_setup
  27. Or set up an environment with a custom configuration::
  28. >>> from lakesuperior import env
  29. >>> from lakesuperior.app_globals import AppGlobals
  30. >>> my_config = {'name': 'value', '...': '...'}
  31. >>> env.app_globals = AppGlobals(my_config)
  32. """
  33. def __init__(self, config):
  34. """
  35. Generate global variables from configuration.
  36. """
  37. from lakesuperior.messaging.messenger import Messenger
  38. app_conf = config['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. ## Initialize metadata store.
  52. #from lakesuperior.store.metadata_store import MetadataStore
  53. # Set up messaging.
  54. self._messenger = Messenger(app_conf['messaging'])
  55. # Exposed globals.
  56. self._config = config
  57. self._rdfly = rdfly_cls(app_conf['store']['ldp_rs'])
  58. self._nonrdfly = nonrdfly_cls(app_conf['store']['ldp_nr'])
  59. #self._md_store = MetadataStore(path.join(
  60. # app_conf['data_dir'], 'metadata'), create=True)
  61. self._changelog = deque()
  62. @property
  63. def config(self):
  64. """
  65. Global configuration.
  66. This is a collection of all configuration options **except** for the
  67. WSGI configuration which is initialized at a different time and is
  68. stored under :data:`lakesuperior.env.wsgi_options`.
  69. *TODO:* Update class reference when interface will be separated from
  70. implementation.
  71. """
  72. return self._config
  73. @property
  74. def rdfly(self):
  75. """
  76. Current RDF layout.
  77. This is an instance of
  78. :class:`~lakesuperior.store.ldp_rs.rsrc_centric_layout.RsrcCentricLayout`.
  79. *TODO:* Update class reference when interface will be separated from
  80. implementation.
  81. """
  82. return self._rdfly
  83. @property
  84. def rdf_store(self):
  85. """
  86. Current RDF low-level store.
  87. This is an instance of
  88. :class:`~lakesuperior.store.ldp_rs.lmdb_store.LmdbStore`.
  89. """
  90. return self._rdfly.store
  91. @property
  92. def nonrdfly(self):
  93. """
  94. Current non-RDF (binary contents) layout.
  95. This is an instance of
  96. :class:`~lakesuperior.store.ldp_nr.base_non_rdf_layout.BaseNonRdfLayout`.
  97. """
  98. return self._nonrdfly
  99. #@property
  100. #def md_store(self):
  101. # """
  102. # Metadata store (LMDB).
  103. # This is an instance of
  104. # :class:`~lakesuperior.store.metadata_store.MetadataStore`.
  105. # """
  106. # return self._md_store
  107. @property
  108. def messenger(self):
  109. """
  110. Current message handler.
  111. This is an instance of
  112. :class:`~lakesuperior.messaging.messenger.Messenger`.
  113. """
  114. return self._messenger
  115. @property
  116. def changelog(self):
  117. return self._changelog
  118. def camelcase(self, word):
  119. """
  120. Convert a string with underscores to a camel-cased one.
  121. Ripped from https://stackoverflow.com/a/6425628
  122. """
  123. return ''.join(x.capitalize() or '_' for x in word.split('_'))