source: TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/reader.py @ 7109

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/reader.py@7109
Revision 7109, 2.0 KB checked in by pjkersha, 10 years ago (diff)

Incomplete - task 2: XACML-Security Integration

  • updating epydoc ready for release.
  • Property svn:keywords set to Id
Line 
1"""NDG XACML ElementTree reader module containing reader base class
2
3NERC DataGrid
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$"
12import logging
13log = logging.getLogger(__name__)
14
15from xml.etree import ElementTree
16
17from ndg.xacml.core import XacmlPolicyBase
18from ndg.xacml.parsers import AbstractReader
19
20
21class ETreeAbstractReader(AbstractReader):
22    """Base class for ElementTree implementation of XACML reader
23   
24    @cvar TYPE: XACML type to instantiate from parsed object
25    @type TYPE: NoneType"""
26    TYPE = None
27   
28    def __init__(self):
29        """Initialise ElementTree namespace map for XACML 2.0 namespaces
30       
31        @raise NotImplementedError: TYPE class variable must be set in a
32        derived class
33        """
34        if self.__class__.TYPE is None:
35            raise NotImplementedError('No "TYPE" class variable set to specify '
36                                      'the XACML type to instantiate')
37           
38        self.__namespace_map_backup = ElementTree._namespace_map.copy()
39        ElementTree._namespace_map[''] = XacmlPolicyBase.XACML_2_0_POLICY_NS
40       
41    def __del__(self):
42        """Restore original global namespace map"""
43        ElementTree._namespace_map = self.__namespace_map_backup
44   
45    @staticmethod
46    def _parse(obj):
47        """Parse helper method
48        @param obj: input object to parse
49        @type obj: ElementTree Element, or stream object
50        @return: ElementTree element
51        @rtype: xml.etree.Element
52        """
53        if ElementTree.iselement(obj):
54            elem = obj
55        else:
56            # Treat as stream object
57            elem = ElementTree.parse(obj).getroot()
58           
59        return elem
60         
61# Set up new class as an abstract base itself             
62AbstractReader.register(ETreeAbstractReader)
63
Note: See TracBrowser for help on using the repository browser.