Package rhn :: Module UserDictCase
[hide private]
[frames] | no frames]

Source Code for Module rhn.UserDictCase

  1  # 
  2  # Copyright (c) 2001--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16  # This file implements a case insensitive dictionary on top of the 
 17  # UserDict standard python class 
 18  # 
 19   
 20   
 21  try: # python2 
 22      from UserDict import UserDict 
 23      from types import StringType 
 24  except ImportError: # python3 
 25      from collections import UserDict 
 26      StringType = bytes 
 27      from functools import reduce 
 28   
 29  # A dictionary with case insensitive keys 
30 -class UserDictCase(UserDict):
31 - def __init__(self, data = None):
32 self.kcase = {} 33 UserDict.__init__(self, data)
34
35 - def __lower_string(self, key):
36 """ Return the lower() of key if it is a string. """ 37 if isinstance(key, StringType): 38 return key.lower() 39 else: 40 return key
41 42 # some methods used to make the class work as a dictionary
43 - def __setitem__(self, key, value):
44 lkey = self.__lower_string(key) 45 self.data[lkey] = value 46 self.kcase[lkey] = key
47
48 - def __getitem__(self, key):
49 key = self.__lower_string(key) 50 return self.data[key]
51 52 get = __getitem__ 53
54 - def __delitem__(self, key):
55 key = self.__lower_string(key) 56 del self.data[key] 57 del self.kcase[key]
58
59 - def __contains__(self, key):
60 key = self.__lower_string(key) 61 return key in self.data
62
63 - def keys(self):
64 return self.kcase.values()
65
66 - def items(self):
67 return self.get_hash().items()
68
69 - def has_key(self, key):
70 # obsoleted, left for compatibility with older python 71 return key in self
72
73 - def clear(self):
74 self.data.clear() 75 self.kcase.clear()
76 77 # return this data as a real hash
78 - def get_hash(self):
79 return reduce(lambda a, t, hc=self.kcase: 80 a.update({ hc[t[0]] : t[1]}) or a, self.data.items(), {})
81 82 # return the data for marshalling
83 - def __getstate__(self):
84 return self.get_hash()
85 86 # we need a setstate because of the __getstate__ presence screws up deepcopy
87 - def __setstate__(self, state):
88 self.__init__(state)
89 90 # get a dictionary out of this instance ({}.update doesn't get instances)
91 - def dict(self):
92 return self.get_hash()
93
94 - def update(self, dict):
95 for (k, v) in dict.items(): 96 self[k] = v
97 98 # Expose an iterator. This would normally fail if there is no iter() 99 # function defined - but __iter__ will never be called on python 1.5.2
100 - def __iter__(self):
101 return iter(self.data)
102