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

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
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')
162  except:
163  raise RuntimeError("Failed to open '%s'" % filename)
164  contents = ''
165  for line in filehandle:
166  contents += line
167  filehandle.close()
168  filehandle = filename = ''
169  contents = quoteRE.sub (fixQuoteValue, contents)
170 
171  ncDict = kwargs.get ('nameChangeDict', {})
172  builder = TreeBuilder (nameChangeDict = ncDict)
173  if contents:
174  xml.sax.parseString(contents, builder)
175  else:
176  if not filehandle:
177  try:
178  filehandle = open (filename, 'r')
179  except:
180  raise RuntimeError("Failed to open '%s'" % filename)
181  xml.sax.parse(filehandle, builder)
182  return builder.topLevel()
183 
def xml2obj(kwargs)
Definition: XML2Python.py:129

Variable Documentation

python.XML2Python.quoteRE

Definition at line 119 of file XML2Python.py.

python.XML2Python.regexList

Definition at line 112 of file XML2Python.py.