source: TI12-security/trunk/NDG_XACML/ndg/xacml/__init__.py @ 6740

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/__init__.py@6740
Revision 6740, 2.4 KB checked in by pjkersha, 10 years ago (diff)
  • fixed import paths changing to new ndg.xacml
  • added unit test package
Line 
1"""NDG XACML package
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "16/03/10"
7__copyright__ = "(C) 2010 Science and Technology Facilities Council"
8__contact__ = "Philip.Kershaw@stfc.ac.uk"
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = "$Id: $"
12
13class XACMLError(Exception):
14    """Base class for XACML package exception types"""
15   
16   
17class XMLParseError(XACMLError):
18    """XACML package XML Parsing error"""
19   
20   
21class PolicyComponent(object):
22    """Base class for Policy and Policy subelements"""
23    XACML_2_0_XMLNS = "urn:oasis:names:tc:xacml:2.0:policy:schema:os"
24
25    __slots__ = ('__xmlns', '__reader', '__writer')
26
27    ELEMENT_LOCAL_NAME = None
28   
29    def __init__(self):
30        self.__xmlns = PolicyComponent.XACML_2_0_XMLNS
31        self.__reader = None
32        self.__writer = None
33       
34    def _getXmlns(self):
35        return self.__xmlns
36
37    def _setXmlns(self, value):
38        if not isinstance(value, basestring):
39            raise TypeError('Expecting string type for "xmlns" '
40                            'attribute; got %r' % type(value))
41        self.__xmlns = value
42
43    xmlns = property(_getXmlns, _setXmlns, 
44                     doc="XML Namespace for policy the document")
45   
46    @property
47    def isValidXmlns(self):
48        return self.xmlns in PolicyComponent.XMLNS
49
50    def read(self, obj):
51        """Read using callable assinged to reader property"""
52        if self.__reader is None:
53            raise AttributeError('No reader set for %r' % self.__class__)
54       
55        self.__reader(self, obj)
56
57    @classmethod
58    def Read(cls, obj):
59        """Construct a new Policy""" 
60        xacmlObj = cls()
61        xacmlObj.read(obj)
62        return xacmlObj
63   
64    def write(self, obj):
65        """Read using callable assinged to reader property"""
66        if self.__writer is None:
67            raise AttributeError('No writer set for %r' % self.__class__)
68       
69        self.__writer(self, obj)
70       
71           
72class RequestPropertyBase(PolicyComponent):
73    """Base type for Subject, Resource, Action and Environment types"""
74    MATCH_TYPE = None
75   
76    __slots__ = ('__matches', )
77   
78    def __init__(self):
79        # Derived types can specify the type for matches via the MATCH_TYPE
80        # class variable
81        self.__matches = TypedList(self.__class__.MATCH_TYPE)
82       
83    @property
84    def matches(self):
85        return self.__matches
Note: See TracBrowser for help on using the repository browser.