source: TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/utils/__init__.py @ 6730

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg-security/TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/utils/__init__.py@6730
Revision 6730, 3.6 KB checked in by pjkersha, 11 years ago (diff)
  • Working Credential Wallet refactored for Python 2.6
  • Pruned out more old code: ZSI and Twisted SAOP/WSDL wrappers, Session Manager and WS-Security, Test 'Site B' Attribute Authority - for testing role mapping.
  • Started XACML package ElementTree based parser.
Line 
1"""Utilities package for NDG Security
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "02/04/09"
7__copyright__ = ""
8__license__ = "BSD - see LICENSE file in top-level directory"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__revision__ = '$Id: $'
11
12# Interpret a string as a boolean
13str2Bool = lambda str: str.lower() in ("yes", "true", "t", "1")
14
15class UniqList(list):
16    """Extended version of list type to enable a list with unique items.
17    If an item is added that is already present then it is silently omitted
18    from the list
19    """
20    def extend(self, iter):
21        return super(UniqList, self).extend([i for i in iter if i not in self])
22       
23    def __iadd__(self, iter):
24        return super(UniqList, self).__iadd__([i for i in iter 
25                                               if i not in self])
26         
27    def append(self, item):
28        for i in self:
29            if i == item:
30                return None
31           
32        return super(UniqList, self).append(item)
33
34
35class TypedList(list):
36    """Extend list type to enabled only items of a given type.  Supports
37    any type where the array type in the Standard Library is restricted to
38    only limited set of primitive types
39    """
40   
41    def __init__(self, elementType, *arg, **kw):
42        """
43        @type elementType: type/tuple
44        @param elementType: object type or types which the list is allowed to
45        contain.  If more than one type, pass as a tuple
46        """
47        self.__elementType = elementType
48        super(TypedList, self).__init__(*arg, **kw)
49   
50    def _getElementType(self):
51        return self.__elementType
52   
53    elementType = property(fget=_getElementType, 
54                           doc="The allowed type or types for list elements")
55     
56    def extend(self, iter):
57        for i in iter:
58            if not isinstance(i, self.__elementType):
59                raise TypeError("List items must be of type %s" % 
60                                (self.__elementType,))
61               
62        return super(TypedList, self).extend(iter)
63       
64    def __iadd__(self, iter):
65        for i in iter:
66            if not isinstance(i, self.__elementType):
67                raise TypeError("List items must be of type %s" % 
68                                (self.__elementType,))
69                   
70        return super(TypedList, self).__iadd__(iter)
71         
72    def append(self, item):
73        if not isinstance(item, self.__elementType):
74                raise TypeError("List items must be of type %s" % 
75                                (self.__elementType,))
76   
77        return super(TypedList, self).append(item)
78
79class RestrictedKeyNamesDict(dict):
80    """Utility class for holding a constrained list of key names
81    """
82   
83    def __init__(self, *arg, **kw):
84        """Alter standard dict() initialisation to fix key names set at
85        initialisation
86        """
87        super(RestrictedKeyNamesDict, self).__init__(*arg, **kw)
88        self.__keyNames = self.keys() 
89         
90    def __setitem__(self, key, val):
91        if key not in self.__keyNames:
92            raise KeyError('Key name %r not recognised.  Valid key names '
93                           'are: %r' % (key, self.__keyNames))
94           
95        dict.__setitem__(self, key, val)
96
97    def update(self, d, **kw):       
98        for dictArg in (d, kw):
99            for k in dictArg:
100                if k not in self.__keyNames:
101                    raise KeyError('Key name "%s" not recognised.  Valid '
102                                   'key names are: %s' % 
103                                   self.__keyNames)
104       
105        dict.update(self, d, **kw)
Note: See TracBrowser for help on using the repository browser.