CMS 3D CMS Logo

Classes | Functions | Variables
python.XML2Python Namespace Reference

Classes

class  DataNode
 
class  TreeBuilder
 

Functions

def fixQuoteValue (match)
 
def xml2obj (kwargs)
 

Variables

 quoteRE
 
 regexList
 

Function Documentation

◆ fixQuoteValue()

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

Definition at line 121 of file XML2Python.py.

121 def fixQuoteValue (match):
122  '''Changes all characters inside of the match'''
123  quote = match.group(2)
124  for regexTup in regexList:
125  quote = regexTup[0].sub( regexTup[1], quote )
126  return match.group(1) + quote + '"'
127 
128 
def fixQuoteValue(match)
Definition: XML2Python.py:121

◆ xml2obj()

def python.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 129 of file XML2Python.py.

129 def xml2obj (**kwargs):
130  ''' Converts XML data into native Python object. Takes either
131  file handle or string as input. Does NOT fix illegal characters.
132 
133  input source: Exactly one of the three following is needed
134  filehandle - input from file handle
135  contents - input from string
136  filename - input from filename
137 
138  options:
139  filtering - boolean value telling code whether or not to fileter
140  input selection to remove illegal XML characters
141  nameChangeDict - dictionaries of names to change in python object'''
142 
143  # make sure we have exactly 1 input source
144  filehandle = kwargs.get ('filehandle')
145  contents = kwargs.get ('contents')
146  filename = kwargs.get ('filename')
147  if not filehandle and not contents and not filename:
148  raise RuntimeError("You must provide 'filehandle', 'contents', or 'filename'")
149  if filehandle and contents or \
150  filehandle and filename or \
151  contents and filename:
152  raise RuntimeError("You must provide only ONE of 'filehandle', 'contents', or 'filename'")
153 
154  # are we filtering?
155  filtering = kwargs.get ('filtering')
156  if filtering:
157  # if we are filtering, we need to read in the contents to modify them
158  if not contents:
159  if not filehandle:
160  try:
161  filehandle = open (filename, 'r') except:
162  raise RuntimeError("Failed to open '%s'" % filename)
163  contents = ''
164  for line in filehandle:
165  contents += line
166  filehandle.close()
167  filehandle = filename = ''
168  contents = quoteRE.sub (fixQuoteValue, contents)
169 
170  ncDict = kwargs.get ('nameChangeDict', {})
171  builder = TreeBuilder (nameChangeDict = ncDict)
172  if contents:
173  xml.sax.parseString(contents, builder)
174  else:
175  if not filehandle:
176  try:
177  filehandle = open (filename, 'r')
178  except:
179  raise RuntimeError("Failed to open '%s'" % filename)
180  xml.sax.parse(filehandle, builder)
181  return builder.topLevel()
182 
def xml2obj(kwargs)
Definition: XML2Python.py:129

Variable Documentation

◆ quoteRE

python.XML2Python.quoteRE

Definition at line 119 of file XML2Python.py.

◆ regexList

python.XML2Python.regexList

Definition at line 112 of file XML2Python.py.