Package ndg :: Package xacml :: Package parsers :: Package etree
[hide private]

Source Code for Package ndg.xacml.parsers.etree

  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: __init__.py 8028 2012-02-27 14:38:01Z rwilkinson $" 
 12  import logging 
 13  log = logging.getLogger(__name__) 
 14  import re 
 15   
 16  from ndg.xacml import Config, importElementTree 
 17  ElementTree = importElementTree() 
 18   
19 -class SerialisedElementTree(unicode):
20 """Marks a unicode string as being serialised ElementTree XML. 21 """ 22 pass
23 24 if Config.use_lxml:
25 - def makeEtreeElement(tag, ns_prefix, ns_uri, attrib={}, **extra):
26 """Makes an ElementTree element handling namespaces in the way 27 appropriate for the ElementTree implementation in use. 28 """ 29 elem = ElementTree.Element(tag, {ns_prefix: ns_uri}, attrib, **extra) 30 return elem
31
32 - def serialiseIfElementTree(obj):
33 if ElementTree.iselement(obj): 34 return SerialisedElementTree(ElementTree.tostring(obj, 35 encoding=unicode)) 36 else: 37 return obj
38
39 - def deserialiseIfElementTree(obj):
40 if isinstance(obj, SerialisedElementTree): 41 return ElementTree.XML(obj) 42 else: 43 return obj
44 else:
45 - def makeEtreeElement(tag, ns_prefix, ns_uri, attrib={}, **extra):
46 """Makes an ElementTree element handling namespaces in the way 47 appropriate for the ElementTree implementation in use. 48 """ 49 elem = ElementTree.Element(tag, attrib, **extra) 50 ElementTree._namespace_map[ns_uri] = ns_prefix 51 return elem
52
53 - def serialiseIfElementTree(obj):
54 return obj
55
56 - def deserialiseIfElementTree(obj):
57 return obj
58
59 -def getElementChildren(element):
60 """Iterator over children of an element that are elements, not, e.g., 61 comments. 62 """ 63 for childElem in element: 64 if (not hasattr(childElem, 'tag') or 65 not isinstance(childElem.tag, basestring)): 66 continue 67 yield childElem
68 69 70 # Generic ElementTree Helper classes
71 -class QName(ElementTree.QName):
72 """Extend ElementTree implementation for improved attribute access support 73 """ 74 75 # ElementTree tag is of the form {namespace}localPart. getNs extracts the 76 # namespace from within the brackets but if not found returns '' 77 getNs = staticmethod(lambda tag: getattr(re.search('(?<=\{).+(?=\})', tag), 78 'group', 79 str)()) 80 81 getLocalPart = staticmethod(lambda tag: tag.rsplit('}', 1)[-1]) 82
83 - def __init__(self, input, tag=None, prefix=None):
84 """ 85 @type input: basestring 86 @param input: ElementTree style namespace URI + tag name - 87 {namespace URI}tag - OR if tag keyword is set, the namespace URI alone 88 @type tag: basestring / None 89 @param tag: element tag name. If None, input must contain the 90 namespace URI and tag name in the ElementTree form {namespace URI}tag. 91 @type prefix: basestring / None 92 @param prefix: namespace prefix 93 """ 94 95 ElementTree.QName.__init__(self, input, tag=tag) 96 97 if tag: 98 self.namespaceURI = input 99 self.localPart = tag 100 else: 101 # No tag provided namespace and localPart of QN must be parsed from 102 # the namespace 103 self.namespaceURI = QName.getNs(input) 104 self.localPart = QName.getLocalPart(input) 105 106 self.prefix = prefix
107
108 - def _getPrefix(self):
109 """Get prefix 110 @return: prefix 111 @rtype: string 112 """ 113 return self.__prefix
114
115 - def _setPrefix(self, value):
116 """Set prefix 117 @param value: prefix 118 @type value: string 119 @raise TypeError: invalid input value type 120 """ 121 self.__prefix = value
122 123 prefix = property(_getPrefix, _setPrefix, None, "Prefix") 124
125 - def _getLocalPart(self):
126 """Get local part 127 @return: local part 128 @rtype: string 129 """ 130 return self.__localPart
131
132 - def _setLocalPart(self, value):
133 """Set local part 134 @param value: local part 135 @type value: string 136 @raise TypeError: invalid input value type 137 """ 138 self.__localPart = value
139 140 localPart = property(_getLocalPart, _setLocalPart, None, "LocalPart") 141
142 - def _getNamespaceURI(self):
143 """Get namespace URI 144 @return: namespace URI 145 @rtype: string 146 """ 147 return self.__namespaceURI
148
149 - def _setNamespaceURI(self, value):
150 """Set namespace URI 151 @param value: namespace URI 152 @type value: string 153 @raise TypeError: invalid input value type 154 """ 155 self.__namespaceURI = value
156 157 namespaceURI = property(_getNamespaceURI, _setNamespaceURI, None, 158 "Namespace URI'") 159
160 - def __eq__(self, qname):
161 """Enable equality check for QName. Note that prefixes don't need to 162 match 163 164 @type qname: ndg.xacml.utils.etree.QName 165 @param qname: Qualified Name to compare with self 166 @return: True if input and this object match 167 @rtype: bool 168 """ 169 if not isinstance(qname, QName): 170 raise TypeError('Expecting %r; got %r' % (QName, type(qname))) 171 172 # Nb. prefixes don't need to agree! 173 return (self.namespaceURI, self.localPart) == \ 174 (qname.namespaceURI, qname.localPart)
175
176 - def __ne__(self, qname):
177 """Enable equality check for QName. Note that prefixes don't need to 178 match 179 180 @type qname: ndg.xacml.utils.etree.QName 181 @param qname: Qualified Name to compare with self 182 """ 183 return not self.__eq__(qname)
184