From 5715ba52f2238af513b0b87f4aa3c0158d2c84ba Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sat, 30 Jul 2011 21:47:31 +0200 Subject: Documentation, and removal of unnecessary imports --- cli.py | 11 ++++++++++- gui.py | 14 ++++++++++---- lib/archive.py | 15 +++++++++++++++ lib/audio.py | 10 +++++----- lib/images.py | 10 ++++++++++ lib/mat.py | 2 -- lib/misc.py | 3 +++ lib/office.py | 23 +++++++++-------------- lib/parser.py | 7 ++++--- test/clitest.py | 2 -- test/test.py | 22 +++++++++------------- 11 files changed, 75 insertions(+), 44 deletions(-) diff --git a/cli.py b/cli.py index bfedbf6..f72602e 100755 --- a/cli.py +++ b/cli.py @@ -12,6 +12,9 @@ __version__ = '0.1' def parse(): + ''' + Get, and parse options passed to the program + ''' parser = optparse.OptionParser(usage='%prog [options] filename') parser.add_option('--add2archive', '-a', action='store_true', default=False, help='Add to outputed archive non-supported filetypes') @@ -33,7 +36,10 @@ def parse(): return values, arguments -def display_version(*args): +def display_version(*_): + ''' + Display the program's version, and exit + ''' print('Metadata Anonymisation Toolkit version %s') % mat.__version__ print('CLI version %s') % __version__ print('Hachoir version %s') % hachoir_core.__version__ @@ -87,6 +93,9 @@ def clean_meta_ugly(class_file, filename): def main(): + ''' + main function : get args, and launch the appropriate function + ''' args, filenames = parse() #func receive the function correponding to the options given as parameters diff --git a/gui.py b/gui.py index e509935..19fff58 100644 --- a/gui.py +++ b/gui.py @@ -1,6 +1,9 @@ #!/usr/bin/env python -#from gi.repository import gtk, GObject +''' + Metadata anonymisation toolkit - GUI edition +''' + import gtk import gobject @@ -232,7 +235,7 @@ class ListStoreApp: w.set_comments('This software was coded during the GSoC 2011') w.set_website('https://gitweb.torproject.org/user/jvoisin/mat.git') w.set_website_label('Website') - w.set_authors(['Julien (jvoisin) Voisin', ]) + w.set_authors(['Julien (jvoisin) Voisin',]) w.set_program_name('Metadata Anonymistion Toolkit') click = w.run() if click: @@ -282,7 +285,7 @@ non-anonymised) file to outputed archive') if response is 0: # gtk.STOCK_OK dialog.destroy() - def invert(self, button, name): # still not better :/ + def invert(self, _, name): # still not better :/ ''' Invert a preference state ''' @@ -293,7 +296,10 @@ non-anonymised) file to outputed archive') elif name is 'backup': self.backup = not self.backup - def clear_model(self, button=None): + def clear_model(self, _): + ''' + Clear the whole list of files + ''' self.liststore.clear() def all_if_empy(self, iterator): diff --git a/lib/archive.py b/lib/archive.py index f9e4dba..108134c 100644 --- a/lib/archive.py +++ b/lib/archive.py @@ -1,3 +1,6 @@ +''' + Take care of archives formats +''' import tarfile import zipfile @@ -67,6 +70,9 @@ class ZipStripper(GenericArchiveStripper): return True def is_clean(self): + ''' + Check if the given file is clean from harmful metadata + ''' zipin = zipfile.ZipFile(self.filename, 'r') if zipin.comment != '': logging.debug('%s has a comment' % self.filename) @@ -154,6 +160,9 @@ harmless format' % item.filename) class TarStripper(GenericArchiveStripper): + ''' + Represent a tarfile archive + ''' def _remove(self, current_file): ''' remove the meta added by tar itself to the file @@ -209,6 +218,9 @@ class TarStripper(GenericArchiveStripper): return True def is_clean(self): + ''' + Check if the file is clean from harmful metadatas + ''' tarin = tarfile.open(self.filename, 'r' + self.compression) for item in tarin.getmembers(): if not self.is_file_clean(item): @@ -233,6 +245,9 @@ class TarStripper(GenericArchiveStripper): return True def get_meta(self): + ''' + Return a dict with all the meta of the file + ''' tarin = tarfile.open(self.filename, 'r' + self.compression) metadata = {} for current_file in tarin.getmembers(): diff --git a/lib/audio.py b/lib/audio.py index 73030af..f1f53ff 100644 --- a/lib/audio.py +++ b/lib/audio.py @@ -3,9 +3,9 @@ ''' try: from mutagen.flac import FLAC - from mutagen.apev2 import APEv2 + from mutagen.apev2 import APEv2File from mutagen.oggvorbis import OggVorbis -except: +except ImportError: pass @@ -70,7 +70,7 @@ class Apev2Stripper(parser.GenericParser): shutil.copy2(self.filename, self.output) self.filename = self.output - mfile = APEv2(self.filename) + mfile = APEv2File(self.filename) mfile.delete() mfile.save() @@ -78,7 +78,7 @@ class Apev2Stripper(parser.GenericParser): ''' Check if the "metadata" block is present in the file ''' - mfile = APEv2(self.filename) + mfile = APEv2File(self.filename) if mfile.tags is None: return True else: @@ -89,7 +89,7 @@ class Apev2Stripper(parser.GenericParser): Return the content of the metadata block if present ''' metadata = {} - mfile = APEv2(self.filename) + mfile = APEv2File(self.filename) if mfile.tags is None: return metadata for key, value in mfile.tags: diff --git a/lib/images.py b/lib/images.py index df3d256..9fa9999 100644 --- a/lib/images.py +++ b/lib/images.py @@ -1,3 +1,7 @@ +''' + Takes care about pictures formats +''' + import parser @@ -6,6 +10,9 @@ class JpegStripper(parser.GenericParser): Represents a .jpeg file ''' def _should_remove(self, field): + ''' + return True if the field is compromizing + ''' if field.name.startswith('comment'): return True elif field.name in ("photoshop", "exif", "adobe"): @@ -19,6 +26,9 @@ class PngStripper(parser.GenericParser): Represents a .png file ''' def _should_remove(self, field): + ''' + return True if the field is compromizing + ''' if field.name.startswith("text["): return True elif field.name is "time": diff --git a/lib/mat.py b/lib/mat.py index fa6cf96..8226c7e 100644 --- a/lib/mat.py +++ b/lib/mat.py @@ -7,7 +7,6 @@ import os import subprocess import logging -import mimetypes import hachoir_core.cmd_line import hachoir_parser @@ -102,7 +101,6 @@ def create_class_file(name, backup, add2archive): return mime = parser.mime_type - print mime if mime.startswith('application/vnd.oasis.opendocument'): mime = 'application/vnd.oasis.opendocument' # opendocument fileformat diff --git a/lib/misc.py b/lib/misc.py index f846388..acbaed8 100644 --- a/lib/misc.py +++ b/lib/misc.py @@ -31,6 +31,9 @@ class TorrentStripper(parser.GenericParser): return True def get_meta(self): + ''' + Return a dict with all the meta of the file + ''' metadata = {} for field in self.editor['root']: if self._should_remove(field): diff --git a/lib/office.py b/lib/office.py index 966a64d..2320e40 100644 --- a/lib/office.py +++ b/lib/office.py @@ -3,14 +3,9 @@ ''' import os -import mimetypes -import subprocess -import tempfile -import glob import logging import zipfile import re -import shutil from xml.etree import ElementTree try: @@ -103,13 +98,13 @@ class OpenDocumentStripper(archive.GenericArchiveStripper): zipin.getinfo('meta.xml') return False except KeyError: # no meta.xml in the file - zipin.close() - czf = archive.ZipStripper(self.filename, self.parser, - 'application/zip', self.backup, self.add2archive) - if czf.is_clean(): - return True - else: - return False + zipin.close() + czf = archive.ZipStripper(self.filename, self.parser, + 'application/zip', self.backup, self.add2archive) + if czf.is_clean(): + return True + else: + return False return True @@ -172,7 +167,7 @@ class PdfStripper(parser.GenericParser): ''' Return a dict with all the meta of the file ''' - metadata={} + metadata = {} for key in self.meta_list: if key == 'creation-date' or key == 'mod-date': #creation and modification are set to -1 @@ -181,5 +176,5 @@ class PdfStripper(parser.GenericParser): else: if self.document.get_property(key) is not None and \ self.document.get_property(key) != '': - metadata[key] = self.document.get_property(key) + metadata[key] = self.document.get_property(key) return metadata diff --git a/lib/parser.py b/lib/parser.py index 385dd78..044ef0a 100644 --- a/lib/parser.py +++ b/lib/parser.py @@ -6,7 +6,6 @@ import hachoir_core import hachoir_editor import os -import mimetypes import mat @@ -14,6 +13,9 @@ NOMETA = ('.bmp', 'html', '.py', '.rdf', '.txt', '.xml') class GenericParser(object): + ''' + Parent class of all parsers + ''' def __init__(self, filename, parser, mime, backup, add2archive): self.filename = '' self.parser = parser @@ -30,7 +32,6 @@ class GenericParser(object): self.basename = os.path.basename(filename) # only filename - def is_clean(self): ''' Check if the file is clean from harmful metadatas @@ -68,7 +69,7 @@ class GenericParser(object): def get_meta(self): ''' - return a dict with all the meta of the file + Return a dict with all the meta of the file ''' metadata = {} for field in self.editor: diff --git a/test/clitest.py b/test/clitest.py index f71a622..453333a 100644 --- a/test/clitest.py +++ b/test/clitest.py @@ -8,7 +8,6 @@ import subprocess import sys sys.path.append('..') -#import cli from lib import mat import test @@ -57,7 +56,6 @@ class TestisCleancli(test.MATTest): ''' check if cli.py correctly check if a file is clean or not ''' - #FIXME : use an external file with string as const ? def test_clean(self): '''test is_clean on clean files''' for clean, _ in self.file_list: diff --git a/test/test.py b/test/test.py index ac63f60..227170b 100644 --- a/test/test.py +++ b/test/test.py @@ -12,32 +12,28 @@ import sys import tempfile import unittest -sys.path.append('..') -from lib import mat - VERBOSITY = 3 FILE_LIST = zip(glob.glob('clean*'), glob.glob('dirty*')) class MATTest(unittest.TestCase): - def setUp(self): - ''' - Create working copy of the clean and the dirty file in the TMP dir ''' + Parent class of all test-functions + ''' + def setUp(self): + ''' + Create working copy of the clean and the dirty file in the TMP dir + ''' self.file_list = [] self.tmpdir = tempfile.mkdtemp() for clean, dirty in FILE_LIST: shutil.copy2(clean, self.tmpdir + os.sep + clean) shutil.copy2(dirty, self.tmpdir + os.sep + dirty) - self.file_list.append((self.tmpdir + os.sep + clean, self.tmpdir + os.sep + dirty)) def tearDown(self): - ''' - Remove the tmp folder - ''' - for clean, dirty in self.file_list: - mat.secure_remove(clean) - mat.secure_remove(dirty) + ''' + Remove the tmp folder + ''' shutil.rmtree(self.tmpdir) -- cgit v1.3