Example #1
0
def main():
    '''
    Main function validates command line arguments and creates a sub directory in the same path of the XML map to store the output files.
    
    '''

    parser = OptionParser()

    parser.add_option("-f", "--file", dest="filename",type="string", 
                  help="Loads the XML FILE", metavar="FILE")
    
    parser.add_option("-l", "--list", dest="list",
                  help="List all the mapped objects in the XML FILE", metavar="LIST")
    
    parser.add_option("-e", "--extract", dest="object",
                  help="Extracts all the mapped objects in the XML FILE", metavar="OBJECTS")
    
    parser.add_option("-n", "--numpy", dest="output",default=None,nargs=0,
                  help="Extracts each selected object in a binary .dat file", metavar="DUMP")
    
    parser.add_option("-d", "--directory", dest="dir",
                  help="Base directory for bulk processing ", metavar="DIR")    

    parser.add_option("-p", "--pattern", dest="pattern",
                  help="UNIX wildcards pattern", metavar="PATTERN")
    
    
    parser.add_option("-v", "--verbose", dest="verbose",default=False,nargs=0,
                  help="verbose, extenden output", metavar="VERB")  
      
    (options, args) = parser.parse_args()
    
    
    #Getting the program name
    args=sys.argv[0].split("/")
    if len(args)==1:
        args=sys.argv[0].split("\\")

    #Relative paths for Windows:
    if options.filename!=None:        
        if not os.path.isabs(options.filename):
            if sys.platform.find("WIN") and (sys.argv[0].find(".exe")!=-1):
                options.filename= os.path.normpath(os.path.join(sys.path[0],"..\\" +options.filename))



    if options.filename==None and options.dir==None: 
        print "Required argument [-f|--file] or [-d] missing"
        print ""
        usage(args)
        sys.exit(-1)
    if options.filename != None:
        if not os.path.exists( options.filename): 
            print "The file does not exist or it is an incorrect filename: " + options.filename
            print ""
            usage(args)
            sys.exit(-1)
  
    if options.list==None and options.object==None:
        print " -l or -e required"
        print ""
        usage()
        sys.exit(-1)
    if (options.list and options.object):
        print "Options -l and -e are mutually exclusive"
        print ""
        print options
        print ""
        usage(args)
        sys.exit(-1)
        
    if options.list not in ["VData","SDS","RIS","MDATA","ALL"] and options.object not in ["VData","SDS","RIS","MDATA","ALL"]:
        print " A valid HDF object name is required"
        print ""
        print options
        print ""
        usage(args)
        sys.exit(-1)
    
    
    if options.filename is not None:                
        
        if not os.path.exists(options.filename + "_dump"):# Be careful with relative paths in Linux
            try:
                os.makedirs(options.filename+ "_dump")
                print "Directory created :" + options.filename
            except:
                print "Failed to create a sub directory to store the output files: " + options.filename
                sys.exit(-1)                        
        else:
            print "The output directory already exist: " + options.filename
                        
        if options.list :# We call the parser just to list the objects mapped in the XML file                
            parser= XMLparser(options.filename,"l",options.list,options.output,True)
            parser.parseAndDumpMapContent()
        else:# We call the parser to extract a given object(s) described in the XML map. 
    
            parser= XMLparser(options.filename,"e",options.object,options.output,options.verbose)
            if parser.tree is None:
                sys.exit(-1)
            exit_code=parser.parseAndDumpMapContent()
            print "Dumping complete"
            sys.exit(exit_code)
    else:
    
        dumper= Reader(options.dir + "/")
        dumper.list_files(options.pattern)
        dumper.dump_files("e",options.object,options.output,options.verbose)
        print "Dumping complete"