source: TI12-security/trunk/NDG_XACML/ndg/xacml/parsers/etree/__init__.py @ 6746

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

Added more !ETree readers for the different types.

Line 
1"""NDG XACML ElementTree parsers 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: $"
12import logging
13log = logging.getLogger(__name__)
14
15from xml.etree import ElementTree
16
17# Generic ElementTree Helper classes
18class QName(ElementTree.QName):
19    """Extend ElementTree implementation for improved attribute access support
20    """ 
21
22    # ElementTree tag is of the form {namespace}localPart.  getNs extracts the
23    # namespace from within the brackets but if not found returns ''
24    getNs = staticmethod(lambda tag: getattr(re.search('(?<=\{).+(?=\})', tag),
25                                             'group', 
26                                             str)())
27                                             
28    getLocalPart = staticmethod(lambda tag: tag.rsplit('}', 1)[-1])
29
30    def __init__(self, input, tag=None, prefix=None):
31        """
32        @type input: basestring
33        @param input: ElementTree style namespace URI + tag name -
34        {namespace URI}tag - OR if tag keyword is set, the namespace URI alone
35        @type tag: basestring / None
36        @param tag: element tag name.  If None, input must contain the
37        namespace URI and tag name in the ElementTree form {namespace URI}tag.
38        @type prefix: basestring / None
39        @param prefix: namespace prefix
40        """
41       
42        ElementTree.QName.__init__(self, input, tag=tag)
43       
44        if tag:
45            self.namespaceURI = input
46            self.localPart = tag
47        else:
48            # No tag provided namespace and localPart of QN must be parsed from
49            # the namespace
50            self.namespaceURI = QName.getNs(input)
51            self.localPart = QName.getLocalPart(input)
52           
53        self.prefix = prefix
54   
55    def _getPrefix(self):
56        return self.__prefix
57
58    def _setPrefix(self, value):
59        self.__prefix = value
60   
61    prefix = property(_getPrefix, _setPrefix, None, "Prefix")
62
63    def _getLocalPart(self):
64        return self.__localPart
65   
66    def _setLocalPart(self, value):
67        self.__localPart = value
68       
69    localPart = property(_getLocalPart, _setLocalPart, None, "LocalPart")
70
71    def _getNamespaceURI(self):
72        return self.__namespaceURI
73
74    def _setNamespaceURI(self, value):
75        self.__namespaceURI = value
76 
77    namespaceURI = property(_getNamespaceURI, _setNamespaceURI, None, 
78                            "Namespace URI'")
79
80    def __eq__(self, qname):
81        """Enable equality check for QName.  Note that prefixes don't need to
82        match
83       
84        @type qname: ndg.xacml.utils.etree.QName
85        @param qname: Qualified Name to compare with self
86        """
87        if not isinstance(qname, QName):
88            raise TypeError('Expecting %r; got %r' % (QName, type(qname)))
89                   
90        # Nb. prefixes don't need to agree!         
91        return (self.namespaceURI, self.localPart) == \
92               (qname.namespaceURI, qname.localPart)
93
94    def __ne__(self, qname):
95        """Enable equality check for QName.  Note that prefixes don't need to
96        match
97       
98        @type qname: ndg.xacml.utils.etree.QName
99        @param qname: Qualified Name to compare with self
100        """
101        return not self.__eq__(qname)
Note: See TracBrowser for help on using the repository browser.