Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def do_set(self, args):
"""
Change a setting.
Overrides cmd2.Cmd.do_set()
"""
if args:
config = EvaluatingConfigParser()
setting_string = "[settings]\n{}".format(args)
try:
config.read_string(setting_string)
except configparser.ParsingError as err:
self.perror(str(err))
self.exit_code = self.exit_codes.error
return
for param_name in config['settings']:
if param_name in self.settable:
self._change_setting(param_name, config['settings'][param_name])
self.exit_code = self.exit_codes.success
else:
self.perror("unknown setting: '{}'".format(param_name))
self.exit_code = self.exit_codes.error
else:
self.do_show(args)
def load_config(self):
"""Open and parse the user config file and set self.config."""
config = None
if self.config_file is not None:
config = EvaluatingConfigParser()
try:
with open(self.config_file, 'r') as fobj:
config.read_file(fobj)
except FileNotFoundError:
pass
try:
settings = config['settings']
for key in settings:
self._change_setting(key, settings[key])
except KeyError:
pass
except ValueError:
pass
self.config = config