summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin2014-06-11 09:01:24 +0200
committerjvoisin2014-06-11 09:01:24 +0200
commit9bb2a4ec6f32704746863b538f503683e7eab22f (patch)
treedf80d67c06c6fb5d4ae0ed92f3756577ef6755ab
parentcd81ab4f9f9cf4afc7dd052c1d0e282169bcdc38 (diff)
Port mat from optparse to argparse
-rwxr-xr-xmat64
1 files changed, 27 insertions, 37 deletions
diff --git a/mat b/mat
index 03a7367..18dd456 100755
--- a/mat
+++ b/mat
@@ -5,7 +5,7 @@
5 5
6import sys 6import sys
7import xml.sax 7import xml.sax
8import optparse 8import argparse
9import os 9import os
10 10
11import hachoir_core 11import hachoir_core
@@ -18,41 +18,28 @@ from libmat import archive
18def parse(): 18def parse():
19 ''' Get, and parse options passed to the program 19 ''' Get, and parse options passed to the program
20 ''' 20 '''
21 parser = optparse.OptionParser(usage='%prog [options] files\n\ 21 parser = argparse.ArgumentParser(description='Metadata anonymisation toolkit')
22The default behaviour is to clean files given in argument') 22 parser.add_argument('files', nargs='*')
23 options = optparse.OptionGroup(parser, 'Options')
24 options.add_option('--add2archive', '-a', action='store_true',
25 default=False, help='Add to output archive non-supported filetypes (Off by default)')
26 options.add_option('--backup', '-b', action='store_true', default=False,
27 help='Keep a backup copy')
28 options.add_option('--low-pdf-quality', '-L', action='store_true', default=False,
29 help='Produces a lighter, but lower quality PDF')
30 info = optparse.OptionGroup(parser, 'Informations')
31 info.add_option('--check', '-c', action='store_true', default=False,
32 help='Check if a file is free of harmful metadatas')
33 info.add_option('--display', '-d', action='store_true', default=False,
34 help='List all the harmful metadata of a file without removing them')
35 info.add_option('--list', '-l', action='store_true', default=False,
36 help='List all supported fileformats')
37 info.add_option('--version', '-v', action='callback',
38 callback=display_version, help='Display version and exit')
39 parser.add_option_group(options)
40 parser.add_option_group(info)
41 23
42 values, arguments = parser.parse_args() 24 options = parser.add_argument_group('Options')
43 if not arguments and not values.list: 25 options.add_argument('-a', '--add2archive', action='store_true',
44 # if no argument and no files are passed, 26 help='add to output archive non-supported filetypes (Off by default)')
45 # print help and exit 27 options.add_argument('-b', '--backup', '-b', action='store_true',
46 parser.print_help() 28 help='keep a backup copy')
47 return values, arguments 29 options.add_argument('-L', '--low-pdf-quality', '-L', action='store_true',
30 help='produces a lighter, but lower quality PDF')
48 31
32 info = parser.add_argument_group('Information')
33 info.add_argument('-c', '--check', action='store_true',
34 help='check if a file is free of harmful metadatas')
35 info.add_argument('-d', '--display', action='store_true',
36 help='list all the harmful metadata of a file without removing them')
37 info.add_argument('-l', '--list', action='store_true',
38 help='list all supported fileformats')
39 info.add_argument('-v', '--version', action='version',
40 version='MAT %s - Hachoir %s' % (mat.__version__, hachoir_core.__version__))
49 41
50def display_version(*_): 42 return parser.parse_args()
51 ''' Display the program's version, and exit
52 '''
53 print('Metadata Anonymisation Toolkit %s' % mat.__version__)
54 print('Hachoir %s' % hachoir_core.__version__)
55 sys.exit(0)
56 43
57 44
58def list_meta(class_file, filename, add2archive): 45def list_meta(class_file, filename, add2archive):
@@ -118,7 +105,7 @@ def list_supported():
118 105
119def main(): 106def main():
120 ''' Main function: get args and launch the appropriate function ''' 107 ''' Main function: get args and launch the appropriate function '''
121 args, filenames = parse() 108 args = parse()
122 109
123 #func receives the function corresponding to the options given as parameters 110 #func receives the function corresponding to the options given as parameters
124 if args.display: # only print metadatas 111 if args.display: # only print metadatas
@@ -131,12 +118,15 @@ def main():
131 func = clean_meta 118 func = clean_meta
132 119
133 ret = 0 120 ret = 0
134 while filenames: 121 # We're using a while loop, instead of a for,
135 filename = filenames.pop() 122 # because we support folders. This allow us
123 # to add their content, and to process it.
124 while args.files:
125 filename = args.files.pop()
136 if os.path.isdir(filename): 126 if os.path.isdir(filename):
137 for root, sub, files in os.walk(filename): 127 for root, sub, files in os.walk(filename):
138 for file in files: 128 for file in files:
139 filenames.append(os.path.join(root, file)) 129 args.files.append(os.path.join(root, file))
140 continue 130 continue
141 131
142 class_file = mat.create_class_file(filename, args.backup, 132 class_file = mat.create_class_file(filename, args.backup,