From 98fb7fe1f0edec16ecd405707cc903d2b4a7dc40 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 28 Mar 2016 00:06:52 +0200 Subject: First step toward python3 --- libmat/archive.py | 27 ++++++++++++++++++--------- libmat/bencode/bencode.py | 16 +++++++--------- libmat/exiftool.py | 2 +- libmat/mat.py | 2 +- libmat/misc.py | 4 ++-- libmat/mutagenstripper.py | 3 ++- libmat/office.py | 4 ++-- libmat/parser.py | 15 +++++++++++---- libmat/strippers.py | 20 +++++++++----------- mat-gui | 5 ++++- test/test.py | 2 +- 11 files changed, 58 insertions(+), 42 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 import tempfile import zipfile -import mat -import parser +from libmat import parser # Zip files do not support dates older than 01/01/1980 ZIP_EPOCH = (1980, 1, 1, 0, 0, 0) @@ -20,6 +19,9 @@ class GenericArchiveStripper(parser.GenericParser): """ Represent a generic archive """ + def get_meta(self): + raise NotImplementedError + def __init__(self, filename, mime, backup, is_writable, **kwargs): super(GenericArchiveStripper, self).__init__(filename, mime, backup, is_writable, **kwargs) self.compression = '' @@ -32,8 +34,9 @@ class GenericArchiveStripper(parser.GenericParser): """ for root, _, files in os.walk(self.tempdir): for item in files: + from libmat.mat import secure_remove path_file = os.path.join(root, item) - mat.secure_remove(path_file) + secure_remove(path_file) shutil.rmtree(self.tempdir) def is_clean(self, list_unsupported=False): @@ -90,7 +93,8 @@ class ZipStripper(GenericArchiveStripper): logging.debug('%s from %s has compromising zipinfo', item.filename, self.filename) return False if os.path.isfile(path): - cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + from libmat.mat import create_class_file + cfile = create_class_file(path, False, add2archive=self.add2archive) if cfile is not None: if not cfile.is_clean(): logging.debug('%s from %s has metadata', item.filename, self.filename) @@ -122,7 +126,8 @@ class ZipStripper(GenericArchiveStripper): zipin.extract(item, self.tempdir) path = os.path.join(self.tempdir, item.filename) if os.path.isfile(path): - cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + from libmat.mat import create_class_file + cfile = create_class_file(path, False, add2archive=self.add2archive) if cfile is not None: cfile_meta = cfile.get_meta() if cfile_meta != {}: @@ -172,7 +177,8 @@ class ZipStripper(GenericArchiveStripper): ending = any((True for f in ending_blacklist if item.filename.endswith(f))) if os.path.isfile(path) and not beginning and not ending: - cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + from libmat.mat import create_class_file + cfile = create_class_file(path, False, add2archive=self.add2archive) if cfile is not None: # Handle read-only files inside archive old_stat = os.stat(path).st_mode @@ -231,7 +237,8 @@ class TarStripper(GenericArchiveStripper): tarin.extract(item, self.tempdir) if item.isfile(): path = os.path.join(self.tempdir, item.name) - cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + from libmat.mat import create_class_file + cfile = create_class_file(path, False, add2archive=self.add2archive) if cfile is not None: # Handle read-only files inside archive old_stat = os.stat(path).st_mode @@ -286,7 +293,8 @@ class TarStripper(GenericArchiveStripper): tarin.extract(item, self.tempdir) path = os.path.join(self.tempdir, item.name) if item.isfile(): - cfile = mat.create_class_file(path, False, add2archive=self.add2archive) + from libmat.mat import create_class_file + cfile = create_class_file(path, False, add2archive=self.add2archive) if cfile is not None: if not cfile.is_clean(): logging.debug('%s from %s has metadata', item.name.decode("utf8"), self.filename) @@ -316,7 +324,8 @@ class TarStripper(GenericArchiveStripper): if item.isfile(): tarin.extract(item, self.tempdir) path = os.path.join(self.tempdir, item.name) - class_file = mat.create_class_file(path, False, add2archive=self.add2archive) + from libmat.mat import create_class_file + class_file = create_class_file(path, False, add2archive=self.add2archive) if class_file is not None: meta = class_file.get_meta() 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 DECODE_FUNC['d'] = decode_dict DECODE_FUNC['i'] = decode_int -ENCODE_FUNC = {} -ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) -ENCODE_FUNC[int] = encode_int -ENCODE_FUNC[int] = encode_int -ENCODE_FUNC[bytes] = lambda x, r: r.extend((str(len(x)), ':', x)) -ENCODE_FUNC[list] = encode_list -ENCODE_FUNC[tuple] = encode_list -ENCODE_FUNC[dict] = encode_dict -ENCODE_FUNC[bool] = encode_bool +ENCODE_FUNC = { + Bencached: lambda x, r: r.append(x.bencoded), + int: encode_int, + bytes: lambda x, r: r.extend((str(len(x)), ':', x)), + list: encode_list, tuple: encode_list, + dict: encode_dict, bool: encode_bool +} 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 @@ """ import subprocess -import parser +from libmat import parser 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' LOGGING_LEVEL = logging.ERROR logging.basicConfig(filename='', level=LOGGING_LEVEL) -import strippers # this is loaded here because we need LOGGING_LEVEL +from libmat import strippers # this is loaded here because we need LOGGING_LEVEL 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 @@ """ Care about misc formats """ -import parser +from libmat import parser -from bencode import bencode +from libmat.bencode import bencode 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 @@ """ Take care of mutagen-supported formats (audio) """ -import parser +from libmat import parser +import mutagen from mutagen.flac import FLAC from mutagen.oggvorbis import OggVorbis 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: except ImportError: logging.info('office.py loaded without PDF support') -import parser -import archive +from libmat import parser +from libmat import archive 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 import tempfile -import mat - NOMETA = frozenset(( '.bmp', # "raw" image '.rdf', # text @@ -35,7 +33,15 @@ class GenericParser(object): """ Remove tempfile if it was not used """ if os.path.exists(self.output): - mat.secure_remove(self.output) + from libmat.mat import secure_remove + secure_remove(self.output) + + def get_meta(self): + """ Return a key-value representation of the file's metadata. + + :ret dict key-value representation of the file's metadata. + """ + raise NotImplementedError def is_clean(self): """ @@ -62,5 +68,6 @@ class GenericParser(object): if self.backup: shutil.move(self.filename, os.path.join(self.filename, '.bak')) else: - mat.secure_remove(self.filename) + from libmat.mat import secure_remove + secure_remove(self.filename) 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 @@ """ Manage which fileformat can be processed """ -import archive -import mutagenstripper +from libmat.archive import TarStripper, Bzip2Stripper, GzipStripper, ZipStripper +from libmat import mutagenstripper, misc, office +from libmat.mat import LOGGING_LEVEL import logging -import mat -import misc -import office import subprocess STRIPPERS = { - 'application/x-tar': archive.TarStripper, - 'application/x-bzip2': archive.Bzip2Stripper, - 'application/x-gzip': archive.GzipStripper, - 'application/zip': archive.ZipStripper, + 'application/x-tar': TarStripper, + 'application/x-bzip2': Bzip2Stripper, + 'application/x-gzip': GzipStripper, + 'application/zip': ZipStripper, 'application/x-bittorrent': misc.TorrentStripper, 'application/torrent': misc.TorrentStripper, 'application/opendocument': office.OpenDocumentStripper, 'application/officeopenxml': office.OpenXmlStripper, } -logging.basicConfig(level=mat.LOGGING_LEVEL) +logging.basicConfig(level=LOGGING_LEVEL) # PDF support pdfSupport = True @@ -63,7 +61,7 @@ except ImportError: # exiftool try: subprocess.check_output(['exiftool', '-ver']) - import exiftool + from libmat import exiftool STRIPPERS['image/jpeg'] = exiftool.JpegStripper STRIPPERS['image/png'] = exiftool.PngStripper STRIPPERS['image/tiff'] = exiftool.TiffStripper diff --git a/mat-gui b/mat-gui index feccbd2..fc927db 100755 --- a/mat-gui +++ b/mat-gui @@ -451,7 +451,10 @@ non-anonymised) file from output archive')) if __name__ == '__main__': - gettext.install('MAT', unicode=True) + try: + gettext.install('MAT', unicode=True) + except TypeError: # python 3 + gettext.install('MAT') gui = GUI() diff --git a/test/test.py b/test/test.py index 818909e..40ba683 100644 --- a/test/test.py +++ b/test/test.py @@ -23,7 +23,7 @@ clean.sort() dirty = glob.glob('dirty*') dirty.sort() -FILE_LIST = zip(clean, dirty) +FILE_LIST = list(zip(clean, dirty)) try: # PDF render processing import cairo -- cgit v1.3