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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/reader.py@7087
Revision 7087, 1.8 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 string: type"""
26    TYPE = None
27   
28    def __init__(self):
29        if self.__class__.TYPE is None:
30            raise NotImplementedError('No "TYPE" class variable set to specify '
31                                      'the XACML type to instantiate')
32           
33        self.__namespace_map_backup = ElementTree._namespace_map.copy()
34        ElementTree._namespace_map[''] = XacmlPolicyBase.XACML_2_0_POLICY_NS
35       
36    def __del__(self):
37        """Restore original global namespace map"""
38        ElementTree._namespace_map = self.__namespace_map_backup
39   
40    @staticmethod
41    def _parse(obj):
42        """Parse helper method
43        @param obj: input object to parse
44        @type obj: ElementTree Element, or stream object
45        @return: ElementTree element
46        @rtype: xml.etree.Element
47        """
48        if ElementTree.iselement(obj):
49            elem = obj
50        else:
51            # Treat as stream object
52            elem = ElementTree.parse(obj).getroot()
53           
54        return elem
55         
56# Set up new class as an abstract base itself             
57AbstractReader.register(ETreeAbstractReader)
58
Note: See TracBrowser for help on using the repository browser.