source: TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/etree/__init__.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/__init__.py@6731
Revision 6731, 3.1 KB checked in by pjkersha, 10 years ago (diff)

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

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