14 self.
_ncDict = kwargs.get (
'nameChangeDict', {})
21 if isinstance (key, str):
22 return self._attrs.get(key,
None)
33 if name.startswith(
'__'):
35 raise AttributeError (name)
36 return self._attrs.get (name,
None)
39 change = self._ncDict.get (name)
44 children = self.
_attrs[name]
45 if not isinstance(children, list):
47 self.
_attrs[name] = children
48 children.append(value)
53 return self.
_data or '' 56 items = sorted (self._attrs.items())
58 items.append((
'data', self.
_data))
59 return u'{%s}' %
', '.
join([
u'%s:%s' % (k,repr(v))
for k,v
in items])
67 non_id_char = re.compile(
'[^_0-9a-zA-Z]')
72 self.
_ncDict = kwargs.get (
'nameChangeDict', {})
81 for k, v
in attrs.items():
82 self.current._add_xml_attr (TreeBuilder._name_mangle(k), v)
87 self.current._data = text
88 if self.current.attributes():
94 self.current._add_xml_attr (TreeBuilder._name_mangle(name), obj)
97 self._text_parts.append(content)
103 '''Returns top level object''' 104 return self._root.attributes().
values()[0]
109 return TreeBuilder.non_id_char.sub(
'_', name)
112 regexList = [ (re.compile (
r'&'),
'&' ),
113 (re.compile (
r'<'),
'<' ),
114 (re.compile (
r'>'),
'>' ),
115 (re.compile (
r'"'),
'"e;' ),
116 (re.compile (
r"'"),
''' )
119 quoteRE = re.compile (
r'(\w\s*=\s*")([^"]+)"')
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 +
'"' 130 ''' Converts XML data into native Python object. Takes either 131 file handle or string as input. Does NOT fix illegal characters. 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 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''' 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'")
155 filtering = kwargs.get (
'filtering')
161 filehandle = open (filename,
'r') 163 raise RuntimeError(
"Failed to open '%s'" % filename)
165 for line
in filehandle:
168 filehandle = filename =
'' 169 contents = quoteRE.sub (fixQuoteValue, contents)
171 ncDict = kwargs.get (
'nameChangeDict', {})
172 builder = TreeBuilder (nameChangeDict = ncDict)
174 xml.sax.parseString(contents, builder)
178 filehandle = open (filename,
'r') 180 raise RuntimeError(
"Failed to open '%s'" % filename)
181 xml.sax.parse(filehandle, builder)
182 return builder.topLevel()
def __init__(self, kwargs)
def endElement(self, name)
def __init__(self, kwargs)
def _add_xml_attr(self, name, value)
static std::string join(char **cmd)
def __getattr__(self, name)
def characters(self, content)
def __contains__(self, name)
def startElement(self, name, attrs)
def __getitem__(self, key)