Imported Upstream version 0.48
[platform/upstream/libical.git] / src / python / Collection.py
1 #!/usr/bin/env python 
2 # -*- Mode: python -*-
3 #======================================================================
4 # FILE: Collection.py
5 # CREATOR: eric 
6 #
7 # DESCRIPTION:
8 #   
9 #
10 #  $Id: Collection.py,v 1.3 2001-03-11 00:46:57 plewis Exp $
11 #  $Locker:  $
12 #
13 # (C) COPYRIGHT 2001, Eric Busboom <eric@softwarestudio.org>
14 # (C) COPYRIGHT 2001, Patrick Lewis <plewis@inetarena.com>  
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of either: 
18 #
19 #    The LGPL as published by the Free Software Foundation, version
20 #    2.1, available at: http://www.fsf.org/copyleft/lesser.html
21 #
22 #  Or:
23 #
24 #    The Mozilla Public License Version 1.0. You may obtain a copy of
25 #    the License at http://www.mozilla.org/MPL/
26 #======================================================================
27
28 from types import *
29
30 class Collection:
31     """A group of components that can be modified somewhat like a list.
32
33     Usage:
34         Collection(componet, propSequence)
35
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
39     """
40
41     def __init__(self, component, propSequence):
42         self._properties = list(propSequence[:])
43         self._component = component
44
45     def __getslice__(self, beg, end):
46         return Collection(self._component, self._properties[beg:end])
47
48     def __setslice__(self, beg, end, sequence):
49
50         if  not isinstance(sequence,ListType):
51             raise TypeError, "must assign list (not instance) to slice"
52
53         oldProps = self._properties[beg:end]
54
55         for p in oldProps:
56             self._component.remove_property(p)
57
58         self._properties[beg:end] = sequence
59         for p in sequence:
60             self._component.add_property(p)
61             
62     def __getitem__(self, i):
63         return self._properties[i]
64
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
69
70     def __delitem__(self, i):
71         self._component.remove_property(self._properties[i])
72         del self._properties[i]
73
74     def __len__(self):
75         return len(self._properties)
76
77     def __str__(self):
78         s = "[ "
79         if len(self._properties) > 0:
80             s = s + str(self._properties[0])
81             for p in self._properties[1:]:
82                 s = "%s, %s" % (s, p)
83         s = s + " ]"
84         return s
85             
86     def append(self, property):
87         self._properties.append(property)
88         self._component.add_property(property)
89
90 class ComponentCollection:
91     
92     def __init__(self, parent, componentSequence):
93         self._parent = parent
94         self._components = list(componentSequence[:])
95
96     def __getslice__(self, beg, end):
97         return ComponentCollection(self._parent, self._components[beg:end])
98
99     def __setslice__(self, beg, end, sequence):
100         oldComps = self._components[beg:end]
101         self._components.__setslice__(beg, end, sequence)
102         for c in sequence:
103             self._parent.add_component(c)
104         for c in oldComps:
105             self._parent.remove_component(c)
106
107     def __getitem__(self, i):
108         return self._components[i]
109
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
114
115     def __delitem__(self, i):
116         self._parent.remove_componet(self._components[i])
117         del self._components[i]
118
119     def __len__(self):
120         return len(self._components)
121     
122     def __add__(self, iterable):
123         for i in iterable:
124             self.append(i)
125     
126     def append(self, property):
127         self._components.append(property)
128         self._parent.add_component(property)