summaryrefslogtreecommitdiff
path: root/libmat2
diff options
context:
space:
mode:
authorjvoisin2023-05-03 22:28:02 +0200
committerjvoisin2023-05-03 22:28:02 +0200
commit1b9608aecf25d5e58ee27b9b45afd7f77b883f8b (patch)
treebec20a8bc88dc81891a8c908090d3221cd0178b0 /libmat2
parent2ac8c24dac5431a39cdc091dec47ba594f509387 (diff)
Use proper type annotations instead of comments
Diffstat (limited to 'libmat2')
-rw-r--r--libmat2/__init__.py8
-rw-r--r--libmat2/abstract.py4
-rw-r--r--libmat2/archive.py10
-rw-r--r--libmat2/audio.py2
-rw-r--r--libmat2/exiftool.py2
-rw-r--r--libmat2/images.py5
-rw-r--r--libmat2/office.py2
-rw-r--r--libmat2/video.py4
-rw-r--r--libmat2/web.py6
9 files changed, 18 insertions, 25 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
3import enum 3import enum
4import importlib 4import importlib
5from typing import Optional, Union, Dict 5from typing import Dict
6 6
7from . import exiftool, video 7from . import exiftool, video
8 8
9# make pyflakes happy
10assert Optional
11assert 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
14UNSUPPORTED_EXTENSIONS = { 10UNSUPPORTED_EXTENSIONS = {
15 '.asc', 11 '.asc',
@@ -68,7 +64,7 @@ CMD_DEPENDENCIES = {
68 64
69 65
70def check_dependencies() -> Dict[str, Dict[str, bool]]: 66def 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
12from . import exiftool, abstract 12from . import exiftool, abstract
13 13
14# Make pyflakes happy
15assert Any
16
17class SVGParser(exiftool.ExiftoolParser): 14class 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
12class AbstractFFmpegParser(exiftool.ExiftoolParser): 12class 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
46class AbstractHTMLParser(abstract.AbstractParser): 46class 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