source: TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/etree/reader.py @ 6731

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/etree/reader.py@6731
Revision 6731, 1.9 KB checked in by pjkersha, 10 years ago (diff)

Work on XACML !ETree based parsing. Added first basic unit test.

Line 
1'''
2Created on 15 Mar 2010
3
4@author: pjkersha
5'''
6import logging
7log = logging.getLogger(__name__)
8from abc import ABCMeta, abstractmethod
9
10from xml.etree import ElementTree
11
12
13class AbstractReader:
14    """Abstract base class for ElementTree implementation of XACML reader"""
15    __metaclass__ = ABCMeta
16     
17    def __init__(self):
18        self.__namespace_map_backup = ElementTree._namespace_map.copy()
19        ElementTree._namespace_map[''] = PolicyComponent.XACML_2_0_XMLNS
20       
21    def __del__(self):
22        """Restore original global namespace map"""
23        ElementTree._namespace_map = self.__namespace_map_backup
24       
25    @classmethod
26    def __subclasshook__(cls, C):
27        """Derived class must implement __call__"""
28        if cls is AbstractReader:
29            if any("__call__" in B.__dict__ for B in C.__mro__):
30                return True
31           
32        return NotImplemented
33       
34    @abstractmethod
35    def __call__(self, obj):
36        """Abstract Parse XACML method
37        @raise NotImplementedError:
38        """
39        raise NotImplementedError()
40   
41    @classmethod
42    def parse(cls, obj):
43        """Parse from input object and return new XACML object"""
44        reader = cls()
45        return reader(obj)
46   
47    @staticmethod
48    def _parse(obj):
49        """Parse helper method
50        @param obj: input object to parse
51        @type obj: ElementTree Element, or stream object
52        @return: ElementTree element
53        @rtype: xml.etree.Element
54        """
55        if ElementTree.iselement(obj):
56            elem = obj
57        else:
58            # Treat as stream object
59            elem = ElementTree.parse(obj).getroot()
60           
61        return elem
62                   
63   
64class RuleReader(object):
65    def __call__(self, obj):
66        pass
67
68#AbstractReader.register(RuleReader)
69
70class VariableDefinitionReader(object):
71    def __call__(self, obj):
72        pass
Note: See TracBrowser for help on using the repository browser.