123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- try:
- from collections import OrderedDict
- except ImportError:
- from mycollections import OrderedDict
- # Define value type
- STRING_TYPE = "string"
- LIST_TYPE = "list"
- INT_TYPE = "int"
- MODULE_NODE_NAME = 'module'
- COMMAND_NODE_NAME = 'command'
- PARAM_NODE_NAME = 'param'
- class ParamInfo:
- """The parameter of one command
- each command parameter have four attributes,
- parameter name, parameter type, parameter value, and parameter description
- """
- def __init__(self, name, desc = '', type = STRING_TYPE,
- optional = False, value = '', default_value = ''):
- self.name = name
- self.type = type
- self.value = value
- self.default_value = default_value
- self.desc = desc
- self.is_optional = optional
-
- def __str__(self):
- return str("\t%s <type: %s> \t(%s)" % (self.name, self.type, self.desc))
- class CommandInfo:
- """One command which provide by one bind10 module, it has zero or
- more parameters
- """
- def __init__(self, name, desc = "", need_inst_param = True):
- self.name = name
- # Wether command needs parameter "instance_name"
- self.need_inst_param = need_inst_param
- self.desc = desc
- self.params = OrderedDict()
- # Set default parameter "help"
- self.add_param(ParamInfo("help",
- desc = "Get help for command",
- optional = True))
-
- def __str__(self):
- return str("%s \t(%s)" % (self.name, self.desc))
-
- def add_param(self, paraminfo):
- self.params[paraminfo.name] = paraminfo
-
- def has_param_with_name(self, param_name):
- return param_name in self.params
-
- def get_param_with_name(self, param_name):
- return self.params[param_name]
-
- def get_params(self):
- return list(self.params.values())
-
- def get_param_names(self):
- return list(self.params.keys())
-
-
- def get_mandatory_param_names(self):
- all_names = self.params.keys()
- return [name for name in all_names
- if not self.params[name].is_optional]
-
- def get_param_name_by_position(self, pos, index, param_count):
- # count mandatories back from the last
- # from the last mandatory; see the number of mandatories before it
- # and compare that to the number of positional arguments left to do
- # if the number of lefts is higher than the number of mandatories,
- # use the first optional. Otherwise, use the first unhandled mandatory
- # (and update the location accordingly?)
- # (can this be done in all cases? this is certainly not the most efficient method;
- # one way to make the whole of this more consistent is to always set mandatories first, but
- # that would make some commands less nice to use ("config set value location" instead of "config set location value")
- if type(pos) == int:
- if param_count == len(self.params) - 1:
- i = 0
- for k in self.params.keys():
- if i == pos:
- return k
- i += 1
- raise KeyError(str(pos) + " out of range")
- elif param_count <= len(self.params):
- mandatory_count = 0
- for k in self.params.keys():
- if not self.params[k].is_optional:
- mandatory_count += 1
- if param_count == mandatory_count:
- # return the first mandatory from pos
- i = 0
- for k in self.params.keys():
- if i >= pos and not self.params[k].is_optional:
- return k
- i += 1
- raise KeyError(str(pos) + " out of range")
- else:
- i = 0
- for k in self.params.keys():
- if i == pos:
- return k
- i += 1
- raise KeyError(str(pos) + " out of range")
- else:
- raise KeyError("Too many parameters")
- else:
- raise KeyError(str(pos) + " is not an integer")
-
- def need_instance_param(self):
- return self.need_inst_param
- def command_help(self, inst_name, inst_type, inst_desc):
- print("Command ", self)
- print("\t\thelp (Get help for command)")
-
- params = self.params.copy()
- del params["help"]
- if len(params) == 0:
- print("\tNo parameters for the command")
- return
-
- print("\n\tMandatory parameters:")
- mandatory_infos = []
- for info in params.values():
- if not info.is_optional:
- print("\t", info)
- mandatory_infos.append(info)
- optional_infos = [info for info in params.values()
- if info not in mandatory_infos]
- if len(optional_infos) > 0:
- print("\n\tOptional parameters:")
- for info in optional_infos:
- print("\t", info)
- class ModuleInfo:
- """Define the information of one module, include module name,
- module supporting commands, instance name and the value type of instance name
- """
-
- def __init__(self, name, inst_name = "", inst_type = STRING_TYPE,
- inst_desc = "", desc = ""):
- self.name = name
- self.inst_name = inst_name
- self.inst_type = inst_type
- self.inst_desc = inst_desc
- self.desc = desc
- self.commands = OrderedDict()
- self.add_command(CommandInfo(name = "help",
- desc = "Get help for module",
- need_inst_param = False))
-
- def __str__(self):
- return str("%s \t%s" % (self.name, self.desc))
-
- def add_command(self, command_info):
- self.commands[command_info.name] = command_info
- if command_info.need_instance_param():
- command_info.add_param(ParamInfo(name = self.inst_name,
- type = self.inst_type,
- desc = self.inst_desc))
-
- def has_command_with_name(self, command_name):
- return command_name in self.commands
-
- def get_command_with_name(self, command_name):
- return self.commands[command_name]
-
-
- def get_commands(self):
- return list(self.commands.values())
-
-
- def get_command_names(self):
- return list(self.commands.keys())
-
-
- def get_instance_param_name(self):
- return self.inst_name
-
-
- def get_instance_param_type(self):
- return self.inst_type
-
- def module_help(self):
- print("Module ", self, "\nAvailable commands:")
- for k in self.commands.keys():
- print("\t", self.commands[k])
-
-
- def command_help(self, command):
- self.commands[command].command_help(self.inst_name,
- self.inst_type,
- self.inst_desc)
-
|