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