Commit a08ddfd2 by fwkz

Refactoring mechanism used by 'show info' command.

parent b6ba0005
...@@ -329,17 +329,11 @@ class RoutersploitInterpreter(BaseInterpreter): ...@@ -329,17 +329,11 @@ class RoutersploitInterpreter(BaseInterpreter):
info, options = 'info', 'options' info, options = 'info', 'options'
sub_command = args[0] sub_command = args[0]
if sub_command == info: if sub_command == info:
info = ["name", "description", "targets", "authors", "references"] utils.pprint_dict_in_order(
("name", "description", "targets", "authors", "references"),
for i in info: # TODO refactor this. self.module_metadata,
if i in self.module_metadata.keys(): )
print("\n{}:".format(i.capitalize())) utils.print_info()
if type(self.module_metadata[i]) is list:
for item in self.module_metadata[i]:
print("- {}".format(item))
else:
print(self.module_metadata[i])
print()
elif sub_command == options: elif sub_command == options:
target_opts = {'port', 'target'} target_opts = {'port', 'target'}
module_opts = set(self.current_module.options) - target_opts module_opts = set(self.current_module.options) - target_opts
......
...@@ -193,7 +193,6 @@ def sanitize_url(address): ...@@ -193,7 +193,6 @@ def sanitize_url(address):
Converts address to valid HTTP url. Converts address to valid HTTP url.
""" """
url = ""
if not address.startswith("http://") and not address.startswith("https://"): if not address.startswith("http://") and not address.startswith("https://"):
url = "http://" + address url = "http://" + address
else: else:
...@@ -201,3 +200,43 @@ def sanitize_url(address): ...@@ -201,3 +200,43 @@ def sanitize_url(address):
return url return url
def pprint_dict_in_order(order, dictionary):
""" Pretty dict print.
Pretty printing dictionary in specific order. (as in 'show info' command)
Keys not mentioned in *order* parameter will be printed in random order.
ex. pprint_dict_in_order(('sex', 'name'), {'name': John, 'sex': 'male', "hobby": ["rugby", "golf"]})
Sex:
male
Name:
John
Hobby:
- rugby
- golf
"""
def prettyprint(title, body):
print_info("\n{}:".format(title.capitalize()))
if not isinstance(body, str):
for value_element in value:
print_info('- ', value_element)
else:
print_info(value)
keys = dictionary.keys()
for element in order:
try:
key = keys.pop(keys.index(element))
value = dictionary[key]
except (KeyError, ValueError):
pass
else:
prettyprint(element, value)
for rest_keys in keys:
prettyprint(rest_keys, dictionary[rest_keys])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment