summaryrefslogtreecommitdiff
path: root/libmat2/archive.py
diff options
context:
space:
mode:
authorjvoisin2022-08-28 22:29:06 +0200
committerjvoisin2022-08-28 22:29:06 +0200
commitcc5be8608b49d74a633b80a95a49a018d4dcd477 (patch)
tree322c21ba2543831d5a1804ebce50a3f7c2391029 /libmat2/archive.py
parent292f44c0861a57b54a289641ead7e59f158e307e (diff)
Simplify the typing annotations
Diffstat (limited to 'libmat2/archive.py')
-rw-r--r--libmat2/archive.py27
1 files changed, 13 insertions, 14 deletions
diff --git a/libmat2/archive.py b/libmat2/archive.py
index 39fb23e..17ec608 100644
--- a/libmat2/archive.py
+++ b/libmat2/archive.py
@@ -7,12 +7,11 @@ import tempfile
7import os 7import os
8import logging 8import logging
9import shutil 9import shutil
10from typing import Dict, Set, Pattern, Union, Any, List 10from typing import Pattern, Union, Any
11 11
12from . import abstract, UnknownMemberPolicy, parser_factory 12from . import abstract, UnknownMemberPolicy, parser_factory
13 13
14# Make pyflakes happy 14# Make pyflakes happy
15assert Set
16assert Pattern 15assert Pattern
17 16
18# pylint: disable=not-callable,assignment-from-no-return,too-many-branches 17# pylint: disable=not-callable,assignment-from-no-return,too-many-branches
@@ -53,11 +52,11 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
53 52
54 # Those are the files that have a format that _isn't_ 53 # Those are the files that have a format that _isn't_
55 # supported by mat2, but that we want to keep anyway. 54 # supported by mat2, but that we want to keep anyway.
56 self.files_to_keep = set() # type: Set[Pattern] 55 self.files_to_keep = set() # type: set[Pattern]
57 56
58 # Those are the files that we _do not_ want to keep, 57 # Those are the files that we _do not_ want to keep,
59 # no matter if they are supported or not. 58 # no matter if they are supported or not.
60 self.files_to_omit = set() # type: Set[Pattern] 59 self.files_to_omit = set() # type: set[Pattern]
61 60
62 # what should the parser do if it encounters an unknown file in 61 # what should the parser do if it encounters an unknown file in
63 # the archive? 62 # the archive?
@@ -76,7 +75,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
76 # pylint: disable=unused-argument,no-self-use 75 # pylint: disable=unused-argument,no-self-use
77 return True # pragma: no cover 76 return True # pragma: no cover
78 77
79 def _specific_get_meta(self, full_path: str, file_path: str) -> Dict[str, Any]: 78 def _specific_get_meta(self, full_path: str, file_path: str) -> dict[str, Any]:
80 """ This method can be used to extract specific metadata 79 """ This method can be used to extract specific metadata
81 from files present in the archive.""" 80 from files present in the archive."""
82 # pylint: disable=unused-argument,no-self-use 81 # pylint: disable=unused-argument,no-self-use
@@ -91,7 +90,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
91 90
92 @staticmethod 91 @staticmethod
93 @abc.abstractmethod 92 @abc.abstractmethod
94 def _get_all_members(archive: ArchiveClass) -> List[ArchiveMember]: 93 def _get_all_members(archive: ArchiveClass) -> list[ArchiveMember]:
95 """Return all the members of the archive.""" 94 """Return all the members of the archive."""
96 95
97 @staticmethod 96 @staticmethod
@@ -101,7 +100,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
101 100
102 @staticmethod 101 @staticmethod
103 @abc.abstractmethod 102 @abc.abstractmethod
104 def _get_member_meta(member: ArchiveMember) -> Dict[str, str]: 103 def _get_member_meta(member: ArchiveMember) -> dict[str, str]:
105 """Return all the metadata of a given member.""" 104 """Return all the metadata of a given member."""
106 105
107 @staticmethod 106 @staticmethod
@@ -132,8 +131,8 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
132 # pylint: disable=unused-argument 131 # pylint: disable=unused-argument
133 return member 132 return member
134 133
135 def get_meta(self) -> Dict[str, Union[str, dict]]: 134 def get_meta(self) -> dict[str, Union[str, dict]]:
136 meta = dict() # type: Dict[str, Union[str, dict]] 135 meta = dict() # type: dict[str, Union[str, dict]]
137 136
138 with self.archive_class(self.filename) as zin: 137 with self.archive_class(self.filename) as zin:
139 temp_folder = tempfile.mkdtemp() 138 temp_folder = tempfile.mkdtemp()
@@ -174,7 +173,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
174 173
175 # Sort the items to process, to reduce fingerprinting, 174 # Sort the items to process, to reduce fingerprinting,
176 # and keep them in the `items` variable. 175 # and keep them in the `items` variable.
177 items = list() # type: List[ArchiveMember] 176 items = list() # type: list[ArchiveMember]
178 for item in sorted(self._get_all_members(zin), key=self._get_member_name): 177 for item in sorted(self._get_all_members(zin), key=self._get_member_name):
179 # Some fileformats do require to have the `mimetype` file 178 # Some fileformats do require to have the `mimetype` file
180 # as the first file in the archive. 179 # as the first file in the archive.
@@ -340,7 +339,7 @@ class TarParser(ArchiveBasedAbstractParser):
340 return member 339 return member
341 340
342 @staticmethod 341 @staticmethod
343 def _get_member_meta(member: ArchiveMember) -> Dict[str, str]: 342 def _get_member_meta(member: ArchiveMember) -> dict[str, str]:
344 assert isinstance(member, tarfile.TarInfo) # please mypy 343 assert isinstance(member, tarfile.TarInfo) # please mypy
345 metadata = {} 344 metadata = {}
346 if member.mtime != 0: 345 if member.mtime != 0:
@@ -362,7 +361,7 @@ class TarParser(ArchiveBasedAbstractParser):
362 archive.add(full_path, member.name, filter=TarParser._clean_member) # type: ignore 361 archive.add(full_path, member.name, filter=TarParser._clean_member) # type: ignore
363 362
364 @staticmethod 363 @staticmethod
365 def _get_all_members(archive: ArchiveClass) -> List[ArchiveMember]: 364 def _get_all_members(archive: ArchiveClass) -> list[ArchiveMember]:
366 assert isinstance(archive, tarfile.TarFile) # please mypy 365 assert isinstance(archive, tarfile.TarFile) # please mypy
367 return archive.getmembers() # type: ignore 366 return archive.getmembers() # type: ignore
368 367
@@ -416,7 +415,7 @@ class ZipParser(ArchiveBasedAbstractParser):
416 return member 415 return member
417 416
418 @staticmethod 417 @staticmethod
419 def _get_member_meta(member: ArchiveMember) -> Dict[str, str]: 418 def _get_member_meta(member: ArchiveMember) -> dict[str, str]:
420 assert isinstance(member, zipfile.ZipInfo) # please mypy 419 assert isinstance(member, zipfile.ZipInfo) # please mypy
421 metadata = {} 420 metadata = {}
422 if member.create_system == 3: # this is Linux 421 if member.create_system == 3: # this is Linux
@@ -443,7 +442,7 @@ class ZipParser(ArchiveBasedAbstractParser):
443 compress_type=member.compress_type) 442 compress_type=member.compress_type)
444 443
445 @staticmethod 444 @staticmethod
446 def _get_all_members(archive: ArchiveClass) -> List[ArchiveMember]: 445 def _get_all_members(archive: ArchiveClass) -> list[ArchiveMember]:
447 assert isinstance(archive, zipfile.ZipFile) # please mypy 446 assert isinstance(archive, zipfile.ZipFile) # please mypy
448 return archive.infolist() # type: ignore 447 return archive.infolist() # type: ignore
449 448