Package iceprod :: Package core :: Module ipxml
[hide private]
[frames] | no frames]

Source Code for Module iceprod.core.ipxml

  1  #! /usr/bin/env python 
  2  # 
  3  """ 
  4    A module for overloading xml functions 
  5    copyright  (c) 2009 the icecube collaboration 
  6   
  7    @version: $Revision: 1.0 $ 
  8    @date: $Date: $ 
  9    @author: Juan Carlos Diaz Velez <juancarlos@icecube.wisc.edu> 
 10    @todo: Add metadata reader to load and update files 
 11   
 12  """ 
 13  import sys,os 
 14  import string 
 15  import re 
 16  import logging 
 17  import time 
 18   
 19  from dataclasses import * 
 20  from os.path import expandvars 
 21  from os.path import join 
 22   
 23  # Import XML libraries 
 24  import xml 
 25  import xml.dom.minidom 
 26  from xml.dom.NodeFilter import NodeFilter 
 27  from xml.dom import EMPTY_NAMESPACE, XML_NAMESPACE 
 28   
 29  logger = logging.getLogger('xml') 
 30   
 31   
 32  # fix minidoms prettyxml 
33 -def fixed_writexml(self, writer, indent="", addindent="", newl=""):
34 # indent = current indentation 35 # addindent = indentation to add to higher levels 36 # newl = newline string 37 writer.write(indent+"<" + self.tagName) 38 39 attrs = self._get_attributes() 40 a_names = attrs.keys() 41 #a_names.sort() 42 43 for a_name in a_names: 44 writer.write(" %s=\"" % a_name) 45 xml.dom.minidom._write_data(writer, attrs[a_name].value) 46 writer.write("\"") 47 if self.childNodes: 48 if len(self.childNodes) == 1 \ 49 and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE: 50 writer.write(">") 51 self.childNodes[0].writexml(writer, "", "", "") 52 writer.write("</%s>%s" % (self.tagName, newl)) 53 return 54 writer.write(">%s"%(newl)) 55 for node in self.childNodes: 56 node.writexml(writer,indent+addindent,addindent,newl) 57 writer.write("%s</%s>%s" % (indent,self.tagName,newl)) 58 else: 59 writer.write("/>%s"%(newl))
60 # replace minidom's function with ours 61 xml.dom.minidom.Element.writexml = fixed_writexml 62 63 64 65 # define default printers
66 -def myPrettyPrint(root,stream=None,encoding='UTF-8',indent='\t',preserveElements=None):
67 """ 68 Default xml prettyprinter in the absence of _xmlplus 69 """ 70 if stream: print >> stream, root.toprettyxml(indent=indent,encoding=encoding) 71 else: print root.toprettyxml(indent=indent,encoding=encoding)
72
73 -def myPrint(root, stream=None,encoding='UTF-8'):
74 """ 75 Default xml printer in the absence of _xmlplus 76 """ 77 if stream: print >> stream, root.toxml(encoding=encoding) 78 else: print root.toxml(encoding=encoding)
79 80
81 -def getDOMImplementation():
82 return xml.dom.minidom.getDOMImplementation()
83 84 _hasSax2 = False 85 86 87 # Import XML_Plus libraries 88 try: 89 import xml.dom.ext 90 import xml.dom.ext.reader.Sax2 91 from xml.dom.ext.reader.Sax2 import FromXmlStream 92 from xml.sax._exceptions import SAXParseException 93 except Exception,e: 94 logger.debug("Unable to import xml libraries. Reverting to minidom") 95 if os.uname()[0] == 'Darwin': 96 logger.debug("try setting environment variable $PY_USE_XMLPLUS") 97 else: 98 _hasSax2 = True 99 100 try: 101 import xml.dom.ext 102 except Exception,e: 103 PrettyPrint = myPrettyPrint 104 Print = myPrint 105 logger.debug("Unable to import xml libraries. Reverting to minidom") 106 else: 107 PrettyPrint = xml.dom.ext.PrettyPrint 108 Print = xml.dom.ext.Print 109 110 _hasXpath = False 111 # Import XML_Plus libraries 112 try: 113 from xml import xpath 114 except Exception,e: 115 logger.debug("Unable to import xpath") 116 else: 117 _hasXpath = True 118
119 -def Parse(stream,ownerDocument=None,validate=0,keepAllWs=0,catName=None,saxHandlerClass=None,parser=None):
120 """ 121 Default xml parser in the absence of _xmlplus 122 """ 123 if _hasSax2 and validate: 124 return FromXmlStream(stream,validate=validate) 125 return xml.dom.minidom.parse(stream,parser=parser)
126
127 -def Evaluate(expr, contentNode=None, context=None):
128 """ 129 Default xml xpath evaluator in the absence of _xmlplus 130 """ 131 if _hasXpath: 132 return xpath.Evaluate(expr,contentNode,context) 133 tree = expr.split('/') 134 if len(tree) == 1: 135 return contentNode.getElementsByTagName(tree[0]) 136 else: 137 nodes = [] 138 for node in contentNode.getElementsByTagName(tree[0]): 139 nodes.extend(Evaluate("/".join(tree[1:]),contentNode)) 140 return nodes
141