1
0
mirror of synced 2024-11-22 17:24:23 +01:00

office: create policy for what to do about unknown members

previously, encountering an unknown member meant that any parser of
this type would abort.

now, the user can set parser.unknown_member_policy to either 'omit' or
'keep' if they don't want the current action of 'abort'

note that this causes pylint to complain about branching depth for
remove_all() because of the nuanced error-handling.  I've disabled
this check.
This commit is contained in:
Daniel Kahn Gillmor 2018-08-31 15:25:46 -04:00
parent 9ce458cb3b
commit 4192a2daa3

View File

@ -40,6 +40,10 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
# no matter if they are supported or not. # no matter if they are supported or not.
files_to_omit = set() # type: Set[Pattern] files_to_omit = set() # type: Set[Pattern]
# what should the parser do if it encounters an unknown file in
# the archive? valid policies are 'abort', 'omit', 'keep'
unknown_member_policy = 'abort' # type: str
def __init__(self, filename): def __init__(self, filename):
super().__init__(filename) super().__init__(filename)
try: # better fail here than later try: # better fail here than later
@ -79,6 +83,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
return metadata return metadata
def remove_all(self) -> bool: def remove_all(self) -> bool:
# pylint: disable=too-many-branches
with zipfile.ZipFile(self.filename) as zin,\ with zipfile.ZipFile(self.filename) as zin,\
zipfile.ZipFile(self.output_filename, 'w') as zout: zipfile.ZipFile(self.output_filename, 'w') as zout:
@ -107,12 +112,24 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
# supported files that we want to clean then add # supported files that we want to clean then add
tmp_parser, mtype = parser_factory.get_parser(full_path) # type: ignore tmp_parser, mtype = parser_factory.get_parser(full_path) # type: ignore
if not tmp_parser: if not tmp_parser:
if self.unknown_member_policy == 'omit':
logging.warning("In file %s, omitting unknown element %s (format: %s)",
self.filename, item.filename, mtype)
continue
elif self.unknown_member_policy == 'keep':
logging.warning("In file %s, keeping unknown element %s (format: %s)",
self.filename, item.filename, mtype)
else:
if self.unknown_member_policy != 'abort':
logging.warning("Invalid unknown_member_policy %s, " +
"treating as 'abort'", self.unknown_member_policy)
shutil.rmtree(temp_folder) shutil.rmtree(temp_folder)
os.remove(self.output_filename) os.remove(self.output_filename)
logging.error("In file %s, element %s's format (%s) " + logging.error("In file %s, element %s's format (%s) " +
"isn't supported", "isn't supported",
self.filename, item.filename, mtype) self.filename, item.filename, mtype)
return False return False
if tmp_parser:
tmp_parser.remove_all() tmp_parser.remove_all()
os.rename(tmp_parser.output_filename, full_path) os.rename(tmp_parser.output_filename, full_path)