Package muntjac :: Package terminal :: Module key_mapper
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.key_mapper

 1  # Copyright (C) 2012 Vaadin Ltd.  
 2  # Copyright (C) 2012 Richard Lincoln 
 3  #  
 4  # Licensed under the Apache License, Version 2.0 (the "License");  
 5  # you may not use this file except in compliance with the License.  
 6  # You may obtain a copy of the License at  
 7  #  
 8  #     http://www.apache.org/licenses/LICENSE-2.0  
 9  #  
10  # Unless required by applicable law or agreed to in writing, software  
11  # distributed under the License is distributed on an "AS IS" BASIS,  
12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
13  # See the License for the specific language governing permissions and  
14  # limitations under the License. 
15   
16  """Simple two-way map.""" 
17   
18   
19 -class KeyMapper(object):
20 """C{KeyMapper} is the simple two-way map for generating textual keys 21 for objects and retrieving the objects later with the key. 22 23 @author: Vaadin Ltd. 24 @author: Richard Lincoln 25 @version: 1.1.2 26 """ 27
28 - def __init__(self):
29 self._lastKey = 0 30 self._objectKeyMap = dict() 31 self._keyObjectMap = dict()
32 33
34 - def key(self, o):
35 """Gets key for an object. 36 37 @param o: the object. 38 """ 39 if o is None: 40 return 'null' 41 42 # If the object is already mapped, use existing key 43 key = self._objectKeyMap.get(o) 44 if key is not None: 45 return key 46 47 # If the object is not yet mapped, map it 48 self._lastKey += 1 49 key = str(self._lastKey) 50 self._objectKeyMap[o] = key 51 self._keyObjectMap[key] = o 52 return key
53 54
55 - def get(self, key):
56 """Retrieves object with the key. 57 58 @param key: 59 the name with the desired value. 60 @return: the object with the key. 61 """ 62 return self._keyObjectMap.get(key)
63 64
65 - def remove(self, removeobj):
66 """Removes object from the mapper. 67 68 @param removeobj: 69 the object to be removed. 70 """ 71 key = self._objectKeyMap.get(removeobj) 72 if key is not None: 73 del self._objectKeyMap[removeobj] 74 if key in self._keyObjectMap: 75 del self._keyObjectMap[key]
76 77
78 - def removeAll(self):
79 """Removes all objects from the mapper.""" 80 self._objectKeyMap.clear() 81 self._keyObjectMap.clear()
82