Commit f13931f49522fcf772a8a3c17b6976fa6968243d

Authored by Christian Herdtweck
Committed by Philippe Lagadec
1 parent 6af5e38e

fixup: create a class

Showing 1 changed file with 9 additions and 10 deletions
oletools/ooxml.py
... ... @@ -167,19 +167,18 @@ class XmlParser(object):
167 167 Subfiles that are not xml (e.g. OLE or image files) are remembered
168 168 internally and can be retrieved using iter_non_xml().
169 169 """
170   - with ZipFile(self.filename) as zip:
  170 + with ZipFile(self.filename) as zipper:
171 171 if args:
172 172 subfiles = args
173 173 else:
174   - subfiles = zip.namelist()
  174 + subfiles = zipper.namelist()
175 175  
176   - failed = []
177 176 events = ('start', 'end')
178 177 for subfile in subfiles:
179 178 logging.debug(u'subfile {0}'.format(subfile))
180 179 depth = 0
181 180 try:
182   - with zip.open(subfile, 'r') as handle:
  181 + with zipper.open(subfile, 'r') as handle:
183 182 for event, elem in ET.iterparse(handle, events):
184 183 if elem is None:
185 184 continue
... ... @@ -191,8 +190,8 @@ class XmlParser(object):
191 190 assert(depth >= 0)
192 191 yield subfile, elem, depth
193 192 except ET.ParseError as err:
194   - logging.warning(' xml-parsing for {0} failed. '
195   - .format(subfile) +
  193 + logging.warning(' xml-parsing for {0} failed ({1}). '
  194 + .format(subfile, err) +
196 195 'Run iter_non_xml to investigate.')
197 196 self.subfiles_no_xml.add(subfile)
198 197 assert(depth == 0)
... ... @@ -237,14 +236,14 @@ class XmlParser(object):
237 236 and file_handle is an open read-only handle for the file
238 237 """
239 238 if not self.did_iter_all:
240   - logging.warning('Did not iterate through complete file. Should run '
241   - 'iter_xml() without args, first.')
  239 + logging.warning('Did not iterate through complete file. '
  240 + 'Should run iter_xml() without args, first.')
242 241 if not self.subfiles_no_xml:
243 242 raise StopIteration()
244 243  
245 244 content_types, content_defaults = self.get_content_types()
246 245  
247   - with ZipFile(self.filename) as zip:
  246 + with ZipFile(self.filename) as zipper:
248 247 for subfile in self.subfiles_no_xml:
249 248 if subfile.startswith('/'):
250 249 subfile = subfile[1:]
... ... @@ -257,7 +256,7 @@ class XmlParser(object):
257 256 extension = extension[1:] # remove the '.'
258 257 if extension in content_defaults:
259 258 content_type = content_defaults[extension]
260   - with zip.open(subfile, 'r') as handle:
  259 + with ZipSubFile(zipper, subfile) as handle:
261 260 yield subfile, content_type, handle
262 261  
263 262  
... ...