3 #======================================================================
10 # $Id: Collection.py,v 1.3 2001-03-11 00:46:57 plewis Exp $
13 # (C) COPYRIGHT 2001, Eric Busboom <eric@softwarestudio.org>
14 # (C) COPYRIGHT 2001, Patrick Lewis <plewis@inetarena.com>
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of either:
19 # The LGPL as published by the Free Software Foundation, version
20 # 2.1, available at: http://www.fsf.org/copyleft/lesser.html
24 # The Mozilla Public License Version 1.0. You may obtain a copy of
25 # the License at http://www.mozilla.org/MPL/
26 #======================================================================
31 """A group of components that can be modified somewhat like a list.
34 Collection(componet, propSequence)
36 component is a Component object
37 propSequence is a list or tuple of Property (or subclass of Property)
38 of objects already in component
41 def __init__(self, component, propSequence):
42 self._properties = list(propSequence[:])
43 self._component = component
45 def __getslice__(self, beg, end):
46 return Collection(self._component, self._properties[beg:end])
48 def __setslice__(self, beg, end, sequence):
50 if not isinstance(sequence,ListType):
51 raise TypeError, "must assign list (not instance) to slice"
53 oldProps = self._properties[beg:end]
56 self._component.remove_property(p)
58 self._properties[beg:end] = sequence
60 self._component.add_property(p)
62 def __getitem__(self, i):
63 return self._properties[i]
65 def __setitem__(self, i, prop):
66 self._component.remove_property(self._properties[i])
67 self._component.add_property(prop)
68 self._properties[i]=prop
70 def __delitem__(self, i):
71 self._component.remove_property(self._properties[i])
72 del self._properties[i]
75 return len(self._properties)
79 if len(self._properties) > 0:
80 s = s + str(self._properties[0])
81 for p in self._properties[1:]:
86 def append(self, property):
87 self._properties.append(property)
88 self._component.add_property(property)
90 class ComponentCollection:
92 def __init__(self, parent, componentSequence):
94 self._components = list(componentSequence[:])
96 def __getslice__(self, beg, end):
97 return ComponentCollection(self._parent, self._components[beg:end])
99 def __setslice__(self, beg, end, sequence):
100 oldComps = self._components[beg:end]
101 self._components.__setslice__(beg, end, sequence)
103 self._parent.add_component(c)
105 self._parent.remove_component(c)
107 def __getitem__(self, i):
108 return self._components[i]
110 def __setitem__(self, i, prop):
111 self._parent.remove_component(self._components[i])
112 self._parent.add_property(prop)
113 self._components[i]=prop
115 def __delitem__(self, i):
116 self._parent.remove_componet(self._components[i])
117 del self._components[i]
120 return len(self._components)
122 def __add__(self, iterable):
126 def append(self, property):
127 self._components.append(property)
128 self._parent.add_component(property)