def __call__(self, *args, **kargs): """Set module parameters to the class and, if run_ is True execute the module, therefore valid parameters are all the module parameters plus some extra parameters that are: run_, stdin_, stdout_, stderr_, env_ and finish_. """ if not args and not kargs: self.run() return self # # check for extra kargs, set attribute and remove from dictionary # if 'flags' in kargs: for flg in kargs['flags']: self.flags[flg].value = True del(kargs['flags']) # set attributs for key in ('run_', 'env_', 'finish_', 'stdout_', 'stderr_', 'check_'): if key in kargs: setattr(self, key, kargs.pop(key)) # set inputs for key in ('stdin_', ): if key in kargs: self.inputs[key[:-1]].value = kargs.pop(key) # # set/update args # for param, arg in zip(self.params_list, args): param.value = arg for key, val in kargs.items(): key = key.strip('_') if key in self.inputs: self.inputs[key].value = val elif key in self.outputs: self.outputs[key].value = val elif key in self.flags: # we need to add this, because some parameters (overwrite, # verbose and quiet) work like parameters self.flags[key].value = val else: raise ParameterError('%s is not a valid parameter.' % key) # # check if execute # if self.run_: # # check reqire parameters # if self.check_: self.check() return self.run() return self
def check_required_columns(vname, layer, reqcols, pname): """Check if the vector input maps has the right columns in the attribute table.""" vname, mset = vname.split("@") if "@" in vname else (vname, "") with VectorTopo(vname, mset, mode="r", layer=layer) as vect: columns = vect.table.columns for col in reqcols: if col not in columns: msg = "Parameter: %s require the following columns: %r," " %s is missing" raise ParameterError(msg % (pname, reqcols, col)) return vect
def check(self): """Check the correctness of the provide parameters""" required = True for flg in self.flags.values(): if flg and flg.suppress_required: required = False if required: for k in self.required: if (k in self.inputs and self.inputs[k].value is None) or ( k in self.outputs and self.outputs[k].value is None): msg = "Required parameter <%s> not set." raise ParameterError(msg % k)
def check_float_or_raster(parameter, min=None, max=None): """Check if the parameter is a float or a raster map in a certain range.""" try: par = float(parameter) if min is not None and max is not None: par = check_range(par, min, max) except: try: rname, mset = parameter.split("@") if "@" in parameter else (parameter, "") if min is not None and max is not None: with RasterRow(rname, mset) as rst: mn, mx = rst.info.range check_range(mn, min, max) check_range(mx, min, max) par = parameter except: raise ParameterError("Raster is not readable or not in a range") return par
def update(self, *args, **kargs): """Update module parameters and selected object attributes. Valid parameters are all the module parameters and additional parameters, namely: run_, stdin_, stdout_, stderr_, env_, and finish_. """ # # check for extra kargs, set attribute and remove from dictionary # if "flags" in kargs: for flg in kargs["flags"]: self.flags[flg].value = True del kargs["flags"] # set attributes for key in ("run_", "env_", "finish_", "stdout_", "stderr_", "check_"): if key in kargs: setattr(self, key, kargs.pop(key)) # set inputs for key in ("stdin_", ): if key in kargs: self.inputs[key[:-1]].value = kargs.pop(key) # # set/update args # for param, arg in zip(self.params_list, args): param.value = arg for key, val in kargs.items(): key = key.strip("_") if key in self.inputs: self.inputs[key].value = val elif key in self.outputs: self.outputs[key].value = val elif key in self.flags: # we need to add this, because some parameters (overwrite, # verbose and quiet) work like parameters self.flags[key].value = val else: raise ParameterError("%s is not a valid parameter." % key)
def check_raster_or_landuse(opts, params): """Return a list of raster name, i f necessary the rasters are generated using the file with the rules to convert a landuse map to the raster that it is needed. """ msgr = get_msgr() rasters = [] for par in params: if opts[par]: rasters.append(opts[par]) elif opts['rules_%s' % par] and opts['landuse']: output = rname(par) msg = 'Creating: {out} using the rules: {rul}' msgr.verbose(msg.format(out=output, rul='rules_%s' % par)) r.reclass(input=opts['landuse'], output=output, rules=opts['rules_%s' % par]) rasters.append(output) else: msg = '{par} or rule_{par} are required' raise ParameterError(msg.format(par=par)) return rasters
def check_range(value, min=0.0, max=1.0): """Check if a value is between a range""" if value < min or value > max: raise ParameterError("Value not between: %f, %f" % (min, max)) return value