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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/__init__.py@7109
Revision 7109, 4.2 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 parsers package
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
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        """Get prefix
57        @return: prefix
58        @rtype: string
59        """
60        return self.__prefix
61
62    def _setPrefix(self, value):
63        """Set prefix
64        @param value: prefix
65        @type value: string
66        @raise TypeError: invalid input value type
67        """
68        self.__prefix = value
69   
70    prefix = property(_getPrefix, _setPrefix, None, "Prefix")
71
72    def _getLocalPart(self):
73        """Get local part
74        @return: local part
75        @rtype: string
76        """
77        return self.__localPart
78   
79    def _setLocalPart(self, value):
80        """Set local part
81        @param value: local part
82        @type value: string
83        @raise TypeError: invalid input value type
84        """
85        self.__localPart = value
86       
87    localPart = property(_getLocalPart, _setLocalPart, None, "LocalPart")
88
89    def _getNamespaceURI(self):
90        """Get namespace URI
91        @return: namespace URI
92        @rtype: string
93        """
94        return self.__namespaceURI
95
96    def _setNamespaceURI(self, value):
97        """Set namespace URI
98        @param value: namespace URI
99        @type value: string
100        @raise TypeError: invalid input value type
101        """
102        self.__namespaceURI = value
103 
104    namespaceURI = property(_getNamespaceURI, _setNamespaceURI, None, 
105                            "Namespace URI'")
106
107    def __eq__(self, qname):
108        """Enable equality check for QName.  Note that prefixes don't need to
109        match
110       
111        @type qname: ndg.xacml.utils.etree.QName
112        @param qname: Qualified Name to compare with self
113        @return: True if input and this object match
114        @rtype: bool
115        """
116        if not isinstance(qname, QName):
117            raise TypeError('Expecting %r; got %r' % (QName, type(qname)))
118                   
119        # Nb. prefixes don't need to agree!         
120        return (self.namespaceURI, self.localPart) == \
121               (qname.namespaceURI, qname.localPart)
122
123    def __ne__(self, qname):
124        """Enable equality check for QName.  Note that prefixes don't need to
125        match
126       
127        @type qname: ndg.xacml.utils.etree.QName
128        @param qname: Qualified Name to compare with self
129        """
130        return not self.__eq__(qname)
Note: See TracBrowser for help on using the repository browser.