1 from advene.model.cam.consts import CAMSYS_TYPE, CAM_NS_PREFIX
2 from advene.model.cam.element import CamElementMixin
3 from advene.model.cam.group import CamGroupMixin
4 from advene.model.core.element import LIST, ElementCollection
5 from advene.model.core.tag import Tag as CoreTag
6 from advene.model.tales import tales_property, tales_use_as_context
7 from advene.util.alias import alias
8 from advene.util.autoproperty import autoproperty
9 from advene.util.session import session
10
11
12 CAM_ELEMENT_CONSTRAINT = CAM_NS_PREFIX + "element-constraint"
13
14 -class Tag(CoreTag, CamElementMixin, CamGroupMixin):
15
16 @classmethod
21
25
27 """
28 Transtypes this Tag to the appropriate subclass according to the given
29 systype (assumed to be the current or future systype).
30
31 If systype is omitted, it is retrieved from the metadata.
32 """
33 if systype is None:
34 systype = self.get_meta(CAMSYS_TYPE, None)
35 if systype == "annotation-type":
36 newtype = AnnotationType
37 elif systype == "relation-type":
38 newtype = RelationType
39 else:
40 newtype = Tag
41 if self.__class__ is not newtype:
42 self.__class__ = newtype
43
46 """
47 Implement features common to annotation and relation types.
48
49 That includes shortcut attributes to the underlying type-constraint,
50 and access to the schemas containing the type.
51 """
52
53
54
70
72 """
73 Applies the element_constraint to the given element and returns the
74 result.
75 """
76 return self.element_constraint.apply_to(e)
77
79 """
80 Applies the element_constraint to all the elements in the given
81 package (session.package) if None, and return the aggregated result.
82 """
83 check = self.element_constraint.apply_to
84 r = True
85 for e in self.iter_elements(package):
86 r = r & check(e)
87 return r
88
89 @autoproperty
92
93 @autoproperty
99
100
101
112
123
124 @autoproperty
126 """
127 Return an ElementCollection of all the schemas containing this type.
128
129 In python, property `my_schemas` uses ``session.package``.
130 In TALES, property `my_schemas` uses ``package``.
131 """
132 if package is None:
133 package = session.package
134 if package is None:
135 raise TypeError("no package set in session, must be specified")
136 class TypeSchemas(ElementCollection):
137 def __iter__(self):
138 return type_.iter_my_schemas(package)
139 def __len__(self):
140 return type_.count_my_schemas(package)
141 def __contains__(self, s):
142 return getattr(s, "ADVENE_TYPE", None) == LIST \
143 and s.get_meta(CAMSYS_TYPE, None) == "schema" \
144 and type_ in s
145 return TypeSchemas(package)
146
147 @tales_property
148 @tales_use_as_context("package")
149 @alias(_get_my_schemas)
153
156 """
157 The class of annotation types.
158 """
159
160
161 pass
162
164 """
165 The class of relation types.
166 """
167
168
169 pass
170
171 Tag.make_metadata_property(CAMSYS_TYPE, "system_type", default=None)
172 Tag.make_metadata_property(CAM_NS_PREFIX + "representation", default=None)
173 Tag.make_metadata_property(CAM_NS_PREFIX + "color", default=None)
174 Tag.make_metadata_property(CAM_NS_PREFIX + "element-color",
175 "element_color", default=None)
176 Tag.make_metadata_property(CAM_ELEMENT_CONSTRAINT,
177 "element_constraint", default=None)
178