1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """
20 A bundle is a collection of objects implementing the _getUri_ method, returning
21 a URI which is supposed to uniquely identify the corresponding instance.
22
23 A bundle is homogeneous to both a list and a dictionary: every element of the
24 list is also indexed by its URI. Hence the 'bundle[index]' notation can be
25 used with integers or strings.
26
27 Since bundle may list/dict views of complex underlying data, many operations
28 which are usual with simple python lists, are just meaningless for bundles.
29
30 Permitted list operations are
31 - len(b)
32 - b.append
33 - b.insert
34 - b.pop
35 - b.remove
36 - b[x]
37 - b[x:y]
38 - del b[x]
39 - del b[x:y]
40 - v in b
41 - b1 + b2
42
43 Permitted dict operations are
44 - b.clear
45 - b.get
46 - b.has_key
47 - b.items
48 - b.iteritems
49 - b.iterkeys
50 - b.itervalues
51 - b.keys
52 - b.popitem
53 - values
54 - k in b
55 - b[k]
56
57
58 Note also that iter(b) iterates over its values (as for lists). Iterating over keys required the _iterkeys_ method.
59 """
60
61 import advene.model.util.uri
62
63 import advene.model.modeled as modeled
64 import advene.model.viewable as viewable
65
66 from advene.model.constants import xlinkNS
67 from advene.model.exception import AdveneException
68
69 from gettext import gettext as _
70
72 """
73 Base class of all Bundles.
74
75 Implements all the read-only methods.
76 """
77
79 """
80 Return the uris of the objects in the bundle.
81
82 Note that items in the bundle are indexed by their keys. Hence, this
83 method is equivalent to _keys_.
84 """
85 return self._dict.keys ()
86
87 - def getQName (self, key, namespaces, default=None):
88 """
89 Resolve the given key as a QName with regard to the given namespaces.
90
91 _namespaces_ is a dict whose keys are qname prefices and values are
92 URIs. QName is resolved by concatenating the URI, '#' and the suffix.
93 """
94 try:
95 colon = key.index (':')
96 prefix = key[:colon]
97 suffix = key[colon+1:]
98 except ValueError:
99 prefix = ''
100 suffix = key
101
102 uri = namespaces.get (prefix)
103 if uri is None:
104 return default
105 else:
106 real_key = '%s#%s' % (uri, suffix)
107 return self.get (real_key, default=default)
108
109
110
111
112
113
117
119 return (v is self._dict.get(v.getUri (absolute=True), None)
120 or v in self._dict)
121
123 if isinstance (index, int):
124 return self._list[index]
125 else:
126 return self._dict[index]
127
128 - def index (self, element):
129 return self._list.index (element)
130
132 """
133 b.__getslice__(i, j) <==> b[i:j]
134 Note that the slice is a copy of this bundle.
135 """
136 return ListBundle (self._list[begin:end])
137
139 return iter (self._list)
140
142 return len (self._list)
143
144
145
146
147
148 - def get (self, id_, default=None):
150
152 return self._dict.has_key (key)
153
155 return self._dict.items ()
156
159
162
165
168
170 """
171 Return the keys of this bundle.
172
173 Note that items in the bundle are indexed by their keys. Hence, this
174 method is equivalent to _uris_.
175 """
176 return self._dict.keys ()
177
179 return self._dict.values ()
180
181
182
183
184
186 """Inefficient but helpful method.
187 """
188 l=[ e for e in self._dict.itervalues() if e.id == id_ ]
189 if len(l) == 1:
190 return l[0]
191 else:
192 return None
193
194
196 """
197 A class of bundle constructed from a list of items.
198 Prerequisite: all items must have a getUri(absolute) method.
199 """
200
202 super(ListBundle, self).__init__ ()
203 self._list = the_list[:]
204 self._dict = dict (
205 [ (i.getUri (absolute=True), i) for i in the_list ]
206 )
207
209 """
210 The concatenation of several bundles.
211
212 A sum bundle is a read-only buffer resulting from the concatenation of
213 several bundles.
214 """
215
222
227
233
234