def test_set_parser_defaults(argsdict, tmpdir): # Build a config parser config = ConfigParser.SafeConfigParser() cfg_file = tmpdir.join("test.cfg") cfg_file.write(export.to_config('test', argsdict)) config.read(str(cfg_file)) # Modify config from the spec defaults config.set("test", "arg2", "3") # Build argparser parser = export.to_argparser('test', argsdict) parser = argutils.set_parser_defaults(parser, config) args = parser.parse_args(["test"]) # Should reflect the modified default assert args.arg2 == 3 # Should warn since it can't find the section parser = export.to_argparser('test2', argsdict) with pytest.warns(UserWarning): argutils.set_parser_defaults(parser, config)
def __init__(self, name, cfg_file, metadata, workspace): """Create a new command with the given name and argument string. :param name: the command name, used to find the appropriate section in the config file :param cfg_file: the path to the config file :param metadata: a swga.metadata namedtuple generated from database.get_metadata() """ spec = utils.specfile(name) opts = argutils.read.from_yaml(spec) self.name = name self.parser = argutils.export.to_argparser(name, opts) self.workspace = workspace config = SafeConfigParser() if config.read(cfg_file) and config.has_section(name): self.parser = argutils.set_parser_defaults(self.parser, config) self._set_metadata(metadata)
def main(): # Used in the config file and argument parser's help prog_name = "example.py" config = ConfigParser.SafeConfigParser() # Read the spec and build a parser from it argsdict = read.from_yaml(open(SPEC_FILE).read()) parser = export.to_argparser(prog_name, argsdict) # If the config file exists and we can read it, use it to set the # defaults if config.read(CONF_FILE): parser = argutils.set_parser_defaults(parser, config) args = parser.parse_args() if args.init: export.to_config_file(prog_name, argsdict, CONF_FILE) for _ in range(args.times): args.output.write(args.message + "\n")
def main(): # Used in the config file and argument parser's help prog_name = 'example.py' config = ConfigParser.SafeConfigParser() # Read the spec and build a parser from it argsdict = read.from_yaml(open(SPEC_FILE).read()) parser = export.to_argparser(prog_name, argsdict) # If the config file exists and we can read it, use it to set the # defaults if config.read(CONF_FILE): parser = argutils.set_parser_defaults(parser, config) args = parser.parse_args() if args.init: export.to_config_file(prog_name, argsdict, CONF_FILE) for _ in range(args.times): args.output.write(args.message + '\n')