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.

model.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #-*- coding:utf-8 -*-
  2. import hashlib
  3. import importlib
  4. from lodel.utils.mlstring import MlString
  5. from lodel.editorial_model.exceptions import *
  6. from lodel.editorial_model.components import EmClass, EmField, EmGroup
  7. ## @brief Describe an editorial model
  8. class EditorialModel(object):
  9. ## @brief Create a new editorial model
  10. # @param name MlString|str|dict : the editorial model name
  11. # @param description MlString|str|dict : the editorial model description
  12. def __init__(self, name, description = None):
  13. self.name = MlString(name)
  14. self.description = MlString(description)
  15. ## @brief Stores all groups indexed by id
  16. self.__groups = dict()
  17. ## @brief Stores all classes indexed by id
  18. self.__classes = dict()
  19. ## @brief EmClass accessor
  20. # @param uid None | str : give this argument to get a specific EmClass
  21. # @return if uid given return an EmClass else return an EmClass iterator
  22. def classes(self, uid = None):
  23. try:
  24. return self.__elt_getter(self.__classes, uid)
  25. except KeyError:
  26. raise EditorialModelException("EmClass not found : '%s'" % uid)
  27. ## @brief EmGroup getter
  28. # @param uid None | str : give this argument to get a specific EmGroup
  29. # @return if uid given return an EmGroup else return an EmGroup iterator
  30. def groups(self, uid = None):
  31. try:
  32. return self.__elt_getter(self.__groups, uid)
  33. except KeyError:
  34. raise EditorialModelException("EmGroup not found : '%s'" % uid)
  35. ## @brief Add a class to the editorial model
  36. # @param emclass EmClass : the EmClass instance to add
  37. # @return emclass
  38. def add_class(self, emclass):
  39. if not isinstance(emclass, EmClass):
  40. raise ValueError("<class EmClass> expected but got %s " % type(emclass))
  41. if emclass.uid in self.classes():
  42. raise EditorialModelException('Duplicated uid "%s"' % emclass.uid)
  43. self.__classes[emclass.uid] = emclass
  44. return emclass
  45. ## @brief Add a group to the editorial model
  46. # @param emgroup EmGroup : the EmGroup instance to add
  47. # @return emgroup
  48. def add_group(self, emgroup):
  49. if not isinstance(emgroup, EmGroup):
  50. raise ValueError("<class EmGroup> expected but got %s" % type(emgroup))
  51. if emgroup.uid in self.groups():
  52. raise EditorialModelException('Duplicated uid "%s"' % emgroup.uid)
  53. self.__groups[emgroup.uid] = emgroup
  54. return emgroup
  55. ## @brief Add a new EmClass to the editorial model
  56. # @param uid str : EmClass uid
  57. # @param **kwargs : EmClass constructor options ( see @ref lodel.editorial_model.component.EmClass.__init__() )
  58. def new_class(self, uid, **kwargs):
  59. return self.add_class(EmClass(uid, **kwargs))
  60. ## @brief Add a new EmGroup to the editorial model
  61. # @param uid str : EmGroup uid
  62. # @param *kwargs : EmGroup constructor keywords arguments (see @ref lodel.editorial_model.component.EmGroup.__init__() )
  63. def new_group(self, uid, **kwargs):
  64. return self.add_group(EmGroup(uid, **kwargs))
  65. # @brief Save a model
  66. # @param translator module : The translator module to use
  67. # @param **translator_args
  68. def save(self, translator, **translator_kwargs):
  69. if isinstance(translator, str):
  70. translator = self.translator_from_name(translator)
  71. return translator.save(self, **translator_kwargs)
  72. ## @brief Load a model
  73. # @param translator module : The translator module to use
  74. # @param **translator_args
  75. @classmethod
  76. def load(cls, translator, **translator_kwargs):
  77. if isinstance(translator, str):
  78. translator = cls.translator_from_name(translator)
  79. return translator.load(**translator_kwargs)
  80. ## @brief Return a translator module given a translator name
  81. # @param translator_name str : The translator name
  82. # @return the translator python module
  83. # @throw NameError if the translator does not exists
  84. @staticmethod
  85. def translator_from_name(translator_name):
  86. pkg_name = 'lodel.editorial_model.translator.%s' % translator_name
  87. try:
  88. mod = importlib.import_module(pkg_name)
  89. except ImportError:
  90. raise NameError("No translator named %s")
  91. return mod
  92. ## @brief Private getter for __groups or __classes
  93. # @see classes() groups()
  94. def __elt_getter(self, elts, uid):
  95. return list(elts.values()) if uid is None else elts[uid]
  96. ## @brief Lodel hash
  97. def d_hash(self):
  98. payload = "%s%s" % (
  99. self.name,
  100. 'NODESC' if self.description is None else self.description.d_hash()
  101. )
  102. for guid in sorted(self.__groups):
  103. payload += str(self.__groups[guid].d_hash())
  104. for cuid in sorted(self.__classes):
  105. payload += str(self.__classes[cuid].d_hash())
  106. return int.from_bytes(
  107. hashlib.md5(bytes(payload, 'utf-8')).digest(),
  108. byteorder='big'
  109. )