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

Source Code for Module advene.model.cam.package

  1  """ 
  2  Note on events 
  3  ============== 
  4   
  5  The creation events emitted by a CAM package, in addition to the ones emitted by 
  6  a CORE package (see `advene.model.events`), include:! 
  7   * ``created::user-tag`` 
  8   * ``created::annotation-type`` 
  9   * ``created::relation-type`` 
 10   * ``created::user-list`` 
 11   * ``created::schema`` 
 12  """ 
 13   
 14  from advene.model.cam.consts import BOOTSTRAP_URI, CAMSYS_TYPE 
 15  from advene.model.cam.exceptions import UnsafeUseWarning, SemanticError 
 16  from advene.model.cam.group import CamGroupMixin 
 17  from advene.model.cam.media import Media 
 18  from advene.model.cam.annotation import Annotation 
 19  from advene.model.cam.relation import Relation 
 20  from advene.model.cam.view import View 
 21  from advene.model.cam.resource import Resource 
 22  from advene.model.cam.tag import Tag 
 23  from advene.model.cam.list import List 
 24  from advene.model.cam.query import Query 
 25  from advene.model.cam.import_ import Import 
 26  import advene.model.cam.util.bookkeeping as bk 
 27  from advene.model.consts import DC_NS_PREFIX, RDFS_NS_PREFIX 
 28  from advene.model.core.package import Package as CorePackage 
 29  from advene.model.core.all_group import AllGroup as CoreAllGroup 
 30  from advene.model.core.own_group import OwnGroup as CoreOwnGroup 
 31   
 32  from warnings import warn 
 33  from weakref import ref as wref 
34 35 -class _AllGroup(CamGroupMixin, CoreAllGroup):
36
37 - def iter_tags(self, meta=None):
38 """ 39 This method is inherited from CoreAllGroup but is unsafe on 40 cam.Package. Use instead `iter_user_tags`. 41 """ 42 warn("use iter_user_tags instead", UnsafeUseWarning, 2) 43 return super(_AllGroup, self).iter_tags()
44
45 - def _iter_tags_nowarn(self, meta=None):
46 """ 47 Allows to call iter_tags internally without raising a warning. 48 """ 49 return super(_AllGroup, self).iter_tags(meta)
50
51 - def iter_user_tags(self, meta=None):
52 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 53 o = self._owner 54 m = [(CAMSYS_TYPE, None)] 55 if meta: m += meta 56 return super(_AllGroup, self).iter_tags(m)
57
58 - def iter_annotation_types(self, meta=None):
59 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 60 o = self._owner 61 m = [(CAMSYS_TYPE, "annotation-type")] 62 if meta: m += meta 63 return super(_AllGroup, self).iter_tags(m)
64
65 - def iter_relation_types(self, meta=None):
66 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 67 o = self._owner 68 m = [(CAMSYS_TYPE, "relation-type")] 69 if meta: m += meta 70 return super(_AllGroup, self).iter_tags(m)
71
72 - def iter_lists(self, item=None, position=None, meta=None):
73 """ 74 This method is inherited from CoreAllGroup but is unsafe on 75 cam.Package. Use instead `iter_user_lists`. 76 """ 77 warn("use iter_user_lists instead", UnsafeUseWarning, 2) 78 return super(_AllGroup, self).iter_lists(item, position, meta)
79
80 - def _iter_lists_nowarn(self, item=None, position=None, meta=None):
81 """ 82 Allows to call iter_lists internally without raising a warning. 83 """ 84 return super(_AllGroup, self).iter_lists(item, position, meta)
85
86 - def iter_user_lists(self, item=None, position=None, meta=None):
87 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 88 o = self._owner 89 m = [(CAMSYS_TYPE, None)] 90 if meta : m += meta 91 return super(_AllGroup, self).iter_lists(item, position, m)
92
93 - def iter_schemas(self, item=None, position=None, meta=None):
94 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 95 o = self._owner 96 m = [(CAMSYS_TYPE, "schema")] 97 if meta: m += meta 98 return super(_AllGroup, self).iter_lists(item, position, m)
99
100 - def count_tags(self, meta=None):
101 """ 102 This method is inherited from CoreAllGroup but is unsafe on 103 cam.Package. Use instead `count_user_tags`. 104 """ 105 warn("use count_user_tags instead", UnsafeUseWarning, 2) 106 return super(_AllGroup, self).count_tags(meta)
107
108 - def _count_tags_nowarn(self, meta=None):
109 """ 110 Allows to call count_tags internally without raising a warning. 111 """ 112 return super(_AllGroup, self).count_tags(meta)
113
114 - def count_user_tags(self, meta=None):
115 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 116 o = self._owner 117 m = [(CAMSYS_TYPE, None)] 118 if meta: m += meta 119 return super(_AllGroup, self).count_tags(m)
120
121 - def count_annotation_types(self, meta=None):
122 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 123 o = self._owner 124 m = [(CAMSYS_TYPE, "annotation-type")] 125 if meta: m += meta 126 return super(_AllGroup, self).count_tags(m)
127
128 - def count_relation_types(self, meta=None):
129 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 130 o = self._owner 131 m = [(CAMSYS_TYPE, "relation-type")] 132 if meta: m += meta 133 return super(_AllGroup, self).count_tags(m)
134
135 - def count_lists(self, item=None, position=None, meta=None):
136 """ 137 This method is inherited from CoreAllGroup but is unsafe on 138 cam.Package. Use instead `count_user_lists`. 139 """ 140 warn("use count_user_lists instead", UnsafeUseWarning, 2) 141 return super(_AllGroup, self).count_lists(item, position, meta)
142
143 - def _count_lists_nowarn(self, item=None, position=None, meta=None):
144 """ 145 Allows to call count_lists internally without raising a warning. 146 """ 147 return super(_AllGroup, self).count_lists(item, position, meta)
148
149 - def count_user_lists(self, item=None, position=None, meta=None):
150 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 151 o = self._owner 152 m = [(CAMSYS_TYPE, None)] 153 if meta: m += meta 154 return super(_AllGroup, self).count_lists(item, position, m)
155
156 - def count_schemas(self, item=None, position=None, meta=None):
157 assert meta is None or CAMSYS_TYPE not in ( i[0] for i in meta ) 158 o = self._owner 159 m = [(CAMSYS_TYPE, "schema")] 160 if meta: m += meta 161 return super(_AllGroup, self).count_lists(item, position, m)
162
163 -class _OwnGroup(CamGroupMixin, CoreOwnGroup):
164 - def iter_tags(self):
165 """ 166 This method is inherited from CoreOwnGroup but is unsafe on 167 cam.Package. Use instead `iter_user_tags`. 168 """ 169 warn("use iter_user_tags instead", UnsafeUseWarning, 2) 170 return super(_OwnGroup, self).iter_tags()
171
172 - def _iter_tags_nowarn(self):
173 """ 174 Allows to call iter_tags internally without raising a warning. 175 """ 176 return super(_OwnGroup, self).iter_tags()
177
178 - def iter_user_tags(self):
179 o = self._owner 180 for i in o._backend.iter_tags((o._id,), 181 meta=[(CAMSYS_TYPE, None, None)]): 182 yield o.get_element(i)
183
184 - def iter_annotation_types(self):
185 o = self._owner 186 for i in o._backend.iter_tags((o._id,), 187 meta=[(CAMSYS_TYPE, "annotation-type", False)]): 188 yield o.get_element(i)
189
190 - def iter_relation_types(self):
191 o = self._owner 192 for i in o._backend.iter_tags((o._id,), 193 meta=[(CAMSYS_TYPE, "relation-type", False)]): 194 yield o.get_element(i)
195
196 - def iter_lists(self, item=None, position=None):
197 """ 198 This method is inherited from CoreOwnGroup but is unsafe on 199 cam.Package. Use instead `iter_user_lists`. 200 """ 201 warn("use iter_user_lists instead", UnsafeUseWarning, 2) 202 return super(_OwnGroup, self).iter_lists(item, position)
203
204 - def _iter_lists_nowarn(self, item=None, position=None):
205 """ 206 Allows to call iter_lists internally without raising a warning. 207 """ 208 return super(_OwnGroup, self).iter_lists(item, position)
209
210 - def iter_user_lists(self, item=None, position=None):
211 o = self._owner 212 meta = [(CAMSYS_TYPE, None, None)] 213 for i in o._backend.iter_lists((o._id,), None, item, position, meta): 214 yield o.get_element(i)
215
216 - def iter_schemas(self, item=None, position=None):
217 o = self._owner 218 meta = [(CAMSYS_TYPE, "schema", False)] 219 for i in o._backend.iter_lists((o._id,), None, item, position, meta): 220 yield o.get_element(i)
221
222 - def count_tags(self):
223 """ 224 This method is inherited from CoreOwnGroup but is unsafe on 225 cam.Package. Use instead `count_user_tags`. 226 """ 227 warn("use count_user_tags instead", UnsafeUseWarning, 2) 228 return super(_OwnGroup, self).count_tags()
229
230 - def _count_tags_nowarn(self):
231 """ 232 Allows to call count_tags internally without raising a warning. 233 """ 234 return super(_OwnGroup, self).count_tags()
235
236 - def count_user_tags(self):
237 o = self._owner 238 return o._backend.count_tags((o._id,), 239 meta=[(CAMSYS_TYPE, None, None)])
240
241 - def count_annotation_types(self):
242 o = self._owner 243 return o._backend.count_tags((o._id,), 244 meta=[(CAMSYS_TYPE, "annotation-type", False)])
245
246 - def count_relation_types(self):
247 o = self._owner 248 return o._backend.count_tags((o._id,), 249 meta=[(CAMSYS_TYPE, "relation-type", False)])
250
251 - def count_lists(self, item=None, position=None):
252 """ 253 This method is inherited from CoreOwnGroup but is unsafe on 254 cam.Package. Use instead `count_user_lists`. 255 """ 256 warn("use count_user_lists instead", UnsafeUseWarning, 2) 257 return super(_OwnGroup, self).count_lists(item=item, position=position)
258
259 - def _count_lists_nowarn(self, item=None, position=None):
260 """ 261 Allows to call count_lists internally without raising a warning. 262 """ 263 return super(_OwnGroup, self).count_lists(item=item, position=position)
264
265 - def count_user_lists(self, item=None, position=None):
266 o = self._owner 267 return o._backend.count_lists((o._id,), item=item, position=position, 268 meta=[(CAMSYS_TYPE, None, None)])
269
270 - def count_schemas(self, item=None, position=None):
271 o = self._owner 272 return o._backend.count_lists((o._id,), item=item, position=position, 273 meta=[(CAMSYS_TYPE, "schema", False)])
274
275 -class Package(CorePackage):
276 277 # use CAM subclasses as element factories 278 annotation_factory = Annotation 279 all_factory = _AllGroup 280 import_factory = Import 281 list_factory = List 282 media_factory = Media 283 relation_factory = Relation 284 resource_factory = Resource 285 own_factory = _OwnGroup 286 query_factory = Query 287 tag_factory = Tag 288 view_factory = View 289
290 - def __init__(self, url, create=False, readonly=False, force=False):
291 CorePackage.__init__(self, url, create, readonly, force) 292 if self.url != BOOTSTRAP_URI and self.uri != BOOTSTRAP_URI \ 293 and self.own.count_imports(uri=BOOTSTRAP_URI) == 0: 294 global _bootstrap_ref 295 b = _bootstrap_ref() 296 if b is None: 297 b = Package(BOOTSTRAP_URI, readonly=True) 298 _bootstrap_ref = wref(b) 299 self.create_import("cam", b) 300 301 ns = self._get_namespaces_as_dict() 302 ns.setdefault(DC_NS_PREFIX, "dc") 303 self._set_namespaces_with_dict(ns) 304 if create: 305 bk.init(self, self) 306 self.connect("modified-meta", bk.update) 307 self.connect("created", bk.init) 308 self.connect("tag::added", bk.update) 309 self.connect("tag::removed", bk.update) 310 self.connect("created::annotation-type", self._create_type_constraint) 311 self.connect("created::relation-type", self._create_type_constraint)
312
313 - def create_tag(self, id):
314 """ 315 This method is inherited from core.Package but is unsafe on 316 cam.Package. Use instead `create_user_tag`. 317 318 :see: `create_user_tag`, `create_annotation_type`, 319 `create_relation_type` 320 """ 321 warn("use create_user_tag instead", UnsafeUseWarning, 2) 322 return super(Package, self).create_tag(id)
323
324 - def create_user_tag(self, id):
325 """FIXME: missing docstring. 326 """ 327 t = super(Package, self).create_tag(id) 328 self.emit("created::user-type", t) 329 return t
330
331 - def create_annotation_type(self, id):
332 """FIXME: missing docstring. 333 """ 334 # NB: we inhibit the emission of created::tag until the system-type 335 # of the tag is set 336 self.enter_no_event_section() 337 try: 338 at = super(Package, self).create_tag(id) 339 finally: 340 self.exit_no_event_section() 341 342 at.enter_no_event_section() 343 try: 344 at._set_camsys_type("annotation-type") 345 finally: 346 at.exit_no_event_section() 347 348 self.emit("created::annotation-type", at) 349 # ^ this would create the associated type-constraint 350 self.emit("created::tag", at) 351 return at
352
353 - def create_relation_type(self, id):
354 """FIXME: missing docstring. 355 """ 356 # NB: we inhibit the emission of created::tag until the system-type 357 # of the tag is set 358 self.enter_no_event_section() 359 try: 360 rt = super(Package, self).create_tag(id) 361 finally: 362 self.exit_no_event_section() 363 364 rt.enter_no_event_section() 365 try: 366 rt._set_camsys_type("relation-type") 367 finally: 368 rt.exit_no_event_section() 369 370 self.emit("created::relation-type", rt) 371 # ^ this would create the associated type-constraint 372 self.emit("created::tag", rt) 373 return rt
374
375 - def create_annotation(self, id, media, begin, end, 376 mimetype, model=None, url="", type=None):
377 """FIXME: missing docstring. 378 """ 379 assert type is None or hasattr(type, "ADVENE_TYPE") \ 380 or type.find(":") > 0 # strict ID-ref 381 382 # NB: we inhibit the emission of created::annotation until the type 383 # of the annotation is set 384 self.enter_no_event_section() 385 try: 386 a = super(Package, self).create_annotation(id, media, begin, end, 387 mimetype, model, url) 388 finally: 389 self.exit_no_event_section() 390 391 if type: 392 type_is_element = hasattr(type, "ADVENE_TYPE") 393 a.enter_no_event_section() 394 if type_is_element: type.enter_no_event_section() 395 try: 396 a.type = type 397 finally: 398 if type_is_element: type.exit_no_event_section() 399 a.exit_no_event_section() 400 401 self.emit("created::annotation", a) 402 return a
403
404 - def create_relation(self, id, mimetype="x-advene/none", model=None, 405 url="", members=(), type=None):
406 """FIXME: missing docstring. 407 """ 408 assert type is None or hasattr(type, "ADVENE_TYPE") \ 409 or type.find(":") > 0 # strict ID-ref 410 411 # NB: we inhibit the emission of created::relation until the type 412 # of the relation is set 413 self.enter_no_event_section() 414 try: 415 r = super(Package, self).create_relation(id, mimetype, model, url, 416 members) 417 finally: 418 self.exit_no_event_section() 419 420 if type: 421 type_is_element = hasattr(type, "ADVENE_TYPE") 422 r.enter_no_event_section() 423 if type_is_element: type.enter_no_event_section() 424 try: 425 r.type = type 426 finally: 427 if type_is_element: type.exit_no_event_section() 428 r.exit_no_event_section() 429 430 self.emit("created::relation", r) 431 return r
432
433 - def create_list(self, id, items=()):
434 """ 435 This method is inherited from core.Package but is unsafe on 436 cam.Package. Use instead `create_user_list`. 437 438 :see: `create_user_list`, `create_schema` 439 """ 440 warn("use create_user_list instead", UnsafeUseWarning, 2) 441 return super(Package, self).create_list(id, items)
442
443 - def create_user_list(self, id, items=()):
444 """FIXME: missing docstring. 445 """ 446 L = super(Package, self).create_list(id, items) 447 self.emit("created::user-list", L) 448 return L
449
450 - def create_schema(self, id, items=()):
451 """FIXME: missing docstring. 452 """ 453 # NB: we inhibit the emission of created::list until the system-type 454 # of the list is set 455 self.enter_no_event_section() 456 try: 457 sc = super(Package, self).create_list(id, items) 458 finally: 459 self.exit_no_event_section() 460 461 sc.enter_no_event_section() 462 try: 463 sc._set_camsys_type("schema") 464 finally: 465 sc.exit_no_event_section() 466 467 self.emit("created::schema", sc) 468 self.emit("created::list", sc) 469 return sc
470
471 - def associate_tag(self, element, tag):
472 """ 473 This method is inherited from core.Package but is unsafe on 474 cam.Package. Use instead `associate_user_tag`. 475 """ 476 warn("use associate_user_tag instead", UnsafeUseWarning, 2) 477 super(Package, self).associate_tag(element, tag)
478
479 - def _associate_tag_nowarn(self, element, tag):
480 """ 481 Allows to call associate_tag internally without raising a warning. 482 """ 483 super(Package, self).associate_tag(element, tag)
484 485
486 - def dissociate_tag(self, element, tag):
487 """ 488 This method is inherited from core.Package but is unsafe on 489 cam.Package. Use instead `dissociate_user_tag`. 490 """ 491 warn("use associate_user_tag instead", UnsafeUseWarning, 2) 492 super(Package, self).dissociate_tag(element, tag)
493
494 - def _dissociate_tag_nowarn(self, element, tag):
495 """ 496 Allows to call dissociate_tag internally without raising a warning. 497 """ 498 super(Package, self).dissociate_tag(element, tag)
499
500 - def associate_user_tag(self, element, tag):
501 """ 502 FIXME: missing docstring. 503 """ 504 if hasattr(tag, "ADVENE_TYPE"): 505 systemtype = tag.get_meta(CAMSYS_TYPE, None) 506 else: 507 # tag is must be a strict id-ref; assume everything is ok 508 systemtype = None 509 if systemtype is not None: 510 raise SemanticError("Tag %s is not simple: %s" % 511 (tag._id, systemtype)) 512 super(Package, self).associate_tag(element, tag)
513
514 - def dissociate_user_tag(self, element, tag):
515 """ 516 FIXME: missing docstring. 517 """ 518 systemtype = tag.get_meta(CAMSYS_TYPE, None) 519 if systemtype is not None: 520 raise SemanticError("Tag %s is not simple: %s", tag._id, systemtype) 521 super(Package, self).dissociate_tag(element, tag)
522
523 - def _create_type_constraint(self, package, type):
524 """ 525 Callback invoked on 'created::tag' to automatically create the 526 type-constraint view associated with annotation/relation types. 527 """ 528 c = self.create_view( 529 ":constraint:%s" % type._id, 530 "application/x-advene-type-constraint", 531 ) 532 type.enter_no_event_section() 533 try: 534 type.element_constraint = c 535 finally: 536 type.exit_no_event_section()
537 538 539 # TALES shortcuts 540 541 @property
542 - def _tales_annotation_types(self):
543 return self.all.annotation_types
544 545 @property
546 - def _tales_relation_types(self):
547 return self.all.relation_types
548 549 @property
550 - def _tales_user_tags(self):
551 return self.all.user_tags
552 553 @property
554 - def _tales_schemas(self):
555 return self.all.schemas
556 557 @property
558 - def _tales_user_lists(self):
559 return self.all.user_lists
560 561 _bootstrap_ref = lambda: None 562 563 Package.make_metadata_property(bk.CREATOR, default="") 564 Package.make_metadata_property(bk.CONTRIBUTOR, default="") 565 Package.make_metadata_property(bk.CREATED, default="") 566 Package.make_metadata_property(bk.MODIFIED, default="") 567 568 Package.make_metadata_property(DC_NS_PREFIX + "title", default="") 569 Package.make_metadata_property(DC_NS_PREFIX + "description", default="") 570 571 Package.make_metadata_property(RDFS_NS_PREFIX + "seeAlso", default=None) 572