diff options
| author | jvoisin | 2016-03-28 00:06:52 +0200 |
|---|---|---|
| committer | jvoisin | 2016-03-28 00:06:52 +0200 |
| commit | 98fb7fe1f0edec16ecd405707cc903d2b4a7dc40 (patch) | |
| tree | a4381c8f10d8d473a26ac878a27c67153d645373 /libmat | |
| parent | e56992e8e0ef197974932cf3849f3e2fab7281df (diff) | |
First step toward python3
Diffstat (limited to 'libmat')
| -rw-r--r-- | libmat/archive.py | 27 | ||||
| -rw-r--r-- | libmat/bencode/bencode.py | 16 | ||||
| -rw-r--r-- | libmat/exiftool.py | 2 | ||||
| -rw-r--r-- | libmat/mat.py | 2 | ||||
| -rw-r--r-- | libmat/misc.py | 4 | ||||
| -rw-r--r-- | libmat/mutagenstripper.py | 3 | ||||
| -rw-r--r-- | libmat/office.py | 4 | ||||
| -rw-r--r-- | libmat/parser.py | 15 | ||||
| -rw-r--r-- | libmat/strippers.py | 20 |
9 files changed, 53 insertions, 40 deletions
diff --git a/libmat/archive.py b/libmat/archive.py index a662e61..12ca55e 100644 --- a/libmat/archive.py +++ b/libmat/archive.py | |||
| @@ -9,8 +9,7 @@ import tarfile | |||
| 9 | import tempfile | 9 | import tempfile |
| 10 | import zipfile | 10 | import zipfile |
| 11 | 11 | ||
| 12 | import mat | 12 | from libmat import parser |
| 13 | import parser | ||
| 14 | 13 | ||
| 15 | # Zip files do not support dates older than 01/01/1980 | 14 | # Zip files do not support dates older than 01/01/1980 |
| 16 | ZIP_EPOCH = (1980, 1, 1, 0, 0, 0) | 15 | ZIP_EPOCH = (1980, 1, 1, 0, 0, 0) |
| @@ -20,6 +19,9 @@ class GenericArchiveStripper(parser.GenericParser): | |||
| 20 | """ Represent a generic archive | 19 | """ Represent a generic archive |
| 21 | """ | 20 | """ |
| 22 | 21 | ||
| 22 | def get_meta(self): | ||
| 23 | raise NotImplementedError | ||
| 24 | |||
| 23 | def __init__(self, filename, mime, backup, is_writable, **kwargs): | 25 | def __init__(self, filename, mime, backup, is_writable, **kwargs): |
| 24 | super(GenericArchiveStripper, self).__init__(filename, mime, backup, is_writable, **kwargs) | 26 | super(GenericArchiveStripper, self).__init__(filename, mime, backup, is_writable, **kwargs) |
| 25 | self.compression = '' | 27 | self.compression = '' |
| @@ -32,8 +34,9 @@ class GenericArchiveStripper(parser.GenericParser): | |||
| 32 | """ | 34 | """ |
| 33 | for root, _, files in os.walk(self.tempdir): | 35 | for root, _, files in os.walk(self.tempdir): |
| 34 | for item in files: | 36 | for item in files: |
| 37 | from libmat.mat import secure_remove | ||
| 35 | path_file = os.path.join(root, item) | 38 | path_file = os.path.join(root, item) |
| 36 | mat.secure_remove(path_file) | 39 | secure_remove(path_file) |
| 37 | shutil.rmtree(self.tempdir) | 40 | shutil.rmtree(self.tempdir) |
| 38 | 41 | ||
| 39 | def is_clean(self, list_unsupported=False): | 42 | def is_clean(self, list_unsupported=False): |
| @@ -90,7 +93,8 @@ class ZipStripper(GenericArchiveStripper): | |||
| 90 | logging.debug('%s from %s has compromising zipinfo', item.filename, self.filename) | 93 | logging.debug('%s from %s has compromising zipinfo', item.filename, self.filename) |
| 91 | return False | 94 | return False |
| 92 | if os.path.isfile(path): | 95 | if os.path.isfile(path): |
| 93 | cfile = mat.create_class_file(path, False, add2archive=self.add2archive) | 96 | from libmat.mat import create_class_file |
| 97 | cfile = create_class_file(path, False, add2archive=self.add2archive) | ||
| 94 | if cfile is not None: | 98 | if cfile is not None: |
| 95 | if not cfile.is_clean(): | 99 | if not cfile.is_clean(): |
| 96 | logging.debug('%s from %s has metadata', item.filename, self.filename) | 100 | logging.debug('%s from %s has metadata', item.filename, self.filename) |
| @@ -122,7 +126,8 @@ class ZipStripper(GenericArchiveStripper): | |||
| 122 | zipin.extract(item, self.tempdir) | 126 | zipin.extract(item, self.tempdir) |
| 123 | path = os.path.join(self.tempdir, item.filename) | 127 | path = os.path.join(self.tempdir, item.filename) |
| 124 | if os.path.isfile(path): | 128 | if os.path.isfile(path): |
| 125 | cfile = mat.create_class_file(path, False, add2archive=self.add2archive) | 129 | from libmat.mat import create_class_file |
| 130 | cfile = create_class_file(path, False, add2archive=self.add2archive) | ||
| 126 | if cfile is not None: | 131 | if cfile is not None: |
| 127 | cfile_meta = cfile.get_meta() | 132 | cfile_meta = cfile.get_meta() |
| 128 | if cfile_meta != {}: | 133 | if cfile_meta != {}: |
| @@ -172,7 +177,8 @@ class ZipStripper(GenericArchiveStripper): | |||
| 172 | ending = any((True for f in ending_blacklist if item.filename.endswith(f))) | 177 | ending = any((True for f in ending_blacklist if item.filename.endswith(f))) |
| 173 | 178 | ||
| 174 | if os.path.isfile(path) and not beginning and not ending: | 179 | if os.path.isfile(path) and not beginning and not ending: |
| 175 | cfile = mat.create_class_file(path, False, add2archive=self.add2archive) | 180 | from libmat.mat import create_class_file |
| 181 | cfile = create_class_file(path, False, add2archive=self.add2archive) | ||
| 176 | if cfile is not None: | 182 | if cfile is not None: |
| 177 | # Handle read-only files inside archive | 183 | # Handle read-only files inside archive |
| 178 | old_stat = os.stat(path).st_mode | 184 | old_stat = os.stat(path).st_mode |
| @@ -231,7 +237,8 @@ class TarStripper(GenericArchiveStripper): | |||
| 231 | tarin.extract(item, self.tempdir) | 237 | tarin.extract(item, self.tempdir) |
| 232 | if item.isfile(): | 238 | if item.isfile(): |
| 233 | path = os.path.join(self.tempdir, item.name) | 239 | path = os.path.join(self.tempdir, item.name) |
| 234 | cfile = mat.create_class_file(path, False, add2archive=self.add2archive) | 240 | from libmat.mat import create_class_file |
| 241 | cfile = create_class_file(path, False, add2archive=self.add2archive) | ||
| 235 | if cfile is not None: | 242 | if cfile is not None: |
| 236 | # Handle read-only files inside archive | 243 | # Handle read-only files inside archive |
| 237 | old_stat = os.stat(path).st_mode | 244 | old_stat = os.stat(path).st_mode |
| @@ -286,7 +293,8 @@ class TarStripper(GenericArchiveStripper): | |||
| 286 | tarin.extract(item, self.tempdir) | 293 | tarin.extract(item, self.tempdir) |
| 287 | path = os.path.join(self.tempdir, item.name) | 294 | path = os.path.join(self.tempdir, item.name) |
| 288 | if item.isfile(): | 295 | if item.isfile(): |
| 289 | cfile = mat.create_class_file(path, False, add2archive=self.add2archive) | 296 | from libmat.mat import create_class_file |
| 297 | cfile = create_class_file(path, False, add2archive=self.add2archive) | ||
| 290 | if cfile is not None: | 298 | if cfile is not None: |
| 291 | if not cfile.is_clean(): | 299 | if not cfile.is_clean(): |
| 292 | logging.debug('%s from %s has metadata', item.name.decode("utf8"), self.filename) | 300 | logging.debug('%s from %s has metadata', item.name.decode("utf8"), self.filename) |
| @@ -316,7 +324,8 @@ class TarStripper(GenericArchiveStripper): | |||
| 316 | if item.isfile(): | 324 | if item.isfile(): |
| 317 | tarin.extract(item, self.tempdir) | 325 | tarin.extract(item, self.tempdir) |
| 318 | path = os.path.join(self.tempdir, item.name) | 326 | path = os.path.join(self.tempdir, item.name) |
| 319 | class_file = mat.create_class_file(path, False, add2archive=self.add2archive) | 327 | from libmat.mat import create_class_file |
| 328 | class_file = create_class_file(path, False, add2archive=self.add2archive) | ||
| 320 | if class_file is not None: | 329 | if class_file is not None: |
| 321 | meta = class_file.get_meta() | 330 | meta = class_file.get_meta() |
| 322 | if meta: | 331 | if meta: |
diff --git a/libmat/bencode/bencode.py b/libmat/bencode/bencode.py index afbd360..15245b5 100644 --- a/libmat/bencode/bencode.py +++ b/libmat/bencode/bencode.py | |||
| @@ -113,15 +113,13 @@ DECODE_FUNC['l'] = decode_list | |||
| 113 | DECODE_FUNC['d'] = decode_dict | 113 | DECODE_FUNC['d'] = decode_dict |
| 114 | DECODE_FUNC['i'] = decode_int | 114 | DECODE_FUNC['i'] = decode_int |
| 115 | 115 | ||
| 116 | ENCODE_FUNC = {} | 116 | ENCODE_FUNC = { |
| 117 | ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) | 117 | Bencached: lambda x, r: r.append(x.bencoded), |
| 118 | ENCODE_FUNC[int] = encode_int | 118 | int: encode_int, |
| 119 | ENCODE_FUNC[int] = encode_int | 119 | bytes: lambda x, r: r.extend((str(len(x)), ':', x)), |
| 120 | ENCODE_FUNC[bytes] = lambda x, r: r.extend((str(len(x)), ':', x)) | 120 | list: encode_list, tuple: encode_list, |
| 121 | ENCODE_FUNC[list] = encode_list | 121 | dict: encode_dict, bool: encode_bool |
| 122 | ENCODE_FUNC[tuple] = encode_list | 122 | } |
| 123 | ENCODE_FUNC[dict] = encode_dict | ||
| 124 | ENCODE_FUNC[bool] = encode_bool | ||
| 125 | 123 | ||
| 126 | 124 | ||
| 127 | def bencode(string): | 125 | def bencode(string): |
diff --git a/libmat/exiftool.py b/libmat/exiftool.py index 78550ed..efe6002 100644 --- a/libmat/exiftool.py +++ b/libmat/exiftool.py | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | """ | 2 | """ |
| 3 | 3 | ||
| 4 | import subprocess | 4 | import subprocess |
| 5 | import parser | 5 | from libmat import parser |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | class ExiftoolStripper(parser.GenericParser): | 8 | class ExiftoolStripper(parser.GenericParser): |
diff --git a/libmat/mat.py b/libmat/mat.py index 2eb9a95..bfe87ed 100644 --- a/libmat/mat.py +++ b/libmat/mat.py | |||
| @@ -19,7 +19,7 @@ __author__ = 'jvoisin' | |||
| 19 | LOGGING_LEVEL = logging.ERROR | 19 | LOGGING_LEVEL = logging.ERROR |
| 20 | logging.basicConfig(filename='', level=LOGGING_LEVEL) | 20 | logging.basicConfig(filename='', level=LOGGING_LEVEL) |
| 21 | 21 | ||
| 22 | import strippers # this is loaded here because we need LOGGING_LEVEL | 22 | from libmat import strippers # this is loaded here because we need LOGGING_LEVEL |
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | def get_logo(): # pragma: no cover | 25 | def get_logo(): # pragma: no cover |
diff --git a/libmat/misc.py b/libmat/misc.py index 20da19d..0bcb25e 100644 --- a/libmat/misc.py +++ b/libmat/misc.py | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | """ Care about misc formats | 1 | """ Care about misc formats |
| 2 | """ | 2 | """ |
| 3 | 3 | ||
| 4 | import parser | 4 | from libmat import parser |
| 5 | 5 | ||
| 6 | from bencode import bencode | 6 | from libmat.bencode import bencode |
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | class TorrentStripper(parser.GenericParser): | 9 | class TorrentStripper(parser.GenericParser): |
diff --git a/libmat/mutagenstripper.py b/libmat/mutagenstripper.py index 10831b3..a17a11a 100644 --- a/libmat/mutagenstripper.py +++ b/libmat/mutagenstripper.py | |||
| @@ -1,8 +1,9 @@ | |||
| 1 | """ Take care of mutagen-supported formats (audio) | 1 | """ Take care of mutagen-supported formats (audio) |
| 2 | """ | 2 | """ |
| 3 | 3 | ||
| 4 | import parser | 4 | from libmat import parser |
| 5 | 5 | ||
| 6 | import mutagen | ||
| 6 | from mutagen.flac import FLAC | 7 | from mutagen.flac import FLAC |
| 7 | from mutagen.oggvorbis import OggVorbis | 8 | from mutagen.oggvorbis import OggVorbis |
| 8 | from mutagen.mp3 import MP3 | 9 | from mutagen.mp3 import MP3 |
diff --git a/libmat/office.py b/libmat/office.py index e2b1915..b23ec84 100644 --- a/libmat/office.py +++ b/libmat/office.py | |||
| @@ -17,8 +17,8 @@ try: | |||
| 17 | except ImportError: | 17 | except ImportError: |
| 18 | logging.info('office.py loaded without PDF support') | 18 | logging.info('office.py loaded without PDF support') |
| 19 | 19 | ||
| 20 | import parser | 20 | from libmat import parser |
| 21 | import archive | 21 | from libmat import archive |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | class OpenDocumentStripper(archive.TerminalZipStripper): | 24 | class OpenDocumentStripper(archive.TerminalZipStripper): |
diff --git a/libmat/parser.py b/libmat/parser.py index b81b576..f7a7ea2 100644 --- a/libmat/parser.py +++ b/libmat/parser.py | |||
| @@ -6,8 +6,6 @@ import shutil | |||
| 6 | import tempfile | 6 | import tempfile |
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | import mat | ||
| 10 | |||
| 11 | NOMETA = frozenset(( | 9 | NOMETA = frozenset(( |
| 12 | '.bmp', # "raw" image | 10 | '.bmp', # "raw" image |
| 13 | '.rdf', # text | 11 | '.rdf', # text |
| @@ -35,7 +33,15 @@ class GenericParser(object): | |||
| 35 | """ Remove tempfile if it was not used | 33 | """ Remove tempfile if it was not used |
| 36 | """ | 34 | """ |
| 37 | if os.path.exists(self.output): | 35 | if os.path.exists(self.output): |
| 38 | mat.secure_remove(self.output) | 36 | from libmat.mat import secure_remove |
| 37 | secure_remove(self.output) | ||
| 38 | |||
| 39 | def get_meta(self): | ||
| 40 | """ Return a key-value representation of the file's metadata. | ||
| 41 | |||
| 42 | :ret dict key-value representation of the file's metadata. | ||
| 43 | """ | ||
| 44 | raise NotImplementedError | ||
| 39 | 45 | ||
| 40 | def is_clean(self): | 46 | def is_clean(self): |
| 41 | """ | 47 | """ |
| @@ -62,5 +68,6 @@ class GenericParser(object): | |||
| 62 | if self.backup: | 68 | if self.backup: |
| 63 | shutil.move(self.filename, os.path.join(self.filename, '.bak')) | 69 | shutil.move(self.filename, os.path.join(self.filename, '.bak')) |
| 64 | else: | 70 | else: |
| 65 | mat.secure_remove(self.filename) | 71 | from libmat.mat import secure_remove |
| 72 | secure_remove(self.filename) | ||
| 66 | shutil.move(self.output, self.filename) | 73 | shutil.move(self.output, self.filename) |
diff --git a/libmat/strippers.py b/libmat/strippers.py index 6a51aa8..2879375 100644 --- a/libmat/strippers.py +++ b/libmat/strippers.py | |||
| @@ -1,26 +1,24 @@ | |||
| 1 | """ Manage which fileformat can be processed | 1 | """ Manage which fileformat can be processed |
| 2 | """ | 2 | """ |
| 3 | 3 | ||
| 4 | import archive | 4 | from libmat.archive import TarStripper, Bzip2Stripper, GzipStripper, ZipStripper |
| 5 | import mutagenstripper | 5 | from libmat import mutagenstripper, misc, office |
| 6 | from libmat.mat import LOGGING_LEVEL | ||
| 6 | import logging | 7 | import logging |
| 7 | import mat | ||
| 8 | import misc | ||
| 9 | import office | ||
| 10 | import subprocess | 8 | import subprocess |
| 11 | 9 | ||
| 12 | STRIPPERS = { | 10 | STRIPPERS = { |
| 13 | 'application/x-tar': archive.TarStripper, | 11 | 'application/x-tar': TarStripper, |
| 14 | 'application/x-bzip2': archive.Bzip2Stripper, | 12 | 'application/x-bzip2': Bzip2Stripper, |
| 15 | 'application/x-gzip': archive.GzipStripper, | 13 | 'application/x-gzip': GzipStripper, |
| 16 | 'application/zip': archive.ZipStripper, | 14 | 'application/zip': ZipStripper, |
| 17 | 'application/x-bittorrent': misc.TorrentStripper, | 15 | 'application/x-bittorrent': misc.TorrentStripper, |
| 18 | 'application/torrent': misc.TorrentStripper, | 16 | 'application/torrent': misc.TorrentStripper, |
| 19 | 'application/opendocument': office.OpenDocumentStripper, | 17 | 'application/opendocument': office.OpenDocumentStripper, |
| 20 | 'application/officeopenxml': office.OpenXmlStripper, | 18 | 'application/officeopenxml': office.OpenXmlStripper, |
| 21 | } | 19 | } |
| 22 | 20 | ||
| 23 | logging.basicConfig(level=mat.LOGGING_LEVEL) | 21 | logging.basicConfig(level=LOGGING_LEVEL) |
| 24 | 22 | ||
| 25 | # PDF support | 23 | # PDF support |
| 26 | pdfSupport = True | 24 | pdfSupport = True |
| @@ -63,7 +61,7 @@ except ImportError: | |||
| 63 | # exiftool | 61 | # exiftool |
| 64 | try: | 62 | try: |
| 65 | subprocess.check_output(['exiftool', '-ver']) | 63 | subprocess.check_output(['exiftool', '-ver']) |
| 66 | import exiftool | 64 | from libmat import exiftool |
| 67 | STRIPPERS['image/jpeg'] = exiftool.JpegStripper | 65 | STRIPPERS['image/jpeg'] = exiftool.JpegStripper |
| 68 | STRIPPERS['image/png'] = exiftool.PngStripper | 66 | STRIPPERS['image/png'] = exiftool.PngStripper |
| 69 | STRIPPERS['image/tiff'] = exiftool.TiffStripper | 67 | STRIPPERS['image/tiff'] = exiftool.TiffStripper |
