summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libmat/archive.py27
-rw-r--r--libmat/bencode/bencode.py16
-rw-r--r--libmat/exiftool.py2
-rw-r--r--libmat/mat.py2
-rw-r--r--libmat/misc.py4
-rw-r--r--libmat/mutagenstripper.py3
-rw-r--r--libmat/office.py4
-rw-r--r--libmat/parser.py15
-rw-r--r--libmat/strippers.py20
-rwxr-xr-xmat-gui5
-rw-r--r--test/test.py2
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
9import tempfile 9import tempfile
10import zipfile 10import zipfile
11 11
12import mat 12from libmat import parser
13import 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
16ZIP_EPOCH = (1980, 1, 1, 0, 0, 0) 15ZIP_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
113DECODE_FUNC['d'] = decode_dict 113DECODE_FUNC['d'] = decode_dict
114DECODE_FUNC['i'] = decode_int 114DECODE_FUNC['i'] = decode_int
115 115
116ENCODE_FUNC = {} 116ENCODE_FUNC = {
117ENCODE_FUNC[Bencached] = lambda x, r: r.append(x.bencoded) 117 Bencached: lambda x, r: r.append(x.bencoded),
118ENCODE_FUNC[int] = encode_int 118 int: encode_int,
119ENCODE_FUNC[int] = encode_int 119 bytes: lambda x, r: r.extend((str(len(x)), ':', x)),
120ENCODE_FUNC[bytes] = lambda x, r: r.extend((str(len(x)), ':', x)) 120 list: encode_list, tuple: encode_list,
121ENCODE_FUNC[list] = encode_list 121 dict: encode_dict, bool: encode_bool
122ENCODE_FUNC[tuple] = encode_list 122}
123ENCODE_FUNC[dict] = encode_dict
124ENCODE_FUNC[bool] = encode_bool
125 123
126 124
127def bencode(string): 125def 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
4import subprocess 4import subprocess
5import parser 5from libmat import parser
6 6
7 7
8class ExiftoolStripper(parser.GenericParser): 8class 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'
19LOGGING_LEVEL = logging.ERROR 19LOGGING_LEVEL = logging.ERROR
20logging.basicConfig(filename='', level=LOGGING_LEVEL) 20logging.basicConfig(filename='', level=LOGGING_LEVEL)
21 21
22import strippers # this is loaded here because we need LOGGING_LEVEL 22from libmat import strippers # this is loaded here because we need LOGGING_LEVEL
23 23
24 24
25def get_logo(): # pragma: no cover 25def 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
4import parser 4from libmat import parser
5 5
6from bencode import bencode 6from libmat.bencode import bencode
7 7
8 8
9class TorrentStripper(parser.GenericParser): 9class 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
4import parser 4from libmat import parser
5 5
6import mutagen
6from mutagen.flac import FLAC 7from mutagen.flac import FLAC
7from mutagen.oggvorbis import OggVorbis 8from mutagen.oggvorbis import OggVorbis
8from mutagen.mp3 import MP3 9from 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:
17except ImportError: 17except ImportError:
18 logging.info('office.py loaded without PDF support') 18 logging.info('office.py loaded without PDF support')
19 19
20import parser 20from libmat import parser
21import archive 21from libmat import archive
22 22
23 23
24class OpenDocumentStripper(archive.TerminalZipStripper): 24class 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
6import tempfile 6import tempfile
7 7
8 8
9import mat
10
11NOMETA = frozenset(( 9NOMETA = 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
4import archive 4from libmat.archive import TarStripper, Bzip2Stripper, GzipStripper, ZipStripper
5import mutagenstripper 5from libmat import mutagenstripper, misc, office
6from libmat.mat import LOGGING_LEVEL
6import logging 7import logging
7import mat
8import misc
9import office
10import subprocess 8import subprocess
11 9
12STRIPPERS = { 10STRIPPERS = {
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
23logging.basicConfig(level=mat.LOGGING_LEVEL) 21logging.basicConfig(level=LOGGING_LEVEL)
24 22
25# PDF support 23# PDF support
26pdfSupport = True 24pdfSupport = True
@@ -63,7 +61,7 @@ except ImportError:
63# exiftool 61# exiftool
64try: 62try:
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
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'))
451 451
452 452
453if __name__ == '__main__': 453if __name__ == '__main__':
454 gettext.install('MAT', unicode=True) 454 try:
455 gettext.install('MAT', unicode=True)
456 except TypeError: # python 3
457 gettext.install('MAT')
455 458
456 gui = GUI() 459 gui = GUI()
457 460
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()
23dirty = glob.glob('dirty*') 23dirty = glob.glob('dirty*')
24dirty.sort() 24dirty.sort()
25 25
26FILE_LIST = zip(clean, dirty) 26FILE_LIST = list(zip(clean, dirty))
27 27
28try: # PDF render processing 28try: # PDF render processing
29 import cairo 29 import cairo