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.

components.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #-*- coding: utf-8 -*-
  2. import itertools
  3. import warnings
  4. import copy
  5. import hashlib
  6. from lodel.utils.mlstring import MlString
  7. from lodel.leapi.datahandlers.field_data_handler import FieldDataHandler
  8. from lodel.editorial_model.exceptions import *
  9. ## @brief Abstract class to represent editorial model components
  10. # @see EmClass EmField
  11. class EmComponent(object):
  12. ## @brief Instanciate an EmComponent
  13. # @param uid str : uniq identifier
  14. # @param display_name MlString|str|dict : component display_name
  15. # @param help_text MlString|str|dict : help_text
  16. def __init__(self, uid, display_name = None, help_text = None, group = None):
  17. if self.__class__ == EmComponent:
  18. raise NotImplementedError('EmComponent is an abstract class')
  19. self.uid = uid
  20. self.display_name = None if display_name is None else MlString(display_name)
  21. self.help_text = None if help_text is None else MlString(help_text)
  22. self.group = group
  23. def __str__(self):
  24. if self.display_name is None:
  25. return str(self.uid)
  26. return str(self.display_name)
  27. def d_hash(self):
  28. m = hashlib.md5()
  29. for data in (
  30. self.uid,
  31. 'NODISPNAME' if self.display_name is None else str(self.display_name.d_hash()),
  32. 'NOHELP' if self.help_text is None else str(self.help_text.d_hash()),
  33. 'NOGROUP' if self.group is None else str(self.group.d_hash()),
  34. ):
  35. m.update(bytes(data, 'utf-8'))
  36. return int.from_bytes(m.digest(), byteorder='big')
  37. ## @brief Handles editorial model objects classes
  38. #
  39. # @note The inheritance system allow child classes to overwrite parents EmField. But it's maybe not a good idea
  40. class EmClass(EmComponent):
  41. ## @brief Instanciate a new EmClass
  42. # @param uid str : uniq identifier
  43. # @param display_name MlString|str|dict : component display_name
  44. # @param abstract bool : set the class as asbtract if True
  45. # @param parents list: parent EmClass list or uid list
  46. # @param help_text MlString|str|dict : help_text
  47. def __init__(self, uid, display_name = None, help_text = None, abstract = False, parents = None, group = None):
  48. super().__init__(uid, display_name, help_text, group)
  49. self.abstract = bool(abstract)
  50. if parents is not None:
  51. if not isinstance(parents, list):
  52. parents = [parents]
  53. for parent in parents:
  54. if not isinstance(parent, EmClass):
  55. raise ValueError("<class EmClass> expected in parents list, but %s found" % type(parent))
  56. else:
  57. parents = list()
  58. self.parents = parents
  59. ## @brief Stores EmFields instances indexed by field uid
  60. self.__fields = dict()
  61. ## @brief Property that represent a dict of all fields (the EmField defined in this class and all its parents)
  62. @property
  63. def __all_fields(self):
  64. res = dict()
  65. for pfields in [ p.__all_fields for p in self.parents]:
  66. res.update(pfields)
  67. res.update(self.__fields)
  68. return res
  69. ## @brief EmField getter
  70. # @param uid None | str : If None returns an iterator on EmField instances else return an EmField instance
  71. # @param no_parents bool : If True returns only fields defined is this class and not the one defined in parents classes
  72. # @return A list on EmFields instances (if uid is None) else return an EmField instance
  73. def fields(self, uid = None, no_parents = False):
  74. fields = self.__fields if no_parents else self.__all_fields
  75. try:
  76. return list(fields.values()) if uid is None else fields[uid]
  77. except KeyError:
  78. raise EditorialModelError("No such EmField '%s'" % uid)
  79. ## @brief Add a field to the EmClass
  80. # @param emfield EmField : an EmField instance
  81. # @warning do not add an EmField allready in another class !
  82. # @throw EditorialModelException if an EmField with same uid allready in this EmClass (overwritting allowed from parents)
  83. # @todo End the override checks (needs methods in data_handlers)
  84. def add_field(self, emfield):
  85. if emfield.uid in self.__fields:
  86. raise EditorialModelException("Duplicated uid '%s' for EmField in this class ( %s )" % (emfield.uid, self))
  87. # Incomplete field override check
  88. if emfield.uid in self.__all_fields:
  89. parent_field = self.__all_fields[emfield.uid]
  90. if emfield.data_handler_name != parent_field.data_handler_name:
  91. raise AttributeError("'%s' field override a parent field, but data_handles are not compatible" % emfield.uid)
  92. self.__fields[emfield.uid] = emfield
  93. emfield._emclass = self
  94. return emfield
  95. ## @brief Create a new EmField and add it to the EmClass
  96. # @param data_handler str : A DataHandler name
  97. # @param uid str : the EmField uniq id
  98. # @param **field_kwargs : EmField constructor parameters ( see @ref EmField.__init__() )
  99. def new_field(self, uid, data_handler, **field_kwargs):
  100. return self.add_field(EmField(uid, data_handler, **field_kwargs))
  101. def d_hash(self):
  102. m = hashlib.md5()
  103. payload = str(super().d_hash()) + ("1" if self.abstract else "0")
  104. for p in sorted(self.parents):
  105. payload += str(p.d_hash())
  106. for fuid in sorted(self.__fields.keys()):
  107. payload += str(self.__fields[fuid].d_hash())
  108. m.update(bytes(payload, 'utf-8'))
  109. return int.from_bytes(m.digest(), byteorder='big')
  110. ## @brief Handles editorial model classes fields
  111. class EmField(EmComponent):
  112. ## @brief Instanciate a new EmField
  113. # @param uid str : uniq identifier
  114. # @param display_name MlString|str|dict : field display_name
  115. # @param data_handler str : A DataHandler name
  116. # @param help_text MlString|str|dict : help text
  117. # @param group EmGroup :
  118. # @param **handler_kwargs : data handler arguments
  119. def __init__(self, uid, data_handler, display_name = None, help_text = None, group = None, **handler_kwargs):
  120. super().__init__(uid, display_name, help_text, group)
  121. self.data_handler_name = data_handler
  122. self.data_handler_cls = FieldDataHandler.from_name(data_handler)
  123. self.data_handler_options = handler_kwargs
  124. self.data_handler_instance = self.data_handler_cls(**handler_kwargs)
  125. ## @brief Stores the emclass that contains this field (set by EmClass.add_field() method)
  126. self._emclass = None
  127. ## @warning Not complete !
  128. # @todo Complete the hash when data handlers becomes available
  129. def d_hash(self):
  130. return int.from_bytes(hashlib.md5(
  131. bytes(
  132. "%s%s%s" % ( super().d_hash(),
  133. self.data_handler_name,
  134. self.data_handler_options),
  135. 'utf-8')
  136. ).digest(), byteorder='big')
  137. ## @brief Handles functionnal group of EmComponents
  138. class EmGroup(object):
  139. ## @brief Create a new EmGroup
  140. # @note you should NEVER call the constructor yourself. Use Model.add_group instead
  141. # @param uid str : Uniq identifier
  142. # @param depends list : A list of EmGroup dependencies
  143. # @param display_name MlString|str :
  144. # @param help_text MlString|str :
  145. def __init__(self, uid, depends = None, display_name = None, help_text = None):
  146. self.uid = uid
  147. ## @brief Stores the list of groups that depends on this EmGroup indexed by uid
  148. self.required_by = dict()
  149. ## @brief Stores the list of dependencies (EmGroup) indexed by uid
  150. self.require = dict()
  151. ## @brief Stores the list of EmComponent instances contained in this group
  152. self.__components = set()
  153. self.display_name = None if display_name is None else MlString(display_name)
  154. self.help_text = None if help_text is None else MlString(help_text)
  155. if depends is not None:
  156. for grp in depends:
  157. if not isinstance(grp, EmGroup):
  158. raise ValueError("EmGroup expected in depends argument but %s found" % grp)
  159. self.add_dependencie(grp)
  160. ## @brief Returns EmGroup dependencie
  161. # @param recursive bool : if True return all dependencies and their dependencies
  162. # @return a dict of EmGroup identified by uid
  163. def dependencies(self, recursive = False):
  164. res = copy.copy(self.require)
  165. if not recursive:
  166. return res
  167. to_scan = list(res.values())
  168. while len(to_scan) > 0:
  169. cur_dep = to_scan.pop()
  170. for new_dep in cur_dep.require.values():
  171. if new_dep not in res:
  172. to_scan.append(new_dep)
  173. res[new_dep.uid] = new_dep
  174. return res
  175. ## @brief Add components in a group
  176. # @param components list : EmComponent instance list
  177. def add_components(self, components):
  178. for component in components:
  179. if isinstance(component, EmField):
  180. if component._emclass is None:
  181. warnings.warn("Adding an orphan EmField to an EmGroup")
  182. elif not isinstance(component, EmClass):
  183. raise EditorialModelError("Expecting components to be a list of EmComponent, but %s found in the list" % type(component))
  184. self.__components |= set(components)
  185. ## @brief Add a dependencie
  186. # @param em_group EmGroup|iterable : an EmGroup instance or list of instance
  187. def add_dependencie(self, grp):
  188. try:
  189. for group in grp:
  190. self.add_dependencie(group)
  191. return
  192. except TypeError: pass
  193. if grp.uid in self.require:
  194. return
  195. if self.__circular_dependencie(grp):
  196. raise EditorialModelError("Circular dependencie detected, cannot add dependencie")
  197. self.require[grp.uid] = grp
  198. grp.required_by[self.uid] = self
  199. ## @brief Search for circular dependencie
  200. # @return True if circular dep found else False
  201. def __circular_dependencie(self, new_dep):
  202. return self.uid in new_dep.dependencies(True)
  203. ## @brief Fancy string representation of an EmGroup
  204. # @return a string
  205. def __str__(self):
  206. if self.display_name is None:
  207. return self.uid
  208. else:
  209. return self.display_name.get()
  210. def d_hash(self):
  211. payload = "%s%s%s" % (
  212. self.uid,
  213. 'NODNAME' if self.display_name is None else self.display_name.d_hash(),
  214. 'NOHELP' if self.help_text is None else self.help_text.d_hash()
  215. )
  216. for recurs in (False, True):
  217. deps = self.dependencies(recurs)
  218. for dep_uid in sorted(deps.keys()):
  219. payload += str(deps[dep_uid].d_hash())
  220. for req_by_uid in self.required_by:
  221. payload += req_by_uid
  222. return int.from_bytes(
  223. bytes(payload, 'utf-8'),
  224. byteorder = 'big'
  225. )
  226. ## @brief Complete string representation of an EmGroup
  227. # @return a string
  228. def __repr__(self):
  229. return "<class EmGroup '%s' depends : [%s]>" % (self.uid, ', '.join([duid for duid in self.dependencies(False)]) )