Ejemplo n.º 1
0
 def parse_single(input):
     try:
         result = load_yaml(input)
     except YAMLError:
         result = None
     if not isinstance(result, dict):
         # special parsing allowing lists without enclosing `{}`
         try:
             result = load_yaml("{" + input + "}")
         except YAMLError as e:
             raise_from(ConfigValueError,
                        "failed to parse `{}` as dict".format(input), e)
     if not isinstance(result, dict):
         raise ConfigValueError(type_error_msg("dict", result))
     return result
Ejemplo n.º 2
0
def main(args=None):
    args = command_handle_args(args, definition)
    try:
        # prepare arguments
        filepath = os.path.abspath(os.path.expanduser(args.rules_file))

        # parse rules file
        with open(filepath, 'rb') as f:
            data = to_str(f.read())
        rules = load_yaml(data)
        rules = expand_rules(rules)
        verify_rules_dict(rules)

        # compact rules
        compacted = compact_rules(rules,
                                  OSSupport().get_default_installer_names())

        # output result
        dump = dump_yaml(compacted)
        if args.write:
            with open(filepath, 'wb') as f:
                f.write(to_bytes(dump))
        else:
            info(dump)
    except (KeyboardInterrupt, EOFError):
        sys.exit(1)
Ejemplo n.º 3
0
    def from_command_line(self, value):
        """Create config value from value of command line parser.

        A value of `UNSET_COMMAND_LINE` should be interpreted as
        'unset'.

        The default implementation parses the input string as yaml and
        then invokes :meth:`from_yaml`.

        Before returning a value, :meth:`verify` should be used to
        verify the validity of the parsed value.

        :param value: value from the command line parser, which is a
            string when :meth:`command_line_multiple()` is false and a
            list of strings otherwise, or `UNSET_COMMAND_LINE` if the
            argument was omitted on the command line.
        :raises ConfigValueError: if ``value`` cannot be parsed properly
            or has invalid type or structure
        """
        if value == UNSET_COMMAND_LINE:
            return self.unset_value()
        try:
            return self.from_yaml(load_yaml(value))
        except YAMLError as e:
            raise_from(
                ConfigValueError, "parsing command line value failed", e)
Ejemplo n.º 4
0
def load_config_file_yaml(path):
    """Utility for loading yaml config files.

    Makes sure to always return a dict with strings as keys.  Non-
    existent or empty files result in an empty dictionary.

    :raises ConfigValueError: if config file cannot be opened, decode or
        parsed
    """
    try:
        if os.path.isfile(path):
            with open(path, 'rb') as f:
                binary_data = f.read()
            config = load_yaml(to_str(binary_data))
        else:
            config = None
        return process_config_file_yaml(config)
    except EnvironmentError as e:
        raise_from(ConfigValueError,
                   "failed to read config file `{}`".format(path), e)
    except UnicodeError as e:
        raise_from(ConfigValueError,
                   "failed to decode config file `{}`".format(path), e)
    except YAMLError as e:
        raise_from(ConfigValueError,
                   "failed to parse config file as YAML `{}`".format(path), e)
    except ConfigValueError as e:
        raise_from(ConfigValueError,
                   "config file has invalid structure `{}`".format(path), e)
Ejemplo n.º 5
0
def parse_source_descriptions(data, file_path='<string>'):
    """Parse a YAML string as source descriptions.

    If parsing failes an error message is printed to console and an
    empty list is returned.

    :param str data: string containing YAML representation of source
        descriptions
    :param str file_path: name of the file whose contents ``data``
        contains
    :returns: tuple of ``file_path`` and parsed source descriptions
    :rtype: `tuple(str, list)`
    """
    try:
        descriptions = load_yaml(data)
        verify_source_description_list(descriptions)

    except yaml.YAMLError as exc:
        if hasattr(exc, 'problem_mark'):
            mark = exc.problem_mark.line
            col = exc.problem_mark.column
            error("Invalid YAML in source list file '{0}' at '{1}:{2}':\n"
                  .format(file_path, mark + 1, col + 1) + to_str(exc))
        else:
            error("Invalid YAML in source list file '{0}':\n"
                  .format(file_path) + to_str(exc))
        descriptions = []
    return (file_path, descriptions)
Ejemplo n.º 6
0
def main(args=None):
    args = command_handle_args(args, definition)
    try:
        # prepare arguments
        filepath = os.path.abspath(os.path.expanduser(args.rules_file))

        # parse rules file
        with open(filepath, 'rb') as f:
            data = to_str(f.read())
        rules = load_yaml(data)
        rules = expand_rules(rules)
        verify_rules_dict(rules)

        # compact rules
        compacted = compact_rules(
            rules, OSSupport().get_default_installer_names())

        # output result
        dump = dump_yaml(compacted)
        if args.write:
            with open(filepath, 'wb') as f:
                f.write(to_bytes(dump))
        else:
            info(dump)
    except (KeyboardInterrupt, EOFError):
        sys.exit(1)
Ejemplo n.º 7
0
 def load_data(self, arguments):
     # TODO: Can we fast-fail if there is no connectivity (instead of
     # doing the retries)? How to find out if there is no internet
     # connection in general (as opposed to the resources not being
     # accessible)?
     data = load_url(arguments)
     rules = load_yaml(data)
     rules = expand_rules(rules)
     return rules
Ejemplo n.º 8
0
def _parse_rules(rules_str):
    rules = expand_rules(load_yaml(rules_str))
    verify_rules_dict(rules)
    return rules
Ejemplo n.º 9
0
from xylem.specs.plugins.rules import compact_rules
from xylem.yaml_utils import load_yaml

_default_installers = dict(ubuntu="apt", debian="apt")

test1 = load_yaml("""
foo:
  ubuntu: libfoo
  debian: libfoo
bar:
  ubuntu:
    lucid: libbar-1.2
    maverick:
    any_version: libbar
  any_os:
    pip: bar
baz:
  any_os:
    any_version:
        pip: [baz]
  ubuntu: [libbaz, libbaz-dev]
unicöde:
  fedorä:
    schröndinger’s:
      füü:
        [baß]
""")

expected1 = load_yaml("""
foo:
  ubuntu:
    any_version: