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