1
0
mirror of synced 2024-11-22 09:14:23 +01:00

Fix some pep8 issues spotted by pyflakes

This commit is contained in:
jvoisin 2018-07-08 22:40:36 +02:00
parent f49aa5cab7
commit 8c21006e6c
6 changed files with 31 additions and 26 deletions

View File

@ -1,7 +1,7 @@
#!/bin/env python3 #!/bin/env python3
# A set of extension that aren't supported, despite matching a supported mimetype # A set of extension that aren't supported, despite matching a supported mimetype
unsupported_extensions = { UNSUPPORTED_EXTENSIONS = {
'.asc', '.asc',
'.bat', '.bat',
'.brf', '.brf',

View File

@ -5,6 +5,7 @@ import os
import shutil import shutil
import tempfile import tempfile
import re import re
from typing import Set
import cairo import cairo
@ -14,8 +15,12 @@ from gi.repository import GdkPixbuf
from . import abstract from . import abstract
# Make pyflakes happy
assert Set
class _ImageParser(abstract.AbstractParser): class _ImageParser(abstract.AbstractParser):
meta_whitelist = set() # type: Set[str]
@staticmethod @staticmethod
def __handle_problematic_filename(filename: str, callback) -> str: def __handle_problematic_filename(filename: str, callback) -> str:
""" This method takes a filename with a problematic name, """ This method takes a filename with a problematic name,

View File

@ -21,7 +21,7 @@ def _parse_xml(full_path: str):
""" This function parse XML with namespace support. """ """ This function parse XML with namespace support. """
def parse_map(f): # etree support for ns is a bit rough def parse_map(f): # etree support for ns is a bit rough
ns_map = dict() ns_map = dict()
for event, (k, v) in ET.iterparse(f, ("start-ns", )): for _, (k, v) in ET.iterparse(f, ("start-ns", )):
ns_map[k] = v ns_map[k] = v
return ns_map return ns_map

View File

@ -4,7 +4,7 @@ import mimetypes
import importlib import importlib
from typing import TypeVar, List, Tuple, Optional from typing import TypeVar, List, Tuple, Optional
from . import abstract, unsupported_extensions from . import abstract, UNSUPPORTED_EXTENSIONS
assert Tuple # make pyflakes happy assert Tuple # make pyflakes happy
@ -34,7 +34,7 @@ def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]:
mtype, _ = mimetypes.guess_type(filename) mtype, _ = mimetypes.guess_type(filename)
_, extension = os.path.splitext(filename) _, extension = os.path.splitext(filename)
if extension in unsupported_extensions: if extension in UNSUPPORTED_EXTENSIONS:
return None, mtype return None, mtype
for parser_class in _get_parsers(): # type: ignore for parser_class in _get_parsers(): # type: ignore

View File

@ -17,17 +17,17 @@ class TorrentParser(abstract.AbstractParser):
def get_meta(self) -> Dict[str, str]: def get_meta(self) -> Dict[str, str]:
metadata = {} metadata = {}
for k, v in self.dict_repr.items(): for key, value in self.dict_repr.items():
if k not in self.whitelist: if key not in self.whitelist:
metadata[k.decode('utf-8')] = v metadata[key.decode('utf-8')] = value
return metadata return metadata
def remove_all(self) -> bool: def remove_all(self) -> bool:
cleaned = dict() cleaned = dict()
for k, v in self.dict_repr.items(): for key, value in self.dict_repr.items():
if k in self.whitelist: if key in self.whitelist:
cleaned[k] = v cleaned[key] = value
with open(self.output_filename, 'wb') as f: with open(self.output_filename, 'wb') as f:
f.write(_BencodeHandler().bencode(cleaned)) f.write(_BencodeHandler().bencode(cleaned))
self.dict_repr = cleaned # since we're stateful self.dict_repr = cleaned # since we're stateful
@ -78,20 +78,20 @@ class _BencodeHandler(object):
return s[colon:colon+str_len], s[colon+str_len:] return s[colon:colon+str_len], s[colon+str_len:]
def __decode_list(self, s: bytes) -> Tuple[list, bytes]: def __decode_list(self, s: bytes) -> Tuple[list, bytes]:
r = list() ret = list()
s = s[1:] # skip leading `l` s = s[1:] # skip leading `l`
while s[0] != ord('e'): while s[0] != ord('e'):
v, s = self.__decode_func[s[0]](s) value, s = self.__decode_func[s[0]](s)
r.append(v) ret.append(value)
return r, s[1:] return ret, s[1:]
def __decode_dict(self, s: bytes) -> Tuple[dict, bytes]: def __decode_dict(self, s: bytes) -> Tuple[dict, bytes]:
r = dict() ret = dict()
s = s[1:] # skip leading `d` s = s[1:] # skip leading `d`
while s[0] != ord(b'e'): while s[0] != ord(b'e'):
k, s = self.__decode_string(s) key, s = self.__decode_string(s)
r[k], s = self.__decode_func[s[0]](s) ret[key], s = self.__decode_func[s[0]](s)
return r, s[1:] return ret, s[1:]
@staticmethod @staticmethod
def __encode_int(x: bytes) -> bytes: def __encode_int(x: bytes) -> bytes:
@ -109,9 +109,9 @@ class _BencodeHandler(object):
def __encode_dict(self, x: dict) -> bytes: def __encode_dict(self, x: dict) -> bytes:
ret = b'' ret = b''
for k, v in sorted(x.items()): for key, value in sorted(x.items()):
ret += self.__encode_func[type(k)](k) ret += self.__encode_func[type(key)](key)
ret += self.__encode_func[type(v)](v) ret += self.__encode_func[type(value)](value)
return b'd' + ret + b'e' return b'd' + ret + b'e'
def bencode(self, s: Union[dict, list, bytes, int]) -> bytes: def bencode(self, s: Union[dict, list, bytes, int]) -> bytes:
@ -119,11 +119,11 @@ class _BencodeHandler(object):
def bdecode(self, s: bytes) -> Union[dict, None]: def bdecode(self, s: bytes) -> Union[dict, None]:
try: try:
r, l = self.__decode_func[s[0]](s) ret, trail = self.__decode_func[s[0]](s)
except (IndexError, KeyError, ValueError) as e: except (IndexError, KeyError, ValueError) as e:
logging.debug("Not a valid bencoded string: %s", e) logging.debug("Not a valid bencoded string: %s", e)
return None return None
if l != b'': if trail != b'':
logging.debug("Invalid bencoded value (data after valid prefix)") logging.debug("Invalid bencoded value (data after valid prefix)")
return None return None
return r return ret

4
mat2
View File

@ -9,7 +9,7 @@ import argparse
import multiprocessing import multiprocessing
try: try:
from libmat2 import parser_factory, unsupported_extensions from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS
except ValueError as e: except ValueError as e:
print(e) print(e)
sys.exit(1) sys.exit(1)
@ -84,7 +84,7 @@ def show_parsers():
for mtype in parser.mimetypes: for mtype in parser.mimetypes:
extensions = set() extensions = set()
for extension in mimetypes.guess_all_extensions(mtype): for extension in mimetypes.guess_all_extensions(mtype):
if extension[1:] not in unsupported_extensions: # skip the dot if extension[1:] not in UNSUPPORTED_EXTENSIONS: # skip the dot
extensions.add(extension) extensions.add(extension)
if not extensions: if not extensions:
# we're not supporting a single extension in the current # we're not supporting a single extension in the current