Package Ganga :: Package GPIDev :: Package Lib :: Package GangaList :: Module GangaList'
[hide private]
[frames] | no frames]

Source Code for Module Ganga.GPIDev.Lib.GangaList.GangaList'

  1  from Ganga.GPIDev.Base.Objects import GangaObject 
  2  from Ganga.GPIDev.Base.Filters import allComponentFilters 
  3  from Ganga.GPIDev.Base.Proxy import addProxy,isType,getProxyAttr,stripProxy, TypeMismatchError 
  4  from Ganga.GPIDev.Base.VPrinter import full_print 
  5  from Ganga.GPIDev.Schema.Schema import ComponentItem,Schema,SimpleItem,Version 
  6  from Ganga.Utility.Plugin.GangaPlugin import allPlugins 
  7  from Ganga.Utility.util import containsGangaObjects,isNestedList 
  8  from Ganga.GPIDev.Base.Proxy import ReadOnlyObjectError 
  9  import copy,sys 
 10   
11 -def makeGangaList(_list, mapfunction = None, parent = None):
12 """Should be used for makeing full gangalists""" 13 14 #work with a simple list always 15 if isType(_list,list): 16 _list = _list 17 elif isType(_list,GangaList): 18 _list = getProxyAttr(_list,'_list') 19 else: 20 _list = [_list] 21 22 if mapfunction is not None: 23 _list = map(mapfunction,_list) 24 25 result = GangaList() 26 result.extend(_list) 27 28 #set the parent if possible 29 if parent is not None: 30 result._setParent(parent) 31 32 for r in result: 33 if isinstance(r,GangaObject) and r._getParent() is None: 34 r._setParent(parent) 35 36 return result
37
38 -def stripGangaList(_list):
39 """Gets the underlying list of non-proxy objects""" 40 result = _list 41 if isType(_list, GangaList): 42 result = getProxyAttr(_list, '_list') 43 return result
44
45 -def makeGangaListByRef(_list):
46 """Faster version of makeGangaList. Does not make a copy of _list but use it by reference.""" 47 result = GangaList() 48 result._list = _list 49 return result
50
51 -def decorateListEntries(entries, typename):
52 return "[%d Entries of type '%s']" % (entries,typename)
53
54 -class GangaListIter(object):
55 """Simple wrapper around the listiterator"""
56 - def __init__(self, it):
57 self.it = it
58 - def next(self):
59 return addProxy(self.it.next())
60 - def __iter__(self):
61 return self
62
63 -class GangaList(GangaObject):
64 65 _category = 'internal' 66 _exportmethods = ['__add__', '__contains__', '__delitem__', '__delslice__', '__eq__', '__ge__',\ 67 '__getitem__', '__getslice__', '__gt__', '__iadd__', '__imul__',\ 68 '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__','__reversed__','__radd__','__rmul__',\ 69 '__setitem__', '__setslice__', 'append', 'count', 'extend', 'index',\ 70 'insert', 'pop', 'remove', 'reverse', 'sort','__hash__'] 71 _hidden = 1 72 _enable_plugin = 1 73 _name = 'GangaList' 74 _schema = Schema(Version(1, 0), { 75 '_list' : SimpleItem(defvalue=[], doc='The raw list', hidden = 1), 76 }) 77 _enable_config = 1 78
79 - def __init__(self):
80 super(GangaList, self).__init__()
81 82 # convenience methods
83 - def is_list(self, obj):
84 result = (obj != None) and (isType(obj, GangaList) or isinstance(obj,list)) 85 return result
86
87 - def strip_proxy(self, obj, filter = False):
88 """Removes proxies and calls shortcut if needed""" 89 90 def applyFilter(obj, item): 91 category = item['category'] 92 filter = allComponentFilters[category] 93 filter_obj = filter(obj,item) 94 if filter_obj is None: 95 raise TypeMismatchError('%s is not of type %s.' % (str(obj),category)) 96 return filter_obj
97 98 obj = stripProxy(obj) 99 #apply a filter if possible 100 if filter: 101 parent = self._getParent() 102 item = self.findSchemaParentSchemaEntry(parent) 103 if item and item.isA(ComponentItem):#only filter ComponentItems 104 category = item['category'] 105 if isType(obj, GangaObject): 106 if obj._category != category: 107 obj = applyFilter(obj, item) 108 obj._setParent(parent) 109 else: 110 obj = applyFilter(obj, item) 111 return obj
112 113
114 - def strip_proxy_list(self, obj_list, filter = False):
115 116 if isType(obj_list, GangaList): 117 return getProxyAttr(obj_list,'_list') 118 result = [] 119 for o in obj_list: 120 result.append(self.strip_proxy(o,filter)) 121 return result
122
123 - def getCategory(self):
124 """Returns a list of categories for the objects in the list. Returns [] for an empty list.""" 125 126 result = [] 127 for o in self._list: 128 category = o._category 129 if not category in result: 130 result.append(category) 131 return result
132
133 - def checkReadOnly(self):
134 """Puts a hook in to stop mutable access to readonly jobs.""" 135 if self._readonly(): 136 raise ReadOnlyObjectError('object %s is readonly and attribute "%s" cannot be modified now'%(repr(self),self._name)) 137 else: 138 self._getWriteAccess() 139 self._setDirty() # TODO: BUG: This should only be set _after_ the change has been done! This can lead to data loss!
140
141 - def checkNestedLists(self,value):
142 """The rule is that if there are nested lists then they 143 must not contain GangaObjects, as this corrupts the repository""" 144 if isNestedList(value) and containsGangaObjects(value): 145 raise TypeMismatchError('Assigning nested lists which contain Ganga GPI Objects is not supported.')
146 147 # list methods 148 # All list methods should be overridden in a way that makes 149 # sure that no proxy objects end up in the list, and no 150 # unproxied objects make it out. 151
152 - def __add__(self, obj_list):
153 #Savanah 32342 154 if not self.is_list(obj_list): 155 raise TypeError('Type %s can not be concatinated to a GangaList' % type(obj_list)) 156 157 return makeGangaList(self._list.__add__(self.strip_proxy_list(obj_list, True)))
158 - def _export___add__(self, obj_list):
159 self.checkReadOnly() 160 self.checkNestedLists(obj_list) 161 return addProxy(self.__add__(obj_list))
162
163 - def __contains__(self, obj):
164 return self._list.__contains__(self.strip_proxy(obj))
165
166 - def __copy__(self):
167 """Bypass any checking when making the copy""" 168 return makeGangaList(_list = copy.copy(self._list))
169
170 - def __delitem__(self, obj):
171 self._list.__delitem__(self.strip_proxy(obj))
172 - def _export___delitem__(self,obj):
173 self.checkReadOnly() 174 self.__delitem__(obj)
175
176 - def __delslice__(self, start, end):
177 self._list.__delslice__(start, end)
178 - def _export___delslice__(self, start, end):
179 self.checkReadOnly() 180 self.__delslice__(start,end)
181
182 - def __deepcopy__(self, memo):
183 """Bypass any checking when making the copy""" 184 return makeGangaList(_list = copy.deepcopy(self._list, memo))
185
186 - def __eq__(self, obj_list):
187 if obj_list is self:#identity check 188 return True 189 result = False 190 if self.is_list(self.strip_proxy(obj_list)): 191 result = self._list.__eq__(self.strip_proxy_list(obj_list)) 192 return result
193
194 - def __ge__(self, obj_list):
195 return self._list.__ge__(self.strip_proxy_list(obj_list))
196
197 - def __getitem__(self, index):
198 return self._list.__getitem__(index)
199 - def _export___getitem__(self, index):
200 return addProxy(self.__getitem__(index))
201
202 - def __getslice__(self, start, end):
203 return makeGangaList(_list = self._list.__getslice__(start, end))
204 - def _export___getslice__(self, start, end):
205 return addProxy(self.__getslice__(start, end))
206
207 - def __gt__(self, obj_list):
208 return self._list.__gt__(self.strip_proxy_list(obj_list))
209
210 - def __hash__(self):
211 #will always throw an error 212 return self._list.__hash__()
213
214 - def __iadd__(self, obj_list):
215 self._list.__iadd__(self.strip_proxy_list(obj_list, True)) 216 return self
217 - def _export___iadd__(self, obj_list):
218 self.checkReadOnly() 219 self.checkNestedLists(obj_list) 220 return addProxy(self.__iadd__(obj_list))
221
222 - def __imul__(self, number):
223 self._list.__imul__(number) 224 return self
225 - def _export___imul__(self, number):
226 self.checkReadOnly() 227 return addProxy(self.__imul__(number))
228
229 - def __iter__(self):
230 return self._list.__iter__()
231 - def _export___iter__(self):
232 return GangaListIter(iter(self._list))
233
234 - def __le__(self, obj_list):
235 return self._list.__le__(self.strip_proxy_list(obj_list))
236
237 - def __len__(self):
238 return len(self._list)
239
240 - def __lt__(self, obj_list):
241 return self._list.__lt__(self.strip_proxy_list(obj_list))
242
243 - def __mul__(self, number):
244 return makeGangaList(self._list.__mul__(number))
245 - def _export___mul__(self, number):
246 return addProxy(self.__mul__(number))
247
248 - def __ne__(self, obj_list):
249 if obj_list is self:#identity check 250 return True 251 result = True 252 if self.is_list(obj_list): 253 result = self._list.__ne__(self.strip_proxy_list(obj_list)) 254 return result
255
256 - def __reversed__(self):
257 """Implements the __reversed__ list method introduced in 2.4""" 258 259 try: 260 return reversed(self._list) 261 except NameError: 262 #workaround for pythion < 2.4 263 class GangaReverseIter(object): 264 """Simple wrapper around the list""" 265 def __init__(self, data): 266 self.data = data 267 self.count = len(self.data) - 1
268 def next(self): 269 self.count -= 1 270 if self.count: 271 result = self.data[self.count + 1] 272 return result 273 else: 274 raise StopIteration() 275 276 def __iter__(self): 277 return self 278 279 return GangaReverseIter(self._list) 280
281 - def _export___reversed__(self):
282 return GangaListIter(self.__reversed__())
283
284 - def __radd__(self, obj):
285 return obj + self._list
286 - def _export___radd__(self, obj):
287 #return the proxied objects 288 cp = [] 289 for i in self._export___iter__(): 290 cp.append(i) 291 return obj + cp
292
293 - def __rmul__(self, number):
294 return makeGangaList(self._list.__rmul__(number))
295 - def _export___rmul__(self, number):
296 return addProxy(self.__rmul__(number))
297
298 - def __setitem__(self, index, obj):
299 self._list.__setitem__(index, self.strip_proxy(obj, True))
300 - def _export___setitem__(self, index, obj):
301 self.checkReadOnly() 302 self.checkNestedLists(obj) 303 self.__setitem__(index, obj)
304
305 - def __setslice__(self, start, end, obj_list):
306 self._list.__setslice__(start, end, self.strip_proxy_list(obj_list, True))
307 - def _export___setslice__(self, start, end, obj_list):
308 self.checkReadOnly() 309 self.checkNestedLists(obj_list) 310 self.__setslice__(start,end,obj_list)
311
312 - def __repr__(self):
313 return self.toString()
314 - def __str__(self):
315 return self.__repr__()
316
317 - def append(self, obj):
318 self._list.append(self.strip_proxy(obj, True))
319 - def _export_append(self, obj):
320 self.checkReadOnly() 321 self.checkNestedLists(obj) 322 self.append(obj)
323
324 - def count(self, obj):
325 return self._list.count(self.strip_proxy(obj))
326
327 - def extend(self, ittr):
328 for i in ittr: 329 self.append(i)
330 - def _export_extend(self, ittr):
331 self.checkReadOnly() 332 self.checkNestedLists(ittr) 333 self.extend(ittr)
334
335 - def index(self, obj):
336 return self._list.index(self.strip_proxy(obj))
337
338 - def insert(self, index, obj):
339 self._list.insert(index, self.strip_proxy(obj, True))
340 - def _export_insert(self, index, obj):
341 self.checkReadOnly() 342 self.checkNestedLists(obj) 343 self.insert(index, obj)
344
345 - def pop(self, index = -1):
346 return self._list.pop(index)
347 - def _export_pop(self, index = -1):
348 self.checkReadOnly() 349 return addProxy(self.pop(index))
350
351 - def remove(self, obj):
352 self._list.remove(self.strip_proxy(obj))
353 - def _export_remove(self, obj):
354 self.checkReadOnly() 355 self.remove(obj)
356
357 - def reverse(self):
358 self._list.reverse()
359 - def _export_reverse(self):
360 self.checkReadOnly() 361 self.reverse()
362
363 - def sort(self, cmpfunc = None):
364 #TODO: Should comparitor have access to unproxied objects? 365 self._list.sort(cmpfunc)
366 - def _export_sort(self, cmpfunc = None):
367 self.checkReadOnly() 368 self.sort(cmpfunc)
369 370 #now some more ganga specific methods
371 - def findSchemaParentSchemaEntry(self, parent):
372 """Finds the schema entry for this GangaList""" 373 result = None 374 if parent and parent._schema: 375 for k, v in parent._schema.allItems(): 376 if getattr(parent,k) is self: 377 result = v 378 break 379 return result
380
381 - def printSummaryTree(self,level = 0, verbosity_level = 0, whitespace_marker = '', out = sys.stdout, selection = ''):
382 parent = self._getParent() 383 schema_entry = self.findSchemaParentSchemaEntry(parent) 384 385 if parent is None: 386 full_print(self,out) 387 return 388 389 if schema_entry: 390 self_len = len(self) 391 print_summary = schema_entry['summary_print'] 392 maxLen = schema_entry['summary_sequence_maxlen'] 393 394 if print_summary: 395 fp = getattr(parent,print_summary) 396 str_val = fp(self._list,verbosity_level) 397 print >>out, str_val, 398 return 399 400 if (maxLen != -1) and (self_len > maxLen): 401 print >>out, decorateListEntries(self_len, type(self[0]).__name__), 402 return 403 else: 404 full_print(self,out) 405 return 406 407 print >>out, str(self._list), 408 return
409
410 - def toString(self):
411 """Returns a simple str of the _list.""" 412 return str(self._list)
413