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 4.3KB

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