Skip to content
Permalink
Browse files

Expanded the goptions command to set and unset global options from th…

…e module context. Closes #47
  • Loading branch information
lanmaster53 committed Jan 13, 2020
1 parent cbae5b3 commit 055de2c050c1623da85da08ceb505827590ef6b2
Showing with 54 additions and 13 deletions.
  1. +14 −11 recon/core/framework.py
  2. +40 −2 recon/core/module.py
@@ -709,7 +709,7 @@ def _load_config(self):
# invalid key, contnue to load valid keys
continue

def _save_config(self, name):
def _save_config(self, name, module=None, options=None):
config_path = os.path.join(self.workspace, 'config.dat')
# create a config file if one doesn't exist
open(config_path, 'a').close()
@@ -720,17 +720,20 @@ def _save_config(self, name):
except ValueError:
# file is empty or corrupt, nothing to load
config_data = {}
# override implicit defaults if specified
module = module or self._modulename
options = options or self.options
# create a container for the current module
if self._modulename not in config_data:
config_data[self._modulename] = {}
if module not in config_data:
config_data[module] = {}
# set the new option value in the config
config_data[self._modulename][name] = self.options[name]
config_data[module][name] = options[name]
# remove the option if it has been unset
if config_data[self._modulename][name] is None:
del config_data[self._modulename][name]
if config_data[module][name] is None:
del config_data[module][name]
# remove the module container if it is empty
if not config_data[self._modulename]:
del config_data[self._modulename]
if not config_data[module]:
del config_data[module]
# write the new config data to the config file
with open(config_path, 'w') as config_file:
json.dump(config_data, config_file, indent=4)
@@ -911,7 +914,7 @@ def _do_options_list(self, params):
def _do_options_set(self, params):
'''Sets a current context option'''
option, value = self._parse_params(params)
if not option and value:
if not (option and value):
self._help_options_set()
return
name = option.upper()
@@ -1297,11 +1300,11 @@ def help_options(self):

def _help_options_set(self):
print(getattr(self, '_do_options_set').__doc__)
print(f"{os.linesep}Usage: set <option> <value>{os.linesep}")
print(f"{os.linesep}Usage: options set <option> <value>{os.linesep}")

def _help_options_unset(self):
print(getattr(self, '_do_options_unset').__doc__)
print(f"{os.linesep}Usage: unset <option>{os.linesep}")
print(f"{os.linesep}Usage: options unset <option>{os.linesep}")

def help_keys(self):
print(getattr(self, 'do_keys').__doc__)
@@ -221,6 +221,32 @@ def _do_goptions_list(self, params):
'''Shows the global context options'''
self._list_options(self._global_options)

def _do_goptions_set(self, params):
'''Sets a global context option'''
option, value = self._parse_params(params)
if not (option and value):
self._help_goptions_set()
return
name = option.upper()
if name in self._global_options:
self._global_options[name] = value
print(f"{name} => {value}")
self._save_config(name, 'base', self._global_options)
else:
self.error('Invalid option name.')

def _do_goptions_unset(self, params):
'''Unsets a global context option'''
option, value = self._parse_params(params)
if not option:
self._help_goptions_unset()
return
name = option.upper()
if name in self._global_options:
self._do_goptions_set(' '.join([name, 'None']))
else:
self.error('Invalid option name.')

def _do_modules_load(self, params):
'''Loads a module'''
if not params:
@@ -347,8 +373,16 @@ def do_run(self, params):
#==================================================

def help_goptions(self):
print(getattr(self, 'do_options').__doc__)
print(f"{os.linesep}Usage: goptions <list>{os.linesep}")
print(getattr(self, 'do_goptions').__doc__)
print(f"{os.linesep}Usage: goptions <{'|'.join(self._parse_subcommands('goptions'))}> [...]{os.linesep}")

def _help_goptions_set(self):
print(getattr(self, '_do_goptions_set').__doc__)
print(f"{os.linesep}Usage: goptions set <option> <value>{os.linesep}")

def _help_goptions_unset(self):
print(getattr(self, '_do_goptions_unset').__doc__)
print(f"{os.linesep}Usage: goptions unset <option>{os.linesep}")

#==================================================
# COMPLETE METHODS
@@ -364,6 +398,10 @@ def complete_goptions(self, text, line, *ignored):
def _complete_goptions_list(self, text, *ignored):
return []

def _complete_goptions_set(self, text, *ignored):
return [x for x in self._global_options if x.startswith(text.upper())]
_complete_goptions_unset = _complete_goptions_set

def complete_reload(self, text, *ignored):
return []
complete_info = complete_input = complete_run = complete_reload

0 comments on commit 055de2c

Please sign in to comment.
You can’t perform that action at this time.