How to use the configargparse.ConfigFileParser function in ConfigArgParse

To help you get started, we’ve selected a few ConfigArgParse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github AntreasAntoniou / DeepClassificationBot / tasks.py View on Github external
def create_standalone_instance(ctx, name, zone, machine_type, address, bot_config, stackdriver_logging):
    args = [
        'gcloud', 'compute', 'instances', 'create', name,
        '--image', 'container-vm',
        '--zone', zone,
        '--machine-type', machine_type,
    ]

    if address:
        args.append('--address')
        args.append(address)

    with open(bot_config) as f:
        bot_config_content = ConfigFileParser().parse(f)
    if len(bot_config_content):
        secret_args = [
            '--metadata',
            ','.join('='.join(item) for item in bot_config_content.items()),
        ]

    with temp_dir() as d:
        # add metadata from file
        args.append('--metadata-from-file')
        metadata_files = ['google-container-manifest=etc/standalone-bot-containers.yaml']
        startup_script_path = os.path.join(d, 'startup-script.sh')
        if stackdriver_logging:
            urllib.urlretrieve(LOGGING_AGENT_INSTALL_SCRIPT, startup_script_path)
        with open(startup_script_path, 'a') as f:
            f.write('\nmkdir -p /var/log/bot\n')
        metadata_files.append('startup-script={}'.format(startup_script_path))
github bw2 / ConfigArgParse / configargparse.py View on Github external
Args:
            items: an OrderedDict of items to be converted to the config file
            format. Keys should be strings, and values should be either strings
            or lists.

        Returns:
            Contents of config file as a string
        """
        raise NotImplementedError("serialize(..) not implemented")


class ConfigFileParserException(Exception):
    """Raised when config file parsing failed."""


class DefaultConfigFileParser(ConfigFileParser):
    """Based on a simplified subset of INI and YAML formats. Here is the
    supported syntax:


        # this is a comment
        ; this is also a comment (.ini style)
        ---            # lines that start with --- are ignored (yaml style)
        -------------------
        [section]      # .ini-style section names are treated as comments

        # how to specify a key-value pair (all of these are equivalent):
        name value     # key is case sensitive: "Name" isn't "name"
        name = value   # (.ini style)  (white space is ignored, so name = value same as name=value)
        name: value    # (yaml style)
        --name value   # (argparse style)
github bw2 / ConfigArgParse / configargparse.py View on Github external
return items

    def serialize(self, items):
        """Does the inverse of config parsing by taking parsed values and
        converting them back to a string representing config file contents.
        """
        r = StringIO()
        for key, value in items.items():
            if isinstance(value, list):
                # handle special case of lists
                value = "["+", ".join(map(str, value))+"]"
            r.write("{} = {}\n".format(key, value))
        return r.getvalue()


class YAMLConfigFileParser(ConfigFileParser):
    """Parses YAML config files. Depends on the PyYAML module.
    https://pypi.python.org/pypi/PyYAML
    """

    def get_syntax_description(self):
        msg = ("The config file uses YAML syntax and must represent a YAML "
            "'mapping' (for details, see http://learn.getgrav.org/advanced/yaml).")
        return msg

    def _load_yaml(self):
        """lazy-import PyYAML so that configargparse doesn't have to dependend
        on it unless this parser is used."""
        try:
            import yaml
        except ImportError:
            raise ConfigFileParserException("Could not import yaml. "