def coerce_flag_value(val, flagdef): """Coerces a flag value based on flagdef settings.""" if ( val is None or not flagdef or not flagdef.type or flag_util.is_flag_function(val) ): return val if isinstance(val, list): return [coerce_flag_value(x, flagdef) for x in val] elif flagdef.type == "string": return _try_coerce_flag_val(val, str, flagdef) elif flagdef.type == "int": if isinstance(val, float): raise ValueError("invalid value for type 'int'") return _try_coerce_flag_val(val, int, flagdef) elif flagdef.type == "float": return _try_coerce_flag_val(val, float, flagdef) elif flagdef.type == "boolean": return _try_coerce_flag_val(val, bool, flagdef) elif flagdef.type == "number": if isinstance(val, (float, int)): return val return _try_coerce_flag_val(val, (int, float), flagdef) elif flagdef.type in ("path", "existing-path"): return _resolve_rel_path(val) else: log.warning( "unknown flag type '%s' for %s - cannot coerce", flagdef.type, flagdef.name ) return val
def _batch_opspec_for_flags(flag_vals): has_list = False for val in flag_vals.values(): if flag_util.is_flag_function(val): return "random" has_list = has_list or isinstance(val, list) if has_list: return "+" return None
def _check_flag_val(val, flag): if isinstance(val, list): for x in val: _check_flag_val(x, flag) elif flag_util.is_flag_function(val): pass else: _check_flag_choice(val, flag) _check_flag_type(val, flag) _check_flag_range(val, flag)