moduleinfo.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. try:
  2. from collections import OrderedDict
  3. except ImportError:
  4. from mycollections import OrderedDict
  5. # Define value type
  6. STRING_TYPE = "string"
  7. LIST_TYPE = "list"
  8. INT_TYPE = "int"
  9. MODULE_NODE_NAME = 'module'
  10. COMMAND_NODE_NAME = 'command'
  11. PARAM_NODE_NAME = 'param'
  12. class ParamInfo:
  13. """The parameter of one command
  14. each command parameter have four attributes,
  15. parameter name, parameter type, parameter value, and parameter description
  16. """
  17. def __init__(self, name, desc = '', type = STRING_TYPE,
  18. optional = False, value = '', default_value = ''):
  19. self.name = name
  20. self.type = type
  21. self.value = value
  22. self.default_value = default_value
  23. self.desc = desc
  24. self.is_optional = optional
  25. def __str__(self):
  26. return str("\t%s <type: %s> \t(%s)" % (self.name, self.type, self.desc))
  27. class CommandInfo:
  28. """One command which provide by one bind10 module, it has zero or
  29. more parameters
  30. """
  31. def __init__(self, name, desc = "", need_inst_param = True):
  32. self.name = name
  33. # Wether command needs parameter "instance_name"
  34. self.need_inst_param = need_inst_param
  35. self.desc = desc
  36. self.params = OrderedDict()
  37. # Set default parameter "help"
  38. self.add_param(ParamInfo("help",
  39. desc = "Get help for command",
  40. optional = True))
  41. def __str__(self):
  42. return str("%s \t(%s)" % (self.name, self.desc))
  43. def add_param(self, paraminfo):
  44. self.params[paraminfo.name] = paraminfo
  45. def has_param_with_name(self, param_name):
  46. return param_name in self.params
  47. def get_param_with_name(self, param_name):
  48. return self.params[param_name]
  49. def get_params(self):
  50. return list(self.params.values())
  51. def get_param_names(self):
  52. return list(self.params.keys())
  53. def get_mandatory_param_names(self):
  54. all_names = self.params.keys()
  55. return [name for name in all_names
  56. if not self.params[name].is_optional]
  57. def get_param_name_by_position(self, pos, index, param_count):
  58. # count mandatories back from the last
  59. # from the last mandatory; see the number of mandatories before it
  60. # and compare that to the number of positional arguments left to do
  61. # if the number of lefts is higher than the number of mandatories,
  62. # use the first optional. Otherwise, use the first unhandled mandatory
  63. # (and update the location accordingly?)
  64. # (can this be done in all cases? this is certainly not the most efficient method;
  65. # one way to make the whole of this more consistent is to always set mandatories first, but
  66. # that would make some commands less nice to use ("config set value location" instead of "config set location value")
  67. if type(pos) == int:
  68. if param_count == len(self.params) - 1:
  69. i = 0
  70. for k in self.params.keys():
  71. if i == pos:
  72. return k
  73. i += 1
  74. raise KeyError(str(pos) + " out of range")
  75. elif param_count <= len(self.params):
  76. mandatory_count = 0
  77. for k in self.params.keys():
  78. if not self.params[k].is_optional:
  79. mandatory_count += 1
  80. if param_count == mandatory_count:
  81. # return the first mandatory from pos
  82. i = 0
  83. for k in self.params.keys():
  84. if i >= pos and not self.params[k].is_optional:
  85. return k
  86. i += 1
  87. raise KeyError(str(pos) + " out of range")
  88. else:
  89. i = 0
  90. for k in self.params.keys():
  91. if i == pos:
  92. return k
  93. i += 1
  94. raise KeyError(str(pos) + " out of range")
  95. else:
  96. raise KeyError("Too many parameters")
  97. else:
  98. raise KeyError(str(pos) + " is not an integer")
  99. def need_instance_param(self):
  100. return self.need_inst_param
  101. def command_help(self, inst_name, inst_type, inst_desc):
  102. print("Command ", self)
  103. print("\t\thelp (Get help for command)")
  104. params = self.params.copy()
  105. del params["help"]
  106. if len(params) == 0:
  107. print("\tNo parameters for the command")
  108. return
  109. print("\n\tMandatory parameters:")
  110. mandatory_infos = []
  111. for info in params.values():
  112. if not info.is_optional:
  113. print("\t", info)
  114. mandatory_infos.append(info)
  115. optional_infos = [info for info in params.values()
  116. if info not in mandatory_infos]
  117. if len(optional_infos) > 0:
  118. print("\n\tOptional parameters:")
  119. for info in optional_infos:
  120. print("\t", info)
  121. class ModuleInfo:
  122. """Define the information of one module, include module name,
  123. module supporting commands, instance name and the value type of instance name
  124. """
  125. def __init__(self, name, inst_name = "", inst_type = STRING_TYPE,
  126. inst_desc = "", desc = ""):
  127. self.name = name
  128. self.inst_name = inst_name
  129. self.inst_type = inst_type
  130. self.inst_desc = inst_desc
  131. self.desc = desc
  132. self.commands = OrderedDict()
  133. self.add_command(CommandInfo(name = "help",
  134. desc = "Get help for module",
  135. need_inst_param = False))
  136. def __str__(self):
  137. return str("%s \t%s" % (self.name, self.desc))
  138. def add_command(self, command_info):
  139. self.commands[command_info.name] = command_info
  140. if command_info.need_instance_param():
  141. command_info.add_param(ParamInfo(name = self.inst_name,
  142. type = self.inst_type,
  143. desc = self.inst_desc))
  144. def has_command_with_name(self, command_name):
  145. return command_name in self.commands
  146. def get_command_with_name(self, command_name):
  147. return self.commands[command_name]
  148. def get_commands(self):
  149. return list(self.commands.values())
  150. def get_command_names(self):
  151. return list(self.commands.keys())
  152. def get_instance_param_name(self):
  153. return self.inst_name
  154. def get_instance_param_type(self):
  155. return self.inst_type
  156. def module_help(self):
  157. print("Module ", self, "\nAvailable commands:")
  158. for k in self.commands.keys():
  159. print("\t", self.commands[k])
  160. def command_help(self, command):
  161. self.commands[command].command_help(self.inst_name,
  162. self.inst_type,
  163. self.inst_desc)