No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import os.path
  2. from lodel.context import LodelContext
  3. LodelContext.expose_modules(globals(), {
  4. 'lodel.plugin.datasource_plugin': ['AbstractDatasource', 'DatasourcePlugin'],
  5. 'lodel.logger': 'logger',
  6. 'lodel.exceptions': ['LodelFatalError'],
  7. 'lodel.settings': 'Settings'})
  8. from .fs_utils import *
  9. from .specs import check as check_leo
  10. from .exceptions import *
  11. ##@brief This datasource handles site creation
  12. #@note Filters evaluation is forwarded to child datasource using our select
  13. #method. Not optimized but much simple
  14. class Datasource(AbstractDatasource):
  15. ##@brief Constructor
  16. #
  17. #Handles child datasource instanciation
  18. #@param db_datasource str : datasource identifier
  19. #@param db_datasource_ro str : read only datasource identitifer
  20. def __init__(self, db_datasource, db_datasource_ro = None):
  21. if db_datasource_ro is None:
  22. db_datasource_ro = db_datasource
  23. self._child_ds = DatasourcePlugin.init_datasource(
  24. db_datasource, False)
  25. self._child_ds_ro = DatasourcePlugin.init_datasource(
  26. db_datasource, True)
  27. pass
  28. ##@brief Checks that given emcomponent is compatible with datasource
  29. #behavior
  30. #@warning 100% hardcoded checks on leo name fieldnames & types
  31. #@param emcomp LeObject subclass (or instance)
  32. #@throws LodelFatalError if not compatible
  33. @staticmethod
  34. def __assert_good_leo(leo):
  35. res, reason = check_leo(leo)
  36. if not res:
  37. msg = 'Bad leo given : %s because %s' % (leo, reason)
  38. logger.critical(msg)
  39. raise LodelFatalError(msg)
  40. ##@brief Provide a new uniq numeric ID
  41. #@param emcomp LeObject subclass (not instance) : To know on wich things we
  42. #have to be uniq
  43. #@return an integer
  44. def new_numeric_id(self, emcomp):
  45. self.__assert_good_leo(emcomp)
  46. return self._child_ds.new_numeric_id(emcomp)
  47. ##@brief returns a selection of documents from the datasource
  48. #@note Simply forwarded to ro child datasource
  49. #@param target Emclass
  50. #@param field_list list
  51. #@param filters list : List of filters
  52. #@param rel_filters list : List of relational filters
  53. #@param order list : List of column to order. ex: order = [('title', 'ASC'),]
  54. #@param group list : List of tupple representing the column to group together. ex: group = [('title', 'ASC'),]
  55. #@param limit int : Number of records to be returned
  56. #@param offset int: used with limit to choose the start record
  57. #@param instanciate bool : If true, the records are returned as instances, else they are returned as dict
  58. #@return list
  59. def select(self, target, field_list, filters, relational_filters=None,
  60. order=None, group=None, limit=None, offset=0, instanciate=True):
  61. return self._child_ds_ro.select(
  62. target, field_list, relational_filters, order, group, limit, offset)
  63. ##@brief Deletes records according to given filters
  64. #@note lazy filters evaluation implementation : to evaluate filters &
  65. #rel_filters we run self.select using them
  66. #@param target Emclass : class of the record to delete
  67. #@param filters list : List of filters
  68. #@param relational_filters list : List of relational filters
  69. #@return int : number of deleted records
  70. def delete(self, target, filters, relational_filters):
  71. shortnames = self.select(
  72. target,
  73. ['shortname'],
  74. filters,
  75. relational_filters)
  76. for shortname in [ item['shortname'] for item in shortnames]:
  77. purge(shortname)
  78. return self._child_ds.delete(target, filters, relational_filters)
  79. ## @brief updates records according to given filters
  80. #
  81. #@note lazy filters evaluation implementation : to evaluate filters &
  82. #rel_filters we run self.select using them
  83. #@note shortname updates are forbidden
  84. #@param target Emclass : class of the object to update
  85. #@param filters list : List of filters
  86. #@param relational_filters list : List of relational filters
  87. #@param upd_datas dict : datas to update (new values)
  88. #@return int : Number of updated records
  89. def update(self, target, filters, relational_filters, upd_datas):
  90. if 'shortname' in upd_datas:
  91. raise LodelSiteDatasourceError('Unable to update the \
  92. shortname, it is a site identifier. The right way for doing so is to copy \
  93. existing site with a new name')
  94. datas = self.select(
  95. target,
  96. ['shortname', 'em_groups', 'extensions'],
  97. filters,
  98. relational_filters)
  99. for data in datas:
  100. if 'em_groups' in upd_datas:
  101. groups = upd_datas['em_groups']
  102. else:
  103. groups = data['em_groups']
  104. if 'extensions' in upd_datas:
  105. extensions = upd_datas['extensions']
  106. else:
  107. extensions = data['extensions']
  108. update_conf(**{
  109. 'sitename': data['shortname'],
  110. 'em_groups': groups,
  111. 'extensions': extensions})
  112. return self._child_ds.update(
  113. target, filters, relational_filters, upd_datas)
  114. ##@brief Inserts a record in a given collection
  115. #@param target Emclass : class of the object to insert
  116. #@param new_datas dict : datas to insert
  117. #@return the inserted uid
  118. def insert(self, target, new_datas):
  119. self.__assert_good_leo(target)
  120. if site_exists(new_datas['shortname']):
  121. raise LodelSiteDatasourceError('A site with "%s" as shortname \
  122. already exists' % (new_datas['shortname']))
  123. site_directories_creation(new_datas['shortname'])
  124. generate_conf(
  125. new_datas['shortname'],
  126. new_datas['em_groups'],
  127. new_datas['extensions'])
  128. return self._child_ds.insert(target, new_datas)
  129. ## @brief Inserts a list of records in a given collection
  130. #@note Here is a simple implementation : a for loop triggering
  131. #insert() calls
  132. # @param target Emclass : class of the objects inserted
  133. # @param datas_list list : list of dict
  134. # @return list : list of the inserted records' ids
  135. def insert_multi(self, target, datas_list):
  136. res = []
  137. for new_datas in datas_list:
  138. res.append(self.insert(target, datas))
  139. return res