1 """
2 I define the class Tag.
3
4 Note that Tag inherits GroupMixin, but can only be considered a group in the
5 context of a package in which tag-element associations are considered. That
6 implicit context-package is given by the ``package`` session variable in
7 `advene.util.session`. If that session variable is not set, using a tag as
8 a group will raise a TypeError.
9 """
10
11 from advene.model.core.element import PackageElement, TAG
12 from advene.model.core.group import GroupMixin
13 from advene.util.alias import alias
14 from advene.util.session import session
15
16 -class Tag(PackageElement, GroupMixin):
17
18 ADVENE_TYPE = TAG
19
22
23 @classmethod
27
31
33 """Iter over the elements associated with this tag in ``package``.
34
35 If ``inherited`` is set to False, the elements associated by imported
36 packages of ``package`` will not be yielded.
37
38 If an element is unreachable, None is yielded.
39
40 See also `iter_element_ids`.
41 """
42 return self._iter_elements_or_element_ids(package, inherited, True)
43
45 """Iter over the id-refs of the elements associated with this tag in
46 ``package``.
47
48 If ``package`` is not provided, the ``package`` session variable is
49 used. If the latter is unset, a TypeError is raised.
50
51 If ``inherited`` is set to False, the elements associated by imported
52 packages of ``package`` will not be yielded.
53
54 See also `iter_elements`.
55 """
56
57 if package is None:
58 package = session.package
59 if package is None:
60 raise TypeError("no package set in session, must be specified")
61 u = self._get_uriref()
62 if not inherited:
63 pids = (package._id,)
64 get_element = package.get_element
65 for pid, eid in package._backend.iter_elements_with_tag(pids, u):
66 if _get:
67 y = package.get_element(eid, None)
68 else:
69 y = eid
70 yield y
71 else:
72 for be, pdict in package._backends_dict.iteritems():
73 for pid, eid in be.iter_elements_with_tag(pdict, u):
74 p = pdict[pid]
75 if _get:
76 y = p.get_element(eid, None)
77 else:
78 y = package.make_id_for(p, eid)
79 yield y
80
81 @alias(iter_element_ids)
83
84
85
86
87
88
89
90 pass
91
92 - def has_element(self, element, package=None, inherited=True):
93 """Is this tag associated to ``element`` by ``package``.
94
95 If ``package`` is not provided, the ``package`` session variable is
96 used. If the latter is unset, a TypeError is raised.
97
98 If ``inherited`` is set to False, only returns True if ``package``
99 itself associates this tag to ``element``; else returns True also if
100 the association is inherited from an imported package.
101 """
102 if not inherited:
103 return element.has_tag(self, package, False)
104 else:
105 return list(element.iter_taggers(self, package))
106
108 """
109 This method is used by adveve.model.tales, when the TALES processor
110 wants to traverse the given object.
111 """
112 return ContextualizedTag(self, context)
113
114
115 -class ContextualizedTag(GroupMixin, object):
116 - def __init__(self, tag, context):
117 self._t = tag
118 self._p = context.locals.get("package") \
119 or context.globals.get("package")
120
121 - def __iter__(self):
122 return self._t.iter_elements(package=self._p)
123
124 - def __getattr__(self, name):
125 if name[0] != "_" or name.startswith("_tales_"):
126 return getattr(self._t, name)
127 else:
128 raise AttributeError(name)
129
130 - def __repr__(self):
131 return repr(self._t) + "#contextualized#"
132