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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg_security_common/ndg/security/common/utils/__init__.py@5870
Revision 5870, 2.6 KB checked in by pjkersha, 10 years ago (diff)
  • added an SQLAlchemy based AX interface for the OpenID Provider
  • Modified the openid_dbinterface egg to enable creation of a unique OpenID identifier based on a given database user table key
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)
Note: See TracBrowser for help on using the repository browser.