def _run_main(self, args, parsed_globals): varname_value = args.varname if len(varname_value) % 2 != 0: raise ParamError( "Unexpected format\n" "Expected input format:\n\n" " $tccli configure set region ap-guangzhou\n" " $tccli configure set region ap-guangzhou output json\n") varnames = varname_value[::2] values = varname_value[1::2] profile_name = parsed_globals.profile \ if parsed_globals.profile else "default" config = {} cred = {} extra = {} for varname, value in zip(varnames, values): if varname in self.cred_list: cred[varname] = value elif varname in self.config_list: config[varname] = value else: extra[varname] = value if config or extra: self._init_configure(profile_name + '.configure', config, extra) if cred: self._init_configure(profile_name + '.credential', cred)
def _unpack_param(self, value): if self.argument_model["type"] == "Boolean": try: if str(value) == 'False': return False elif str(value) == 'True': return True else: raise ValueError except ValueError as e: raise ParamError( self.cli_name, "Invalid Boolean: %s\n " "Boolean received: %s\n " "Boolean only take values: True/False" % (e, value)) elif self.argument_model["type"] in CLI_BASE_TYPE: return value else: try: return json.loads(value.encode("utf8")) except ValueError as e: raise ParamError( self.cli_name, "Invalid JSON: %s\n " "JSON received: %s" % (e, value))
def add_to_call_parameters(self, call_parameters, parsed_args, **kwargs): # Read parameters from the file arg_value = self._get_arg_value(parsed_args) if arg_value is None: return loaded_params = self._load_parameters(arg_value) if not isinstance(loaded_params, dict): raise ParamError( self.cli_name, "Invalid type: expecting dict, " "received %s" % type(loaded_params) ) self._update_call_parameters(call_parameters, loaded_params)
def _run_main(self, args, parsed_globals): varname_value = args.varname if len(varname_value) % 2 != 0: raise ParamError( "Unexpected format\n" "Expected input format:\n\n" " $tccli configure set region ap-guangzhou\n" " $tccli configure set region ap-guangzhou output json\n") varnames = varname_value[::2] values = varname_value[1::2] profile_name = parsed_globals.profile \ if parsed_globals.profile else "default" config = {} cred = {} extra = {} for varname, value in zip(varnames, values): if varname in self.cred_list: cred[varname] = value elif varname in self.config_list: if varname == OptionsDefine.Output and value not in [ 'json', 'text', 'table' ]: print( 'Output format must be json, text or table. Set as default: json' ) config[varname] = 'json' else: config[varname] = value else: extra[varname] = value if config or extra: self._init_configure(profile_name + '.configure', config, extra) if cred: self._init_configure(profile_name + '.credential', cred)
def _load_parameters(self, arg_value): try: return json.loads(arg_value) except ValueError: raise ParamError(self.name, "Invalid JSON received.")