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

Source Code for Module advene.model.bundle

  1  # 
  2  # Advene: Annotate Digital Videos, Exchange on the NEt 
  3  # Copyright (C) 2008 Olivier Aubert <olivier.aubert@liris.cnrs.fr> 
  4  # 
  5  # Advene is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation; either version 2 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Advene is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with Advene; if not, write to the Free Software 
 17  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 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   
71 -class AbstractBundle (object):
72 """ 73 Base class of all Bundles. 74 75 Implements all the read-only methods. 76 """ 77
78 - def uris(self):
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 # list implementation 112 # 113
114 - def __add__ (self, other):
115 assert isinstance (other, AbstractBundle) 116 return SumBundle (self, other)
117
118 - def __contains__ (self, v):
119 return (v is self._dict.get(v.getUri (absolute=True), None) 120 or v in self._dict)
121
122 - def __getitem__ (self, index):
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
131 - def __getslice__ (self, begin, end):
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
138 - def __iter__ (self):
139 return iter (self._list)
140
141 - def __len__ (self):
142 return len (self._list)
143 144 # 145 # dict implementation 146 # 147
148 - def get (self, id_, default=None):
149 return self._dict.get (id_, default)
150
151 - def has_key (self, key):
152 return self._dict.has_key (key)
153
154 - def items (self):
155 return self._dict.items ()
156
157 - def iteritems (self):
158 return self._dict.iteritems ()
159
160 - def iterkeys (self):
161 return self._dict.iterkeys ()
162
163 - def itervalues (self):
164 return self._dict.itervalues ()
165
166 - def ids (self):
167 return [ e.id for e in self._dict.itervalues() ]
168
169 - def keys (self):
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
178 - def values (self):
179 return self._dict.values ()
180 181 # 182 # helper method 183 # 184
185 - def get_by_id(self, id_):
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
195 -class ListBundle (AbstractBundle):
196 """ 197 A class of bundle constructed from a list of items. 198 Prerequisite: all items must have a getUri(absolute) method. 199 """ 200
201 - def __init__ (self, the_list):
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
208 -class SumBundle (AbstractBundle):
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
216 - def __init__ (self, *bundles):
217 super(SumBundle, self).__init__ () 218 self._list = [] 219 self._dict = {} 220 for b in bundles: 221 self.__iadd__ (b)
222
223 - def __add__ (self, bundle):
224 r = SumBundle (*(self.__bundle)) 225 r += bundle 226 return r
227
228 - def __iadd__ (self, bundle):
229 assert isinstance (bundle, AbstractBundle) 230 self._list += bundle._list 231 self._dict.update (bundle._dict) 232 return self
233 234
235 -class