diff options
| author | jvoisin | 2023-05-03 22:28:02 +0200 |
|---|---|---|
| committer | jvoisin | 2023-05-03 22:28:02 +0200 |
| commit | 1b9608aecf25d5e58ee27b9b45afd7f77b883f8b (patch) | |
| tree | bec20a8bc88dc81891a8c908090d3221cd0178b0 | |
| parent | 2ac8c24dac5431a39cdc091dec47ba594f509387 (diff) | |
Use proper type annotations instead of comments
| -rw-r--r-- | libmat2/__init__.py | 8 | ||||
| -rw-r--r-- | libmat2/abstract.py | 4 | ||||
| -rw-r--r-- | libmat2/archive.py | 10 | ||||
| -rw-r--r-- | libmat2/audio.py | 2 | ||||
| -rw-r--r-- | libmat2/exiftool.py | 2 | ||||
| -rw-r--r-- | libmat2/images.py | 5 | ||||
| -rw-r--r-- | libmat2/office.py | 2 | ||||
| -rw-r--r-- | libmat2/video.py | 4 | ||||
| -rw-r--r-- | libmat2/web.py | 6 | ||||
| -rwxr-xr-x | mat2 | 4 |
10 files changed, 20 insertions, 27 deletions
diff --git a/libmat2/__init__.py b/libmat2/__init__.py index 2f20265..4974377 100644 --- a/libmat2/__init__.py +++ b/libmat2/__init__.py | |||
| @@ -2,14 +2,10 @@ | |||
| 2 | 2 | ||
| 3 | import enum | 3 | import enum |
| 4 | import importlib | 4 | import importlib |
| 5 | from typing import Optional, Union, Dict | 5 | from typing import Dict |
| 6 | 6 | ||
| 7 | from . import exiftool, video | 7 | from . import exiftool, video |
| 8 | 8 | ||
| 9 | # make pyflakes happy | ||
| 10 | assert Optional | ||
| 11 | assert Union | ||
| 12 | |||
| 13 | # A set of extension that aren't supported, despite matching a supported mimetype | 9 | # A set of extension that aren't supported, despite matching a supported mimetype |
| 14 | UNSUPPORTED_EXTENSIONS = { | 10 | UNSUPPORTED_EXTENSIONS = { |
| 15 | '.asc', | 11 | '.asc', |
| @@ -68,7 +64,7 @@ CMD_DEPENDENCIES = { | |||
| 68 | 64 | ||
| 69 | 65 | ||
| 70 | def check_dependencies() -> Dict[str, Dict[str, bool]]: | 66 | def check_dependencies() -> Dict[str, Dict[str, bool]]: |
| 71 | ret = dict() # type: Dict[str, Dict] | 67 | ret: Dict[str, Dict] = dict() |
| 72 | 68 | ||
| 73 | for key, value in DEPENDENCIES.items(): | 69 | for key, value in DEPENDENCIES.items(): |
| 74 | ret[key] = { | 70 | ret[key] = { |
diff --git a/libmat2/abstract.py b/libmat2/abstract.py index 1aff630..c531fbd 100644 --- a/libmat2/abstract.py +++ b/libmat2/abstract.py | |||
| @@ -9,8 +9,8 @@ class AbstractParser(abc.ABC): | |||
| 9 | It might yield `ValueError` on instantiation on invalid files, | 9 | It might yield `ValueError` on instantiation on invalid files, |
| 10 | and `RuntimeError` when something went wrong in `remove_all`. | 10 | and `RuntimeError` when something went wrong in `remove_all`. |
| 11 | """ | 11 | """ |
| 12 | meta_list = set() # type: Set[str] | 12 | meta_list: Set[str] = set() |
| 13 | mimetypes = set() # type: Set[str] | 13 | mimetypes: Set[str] = set() |
| 14 | 14 | ||
| 15 | def __init__(self, filename: str) -> None: | 15 | def __init__(self, filename: str) -> None: |
| 16 | """ | 16 | """ |
diff --git a/libmat2/archive.py b/libmat2/archive.py index beadd50..847f81c 100644 --- a/libmat2/archive.py +++ b/libmat2/archive.py | |||
| @@ -49,15 +49,15 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser): | |||
| 49 | 49 | ||
| 50 | # Those are the files that have a format that _isn't_ | 50 | # Those are the files that have a format that _isn't_ |
| 51 | # supported by mat2, but that we want to keep anyway. | 51 | # supported by mat2, but that we want to keep anyway. |
| 52 | self.files_to_keep = set() # type: Set[Pattern] | 52 | self.files_to_keep: Set[Pattern] = set() |
| 53 | 53 | ||
| 54 | # Those are the files that we _do not_ want to keep, | 54 | # Those are the files that we _do not_ want to keep, |
| 55 | # no matter if they are supported or not. | 55 | # no matter if they are supported or not. |
| 56 | self.files_to_omit = set() # type: Set[Pattern] | 56 | self.files_to_omit: Set[Pattern] = set() |
| 57 | 57 | ||
| 58 | # what should the parser do if it encounters an unknown file in | 58 | # what should the parser do if it encounters an unknown file in |
| 59 | # the archive? | 59 | # the archive? |
| 60 | self.unknown_member_policy = UnknownMemberPolicy.ABORT # type: UnknownMemberPolicy | 60 | self.unknown_member_policy: UnknownMemberPolicy = UnknownMemberPolicy.ABORT |
| 61 | 61 | ||
| 62 | # The LGTM comment is to mask a false-positive, | 62 | # The LGTM comment is to mask a false-positive, |
| 63 | # see https://lgtm.com/projects/g/jvoisin/mat2/ | 63 | # see https://lgtm.com/projects/g/jvoisin/mat2/ |
| @@ -134,7 +134,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser): | |||
| 134 | return member | 134 | return member |
| 135 | 135 | ||
| 136 | def get_meta(self) -> Dict[str, Union[str, Dict]]: | 136 | def get_meta(self) -> Dict[str, Union[str, Dict]]: |
| 137 | meta = dict() # type: Dict[str, Union[str, Dict]] | 137 | meta: Dict[str, Union[str, Dict]] = dict() |
| 138 | 138 | ||
| 139 | with self.archive_class(self.filename) as zin: | 139 | with self.archive_class(self.filename) as zin: |
| 140 | temp_folder = tempfile.mkdtemp() | 140 | temp_folder = tempfile.mkdtemp() |
| @@ -174,7 +174,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser): | |||
| 174 | 174 | ||
| 175 | # Sort the items to process, to reduce fingerprinting, | 175 | # Sort the items to process, to reduce fingerprinting, |
| 176 | # and keep them in the `items` variable. | 176 | # and keep them in the `items` variable. |
| 177 | items = list() # type: List[ArchiveMember] | 177 | items: List[ArchiveMember] = list() |
| 178 | for item in sorted(self._get_all_members(zin), key=self._get_member_name): | 178 | for item in sorted(self._get_all_members(zin), key=self._get_member_name): |
| 179 | # Some fileformats do require to have the `mimetype` file | 179 | # Some fileformats do require to have the `mimetype` file |
| 180 | # as the first file in the archive. | 180 | # as the first file in the archive. |
diff --git a/libmat2/audio.py b/libmat2/audio.py index aa4afdb..13ed291 100644 --- a/libmat2/audio.py +++ b/libmat2/audio.py | |||
| @@ -39,7 +39,7 @@ class MP3Parser(MutagenParser): | |||
| 39 | mimetypes = {'audio/mpeg', } | 39 | mimetypes = {'audio/mpeg', } |
| 40 | 40 | ||
| 41 | def get_meta(self) -> Dict[str, Union[str, Dict]]: | 41 | def get_meta(self) -> Dict[str, Union[str, Dict]]: |
| 42 | metadata = {} # type: Dict[str, Union[str, Dict]] | 42 | metadata: Dict[str, Union[str, Dict]] = dict() |
| 43 | meta = mutagen.File(self.filename).tags | 43 | meta = mutagen.File(self.filename).tags |
| 44 | if not meta: | 44 | if not meta: |
| 45 | return metadata | 45 | return metadata |
diff --git a/libmat2/exiftool.py b/libmat2/exiftool.py index 2b91ac2..605ef0d 100644 --- a/libmat2/exiftool.py +++ b/libmat2/exiftool.py | |||
| @@ -15,7 +15,7 @@ class ExiftoolParser(abstract.AbstractParser): | |||
| 15 | from a import file, hence why several parsers are re-using its `get_meta` | 15 | from a import file, hence why several parsers are re-using its `get_meta` |
| 16 | method. | 16 | method. |
| 17 | """ | 17 | """ |
| 18 | meta_allowlist = set() # type: Set[str] | 18 | meta_allowlist: Set[str] = set() |
| 19 | 19 | ||
| 20 | def get_meta(self) -> Dict[str, Union[str, Dict]]: | 20 | def get_meta(self) -> Dict[str, Union[str, Dict]]: |
| 21 | try: | 21 | try: |
diff --git a/libmat2/images.py b/libmat2/images.py index e7cdf5a..254438b 100644 --- a/libmat2/images.py +++ b/libmat2/images.py | |||
| @@ -11,9 +11,6 @@ from gi.repository import GdkPixbuf, GLib, Rsvg | |||
| 11 | 11 | ||
| 12 | from . import exiftool, abstract | 12 | from . import exiftool, abstract |
| 13 | 13 | ||
| 14 | # Make pyflakes happy | ||
| 15 | assert Any | ||
| 16 | |||
| 17 | class SVGParser(exiftool.ExiftoolParser): | 14 | class SVGParser(exiftool.ExiftoolParser): |
| 18 | mimetypes = {'image/svg+xml', } | 15 | mimetypes = {'image/svg+xml', } |
| 19 | meta_allowlist = {'Directory', 'ExifToolVersion', 'FileAccessDate', | 16 | meta_allowlist = {'Directory', 'ExifToolVersion', 'FileAccessDate', |
| @@ -162,7 +159,7 @@ class PPMParser(abstract.AbstractParser): | |||
| 162 | mimetypes = {'image/x-portable-pixmap'} | 159 | mimetypes = {'image/x-portable-pixmap'} |
| 163 | 160 | ||
| 164 | def get_meta(self) -> Dict[str, Union[str, Dict]]: | 161 | def get_meta(self) -> Dict[str, Union[str, Dict]]: |
| 165 | meta = {} # type: Dict[str, Union[str, Dict[Any, Any]]] | 162 | meta: Dict[str, Union[str, Dict[Any, Any]]] = dict() |
| 166 | with open(self.filename) as f: | 163 | with open(self.filename) as f: |
| 167 | for idx, line in enumerate(f): | 164 | for idx, line in enumerate(f): |
| 168 | if line.lstrip().startswith('#'): | 165 | if line.lstrip().startswith('#'): |
diff --git a/libmat2/office.py b/libmat2/office.py index f0ab404..16b20c9 100644 --- a/libmat2/office.py +++ b/libmat2/office.py | |||
| @@ -148,7 +148,7 @@ class MSOfficeParser(ZipParser): | |||
| 148 | return False | 148 | return False |
| 149 | xml_data = zin.read('[Content_Types].xml') | 149 | xml_data = zin.read('[Content_Types].xml') |
| 150 | 150 | ||
| 151 | self.content_types = dict() # type: Dict[str, str] | 151 | self.content_types: Dict[str, str] = dict() |
| 152 | try: | 152 | try: |
| 153 | tree = ET.fromstring(xml_data) | 153 | tree = ET.fromstring(xml_data) |
| 154 | except ET.ParseError: | 154 | except ET.ParseError: |
diff --git a/libmat2/video.py b/libmat2/video.py index 39059c5..3e003df 100644 --- a/libmat2/video.py +++ b/libmat2/video.py | |||
| @@ -12,7 +12,7 @@ from . import bubblewrap | |||
| 12 | class AbstractFFmpegParser(exiftool.ExiftoolParser): | 12 | class AbstractFFmpegParser(exiftool.ExiftoolParser): |
| 13 | """ Abstract parser for all FFmpeg-based ones, mainly for video. """ | 13 | """ Abstract parser for all FFmpeg-based ones, mainly for video. """ |
| 14 | # Some fileformats have mandatory metadata fields | 14 | # Some fileformats have mandatory metadata fields |
| 15 | meta_key_value_allowlist = {} # type: Dict[str, Union[str, int]] | 15 | meta_key_value_allowlist: Dict[str, Union[str, int]] = dict() |
| 16 | 16 | ||
| 17 | def remove_all(self) -> bool: | 17 | def remove_all(self) -> bool: |
| 18 | if self.meta_key_value_allowlist: | 18 | if self.meta_key_value_allowlist: |
| @@ -48,7 +48,7 @@ class AbstractFFmpegParser(exiftool.ExiftoolParser): | |||
| 48 | def get_meta(self) -> Dict[str, Union[str, Dict]]: | 48 | def get_meta(self) -> Dict[str, Union[str, Dict]]: |
| 49 | meta = super().get_meta() | 49 | meta = super().get_meta() |
| 50 | 50 | ||
| 51 | ret = dict() # type: Dict[str, Union[str, Dict]] | 51 | ret: Dict[str, Union[str, Dict]] = dict() |
| 52 | for key, value in meta.items(): | 52 | for key, value in meta.items(): |
| 53 | if key in self.meta_key_value_allowlist: | 53 | if key in self.meta_key_value_allowlist: |
| 54 | if value == self.meta_key_value_allowlist[key]: | 54 | if value == self.meta_key_value_allowlist[key]: |
diff --git a/libmat2/web.py b/libmat2/web.py index f2938e2..e33288e 100644 --- a/libmat2/web.py +++ b/libmat2/web.py | |||
| @@ -44,10 +44,10 @@ class CSSParser(abstract.AbstractParser): | |||
| 44 | 44 | ||
| 45 | 45 | ||
| 46 | class AbstractHTMLParser(abstract.AbstractParser): | 46 | class AbstractHTMLParser(abstract.AbstractParser): |
| 47 | tags_blocklist = set() # type: Set[str] | 47 | tags_blocklist: Set[str] = set() |
| 48 | # In some html/xml-based formats some tags are mandatory, | 48 | # In some html/xml-based formats some tags are mandatory, |
| 49 | # so we're keeping them, but are discarding their content | 49 | # so we're keeping them, but are discarding their content |
| 50 | tags_required_blocklist = set() # type: Set[str] | 50 | tags_required_blocklist: Set[str] = set() |
| 51 | 51 | ||
| 52 | def __init__(self, filename): | 52 | def __init__(self, filename): |
| 53 | super().__init__(filename) | 53 | super().__init__(filename) |
| @@ -91,7 +91,7 @@ class _HTMLParser(parser.HTMLParser): | |||
| 91 | self.filename = filename | 91 | self.filename = filename |
| 92 | self.__textrepr = '' | 92 | self.__textrepr = '' |
| 93 | self.__meta = {} | 93 | self.__meta = {} |
| 94 | self.__validation_queue = [] # type: list[str] | 94 | self.__validation_queue: List[str] = list() |
| 95 | 95 | ||
| 96 | # We're using counters instead of booleans, to handle nested tags | 96 | # We're using counters instead of booleans, to handle nested tags |
| 97 | self.__in_dangerous_but_required_tag = 0 | 97 | self.__in_dangerous_but_required_tag = 0 |
| @@ -36,7 +36,7 @@ def __check_file(filename: str, mode: int = os.R_OK) -> bool: | |||
| 36 | __print_without_chars("[-] %s is not a regular file." % filename) | 36 | __print_without_chars("[-] %s is not a regular file." % filename) |
| 37 | return False | 37 | return False |
| 38 | elif not os.access(filename, mode): | 38 | elif not os.access(filename, mode): |
| 39 | mode_str = [] # type: List[str] | 39 | mode_str: List[str] = list() |
| 40 | if mode & os.R_OK: | 40 | if mode & os.R_OK: |
| 41 | mode_str += 'readable' | 41 | mode_str += 'readable' |
| 42 | if mode & os.W_OK: | 42 | if mode & os.W_OK: |
| @@ -168,7 +168,7 @@ def show_parsers(): | |||
| 168 | 168 | ||
| 169 | 169 | ||
| 170 | def __get_files_recursively(files: List[str]) -> List[str]: | 170 | def __get_files_recursively(files: List[str]) -> List[str]: |
| 171 | ret = set() # type: Set[str] | 171 | ret: Set[str] = set() |
| 172 | for f in files: | 172 | for f in files: |
| 173 | if os.path.isdir(f): | 173 | if os.path.isdir(f): |
| 174 | for path, _, _files in os.walk(f): | 174 | for path, _, _files in os.walk(f): |
