def __init__(self, config_dir, args): self.config_dir = config_dir self.args = _normalize_args(args) logger.debug("Generator: config_dir: %s, args: %s", config_dir, args) self._doc_string = Generator.__doc__.format( options=docstring.Generator(config_dir).generate()) self.settings = None self.output_file = None self.rules_file = None self._rules = None self.parsed = None self.extra_vars = None
def usage(path): doc_string = "{docs} \n Valid configs are: {config}".format( docs=__doc__, config=docstring.Generator(path).generate()) print(doc_string)
def generate(config_dir, args): """ Usage: generate [options] <output-file> generate [--extra-vars=KEY_PAIR]... [options] <output-file> Options: --extra-vars=<val>... Provide extra vars {options} """ logging.debug("config_dir: %s, args: %s", config_dir, args) doc_string = generate.__doc__.format( options=docstring.Generator(config_dir).generate()) logging.debug("Parsing: %s", args) logging.debug("DocString for Generate: %s", doc_string) parsed = docopt(doc_string, options_first=True, argv=args) logging.info("Parsed: \n%s", parsed) output_file = _extract_value_for_option(parsed, '<output-file>') extra_vars = _extract_value_for_option(parsed, '--extra-vars', must_exist=False) # create the settings tree and preserve the order in which arguments # are passed. Convert all args into an ordered tree so that # --foo fooz --too moo --foo-bar baaz --foo-arg vali # will be like the ordered tree below # foo: # <special-key>: fooz # bar: ### <-- foo-bar is not foo/bar # <special-key>: baaz # arg: ### <-- arg comes after bar # <special-key>: val # too: # <special-key>: moo settings_tree = OrderedTree(delimiter='-') # filter only options; [ --foo, fooz, --bar baz ] -> [--foo, --bar] options = [x for x in args if x.startswith('--')] for option in options: # iterate options to preserve order of args option = option.split('=')[0] value = parsed.get(option) if not value: continue key = option[2:] + '-' + settings.VALUES_KEY settings_tree[key] = value logging.debug("%s: %s", key, value) logging.debug( yaml_utils.to_yaml("Directory structure from args:", settings_tree)) loader = settings.Loader(config_dir, settings_tree) _update_extra_vars(extra_vars, loader) all_settings = loader.settings_tree() logging.debug("\n" + yaml.safe_dump(all_settings, default_flow_style=False)) logging.info("Writing to file: %s", output_file) with open(output_file, 'w') as out: out.write(yaml.safe_dump(all_settings, default_flow_style=False))