Package advene :: Package model :: Package cam :: Module tag
[hide private]
[frames] | no frames]

Source Code for Module advene.model.cam.tag

  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
17 - def instantiate(cls, owner, id, *args):
18 r = super(Tag, cls).instantiate(owner, id, *args) 19 r._transtype() 20 return r
21
22 - def _set_camsys_type(self, value, val_is_idref=False):
23 super(Tag, self)._set_camsys_type(value, val_is_idref) 24 self._transtype(value)
25
26 - def _transtype(self, systype=None):
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
44 45 -class CamTypeMixin(object):
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 # constraint related 54
55 - def set_meta(self, key, value, val_is_idref=False):
56 if key == CAM_ELEMENT_CONSTRAINT: 57 expected_id = ":constraint:" + self._id 58 if val_is_idref: 59 got_id = value 60 got = self._owner.get(value, None) 61 else: 62 got_id = getattr(value, "_id", None) 63 got = value 64 if got_id != expected_id \ 65 or got is None \ 66 or got.content_mimetype != "application/x-advene-type-constraint": 67 raise TypeError("element-constraint can not be changed") 68 69 super(CamTypeMixin, self).set_meta(key, value, val_is_idref)
70
71 - def check_element(self, e):
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
78 - def check_all(self, package=None):
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
90 - def _get_mimetype(self):
91 return self.element_constraint.content_parsed.get("mimetype", None) or "*/*"
92 93 @autoproperty
94 - def _set_mimetype(self, mimetype):
95 c = self.element_constraint 96 p = c.content_parsed 97 p["mimetype"] = mimetype 98 c.content_parsed = p
99 100 # schema related 101
102 - def iter_my_schemas(self, package=None, inherited=True):
103 if package is None: 104 package = session.package 105 if package is None: 106 raise TypeError("no package set in session, must be specified") 107 if inherited: 108 g = package.all 109 else: 110 g = package.own 111 return g.iter_schemas(item=self)
112
113 - def count_my_schemas(self, package=None, inherited=True):
114 if package is None: 115 package = session.package 116 if package is None: 117 raise TypeError("no package set in session, must be specified") 118 if inherited: 119 g = package.all 120 else: 121 g = package.own 122 return g.count_schemas(item=self)
123 124 @autoproperty
125 - def _get_my_schemas(type_, package=None):
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)
150 - def _tales_my_schemas(self, context):
151 # recycle _get_my_schemas implementation 152 pass
153
154 155 -class AnnotationType(CamTypeMixin, Tag):
156 """ 157 The class of annotation types. 158 """ 159 # This class is automatically transtyped from Tag (and back) when 160 # CAMSYS_TYPE is modified. See Tag.set_meta 161 pass
162
163 -class RelationType(CamTypeMixin, Tag):
164 """ 165 The class of relation types. 166 """ 167 # This class is automatically transtyped from Tag (and back) when 168 # CAMSYS_TYPE is modified. See Tag.set_meta 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