CMS 3D CMS Logo

Classes | Functions | Variables
XML2Python Namespace Reference

Classes

class  DataNode
 
class  TreeBuilder
 

Functions

def fixQuoteValue (match)
 
def xml2obj (kwargs)
 

Variables

 quoteRE
 
 regexList
 

Function Documentation

def XML2Python.fixQuoteValue (   match)
Changes all characters inside of the match

Definition at line 215 of file XML2Python.py.

215 def fixQuoteValue (match):
216  '''Changes all characters inside of the match'''
217  quote = match.group(2)
218  for regexTup in regexList:
219  quote = regexTup[0].sub( regexTup[1], quote )
220  return match.group(1) + quote + '"'
221 
222 
def fixQuoteValue(match)
Definition: XML2Python.py:215
def XML2Python.xml2obj (   kwargs)
Converts XML data into native Python object.  Takes either
file handle or string as input.  Does NOT fix illegal characters.

input source:  Exactly one of the three following is needed
filehandle     - input from file handle
contents       - input from string
filename       - input from filename

options:
filtering      - boolean value telling code whether or not to fileter
                 input selection to remove illegal XML characters
nameChangeDict - dictionaries of names to change in python object

Definition at line 223 of file XML2Python.py.

223 def xml2obj (**kwargs):
224  ''' Converts XML data into native Python object. Takes either
225  file handle or string as input. Does NOT fix illegal characters.
226 
227  input source: Exactly one of the three following is needed
228  filehandle - input from file handle
229  contents - input from string
230  filename - input from filename
231 
232  options:
233  filtering - boolean value telling code whether or not to fileter
234  input selection to remove illegal XML characters
235  nameChangeDict - dictionaries of names to change in python object'''
236 
237  # make sure we have exactly 1 input source
238  filehandle = kwargs.get ('filehandle')
239  contents = kwargs.get ('contents')
240  filename = kwargs.get ('filename')
241  if not filehandle and not contents and not filename:
242  raise RuntimeError("You must provide 'filehandle', 'contents', or 'filename'")
243  if filehandle and contents or \
244  filehandle and filename or \
245  contents and filename:
246  raise RuntimeError("You must provide only ONE of 'filehandle', 'contents', or 'filename'")
247 
248  # are we filtering?
249  filtering = kwargs.get ('filtering')
250  if filtering:
251  # if we are filtering, we need to read in the contents to modify them
252  if not contents:
253  if not filehandle:
254  try:
255  filehandle = open (filename, 'r')
256  except:
257  raise RuntimeError("Failed to open '%s'" % filename)
258  contents = ''
259  for line in filehandle:
260  contents += line
261  filehandle.close()
262  filehandle = filename = ''
263  contents = quoteRE.sub (fixQuoteValue, contents)
264 
265  ncDict = kwargs.get ('nameChangeDict', {})
266  builder = TreeBuilder (nameChangeDict = ncDict)
267  if contents:
268  xml.sax.parseString(contents, builder)
269  else:
270  if not filehandle:
271  try:
272  filehandle = open (filename, 'r')
273  except:
274  raise RuntimeError("Failed to open '%s'" % filename)
275  xml.sax.parse(filehandle, builder)
276  return builder.topLevel()
277 
def xml2obj(kwargs)
Definition: XML2Python.py:223

Variable Documentation

XML2Python.quoteRE

Definition at line 119 of file XML2Python.py.

XML2Python.regexList

Definition at line 112 of file XML2Python.py.