Ejemplo n.º 1
0
def type_error_msg(expected_type_name, value, what_for=None):
    """Helper for exception error messages about wrong type."""
    if what_for:
        what_for = ' as ' + what_for
    else:
        what_for = ''
    return "Expected type '{}'{}, but got '{}' of type '{}'.".format(
        expected_type_name, what_for, value, type_name(value))
Ejemplo n.º 2
0
def load_command_definition(command_name):
    for entry_point in pkg_resources.iter_entry_points(group=XYLEM_CMDS_GROUP):
        if entry_point.name == command_name:
            defi = entry_point.load()
            if not isinstance(defi, dict):
                error("Invalid entry point: '{0}', expected dict got '{1}'"
                      .format(entry_point, type_name(defi)))
                return None
            return defi
Ejemplo n.º 3
0
 def help_string(self):
     """Compose help string for command line parser."""
     help = ""
     if self.help:
         help += self.help + "\n\n"
     info = []
     if self.is_group() or self.command_line_argument != dashify(self.name):
         info += ["config: `{}`".format(self.name)]
     info += ["type: {}".format(type_name(self.type))]
     if self.default != self.type.unset_value():
         info.append("default: {}".format(self.default))
     help += "(" + ", ".join(info) + ")"
     return help
Ejemplo n.º 4
0
Archivo: impl.py Proyecto: catkin/xylem
def ensure_installer_context(installer_context, config):
    """Helper for processing ``installer_context`` arguments in public API.

    Return installer context. If ``installer_context`` is none, create
    new one using ``config``.

    :param installer_context: `InstallerContext` or `None`
    :param ConfigDict config: config dict to create installer context with
    :rtype: `InstallerContext`
    :raises ValueError: if ``config`` is invalid type
    """
    if installer_context is None:
        return InstallerContext(config=config)
    if isinstance(installer_context, InstallerContext):
        return installer_context
    raise ValueError("invalid installer context of type '{}'".
                     format(type_name(config)))
Ejemplo n.º 5
0
def ensure_sources_context(sources_context, config):
    """Helper for processing ``sources_context`` arguments in public API.

    Return sources context. If ``sources_context`` is none, create new
    one using ``config``.

    :param sources_context: `SourcesContext` or `None`
    :param ConfigDict config: config dict to create sources context with
    :rtype: `SourcesContext`
    :raises ValueError: if ``config`` is invalid type
    """
    if sources_context is None:
        return SourcesContext(config=config)
    if isinstance(sources_context, SourcesContext):
        return sources_context
    raise ValueError("invalid sources context of type '{}'".
                     format(type_name(config)))
Ejemplo n.º 6
0
def ensure_installer_context(installer_context, config):
    """Helper for processing ``installer_context`` arguments in public API.

    Return installer context. If ``installer_context`` is none, create
    new one using ``config``.

    :param installer_context: `InstallerContext` or `None`
    :param ConfigDict config: config dict to create installer context with
    :rtype: `InstallerContext`
    :raises ValueError: if ``config`` is invalid type
    """
    if installer_context is None:
        return InstallerContext(config=config)
    if isinstance(installer_context, InstallerContext):
        return installer_context
    raise ValueError("invalid installer context of type '{}'".format(
        type_name(config)))
Ejemplo n.º 7
0
def ensure_config(config):
    """Helper for processing ``config`` arguments in public API.

    If ``config`` is ``None``, return :func:`get_config`, if it is of
    type `ConfigDict`, return as-is, if it is a regular `dict`, parse
    combine with defaults from :func:`get_config_description` to return
    a `ConfigDict`, and otherwise raise a `ValueError`.

    :type config: `None` or `ConfigDict` or `dict`
    :rtype: `ConfigDict`
    :raises ValueError: if ``config`` is invalid type
    """
    if config is None:
        return get_config()
    if isinstance(config, ConfigDict):
        return config
    if isinstance(config, dict):
        return config_from_parsed_yaml(
            config, get_config_description(), use_defaults=True)
    raise ValueError("invalid config of type '{}'".format(type_name(config)))
Ejemplo n.º 8
0
def ensure_config(config):
    """Helper for processing ``config`` arguments in public API.

    If ``config`` is ``None``, return :func:`get_config`, if it is of
    type `ConfigDict`, return as-is, if it is a regular `dict`, parse
    combine with defaults from :func:`get_config_description` to return
    a `ConfigDict`, and otherwise raise a `ValueError`.

    :type config: `None` or `ConfigDict` or `dict`
    :rtype: `ConfigDict`
    :raises ValueError: if ``config`` is invalid type
    """
    if config is None:
        return get_config()
    if isinstance(config, ConfigDict):
        return config
    if isinstance(config, dict):
        return config_from_parsed_yaml(config,
                                       get_config_description(),
                                       use_defaults=True)
    raise ValueError("invalid config of type '{}'".format(type_name(config)))
Ejemplo n.º 9
0
def process_config_file_yaml(config):
    """Utility for parsing yaml config files.

    Handles empty files and makes sure config file is dictionary with
    strings as keys.

    :raises ConfigValueError: if config is not ``None`` or dictionary
        with string keys
    """
    if config is None:
        # allow empty config file
        config = dict()
    if not isinstance(config, dict):
        raise ConfigValueError(
            "Config file cannot be interpreted as dictionary. "
            "Parsed as type '{}'.".format(type_name(config)))
    for key in config:
        if not isinstance(key, text_type):
            raise ConfigValueError(
                type_error_msg('text', key, what_for="config key"))
    return config