summaryrefslogtreecommitdiff
path: root/libmat2/office.py
diff options
context:
space:
mode:
authorDaniel Kahn Gillmor2018-09-05 18:49:35 -0400
committerDaniel Kahn Gillmor2018-09-05 18:59:33 -0400
commitf3cef319b90a5a82ca879380c213651d74390a72 (patch)
treebb3018b72ab9ac60a47cd061028fae4d2d73228a /libmat2/office.py
parent2d9ba81a84a122f09770ed53d8c8284bf3b61dc0 (diff)
Unknown Members: make policy use an Enum
Closes #60 Note: this changeset also ensures that clean.cleaned.docx is removed up after the pytest is over.
Diffstat (limited to 'libmat2/office.py')
-rw-r--r--libmat2/office.py14
1 files changed, 5 insertions, 9 deletions
diff --git a/libmat2/office.py b/libmat2/office.py
index 29100df..60c5478 100644
--- a/libmat2/office.py
+++ b/libmat2/office.py
@@ -9,7 +9,7 @@ from typing import Dict, Set, Pattern
9 9
10import xml.etree.ElementTree as ET # type: ignore 10import xml.etree.ElementTree as ET # type: ignore
11 11
12from . import abstract, parser_factory 12from . import abstract, parser_factory, UnknownMemberPolicy
13 13
14# Make pyflakes happy 14# Make pyflakes happy
15assert Set 15assert Set
@@ -37,8 +37,8 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
37 files_to_omit = set() # type: Set[Pattern] 37 files_to_omit = set() # type: Set[Pattern]
38 38
39 # what should the parser do if it encounters an unknown file in 39 # what should the parser do if it encounters an unknown file in
40 # the archive? valid policies are 'abort', 'omit', 'keep' 40 # the archive?
41 unknown_member_policy = 'abort' # type: str 41 unknown_member_policy = UnknownMemberPolicy.ABORT # type: UnknownMemberPolicy
42 42
43 def __init__(self, filename): 43 def __init__(self, filename):
44 super().__init__(filename) 44 super().__init__(filename)
@@ -81,10 +81,6 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
81 def remove_all(self) -> bool: 81 def remove_all(self) -> bool:
82 # pylint: disable=too-many-branches 82 # pylint: disable=too-many-branches
83 83
84 if self.unknown_member_policy not in ['omit', 'keep', 'abort']:
85 logging.error("The policy %s is invalid.", self.unknown_member_policy)
86 raise ValueError
87
88 with zipfile.ZipFile(self.filename) as zin,\ 84 with zipfile.ZipFile(self.filename) as zin,\
89 zipfile.ZipFile(self.output_filename, 'w') as zout: 85 zipfile.ZipFile(self.output_filename, 'w') as zout:
90 86
@@ -113,11 +109,11 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
113 # supported files that we want to clean then add 109 # supported files that we want to clean then add
114 tmp_parser, mtype = parser_factory.get_parser(full_path) # type: ignore 110 tmp_parser, mtype = parser_factory.get_parser(full_path) # type: ignore
115 if not tmp_parser: 111 if not tmp_parser:
116 if self.unknown_member_policy == 'omit': 112 if self.unknown_member_policy == UnknownMemberPolicy.OMIT:
117 logging.warning("In file %s, omitting unknown element %s (format: %s)", 113 logging.warning("In file %s, omitting unknown element %s (format: %s)",
118 self.filename, item.filename, mtype) 114 self.filename, item.filename, mtype)
119 continue 115 continue
120 elif self.unknown_member_policy == 'keep': 116 elif self.unknown_member_policy == UnknownMemberPolicy.KEEP:
121 logging.warning("In file %s, keeping unknown element %s (format: %s)", 117 logging.warning("In file %s, keeping unknown element %s (format: %s)",
122 self.filename, item.filename, mtype) 118 self.filename, item.filename, mtype)
123 else: 119 else: