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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/core/__init__.py@6753
Revision 6753, 2.5 KB checked in by pjkersha, 10 years ago (diff)

Started adding parsing for Condition and Apply types.

Line 
1"""NDG XACML core 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: $"
12from ndg.xacml.utils import TypedList
13
14   
15class PolicyComponent(object):
16    """Base class for Policy and Policy subelements"""
17    XACML_2_0_XMLNS = "urn:oasis:names:tc:xacml:2.0:policy:schema:os"
18
19    __slots__ = ('__xmlns', '__reader', '__writer')
20
21    ELEMENT_LOCAL_NAME = None
22   
23    def __init__(self):
24        self.__xmlns = PolicyComponent.XACML_2_0_XMLNS
25        self.__reader = None
26        self.__writer = None
27       
28        if not isinstance(self.__class__.ELEMENT_LOCAL_NAME, basestring):
29            raise NotImplementedError('"ELEMENT_LOCAL_NAME" must be defined in '
30                                      'a derived class')
31       
32    def _getXmlns(self):
33        return self.__xmlns
34
35    def _setXmlns(self, value):
36        if not isinstance(value, basestring):
37            raise TypeError('Expecting string type for "xmlns" '
38                            'attribute; got %r' % type(value))
39        self.__xmlns = value
40
41    xmlns = property(_getXmlns, _setXmlns, 
42                     doc="XML Namespace for policy the document")
43   
44    @property
45    def isValidXmlns(self):
46        return self.xmlns in PolicyComponent.XMLNS
47
48    def read(self, obj):
49        """Read using callable assinged to reader property"""
50        if self.__reader is None:
51            raise AttributeError('No reader set for %r' % self.__class__)
52       
53        self.__reader(self, obj)
54
55    @classmethod
56    def Read(cls, obj):
57        """Construct a new Policy""" 
58        xacmlObj = cls()
59        xacmlObj.read(obj)
60        return xacmlObj
61   
62    def write(self, obj):
63        """Read using callable assinged to reader property"""
64        if self.__writer is None:
65            raise AttributeError('No writer set for %r' % self.__class__)
66       
67        self.__writer(self, obj)
68       
69           
70class RequestPropertyBase(PolicyComponent):
71    """Base type for Subject, Resource, Action and Environment types"""
72    MATCH_TYPE = None
73   
74    __slots__ = ('__matches', )
75   
76    def __init__(self):
77        # Derived types can specify the type for matches via the MATCH_TYPE
78        # class variable
79        self.__matches = TypedList(self.__class__.MATCH_TYPE)
80       
81    @property
82    def matches(self):
83        return self.__matches
Note: See TracBrowser for help on using the repository browser.