1 #Copyright (C) 2008 Codethink Ltd
2 #copyright: Copyright (c) 2005, 2007 IBM Corporation
4 #This library is free software; you can redistribute it and/or
5 #modify it under the terms of the GNU Lesser General Public
6 #License version 2 as published by the Free Software Foundation.
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 #GNU General Public License for more details.
12 #You should have received a copy of the GNU Lesser General Public License
13 #along with this program; if not, write to the Free Software
14 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 #Portions of this code originally licensed and copyright (c) 2005, 2007
17 #IBM Corporation under the BSD license, available at
18 #U{http://www.opensource.org/licenses/bsd-license.php}
20 #authors: Peter Parente, Mark Doffman
22 class _StateSetImpl(Accessibility__POA.StateSet):
24 Implementation of the StateSet interface. Clients should not use this class
25 directly, but rather the L{StateSet} proxy class.
27 @param states: Set of states
31 """Initializes the state set."""
34 def contains(self, state):
36 Checks if this StateSet contains the given state.
38 @param state: State to check
39 @type state: Accessibility.StateType
40 @return: True if the set contains the given state
43 return state in self.states
47 Adds a state to this set.
49 @param state: State to add
50 @type state: Accessibility.StateType
52 self.states.add(state)
54 def remove(self, state):
56 Removes a state from this set.
58 @param state: State to remove
59 @type state: Accessibility.StateType
61 self.states.remove(state)
63 def equals(self, state_set):
65 Checks if this StateSet contains exactly the same members as the given
68 @param state_set: Another set
69 @type state_set: Accessibility.StateSet
70 @return: Are the sets equivalent in terms of their contents?
73 # don't check private members, object might be from another process
75 return set(state_set.getStates()) == self.states
77 def compare(self, state_set):
79 Computes the symmetric differences of this L{StateSet} and the given
82 @note: This method is not currently implemented because of difficulties
83 with reference counting. This method needs to return a new
84 Accessibility.StateSet object, but the Python implementation for that
85 object needs to be kept alive. The problem is who will keep that
86 server implementation alive? As soon as it goes out of scope, it's
87 GC'ed. This object cannot keep it alive either as it may go out of
88 scope before the new object is ready to be finalized. With a global
89 cache of objects, we don't know when to invalidate.
91 @param state_set: Another set
92 @type state_set: Accessibility.StateSet
93 @return: Elements in only one of the two sets
94 @rtype: Accessibility.StateSet
96 raise ORBit.CORBA.NO_IMPLEMENT
98 # don't check private members, object might be from another process
100 #states = set(state_set.getStates())
101 #diff = self.states.symmetric_difference(states)
102 #new_ss = _StateSetImpl()
103 #map(new_ss._this().add, diff)
104 #return new_ss._this()
108 Checks if this L{StateSet} is empty.
110 @return: Is it empty?
113 return len(self.states) == 0
117 Gets the sequence of all states in this set.
119 @return: List of states
122 return list(self.states)
124 class StateSet(object):
126 Python proxy for the L{_StateSetImpl} class. Use this to safely instantiate
127 new StateSet objects in Python.
129 @param impl: State set implementation
130 @type impl: L{_StateSetImpl}
132 def __init__(self, *states):
134 Initializes the state set with the given states.
136 @param states: States to add immediately
139 self.impl = _StateSetImpl()
140 map(self.impl._this().add, states)
142 def contains(self, state):
144 Checks if this StateSet contains the given state.
146 @param state: State to check
147 @type state: Accessibility.StateType
148 @return: True if the set contains the given state
151 return self.impl._this().contains(state)
153 def add(self, *states):
155 Adds states to this set.
157 @param states: State(s) to add
158 @type states: Accessibility.StateType
160 map(self.impl._this().add, states)
162 def remove(self, state):
164 Removes states from this set.
166 @param states: State(s) to remove
167 @type states: Accessibility.StateType
169 map(self.impl._this().remove, state)
171 def equals(self, state_set):
173 Checks if this StateSet contains exactly the same members as the given
176 @param state_set: Another set
177 @type state_set: Accessibility.StateSet
178 @return: Are the sets equivalent in terms of their contents?
181 if isinstance(state_set, self.__class__):
182 # convenience if we're given a proxy
183 state_set = state_set.raw()
184 return self.impl._this().equals(state_set)
186 def compare(self, state_set):
188 Finds the symmetric difference between this state set andthe one provided,
189 and returns it as a new StateSet.
191 @note: This does not use L{_StateSetImpl.compare} which cannot be
192 implemented at this time
193 @param state_set: Set to compare against
194 @type state_set: Accessibility.StateSet
195 @return: Proxy for the new set
198 if isinstance(state_set, self.__class__):
199 # shortcut if it's another one of our proxies
200 state_set = state_set.raw()
201 a = set(self.impl._this().getStates())
202 b = set(state_set.getStates())
203 diff = a.symmetric_difference(b)
204 return StateSet(*diff)
208 Checks if this StateSet is empty.
210 @return: Is it empty?
213 return self.impl._this().isEmpty()
217 Gets the sequence of all states in this set.
219 @return: List of states
222 return self.impl._this().getStates()
226 Gets the Accessibility.StateSet object proxied for use in a remote
230 @rtype: Accessibility.StateSet
232 return self.impl._this()