diff options
| author | Daniel Kahn Gillmor | 2018-09-05 18:49:35 -0400 |
|---|---|---|
| committer | Daniel Kahn Gillmor | 2018-09-05 18:59:33 -0400 |
| commit | f3cef319b90a5a82ca879380c213651d74390a72 (patch) | |
| tree | bb3018b72ab9ac60a47cd061028fae4d2d73228a /libmat2 | |
| parent | 2d9ba81a84a122f09770ed53d8c8284bf3b61dc0 (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')
| -rw-r--r-- | libmat2/__init__.py | 6 | ||||
| -rw-r--r-- | libmat2/office.py | 14 |
2 files changed, 11 insertions, 9 deletions
diff --git a/libmat2/__init__.py b/libmat2/__init__.py index bf4e813..8a5b064 100644 --- a/libmat2/__init__.py +++ b/libmat2/__init__.py | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | import os | 3 | import os |
| 4 | import collections | 4 | import collections |
| 5 | from enum import Enum | ||
| 5 | import importlib | 6 | import importlib |
| 6 | from typing import Dict, Optional | 7 | from typing import Dict, Optional |
| 7 | 8 | ||
| @@ -62,3 +63,8 @@ def check_dependencies() -> dict: | |||
| 62 | ret[value] = False # pragma: no cover | 63 | ret[value] = False # pragma: no cover |
| 63 | 64 | ||
| 64 | return ret | 65 | return ret |
| 66 | |||
| 67 | class UnknownMemberPolicy(Enum): | ||
| 68 | ABORT = 'abort' | ||
| 69 | OMIT = 'omit' | ||
| 70 | KEEP = 'keep' | ||
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 | ||
| 10 | import xml.etree.ElementTree as ET # type: ignore | 10 | import xml.etree.ElementTree as ET # type: ignore |
| 11 | 11 | ||
| 12 | from . import abstract, parser_factory | 12 | from . import abstract, parser_factory, UnknownMemberPolicy |
| 13 | 13 | ||
| 14 | # Make pyflakes happy | 14 | # Make pyflakes happy |
| 15 | assert Set | 15 | assert 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: |
