def command_line(parser, args, log): """ command line oriented parsing in one common location. """ # this is done first because command line options can be set in config file if args.paramfile: ups = [] gparms = {} lines = [] astrotype = None primname = None cl = get_classification_library() lines = [re.sub("#.*?$", "", line).strip() \ for line in open(args.paramfile).readlines()] # see if they are command options plines = [] i = 0 for line in lines: i += 1 pline = line plines.append(pline) if not line: continue # '--xoption' or '--xoption=value' long switch elif len(line)>2 and line[:2] == "--": #then it's an option if "=" not in line: try: assert(parser_has_option(parser, line.strip())) opt = line.strip() val = True except AssertionError: log.error("Badly formatted parameter file (%s)\n" \ " Line #%d: %s""" % (args.paramfile, i, pline)) log.error("Unrecognized argument: %s " % line) abortBadParamfile(log, plines) else: opt,val = line.split("=") opt = opt.strip() val = val.strip() # --files may have multiple values ... # eg., --files=file1 file2 file3 if opt == "--files": log.info("found --files option in param file.") files = val.split() log.info("Extending args.files by %s" % files) args.files.extend(files) continue # Emulator funcs perform has_option(), get_option() tasks. # insert_option_value() wraps get_option() and exec(). try: assert(parser_has_option(parser, opt)) log.info("Got option in paramfile: %s" % opt) insert_option_value(parser, args, opt, val) continue except AssertionError: log.error("Badly formatted parameter file (%s)\n" \ " Line #%d: %s""" % (args.paramfile, i, pline)) log.error("Unrecognized argument: %s " % opt) abortBadParamfile(log, plines) # '-x xvalue' short option & value elif len(line.split()) == 2 and line[0] == '-': opt, val = line.split() try: assert(parser_has_option(parser, opt)) log.info("Got option, value in paramfile: %s, %s" % (opt, val)) insert_option_value(parser, args, opt, val) continue except AssertionError: log.error("Badly formatted parameter file (%s)\n" \ " Line #%d: %s""" % (args.paramfile, i, pline)) log.error("Unrecognized argument: %s " % line) abortBadParamfile(log, plines) # '-x' short switch elif len(line) == 2 and line[0] == '-': opt = line.strip() val = True try: assert(parser_has_option(parser, opt)) log.info("Got option in paramfile: %s" % opt) insert_option_value(parser, args, opt, val) continue except AssertionError: log.error("Badly formatted parameter file (%s)\n" \ " Line #%d: %s""" % (args.paramfile, i, pline)) log.error("Unrecognized argument: %s " % line) abortBadParamfile(log, plines) elif len(line)>0: if "]" in line: # section line name = re.sub("[\[\]]", "", line).strip() if len(name)== 0: astrotype = None primname = None elif cl.is_name_of_type(name): astrotype = name else: primname = name else: # par=val line keyval = line.split("=") if len(keyval)<2: log.error("Badly formatted parameter file (%s)" \ "\n Line #%d: %s""" % (args.paramfile, i, pline)) abortBadParamfile(log, plines) # sys.exit(1) # abortBAdParamfile calls sys.exit(1) key = keyval[0].strip() val = keyval[1].strip() if val[0] == "'" or val[0] == '"': val = val[1:] if val[-1] == "'" or val[-1] == '"': val = val[0:-1] if primname and not astrotype: log.error("Badly formatted parameter file (%s)" \ '\n The primitive name is set to "%s", but the astrotype is not set' \ "\n Line #%d: %s" % (args.paramfile, primname, i, pline[:-1])) abortBadParamfile(log, plines) if not primname and astrotype: log.error("Badly formatted parameter file (%s)" \ '\n The astrotype is set to "%s", but the primitive name is not set' \ "\n Line #%d: %s" % (args.paramfile, astrotype, i, pline)) abortBadParamfile(log, plines) if not primname and not astrotype: gparms.update({key:val}) else: up = RecipeManager.UserParam(astrotype, primname, key, val) ups.append(up) # parameter file ups and gparms pfups = ups pfgparms = gparms if args.show_colors: print dir(filteredstdout.term) sys.exit(0) try: assert(args.files or args.astrotype) except AssertionError: log.info("Either file(s) OR an astrotype is required;" "-t or --astrotype.") log.error("NO INPUT FILE or ASTROTYPE specified") log.info("type 'reduce -h' for usage information") sys.exit(1) input_files = [] badList = [] for inf in args.files: olist,blist = checkImageParam(inf) # checkImageParam return 2-tuple. input_files.extend(olist) badList.extend(blist) try: assert(badList) print "Got a badList ... ", badList print "I.e. File not found or unreadable." err = "\n\t".join(badList) log.error("Some files not found or can't be loaded:\n\t"+err) log.error("Exiting due to missing datasets.") try: assert(input_files) found = "\n\t".join(input_files) log.info("These datasets were found and loaded:\n\t"+found) except AssertionError: print "Got no input files" pass sys.exit(1) except AssertionError: pass # parameters from command line and/or parameter file clups = [] pfups = [] clgparms = {} pfgparms = {} if args.userparam: # print "r451: user params", args.userparam ups = [] gparms = {} allupstr = args.userparam allparams = allupstr.split(",") # print "r456:", repr(allparams) for upstr in allparams: # print "r458:", upstr tmp = upstr.split("=") spec = tmp[0].strip() # @@TODO: check and convert to correct type val = tmp[1].strip() if ":" in spec: typ,prim,param = spec.split(":") up = RecipeManager.UserParam(typ, prim, param, val) ups.append(up) else: up = RecipeManager.UserParam(None, None, spec, val) ups.append(up) gparms.update({spec:val}) # command line ups and gparms clups = ups clgparms = gparms # print "r473:", repr(clgparms) fups = RecipeManager.UserParams() for up in clups: #print "r473:", up fups.add_user_param(up) for up in pfups: #print "r476:", up fups.add_user_param(up) args.user_params = fups args.globalParams = {} args.globalParams.update(clgparms) args.globalParams.update(pfgparms) return input_files
# Copyright (C) 2014 Novem LLC, created Craig Allen 2014 # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/.import generalclassification import os, re from astrodata.Errors import Error from astrodata.AstroDataType import get_classification_library from astrodata.adutils import termcolor as tc from astrodata.Lookups import compose_multi_table cl = get_classification_library() class GD_OperationNotPermitted(Error): """ GeneralData related exceptions, raised if an operation is not permitted, like setting an output_filename property (it's constructed from the output_directory and the basename). """ message = "Operation Not Permitted" filetypes = compose_multi_table("*/filetypes.py", "data_object_precedence", "data_object_classes") _data_object_classes = filetypes["data_object_classes"] _data_object_precedence = filetypes["data_object_precedence"] _gen_classification_library = cl class HDUnit(object): # abstracts the idea of a unit of data and it's metadata pass