From 1583a58289318e612e099d47bdb53880ee877a21 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Tue, 14 Jun 2011 22:00:10 +0200 Subject: Use getopt (python2.3) instead of argparse (2.7) Getopt is deprecated in 2.7, but debian is in 2.6 --- cli.py | 81 ++++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 39 insertions(+), 42 deletions(-) (limited to 'cli.py') diff --git a/cli.py b/cli.py index fedb40c..14a848d 100644 --- a/cli.py +++ b/cli.py @@ -1,84 +1,81 @@ #!/usr/bin/python -""" +''' Metadata anonymisation toolkit -""" +''' import sys import mat -import argparse +import optparse -__version__ = "0.1" +__version__ = '0.1' -def parsing(): - ''' - Parse the arguments, - and returns a dict - ''' - parser = argparse.ArgumentParser(version=__version__, - description="Metadata Anonymisation Toolkit - CLI %s" % __version__) - - #list and check clean are mutually exclusives - group = parser.add_mutually_exclusive_group() - - #list meta - group.add_argument('--print-meta', '-p', action="store_true", default=False, - dest='just_list', help='List all the meta of a file,\ - without removing them') +def parse(): + parser = optparse.OptionParser(usage='%prog [options] filename') + common = optparse.OptionGroup(parser, 'Metadata Anonymisation Toolkit - CLI') + common.add_option('--display', '-d', action='store_true', default=False, + help='List all the meta of a file without removing them') + common.add_option('--check', '-c', action='store_true', default=False, + help='Check if a file is free of harmfull metadatas') + common.add_option('--version', action='callback', callback=displayVersion, + help='Display version and exit') - #check if the file is clean - group.add_argument('--check-clean', '-c', action="store_true", - default=False, dest='just_check', - help='Check if a file is clean of harmfull metadatas') + parser.add_option_group(common) - #list of files to process - parser.add_argument('filelist', action="store", type=str, nargs="+", - metavar='file', help='File(s) to process') + values, arguments = parser.parse_args() + if not arguments: + parser.print_help() + sys.exit(1) + return values, arguments - return parser.parse_args() +def displayVersion(): + print('Metadata Anonymisation Toolkit - CLI %s') % __version__ + print('Hachoir library version %s') % hachoir_core.__version__ + sys.exit(0) def list_meta(class_file, filename): ''' - Print all the meta of "filename" on stdout + Print all the meta of 'filename' on stdout ''' - print("[+] File %s :" % filename) + print('[+] File %s :' % filename) for key, item in class_file.get_meta().iteritems(): - print("\t%s : %s" % (key, item) ) + print('\t%s : %s' % (key, item) ) def is_clean(class_file, filename): ''' - Say if "filename" is clean or not + Say if 'filename' is clean or not ''' if class_file.is_clean(): - print("[+] %s is clean" % filename) + print('[+] %s is clean' % filename) else: - print("[+] %s is not clean" % filename) + print('[+] %s is not clean' % filename) def clean_meta(class_file, filename): ''' - Clean the file "filename" + Clean the file 'filename' ''' - print("[+] Cleaning %s" % filename) + print('[+] Cleaning %s' % filename) if class_file.is_clean(): - print("%s is already clean" % filename) + print('%s is already clean' % filename) else: class_file.remove_all() - print("%s cleaned !" % filename) + print('%s cleaned !' % filename) def main(): - args = parsing() + args, filenames = parse() + #args = parsing() #func receive the function correponding to the options given as parameters - if args.just_list is True: #only print metadatas + if args.display is True: #only print metadatas func = list_meta - elif args.just_check is True: #only check if the file is clean + elif args.check is True: #only check if the file is clean func = is_clean else: #clean the file func = clean_meta - for filename in args.filelist: + for filename in filenames: class_file = mat.create_class_file(filename) func(class_file, filename) - print("\n") + print('\n') if __name__ == '__main__': main() -- cgit v1.3