1 | """NDG XACML ElementTree parsers package |
---|
2 | |
---|
3 | NERC 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$" |
---|
12 | import logging |
---|
13 | log = logging.getLogger(__name__) |
---|
14 | |
---|
15 | from xml.etree import ElementTree |
---|
16 | |
---|
17 | # Generic ElementTree Helper classes |
---|
18 | class 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) |
---|