summaryrefslogtreecommitdiff
path: root/mat
diff options
context:
space:
mode:
authorjvoisin2015-07-25 17:14:23 +0200
committerjvoisin2015-07-25 17:14:23 +0200
commit6ba3e3f20d7d52895bc44f9fc35b068cfce47133 (patch)
tree15df2aca17d56d941c6376ef729e0c1fea4c396f /mat
parent85e6279d16af063e5150c7cf4bd491185b8ae788 (diff)
_MASSIVE_ pep8 revamp
Thank you so much PyCharm
Diffstat (limited to 'mat')
-rwxr-xr-xmat51
1 files changed, 25 insertions, 26 deletions
diff --git a/mat b/mat
index 18dd456..f66db41 100755
--- a/mat
+++ b/mat
@@ -1,50 +1,48 @@
1#!/usr/bin/env python 1#!/usr/bin/env python
2''' 2"""
3 Metadata anonymisation toolkit - CLI edition 3 Metadata anonymisation toolkit - CLI edition
4''' 4"""
5 5
6import sys 6import sys
7import xml.sax
8import argparse 7import argparse
9import os 8import os
10 9
11import hachoir_core 10import hachoir_core
12 11
13from libmat import mat 12from libmat import mat
14from libmat import strippers
15from libmat import archive 13from libmat import archive
16 14
17 15
18def parse(): 16def parse():
19 ''' Get, and parse options passed to the program 17 """ Get, and parse options passed to the program
20 ''' 18 """
21 parser = argparse.ArgumentParser(description='Metadata anonymisation toolkit') 19 parser = argparse.ArgumentParser(description='Metadata anonymisation toolkit')
22 parser.add_argument('files', nargs='*') 20 parser.add_argument('files', nargs='*')
23 21
24 options = parser.add_argument_group('Options') 22 options = parser.add_argument_group('Options')
25 options.add_argument('-a', '--add2archive', action='store_true', 23 options.add_argument('-a', '--add2archive', action='store_true',
26 help='add to output archive non-supported filetypes (Off by default)') 24 help='add to output archive non-supported filetypes (Off by default)')
27 options.add_argument('-b', '--backup', '-b', action='store_true', 25 options.add_argument('-b', '--backup', '-b', action='store_true',
28 help='keep a backup copy') 26 help='keep a backup copy')
29 options.add_argument('-L', '--low-pdf-quality', '-L', action='store_true', 27 options.add_argument('-L', '--low-pdf-quality', '-L', action='store_true',
30 help='produces a lighter, but lower quality PDF') 28 help='produces a lighter, but lower quality PDF')
31 29
32 info = parser.add_argument_group('Information') 30 info = parser.add_argument_group('Information')
33 info.add_argument('-c', '--check', action='store_true', 31 info.add_argument('-c', '--check', action='store_true',
34 help='check if a file is free of harmful metadatas') 32 help='check if a file is free of harmful metadatas')
35 info.add_argument('-d', '--display', action='store_true', 33 info.add_argument('-d', '--display', action='store_true',
36 help='list all the harmful metadata of a file without removing them') 34 help='list all the harmful metadata of a file without removing them')
37 info.add_argument('-l', '--list', action='store_true', 35 info.add_argument('-l', '--list', action='store_true',
38 help='list all supported fileformats') 36 help='list all supported fileformats')
39 info.add_argument('-v', '--version', action='version', 37 info.add_argument('-v', '--version', action='version',
40 version='MAT %s - Hachoir %s' % (mat.__version__, hachoir_core.__version__)) 38 version='MAT %s - Hachoir %s' % (mat.__version__, hachoir_core.__version__))
41 39
42 return parser.parse_args() 40 return parser.parse_args()
43 41
44 42
45def list_meta(class_file, filename, add2archive): 43def list_meta(class_file, filename, add2archive):
46 ''' Print all the metadata of 'filename' on stdout 44 """ Print all the metadata of 'filename' on stdout
47 ''' 45 """
48 print('[+] File %s :' % filename) 46 print('[+] File %s :' % filename)
49 if class_file.is_clean(): 47 if class_file.is_clean():
50 print('No harmful metadata found') 48 print('No harmful metadata found')
@@ -58,7 +56,7 @@ def list_meta(class_file, filename, add2archive):
58 56
59 57
60def is_clean(class_file, filename, add2archive): 58def is_clean(class_file, filename, add2archive):
61 ''' Tell if 'filename' is clean or not ''' 59 """ Tell if 'filename' is clean or not """
62 if class_file.is_clean(): 60 if class_file.is_clean():
63 print('[+] %s is clean' % filename) 61 print('[+] %s is clean' % filename)
64 else: 62 else:
@@ -67,7 +65,7 @@ def is_clean(class_file, filename, add2archive):
67 65
68 66
69def clean_meta(class_file, filename, add2archive): 67def clean_meta(class_file, filename, add2archive):
70 ''' Clean the file 'filename' ''' 68 """ Clean the file 'filename' """
71 if not class_file.is_writable: 69 if not class_file.is_writable:
72 print('[-] %s is not writable' % filename) 70 print('[-] %s is not writable' % filename)
73 return 1 71 return 1
@@ -78,8 +76,8 @@ def clean_meta(class_file, filename, add2archive):
78 if is_archive and not is_terminal: 76 if is_archive and not is_terminal:
79 unsupported_list = class_file.list_unsupported() 77 unsupported_list = class_file.list_unsupported()
80 if type(unsupported_list) == list and unsupported_list: 78 if type(unsupported_list) == list and unsupported_list:
81 print('[-] Can not clean: %s.'\ 79 print('[-] Can not clean: %s.'
82 'It contains unsupported filetypes:' % filename) 80 'It contains unsupported filetypes:' % filename)
83 for i in unsupported_list: 81 for i in unsupported_list:
84 print('- %s' % i) 82 print('- %s' % i)
85 return 1 83 return 1
@@ -92,7 +90,7 @@ def clean_meta(class_file, filename, add2archive):
92 90
93 91
94def list_supported(): 92def list_supported():
95 ''' Print all supported fileformat, and exit ''' 93 """ Print all supported fileformat """
96 for item in mat.list_supported_formats(): 94 for item in mat.list_supported_formats():
97 print('%s (%s)' % (item['name'], item['extension'])) 95 print('%s (%s)' % (item['name'], item['extension']))
98 print('\tsupport: %s' % item['support']) 96 print('\tsupport: %s' % item['support'])
@@ -100,20 +98,20 @@ def list_supported():
100 print('\tmethod: %s' % item['method']) 98 print('\tmethod: %s' % item['method'])
101 print('\tremaining: %s' % item['remaining']) 99 print('\tremaining: %s' % item['remaining'])
102 print('\n') 100 print('\n')
103 sys.exit(0)
104 101
105 102
106def main(): 103def main():
107 ''' Main function: get args and launch the appropriate function ''' 104 """ Main function: get args and launch the appropriate function """
108 args = parse() 105 args = parse()
109 106
110 #func receives the function corresponding to the options given as parameters 107 # func receives the function corresponding to the options given as parameters
111 if args.display: # only print metadatas 108 if args.display: # only print metadatas
112 func = list_meta 109 func = list_meta
113 elif args.check: # only check if the file is clean 110 elif args.check: # only check if the file is clean
114 func = is_clean 111 func = is_clean
115 elif args.list: # print the list of all supported format 112 elif args.list: # print the list of all supported format
116 list_supported() 113 list_supported()
114 sys.exit(0)
117 else: # clean the file 115 else: # clean the file
118 func = clean_meta 116 func = clean_meta
119 117
@@ -125,12 +123,12 @@ def main():
125 filename = args.files.pop() 123 filename = args.files.pop()
126 if os.path.isdir(filename): 124 if os.path.isdir(filename):
127 for root, sub, files in os.walk(filename): 125 for root, sub, files in os.walk(filename):
128 for file in files: 126 for fname in files:
129 args.files.append(os.path.join(root, file)) 127 args.files.append(os.path.join(root, fname))
130 continue 128 continue
131 129
132 class_file = mat.create_class_file(filename, args.backup, 130 class_file = mat.create_class_file(filename, args.backup,
133 add2archive=args.add2archive, low_pdf_quality=args.low_pdf_quality) 131 add2archive=args.add2archive, low_pdf_quality=args.low_pdf_quality)
134 if class_file: 132 if class_file:
135 ret += func(class_file, filename, args.add2archive) 133 ret += func(class_file, filename, args.add2archive)
136 else: 134 else:
@@ -138,5 +136,6 @@ def main():
138 print('[-] Unable to process %s' % filename) 136 print('[-] Unable to process %s' % filename)
139 sys.exit(ret) 137 sys.exit(ret)
140 138
139
141if __name__ == '__main__': 140if __name__ == '__main__':
142 main() 141 main()