diff --git a/utils/kolabformatchecker.py b/utils/kolabformatchecker.py index b71e441..87eb70f 100644 --- a/utils/kolabformatchecker.py +++ b/utils/kolabformatchecker.py @@ -1,77 +1,119 @@ #!/usr/bin/env python2.7 import sys import kolabformat import argparse import email +from os import listdir +from os.path import isfile, join, basename parser = argparse.ArgumentParser(description='Check kolab xml objects.') -parser.add_argument('messagetype', - choices=['xml', 'mime'], - help='The type of the object to parse') -parser.add_argument('kolabtype', - choices=['event', 'todo', 'contact', 'journal', 'distlist', 'note', 'configuration', 'file'], - help='The type of the object to parse') -parser.add_argument('filename', - help='The file to parse') +parser.add_argument('--messagetype', '-m', + dest='messageType', + choices=['xml', 'mime'], + required=True, + help='The type of the object to parse') +parser.add_argument('--kolabtype', '-k', + dest='kolabType', + choices=['event', 'todo', 'contact', 'journal', 'distlist', 'note', 'configuration', 'file'], + required=True, + help='The type of the object to parse') +parser.add_argument('--verbose', '-v', + dest='verbose', + default=False, + action='store_true', + help='Verbose output') +parser.add_argument('-p', '--path', + dest='path', + required=True, + help='The file to read') +parser.add_argument('-f', '--filetype', + dest='fileType', + default='', + help='Pattern for file name/type matching in directory') args = parser.parse_args() -messageType = args.messagetype -objectType = args.kolabtype -filename = args.filename +messageType = args.messageType +objectType = args.kolabType +verbose = args.verbose -if messageType == 'mime': - f = open(filename,"r") - msg = email.message_from_string(f.read()) - - kolabType = msg.get('X-Kolab-Type') - if not kolabType: - print("Missing kolab type X-Kolab-Type") - else: - print("Found X-Kolab-Type ", kolabType) +if isfile(args.path): + filenames = [args.path] +else: + filenames = [join(args.path,fn) for fn in listdir(args.path)] - version = msg.get('X-Kolab-Mime-Version') - if not version: - print("Missing kolab type X-Kolab-Mime-Version") - else: - print("Found X-Kolab-Mime-Version ", version) +for filename in filenames: + if filename.endswith(args.fileType): + if verbose: + print("NAME %s" % filename) - for part in msg.walk(): - if part.get_content_type() in ['application/calendar+xml', 'application/vcard+xml', 'application/vnd.kolab+xm']: - print("Content type of part is ", part.get_content_type()) - print("Filename of part is ", part.get_filename()) - xml = part.get_payload(decode=True) - print("Found xml ", xml) + if messageType == 'mime': + f = open(filename,"r") + msg = email.message_from_string(f.read()) - isPath = False -else: - xml = filename - isPath = True + kolabType = msg.get('X-Kolab-Type') + if not kolabType: + print("Missing kolab type X-Kolab-Type") + else: + if verbose: + print("Found X-Kolab-Type ", kolabType) -if objectType == 'event': - kolabformat.readEvent(xml, isPath) -elif objectType == 'todo': - kolabformat.readTodo(xml, isPath) -elif objectType == 'contact': - kolabformat.readContact(xml, isPath) -elif objectType == 'journal': - kolabformat.readJournal(xml, isPath) -elif objectType == 'distlist': - kolabformat.readDistlist(xml, isPath) -elif objectType == 'note': - kolabformat.readNote(xml, isPath) -elif objectType == 'configuration': - kolabformat.readConfiguration(xml, isPath) -elif objectType == 'file': - kolabformat.readFile(xml, isPath) + version = msg.get('X-Kolab-Mime-Version') + if not version: + print("Missing kolab type X-Kolab-Mime-Version") + else: + if verbose: + print("Found X-Kolab-Mime-Version ", version) + for part in msg.walk(): + if part.get_content_type() in ['application/calendar+xml', + 'application/vcard+xml', + 'application/vnd.kolab+xml', + 'application/x-vnd.kolab.contact.distlist', + 'application/x-vnd.kolab.contact']: + xml = part.get_payload(decode=True) + if verbose: + print("Content type of part is ", part.get_content_type()) + print("Filename of part is ", part.get_filename()) + print "Found xml \n%s", xml -error = kolabformat.error() + isPath = False + else: + xml = filename + isPath = True -if error == None or not error: - print("Successfully parsed the message") - sys.exit(0) -else: - print("There was an error while parsing ", kolabformat.errorMessage()) - sys.exit(1) + if objectType == 'event': + kolabObject = kolabformat.readEvent(xml, isPath) + if verbose: + print("Event uid is: %s" % kolabObject.uid()) + print("Event is valid: %s" % kolabObject.isValid() ) + print("Event start_date is valid: %s" % kolabObject.start().isValid() ) + print("Event end_date is valid: %s" % kolabObject.end().isValid() ) + print("Event URL is: %s" % kolabObject.url() ) + print("Event summary is: %s" % kolabObject.summary() ) + print("Event description is: %s" % kolabObject.description() ) + print("Event comment is: %s" % kolabObject.comment() ) + elif objectType == 'todo': + kolabObject = kolabformat.readTodo(xml, isPath) + elif objectType == 'contact': + kolabObject = kolabformat.readContact(xml, isPath) + elif objectType == 'journal': + kolabObject = kolabformat.readJournal(xml, isPath) + elif objectType == 'distlist': + kolabObject = kolabformat.readDistlist(xml, isPath) + elif objectType == 'note': + kolabObject = kolabformat.readNote(xml, isPath) + elif objectType == 'configuration': + kolabObject = kolabformat.readConfiguration(xml, isPath) + elif objectType == 'file': + kolabObject = kolabformat.readFile(xml, isPath) + + error = kolabformat.error() + + if error == None or not error: + print("Successfully parsed file %s with kolab object %s" % + (basename(filename), kolabObject.uid()) + ) + else: + print("There was an error while parsing ", kolabformat.errorMessage())