aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/python/Collection.py
blob: 48092aadaa296fed19f7e8f758728db6e0cd08d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python 
# -*- Mode: python -*-
#======================================================================
# FILE: Collection.py
# CREATOR: eric 
#
# DESCRIPTION:
#   
#
#  $Id$
#  $Locker$
#
# (C) COPYRIGHT 2001, Eric Busboom <eric@softwarestudio.org>
# (C) COPYRIGHT 2001, Patrick Lewis <plewis@inetarena.com>  
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of either: 
#
#    The LGPL as published by the Free Software Foundation, version
#    2.1, available at: http://www.fsf.org/copyleft/lesser.html
#
#  Or:
#
#    The Mozilla Public License Version 1.0. You may obtain a copy of
#    the License at http://www.mozilla.org/MPL/
#======================================================================

from types import *

class Collection:
    """A group of components that can be modified somewhat like a list.

    Usage:
        Collection(componet, propSequence)

    component is a Component object
    propSequence is a list or tuple of Property (or subclass of Property)
        of objects already in component
    """

    def __init__(self, component, propSequence):
        self._properties = list(propSequence[:])
        self._component = component

    def __getslice__(self, beg, end):
        return Collection(self._component, self._properties[beg:end])

    def __setslice__(self, beg, end, sequence):

        if  not isinstance(sequence,ListType):
            raise TypeError, "must assign list (not instance) to slice"

        oldProps = self._properties[beg:end]

        for p in oldProps:
            self._component.remove_property(p)

        self._properties[beg:end] = sequence
        for p in sequence:
            self._component.add_property(p)
            
    def __getitem__(self, i):
        return self._properties[i]

    def __setitem__(self, i, prop):
        self._component.remove_property(self._properties[i])
        self._component.add_property(prop)
        self._properties[i]=prop

    def __delitem__(self, i):
        self._component.remove_property(self._properties[i])
        del self._properties[i]

    def __len__(self):
        return len(self._properties)

    def __str__(self):
        s = "[ "
        if len(self._properties) > 0:
            s = s + str(self._properties[0])
            for p in self._properties[1:]:
                s = "%s, %s" % (s, p)
        s = s + " ]"
        return s
            
    def append(self, property):
        self._properties.append(property)
        self._component.add_property(property)

class ComponentCollection:
    
    def __init__(self, parent, componentSequence):
        self._parent = parent
        self._components = list(componentSequence[:])

    def __getslice__(self, beg, end):
        return ComponentCollection(self._parent, self._components[beg:end])

    def __setslice__(self, beg, end, sequence):
        oldComps = self._components[beg:end]
        self._components.__setslice__(beg, end, sequence)
        for c in sequence:
            self._components.addComponent(c)
        for c in oldComps:
            self._parent.remove_component(c)

    def __getitem__(self, i):
        return self._components[i]

    def __setitem__(self, i, prop):
        self._parent.remove_component(self._components[i])
        self._parent.add_property(prop)
        self._components[i]=prop

    def __delitem__(self, i):
        self._parent.remove_componet(self._components[i])
        del self._components[i]

    def __len__(self):
        return len(self._components)
            
    def append(self, property):
        self._components.append(property)
        self._parent.addComponent(property)