diff options
| author | jvoisin | 2018-07-02 00:22:05 +0200 |
|---|---|---|
| committer | jvoisin | 2018-07-02 00:22:05 +0200 |
| commit | 893f58554ab963f8abd4a08222a311014322fff1 (patch) | |
| tree | fe4ec24bc5089a6701ab985e1ad3f6a1151ea9a4 /libmat2 | |
| parent | 11008f8fd436f0b8b00bc600a2cd7a77b55c7494 (diff) | |
Improve a bit the formatting of the code thanks to pyflakes3
Diffstat (limited to 'libmat2')
| -rw-r--r-- | libmat2/audio.py | 8 | ||||
| -rw-r--r-- | libmat2/images.py | 8 | ||||
| -rw-r--r-- | libmat2/office.py | 52 | ||||
| -rw-r--r-- | libmat2/parser_factory.py | 6 | ||||
| -rw-r--r-- | libmat2/pdf.py | 4 | ||||
| -rw-r--r-- | libmat2/torrent.py | 2 |
6 files changed, 39 insertions, 41 deletions
diff --git a/libmat2/audio.py b/libmat2/audio.py index a26f36f..99a335d 100644 --- a/libmat2/audio.py +++ b/libmat2/audio.py | |||
| @@ -10,11 +10,7 @@ class MutagenParser(abstract.AbstractParser): | |||
| 10 | super().__init__(filename) | 10 | super().__init__(filename) |
| 11 | try: | 11 | try: |
| 12 | mutagen.File(self.filename) | 12 | mutagen.File(self.filename) |
| 13 | except mutagen.flac.MutagenError: | 13 | except mutagen.MutagenError: |
| 14 | raise ValueError | ||
| 15 | except mutagen.mp3.MutagenError: | ||
| 16 | raise ValueError | ||
| 17 | except mutagen.ogg.MutagenError: | ||
| 18 | raise ValueError | 14 | raise ValueError |
| 19 | 15 | ||
| 20 | def get_meta(self): | 16 | def get_meta(self): |
| @@ -47,4 +43,4 @@ class OGGParser(MutagenParser): | |||
| 47 | 43 | ||
| 48 | 44 | ||
| 49 | class FLACParser(MutagenParser): | 45 | class FLACParser(MutagenParser): |
| 50 | mimetypes = {'audio/flac', 'audio/x-flac' } | 46 | mimetypes = {'audio/flac', 'audio/x-flac'} |
diff --git a/libmat2/images.py b/libmat2/images.py index a7a9cad..74533b5 100644 --- a/libmat2/images.py +++ b/libmat2/images.py | |||
| @@ -15,9 +15,9 @@ from gi.repository import GdkPixbuf | |||
| 15 | from . import abstract | 15 | from . import abstract |
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | class __ImageParser(abstract.AbstractParser): | 18 | class _ImageParser(abstract.AbstractParser): |
| 19 | @staticmethod | 19 | @staticmethod |
| 20 | def __handle_problematic_filename(filename:str, callback) -> str: | 20 | def __handle_problematic_filename(filename: str, callback) -> str: |
| 21 | """ This method takes a filename with a problematic name, | 21 | """ This method takes a filename with a problematic name, |
| 22 | and safely applies it a `callback`.""" | 22 | and safely applies it a `callback`.""" |
| 23 | tmpdirname = tempfile.mkdtemp() | 23 | tmpdirname = tempfile.mkdtemp() |
| @@ -42,7 +42,7 @@ class __ImageParser(abstract.AbstractParser): | |||
| 42 | meta.pop(key, None) | 42 | meta.pop(key, None) |
| 43 | return meta | 43 | return meta |
| 44 | 44 | ||
| 45 | class PNGParser(__ImageParser): | 45 | class PNGParser(_ImageParser): |
| 46 | mimetypes = {'image/png', } | 46 | mimetypes = {'image/png', } |
| 47 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | 47 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', |
| 48 | 'Directory', 'FileSize', 'FileModifyDate', | 48 | 'Directory', 'FileSize', 'FileModifyDate', |
| @@ -65,7 +65,7 @@ class PNGParser(__ImageParser): | |||
| 65 | return True | 65 | return True |
| 66 | 66 | ||
| 67 | 67 | ||
| 68 | class GdkPixbufAbstractParser(__ImageParser): | 68 | class GdkPixbufAbstractParser(_ImageParser): |
| 69 | """ GdkPixbuf can handle a lot of surfaces, so we're rending images on it, | 69 | """ GdkPixbuf can handle a lot of surfaces, so we're rending images on it, |
| 70 | this has the side-effect of removing metadata completely. | 70 | this has the side-effect of removing metadata completely. |
| 71 | """ | 71 | """ |
diff --git a/libmat2/office.py b/libmat2/office.py index acd8ca2..eae84f7 100644 --- a/libmat2/office.py +++ b/libmat2/office.py | |||
| @@ -26,7 +26,7 @@ def _parse_xml(full_path: str): | |||
| 26 | ns = parse_map(full_path) | 26 | ns = parse_map(full_path) |
| 27 | 27 | ||
| 28 | # Register the namespaces | 28 | # Register the namespaces |
| 29 | for k,v in ns.items(): | 29 | for k, v in ns.items(): |
| 30 | ET.register_namespace(k, v) | 30 | ET.register_namespace(k, v) |
| 31 | 31 | ||
| 32 | return ET.parse(full_path), ns | 32 | return ET.parse(full_path), ns |
| @@ -35,11 +35,11 @@ def _parse_xml(full_path: str): | |||
| 35 | class ArchiveBasedAbstractParser(abstract.AbstractParser): | 35 | class ArchiveBasedAbstractParser(abstract.AbstractParser): |
| 36 | # Those are the files that have a format that _isn't_ | 36 | # Those are the files that have a format that _isn't_ |
| 37 | # supported by MAT2, but that we want to keep anyway. | 37 | # supported by MAT2, but that we want to keep anyway. |
| 38 | files_to_keep = set() # type: Set[str] | 38 | files_to_keep = set() # type: Set[str] |
| 39 | 39 | ||
| 40 | # Those are the files that we _do not_ want to keep, | 40 | # Those are the files that we _do not_ want to keep, |
| 41 | # no matter if they are supported or not. | 41 | # no matter if they are supported or not. |
| 42 | files_to_omit = set() # type: Set[Pattern] | 42 | files_to_omit = set() # type: Set[Pattern] |
| 43 | 43 | ||
| 44 | def __init__(self, filename): | 44 | def __init__(self, filename): |
| 45 | super().__init__(filename) | 45 | super().__init__(filename) |
| @@ -48,7 +48,7 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser): | |||
| 48 | except zipfile.BadZipFile: | 48 | except zipfile.BadZipFile: |
| 49 | raise ValueError | 49 | raise ValueError |
| 50 | 50 | ||
| 51 | def _specific_cleanup(self, full_path:str) -> bool: | 51 | def _specific_cleanup(self, full_path: str) -> bool: |
| 52 | """ This method can be used to apply specific treatment | 52 | """ This method can be used to apply specific treatment |
| 53 | to files present in the archive.""" | 53 | to files present in the archive.""" |
| 54 | return True | 54 | return True |
| @@ -128,19 +128,19 @@ class MSOfficeParser(ArchiveBasedAbstractParser): | |||
| 128 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation' | 128 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation' |
| 129 | } | 129 | } |
| 130 | files_to_keep = { | 130 | files_to_keep = { |
| 131 | '[Content_Types].xml', | 131 | '[Content_Types].xml', |
| 132 | '_rels/.rels', | 132 | '_rels/.rels', |
| 133 | 'word/_rels/document.xml.rels', | 133 | 'word/_rels/document.xml.rels', |
| 134 | 'word/document.xml', | 134 | 'word/document.xml', |
| 135 | 'word/fontTable.xml', | 135 | 'word/fontTable.xml', |
| 136 | 'word/settings.xml', | 136 | 'word/settings.xml', |
| 137 | 'word/styles.xml', | 137 | 'word/styles.xml', |
| 138 | } | 138 | } |
| 139 | files_to_omit = set(map(re.compile, { # type: ignore | 139 | files_to_omit = set(map(re.compile, { # type: ignore |
| 140 | '^docProps/', | 140 | '^docProps/', |
| 141 | })) | 141 | })) |
| 142 | 142 | ||
| 143 | def __remove_revisions(self, full_path:str) -> bool: | 143 | def __remove_revisions(self, full_path: str) -> bool: |
| 144 | """ In this function, we're changing the XML | 144 | """ In this function, we're changing the XML |
| 145 | document in two times, since we don't want | 145 | document in two times, since we don't want |
| 146 | to change the tree we're iterating on.""" | 146 | to change the tree we're iterating on.""" |
| @@ -152,7 +152,7 @@ class MSOfficeParser(ArchiveBasedAbstractParser): | |||
| 152 | elif tree.find('.//w:ins', ns) is None: | 152 | elif tree.find('.//w:ins', ns) is None: |
| 153 | return True | 153 | return True |
| 154 | 154 | ||
| 155 | parent_map = {c:p for p in tree.iter( ) for c in p} | 155 | parent_map = {c:p for p in tree.iter() for c in p} |
| 156 | 156 | ||
| 157 | elements = list([element for element in tree.iterfind('.//w:del', ns)]) | 157 | elements = list([element for element in tree.iterfind('.//w:del', ns)]) |
| 158 | for element in elements: | 158 | for element in elements: |
| @@ -174,7 +174,7 @@ class MSOfficeParser(ArchiveBasedAbstractParser): | |||
| 174 | 174 | ||
| 175 | return True | 175 | return True |
| 176 | 176 | ||
| 177 | def _specific_cleanup(self, full_path:str) -> bool: | 177 | def _specific_cleanup(self, full_path: str) -> bool: |
| 178 | if full_path.endswith('/word/document.xml'): | 178 | if full_path.endswith('/word/document.xml'): |
| 179 | return self.__remove_revisions(full_path) | 179 | return self.__remove_revisions(full_path) |
| 180 | return True | 180 | return True |
| @@ -214,21 +214,21 @@ class LibreOfficeParser(ArchiveBasedAbstractParser): | |||
| 214 | 'application/vnd.oasis.opendocument.image', | 214 | 'application/vnd.oasis.opendocument.image', |
| 215 | } | 215 | } |
| 216 | files_to_keep = { | 216 | files_to_keep = { |
| 217 | 'META-INF/manifest.xml', | 217 | 'META-INF/manifest.xml', |
| 218 | 'content.xml', | 218 | 'content.xml', |
| 219 | 'manifest.rdf', | 219 | 'manifest.rdf', |
| 220 | 'mimetype', | 220 | 'mimetype', |
| 221 | 'settings.xml', | 221 | 'settings.xml', |
| 222 | 'styles.xml', | 222 | 'styles.xml', |
| 223 | } | 223 | } |
| 224 | files_to_omit = set(map(re.compile, { # type: ignore | 224 | files_to_omit = set(map(re.compile, { # type: ignore |
| 225 | '^meta\.xml$', | 225 | r'^meta\.xml$', |
| 226 | '^Configurations2/', | 226 | '^Configurations2/', |
| 227 | '^Thumbnails/', | 227 | '^Thumbnails/', |
| 228 | })) | 228 | })) |
| 229 | 229 | ||
| 230 | 230 | ||
| 231 | def __remove_revisions(self, full_path:str) -> bool: | 231 | def __remove_revisions(self, full_path: str) -> bool: |
| 232 | tree, ns = _parse_xml(full_path) | 232 | tree, ns = _parse_xml(full_path) |
| 233 | 233 | ||
| 234 | if 'office' not in ns.keys(): # no revisions in the current file | 234 | if 'office' not in ns.keys(): # no revisions in the current file |
| @@ -242,7 +242,7 @@ class LibreOfficeParser(ArchiveBasedAbstractParser): | |||
| 242 | 242 | ||
| 243 | return True | 243 | return True |
| 244 | 244 | ||
| 245 | def _specific_cleanup(self, full_path:str) -> bool: | 245 | def _specific_cleanup(self, full_path: str) -> bool: |
| 246 | if os.path.basename(full_path) == 'content.xml': | 246 | if os.path.basename(full_path) == 'content.xml': |
| 247 | return self.__remove_revisions(full_path) | 247 | return self.__remove_revisions(full_path) |
| 248 | return True | 248 | return True |
diff --git a/libmat2/parser_factory.py b/libmat2/parser_factory.py index 9f4740b..7d4f43f 100644 --- a/libmat2/parser_factory.py +++ b/libmat2/parser_factory.py | |||
| @@ -37,10 +37,10 @@ def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]: | |||
| 37 | if extension in unsupported_extensions: | 37 | if extension in unsupported_extensions: |
| 38 | return None, mtype | 38 | return None, mtype |
| 39 | 39 | ||
| 40 | for c in _get_parsers(): # type: ignore | 40 | for parser_class in _get_parsers(): # type: ignore |
| 41 | if mtype in c.mimetypes: | 41 | if mtype in parser_class.mimetypes: |
| 42 | try: | 42 | try: |
| 43 | return c(filename), mtype | 43 | return parser_class(filename), mtype |
| 44 | except ValueError: | 44 | except ValueError: |
| 45 | return None, mtype | 45 | return None, mtype |
| 46 | return None, mtype | 46 | return None, mtype |
diff --git a/libmat2/pdf.py b/libmat2/pdf.py index 300fd4a..fa7f764 100644 --- a/libmat2/pdf.py +++ b/libmat2/pdf.py | |||
| @@ -83,7 +83,9 @@ class PDFParser(abstract.AbstractParser): | |||
| 83 | page_width, page_height = page.get_size() | 83 | page_width, page_height = page.get_size() |
| 84 | logging.info("Rendering page %d/%d", pagenum + 1, pages_count) | 84 | logging.info("Rendering page %d/%d", pagenum + 1, pages_count) |
| 85 | 85 | ||
| 86 | img_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(page_width) * self.__scale, int(page_height) * self.__scale) | 86 | width = int(page_width) * self.__scale |
| 87 | height = int(page_height) * self.__scale | ||
| 88 | img_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) | ||
| 87 | img_context = cairo.Context(img_surface) | 89 | img_context = cairo.Context(img_surface) |
| 88 | 90 | ||
| 89 | img_context.scale(self.__scale, self.__scale) | 91 | img_context.scale(self.__scale, self.__scale) |
diff --git a/libmat2/torrent.py b/libmat2/torrent.py index b598065..ad49f47 100644 --- a/libmat2/torrent.py +++ b/libmat2/torrent.py | |||
| @@ -125,7 +125,7 @@ class _BencodeHandler(object): | |||
| 125 | try: | 125 | try: |
| 126 | r, l = self.__decode_func[s[0]](s) | 126 | r, l = self.__decode_func[s[0]](s) |
| 127 | except (IndexError, KeyError, ValueError) as e: | 127 | except (IndexError, KeyError, ValueError) as e: |
| 128 | logging.debug("Not a valid bencoded string: %s" % e) | 128 | logging.debug("Not a valid bencoded string: %s", e) |
| 129 | return None | 129 | return None |
| 130 | if l != b'': | 130 | if l != b'': |
| 131 | logging.debug("Invalid bencoded value (data after valid prefix)") | 131 | logging.debug("Invalid bencoded value (data after valid prefix)") |
