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