diff options
Diffstat (limited to 'libmat2')
| -rw-r--r-- | libmat2/__init__.py | 2 | ||||
| -rw-r--r-- | libmat2/images.py | 5 | ||||
| -rw-r--r-- | libmat2/office.py | 2 | ||||
| -rw-r--r-- | libmat2/parser_factory.py | 4 | ||||
| -rw-r--r-- | libmat2/torrent.py | 40 |
5 files changed, 29 insertions, 24 deletions
diff --git a/libmat2/__init__.py b/libmat2/__init__.py index 190abe5..d910215 100644 --- a/libmat2/__init__.py +++ b/libmat2/__init__.py | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #!/bin/env python3 | 1 | #!/bin/env python3 |
| 2 | 2 | ||
| 3 | # A set of extension that aren't supported, despite matching a supported mimetype | 3 | # A set of extension that aren't supported, despite matching a supported mimetype |
| 4 | unsupported_extensions = { | 4 | UNSUPPORTED_EXTENSIONS = { |
| 5 | '.asc', | 5 | '.asc', |
| 6 | '.bat', | 6 | '.bat', |
| 7 | '.brf', | 7 | '.brf', |
diff --git a/libmat2/images.py b/libmat2/images.py index 311186d..f9171e5 100644 --- a/libmat2/images.py +++ b/libmat2/images.py | |||
| @@ -5,6 +5,7 @@ import os | |||
| 5 | import shutil | 5 | import shutil |
| 6 | import tempfile | 6 | import tempfile |
| 7 | import re | 7 | import re |
| 8 | from typing import Set | ||
| 8 | 9 | ||
| 9 | import cairo | 10 | import cairo |
| 10 | 11 | ||
| @@ -14,8 +15,12 @@ from gi.repository import GdkPixbuf | |||
| 14 | 15 | ||
| 15 | from . import abstract | 16 | from . import abstract |
| 16 | 17 | ||
| 18 | # Make pyflakes happy | ||
| 19 | assert Set | ||
| 17 | 20 | ||
| 18 | class _ImageParser(abstract.AbstractParser): | 21 | class _ImageParser(abstract.AbstractParser): |
| 22 | meta_whitelist = set() # type: Set[str] | ||
| 23 | |||
| 19 | @staticmethod | 24 | @staticmethod |
| 20 | def __handle_problematic_filename(filename: str, callback) -> str: | 25 | def __handle_problematic_filename(filename: str, callback) -> str: |
| 21 | """ This method takes a filename with a problematic name, | 26 | """ This method takes a filename with a problematic name, |
diff --git a/libmat2/office.py b/libmat2/office.py index e0ee6d2..75d6744 100644 --- a/libmat2/office.py +++ b/libmat2/office.py | |||
| @@ -21,7 +21,7 @@ def _parse_xml(full_path: str): | |||
| 21 | """ This function parse XML with namespace support. """ | 21 | """ This function parse XML with namespace support. """ |
| 22 | def parse_map(f): # etree support for ns is a bit rough | 22 | def parse_map(f): # etree support for ns is a bit rough |
| 23 | ns_map = dict() | 23 | ns_map = dict() |
| 24 | for event, (k, v) in ET.iterparse(f, ("start-ns", )): | 24 | for _, (k, v) in ET.iterparse(f, ("start-ns", )): |
| 25 | ns_map[k] = v | 25 | ns_map[k] = v |
| 26 | return ns_map | 26 | return ns_map |
| 27 | 27 | ||
diff --git a/libmat2/parser_factory.py b/libmat2/parser_factory.py index 7d4f43f..bd442b8 100644 --- a/libmat2/parser_factory.py +++ b/libmat2/parser_factory.py | |||
| @@ -4,7 +4,7 @@ import mimetypes | |||
| 4 | import importlib | 4 | import importlib |
| 5 | from typing import TypeVar, List, Tuple, Optional | 5 | from typing import TypeVar, List, Tuple, Optional |
| 6 | 6 | ||
| 7 | from . import abstract, unsupported_extensions | 7 | from . import abstract, UNSUPPORTED_EXTENSIONS |
| 8 | 8 | ||
| 9 | assert Tuple # make pyflakes happy | 9 | assert Tuple # make pyflakes happy |
| 10 | 10 | ||
| @@ -34,7 +34,7 @@ def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]: | |||
| 34 | mtype, _ = mimetypes.guess_type(filename) | 34 | mtype, _ = mimetypes.guess_type(filename) |
| 35 | 35 | ||
| 36 | _, extension = os.path.splitext(filename) | 36 | _, extension = os.path.splitext(filename) |
| 37 | if extension in unsupported_extensions: | 37 | if extension in UNSUPPORTED_EXTENSIONS: |
| 38 | return None, mtype | 38 | return None, mtype |
| 39 | 39 | ||
| 40 | for parser_class in _get_parsers(): # type: ignore | 40 | for parser_class in _get_parsers(): # type: ignore |
diff --git a/libmat2/torrent.py b/libmat2/torrent.py index 0f122b0..ca7715a 100644 --- a/libmat2/torrent.py +++ b/libmat2/torrent.py | |||
| @@ -17,17 +17,17 @@ class TorrentParser(abstract.AbstractParser): | |||
| 17 | 17 | ||
| 18 | def get_meta(self) -> Dict[str, str]: | 18 | def get_meta(self) -> Dict[str, str]: |
| 19 | metadata = {} | 19 | metadata = {} |
| 20 | for k, v in self.dict_repr.items(): | 20 | for key, value in self.dict_repr.items(): |
| 21 | if k not in self.whitelist: | 21 | if key not in self.whitelist: |
| 22 | metadata[k.decode('utf-8')] = v | 22 | metadata[key.decode('utf-8')] = value |
| 23 | return metadata | 23 | return metadata |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | def remove_all(self) -> bool: | 26 | def remove_all(self) -> bool: |
| 27 | cleaned = dict() | 27 | cleaned = dict() |
| 28 | for k, v in self.dict_repr.items(): | 28 | for key, value in self.dict_repr.items(): |
| 29 | if k in self.whitelist: | 29 | if key in self.whitelist: |
| 30 | cleaned[k] = v | 30 | cleaned[key] = value |
| 31 | with open(self.output_filename, 'wb') as f: | 31 | with open(self.output_filename, 'wb') as f: |
| 32 | f.write(_BencodeHandler().bencode(cleaned)) | 32 | f.write(_BencodeHandler().bencode(cleaned)) |
| 33 | self.dict_repr = cleaned # since we're stateful | 33 | self.dict_repr = cleaned # since we're stateful |
| @@ -78,20 +78,20 @@ class _BencodeHandler(object): | |||
| 78 | return s[colon:colon+str_len], s[colon+str_len:] | 78 | return s[colon:colon+str_len], s[colon+str_len:] |
| 79 | 79 | ||
| 80 | def __decode_list(self, s: bytes) -> Tuple[list, bytes]: | 80 | def __decode_list(self, s: bytes) -> Tuple[list, bytes]: |
| 81 | r = list() | 81 | ret = list() |
| 82 | s = s[1:] # skip leading `l` | 82 | s = s[1:] # skip leading `l` |
| 83 | while s[0] != ord('e'): | 83 | while s[0] != ord('e'): |
| 84 | v, s = self.__decode_func[s[0]](s) | 84 | value, s = self.__decode_func[s[0]](s) |
| 85 | r.append(v) | 85 | ret.append(value) |
| 86 | return r, s[1:] | 86 | return ret, s[1:] |
| 87 | 87 | ||
| 88 | def __decode_dict(self, s: bytes) -> Tuple[dict, bytes]: | 88 | def __decode_dict(self, s: bytes) -> Tuple[dict, bytes]: |
| 89 | r = dict() | 89 | ret = dict() |
| 90 | s = s[1:] # skip leading `d` | 90 | s = s[1:] # skip leading `d` |
| 91 | while s[0] != ord(b'e'): | 91 | while s[0] != ord(b'e'): |
| 92 | k, s = self.__decode_string(s) | 92 | key, s = self.__decode_string(s) |
| 93 | r[k], s = self.__decode_func[s[0]](s) | 93 | ret[key], s = self.__decode_func[s[0]](s) |
| 94 | return r, s[1:] | 94 | return ret, s[1:] |
| 95 | 95 | ||
| 96 | @staticmethod | 96 | @staticmethod |
| 97 | def __encode_int(x: bytes) -> bytes: | 97 | def __encode_int(x: bytes) -> bytes: |
| @@ -109,9 +109,9 @@ class _BencodeHandler(object): | |||
| 109 | 109 | ||
| 110 | def __encode_dict(self, x: dict) -> bytes: | 110 | def __encode_dict(self, x: dict) -> bytes: |
| 111 | ret = b'' | 111 | ret = b'' |
| 112 | for k, v in sorted(x.items()): | 112 | for key, value in sorted(x.items()): |
| 113 | ret += self.__encode_func[type(k)](k) | 113 | ret += self.__encode_func[type(key)](key) |
| 114 | ret += self.__encode_func[type(v)](v) | 114 | ret += self.__encode_func[type(value)](value) |
| 115 | return b'd' + ret + b'e' | 115 | return b'd' + ret + b'e' |
| 116 | 116 | ||
| 117 | def bencode(self, s: Union[dict, list, bytes, int]) -> bytes: | 117 | def bencode(self, s: Union[dict, list, bytes, int]) -> bytes: |
| @@ -119,11 +119,11 @@ class _BencodeHandler(object): | |||
| 119 | 119 | ||
| 120 | def bdecode(self, s: bytes) -> Union[dict, None]: | 120 | def bdecode(self, s: bytes) -> Union[dict, None]: |
| 121 | try: | 121 | try: |
| 122 | r, l = self.__decode_func[s[0]](s) | 122 | ret, trail = self.__decode_func[s[0]](s) |
| 123 | except (IndexError, KeyError, ValueError) as e: | 123 | except (IndexError, KeyError, ValueError) as e: |
| 124 | logging.debug("Not a valid bencoded string: %s", e) | 124 | logging.debug("Not a valid bencoded string: %s", e) |
| 125 | return None | 125 | return None |
| 126 | if l != b'': | 126 | if trail != b'': |
| 127 | logging.debug("Invalid bencoded value (data after valid prefix)") | 127 | logging.debug("Invalid bencoded value (data after valid prefix)") |
| 128 | return None | 128 | return None |
| 129 | return r | 129 | return ret |
