diff options
| author | jvoisin | 2011-06-10 01:29:29 +0200 |
|---|---|---|
| committer | jvoisin | 2011-06-10 01:29:29 +0200 |
| commit | c308cf7daaa4fa46377e2df0f2e9a397981e19b2 (patch) | |
| tree | f016ce17cd6747acc068a7d2fc5093d1bd96fa9e /lib | |
| parent | f7082a21d6511c5069fbb9ff186ce22f3e22fed7 (diff) | |
The current version is (mostly) working
Diffstat (limited to '')
| -rw-r--r-- | clitest.py (renamed from lib/test.py) | 40 | ||||
| -rw-r--r-- | hachoir_editor/__init__.py (renamed from lib/hachoir_editor/__init__.py) | 0 | ||||
| -rw-r--r-- | hachoir_editor/field.py (renamed from lib/hachoir_editor/field.py) | 0 | ||||
| -rw-r--r-- | hachoir_editor/fieldset.py (renamed from lib/hachoir_editor/fieldset.py) | 0 | ||||
| -rw-r--r-- | hachoir_editor/typed_field.py (renamed from lib/hachoir_editor/typed_field.py) | 0 | ||||
| -rw-r--r-- | lib/check/images/jpg.py | 17 | ||||
| -rw-r--r-- | lib/mat.py | 104 | ||||
| -rw-r--r-- | lib/strippers.py | 3 | ||||
| -rw-r--r-- | libtest.py | 56 |
9 files changed, 68 insertions, 152 deletions
| @@ -1,45 +1,29 @@ | |||
| 1 | import mat | 1 | import cli |
| 2 | import unittest | 2 | import unittest |
| 3 | import shutil | 3 | import test |
| 4 | import glob | ||
| 5 | import tempfile | ||
| 6 | 4 | ||
| 7 | FILE_LIST = zip(glob.glob('clean*'), glob.glob('dirty*')) | 5 | import shlex |
| 6 | import subprocess | ||
| 8 | 7 | ||
| 9 | class MATTest(unittest.TestCase): | 8 | class Test_Remove_cli(test.MATTest): |
| 10 | def setUp(self): | ||
| 11 | '''create working copy of the clean and the dirty file in the TMP dir''' | ||
| 12 | self.file_list = [] | ||
| 13 | self.tmpdir = tempfile.mkdtemp() | ||
| 14 | |||
| 15 | for clean, dirty in FILE_LIST: | ||
| 16 | shutil.copy2(clean, self.tmpdir + clean) | ||
| 17 | shutil.copy2(dirty, self.tmpdir + dirty) | ||
| 18 | self.file_list.append((self.tmpdir + clean, self.tmpdir + dirty)) | ||
| 19 | |||
| 20 | def tearDown(self): | ||
| 21 | '''Remove the tmp folder''' | ||
| 22 | shutil.rmtree(self.tmpdir) | ||
| 23 | |||
| 24 | class Test_Remove(MATTest): | ||
| 25 | def test_remove(self): | 9 | def test_remove(self): |
| 26 | '''make sure that the lib remove all compromizing meta''' | 10 | '''make sure that the cli remove all compromizing meta''' |
| 27 | for clean, dirty in self.file_list: | 11 | for clean, dirty in self.file_list: |
| 28 | mat.file(dirty).remove_all() | 12 | subprocess.call("cli.py %s" dirty) |
| 29 | self.assertTrue(mat.file(dirty).is_clean()) | 13 | self.assertTrue(mat.file(dirty).is_clean()) |
| 30 | 14 | ||
| 31 | def test_remove_empty(self): | 15 | def test_remove_empty(self): |
| 32 | '''Test removal with clean files''' | 16 | '''Test removal with clean files''' |
| 33 | for clean, dirty in self.file_list: | 17 | for clean, dirty in self.file_list: |
| 34 | mat.file(clean).remove_all() | 18 | subprocess.call("cli.py %s" clean) |
| 35 | self.assertTrue(mat.file(clean).is_clean()) | 19 | self.assertTrue(mat.file(dirty).is_clean()) |
| 36 | 20 | ||
| 37 | 21 | ||
| 38 | class Test_List(MATTest): | 22 | class Test_List_cli(test.MATTest): |
| 39 | def test_list(self): | 23 | def test_list(self): |
| 40 | '''check if get_meta returns all the expected meta''' | 24 | '''check if get_meta returns all the expected meta''' |
| 41 | for clean, dirty in self.file_list: | 25 | for clean, dirty in self.file_list: |
| 42 | meta_list = dict() #FIXME | 26 | meta_list = dict("fixme":"please",) #FIXME |
| 43 | self.assertDictEqual(mat.file(dirty).get_meta(), meta_list) | 27 | self.assertDictEqual(mat.file(dirty).get_meta(), meta_list) |
| 44 | 28 | ||
| 45 | def testlist_list_empty(self): | 29 | def testlist_list_empty(self): |
| @@ -48,7 +32,7 @@ class Test_List(MATTest): | |||
| 48 | self.assertEqual(mat.file(clean).get_meta(), None) | 32 | self.assertEqual(mat.file(clean).get_meta(), None) |
| 49 | 33 | ||
| 50 | 34 | ||
| 51 | class Test_isClean(MATTest): | 35 | class Test_isClean_cli(test.MATTest): |
| 52 | def test_clean(self): | 36 | def test_clean(self): |
| 53 | '''test is_clean on clean files''' | 37 | '''test is_clean on clean files''' |
| 54 | for clean, dirty in self.file_list: | 38 | for clean, dirty in self.file_list: |
diff --git a/lib/hachoir_editor/__init__.py b/hachoir_editor/__init__.py index b106278..b106278 100644 --- a/lib/hachoir_editor/__init__.py +++ b/hachoir_editor/__init__.py | |||
diff --git a/lib/hachoir_editor/field.py b/hachoir_editor/field.py index 6b1efe3..6b1efe3 100644 --- a/lib/hachoir_editor/field.py +++ b/hachoir_editor/field.py | |||
diff --git a/lib/hachoir_editor/fieldset.py b/hachoir_editor/fieldset.py index 1669b5a..1669b5a 100644 --- a/lib/hachoir_editor/fieldset.py +++ b/hachoir_editor/fieldset.py | |||
diff --git a/lib/hachoir_editor/typed_field.py b/hachoir_editor/typed_field.py index 4abc989..4abc989 100644 --- a/lib/hachoir_editor/typed_field.py +++ b/hachoir_editor/typed_field.py | |||
diff --git a/lib/check/images/jpg.py b/lib/check/images/jpg.py deleted file mode 100644 index 7f29587..0000000 --- a/lib/check/images/jpg.py +++ /dev/null | |||
| @@ -1,17 +0,0 @@ | |||
| 1 | import hachoir_core.error | ||
| 2 | import hachoir_core.cmd_line | ||
| 3 | import hachoir_parser | ||
| 4 | import hachoir_metadata | ||
| 5 | import sys | ||
| 6 | import mat | ||
| 7 | |||
| 8 | |||
| 9 | class JpegStripper(file): | ||
| 10 | def checkField(self, field): | ||
| 11 | print(field.description) | ||
| 12 | if field.name.startswith("comment"): | ||
| 13 | return True | ||
| 14 | return field.name in ("photoshop", "exif", "adobe") | ||
| 15 | return False | ||
| 16 | |||
| 17 | |||
diff --git a/lib/mat.py b/lib/mat.py deleted file mode 100644 index d22c9ab..0000000 --- a/lib/mat.py +++ /dev/null | |||
| @@ -1,104 +0,0 @@ | |||
| 1 | import hachoir_core.error | ||
| 2 | import hachoir_core.cmd_line | ||
| 3 | import hachoir_parser | ||
| 4 | import hachoir_metadata | ||
| 5 | |||
| 6 | from strippers import * | ||
| 7 | |||
| 8 | from hachoir_editor import (createEditor, | ||
| 9 | NewFieldSet, EditableInteger, EditableBytes) | ||
| 10 | |||
| 11 | import hachoir_editor | ||
| 12 | |||
| 13 | import sys | ||
| 14 | |||
| 15 | __version__ = "0.1" | ||
| 16 | __author__ = "jvoisin" | ||
| 17 | |||
| 18 | |||
| 19 | class file(): | ||
| 20 | def __init__(self, filename): | ||
| 21 | self.metadata = {} | ||
| 22 | self.clean = False | ||
| 23 | self.editor = createEditor(self.parser) | ||
| 24 | self.filename = filename | ||
| 25 | self.filename, self.realname = hachoir_core.cmd_line.unicodeFilename( | ||
| 26 | self.filename), self.filename | ||
| 27 | self.parser = hachoir_parser.createParser(self.filename, self.realname) | ||
| 28 | |||
| 29 | if not self.parser: | ||
| 30 | print("Unable to parse file : sorry") | ||
| 31 | sys.exit(1) | ||
| 32 | |||
| 33 | try: | ||
| 34 | self.meta = hachoir_metadata.extractMetadata(self.parser) | ||
| 35 | except hachoir_core.error.HachoirError, err: | ||
| 36 | print "Metadata extraction error: %s" % unicode(err) | ||
| 37 | self.data = None | ||
| 38 | |||
| 39 | if not self.meta: | ||
| 40 | print "Unable to extract metadata" | ||
| 41 | sys.exit(1) | ||
| 42 | |||
| 43 | def is_clean(self): | ||
| 44 | ''' | ||
| 45 | Return true if the file is clean from any compromizing meta | ||
| 46 | ''' | ||
| 47 | return self.clean | ||
| 48 | |||
| 49 | def remove_all(self): | ||
| 50 | ''' | ||
| 51 | Remove all the files that are compromizing | ||
| 52 | ''' | ||
| 53 | stripEditor(self.editor, self.realname, level, not(values.quiet)) | ||
| 54 | for key, field in metadata: | ||
| 55 | if should_remove(key): | ||
| 56 | remove(self, key) | ||
| 57 | |||
| 58 | def remove(self, field): | ||
| 59 | ''' | ||
| 60 | Remove the given file | ||
| 61 | ''' | ||
| 62 | del editor[field] | ||
| 63 | return True | ||
| 64 | |||
| 65 | |||
| 66 | def get_meta(self): | ||
| 67 | '''return a dict with all the meta of the file''' | ||
| 68 | #FIXME : sooooooooooo dirty ! | ||
| 69 | for title in self.meta: | ||
| 70 | if title.values != []: #if the field is not empty | ||
| 71 | value = "" | ||
| 72 | for item in title.values: | ||
| 73 | value = item.text | ||
| 74 | self.metadata[title.key] = value | ||
| 75 | return self.metadata | ||
| 76 | |||
| 77 | def should_remove(self, field): | ||
| 78 | ''' | ||
| 79 | return True if the field is compromizing | ||
| 80 | abstract method | ||
| 81 | ''' | ||
| 82 | raise NotImplementedError() | ||
| 83 | |||
| 84 | def stripEditor(editor, filename, realname, level, verbose): | ||
| 85 | ''' | ||
| 86 | Assign a stripper to an editor | ||
| 87 | ''' | ||
| 88 | cls = editor.input.__class__ | ||
| 89 | try: | ||
| 90 | stripper_cls = strippers[cls] | ||
| 91 | except KeyError: | ||
| 92 | print "Don't have stripper for file type: %s" % editor.description | ||
| 93 | return False | ||
| 94 | stripper = stripper_cls(editor, level, verbose) | ||
| 95 | |||
| 96 | if stripper(): | ||
| 97 | output = FileOutputStream(filename, realname) | ||
| 98 | editor.writeInto(output) | ||
| 99 | |||
| 100 | else: | ||
| 101 | print _("Stripper doesn't touch the file") | ||
| 102 | return True | ||
| 103 | |||
| 104 | file(sys.argv[1]).get_meta() | ||
diff --git a/lib/strippers.py b/lib/strippers.py deleted file mode 100644 index 70d0fc7..0000000 --- a/lib/strippers.py +++ /dev/null | |||
| @@ -1,3 +0,0 @@ | |||
| 1 | strippers = { | ||
| 2 | JpegFile: JpegStripper, | ||
| 3 | } | ||
diff --git a/libtest.py b/libtest.py new file mode 100644 index 0000000..191a981 --- /dev/null +++ b/libtest.py | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | import mat | ||
| 2 | import unittest | ||
| 3 | import test | ||
| 4 | |||
| 5 | class Test_Remove_lib(test.MATTest): | ||
| 6 | def test_remove(self): | ||
| 7 | '''make sure that the lib remove all compromizing meta''' | ||
| 8 | for clean, dirty in self.file_list: | ||
| 9 | current_file = mat.create_class_file(dirty) | ||
| 10 | current_file.remove_all() | ||
| 11 | self.assertTrue(current_file.is_clean()) | ||
| 12 | |||
| 13 | def test_remove_empty(self): | ||
| 14 | '''Test removal with clean files''' | ||
| 15 | for clean, dirty in self.file_list: | ||
| 16 | current_file = mat.create_class_file(clean) | ||
| 17 | current_file.remove_all() | ||
| 18 | self.assertTrue(current_file.is_clean()) | ||
| 19 | |||
| 20 | |||
| 21 | class Test_List_lib(test.MATTest): | ||
| 22 | def test_list(self): | ||
| 23 | '''check if get_meta returns all the expected meta''' | ||
| 24 | for clean, dirty in self.file_list: | ||
| 25 | current_file = mat.create_class_file(dirty) | ||
| 26 | meta_list = dict({"fixme":"please"},) | ||
| 27 | self.assertDictEqual(current_file.get_meta(), meta_list) | ||
| 28 | |||
| 29 | def testlist_list_empty(self): | ||
| 30 | '''check that a listing of a clean file return an empty dict''' | ||
| 31 | for clean, dirty in self.file_list: | ||
| 32 | current_file = mat.create_class_file(clean) | ||
| 33 | self.assertEqual(current_file.get_meta(), dict()) #dirty, isn't it ? | ||
| 34 | |||
| 35 | |||
| 36 | class Test_isClean_lib(test.MATTest): | ||
| 37 | def test_clean(self): | ||
| 38 | '''test is_clean on clean files''' | ||
| 39 | for clean, dirty in self.file_list: | ||
| 40 | current_file = mat.create_class_file(dirty) | ||
| 41 | self.assertTrue(current_file.is_clean()) | ||
| 42 | |||
| 43 | def test_clean(self): | ||
| 44 | '''test is_clean on dirty files''' | ||
| 45 | for clean, dirty in self.file_list: | ||
| 46 | current_file = mat.create_class_file(clean) | ||
| 47 | self.assertFalse(current_file.is_clean()) | ||
| 48 | |||
| 49 | |||
| 50 | if __name__ == '__main__': | ||
| 51 | suite = unittest.TestSuite() | ||
| 52 | suite.addTest(unittest.makeSuite(Test_Remove_lib)) | ||
| 53 | suite.addTest(unittest.makeSuite(Test_List_lib)) | ||
| 54 | suite.addTest(unittest.makeSuite(Test_isClean_lib)) | ||
| 55 | unittest.TextTestRunner(verbosity=2).run(suite) | ||
| 56 | |||
