How to use the peru.parser.ParserError function in peru

To help you get started, we’ve selected a few peru 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 buildinspace / peru / tests / test_parser.py View on Github external
def test_build_field_deprecated_message(self):
        input = dedent('''\
            rule foo:
                build: shell command
            ''')
        try:
            parse_string(input)
        except ParserError as e:
            assert 'The "build" field is no longer supported.' in e.message
        else:
            assert False, 'expected ParserError'
github buildinspace / peru / peru / parser.py View on Github external
def _extract_named_rules(blob, name_prefix):
    scope = {}
    for field in list(blob.keys()):
        parts = field.split(' ')
        if len(parts) == 2 and parts[0] == "rule":
            _, name = parts
            if name in scope:
                raise ParserError('Rule "{}" already exists.'.format(name))
            inner_blob = typesafe_pop(blob, field)
            inner_blob = {} if inner_blob is None else inner_blob
            rule = _extract_rule(name_prefix + name, inner_blob)
            if inner_blob:
                raise ParserError("Unknown rule fields: " +
                                  ", ".join(inner_blob.keys()))
            scope[name] = rule
    return scope
github buildinspace / peru / peru / parser.py View on Github external
def _extract_rule(name, blob):
    _validate_name(name)
    if 'build' in blob:
        raise ParserError(
            textwrap.dedent('''\
            The "build" field is no longer supported. If you need to
            untar/unzip a curl module, use the "unpack" field.'''))
    if 'files' in blob:
        raise ParserError(
            'The "files" field is no longer supported. Use "pick" instead.')
    copy = _extract_multimap_field(blob, 'copy')
    move = _extract_multimap_field(blob, 'move')
    executable = _extract_optional_list_field(blob, 'executable')
    drop = _extract_optional_list_field(blob, 'drop')
    pick = _extract_optional_list_field(blob, 'pick')
    export = typesafe_pop(blob, 'export', None)
    if not any((copy, move, executable, drop, pick, export)):
        return None
    rule = Rule(name, copy, move, executable, drop, pick, export)
    return rule
github buildinspace / peru / peru / parser.py View on Github external
def _extract_rule(name, blob):
    _validate_name(name)
    if 'build' in blob:
        raise ParserError(
            textwrap.dedent('''\
            The "build" field is no longer supported. If you need to
            untar/unzip a curl module, use the "unpack" field.'''))
    if 'files' in blob:
        raise ParserError(
            'The "files" field is no longer supported. Use "pick" instead.')
    copy = _extract_multimap_field(blob, 'copy')
    move = _extract_multimap_field(blob, 'move')
    executable = _extract_optional_list_field(blob, 'executable')
    drop = _extract_optional_list_field(blob, 'drop')
    pick = _extract_optional_list_field(blob, 'pick')
    export = typesafe_pop(blob, 'export', None)
    if not any((copy, move, executable, drop, pick, export)):
        return None
    rule = Rule(name, copy, move, executable, drop, pick, export)
    return rule
github buildinspace / peru / peru / parser.py View on Github external
def _validate_name(name):
    if re.search(r"[\s:.]", name):
        raise ParserError("Invalid name: " + repr(name))
    return name
github buildinspace / peru / peru / parser.py View on Github external
def _parse_toplevel(blob, name_prefix):
    modules = _extract_modules(blob, name_prefix)
    rules = _extract_named_rules(blob, name_prefix)
    imports = _extract_multimap_field(blob, 'imports')
    if blob:
        raise ParserError("Unknown toplevel fields: " + ", ".join(blob.keys()))
    return Scope(modules, rules), imports
github buildinspace / peru / peru / parser.py View on Github external
- baz/'''
    message = ('"{}" field must be a map whose values are either a string or '
               'list of strings.'.format(name))
    raw_map = typesafe_pop(blob, name, {}) or {}
    if not isinstance(raw_map, dict):
        raise ParserError(message)
    # We use an `OrderedDict` to ensure that multimap fields are processed in a
    # determinist order. This prevents obscure bugs caused by subtly different
    # behavior.
    multimap = collections.OrderedDict()
    # Sort by key so that processing occurs in convenient lexographical order.
    # This keeps things deterministic in a simple way.
    for key, raw_value in sorted(raw_map.items()):
        value = _optional_list(raw_value)
        if value is None:
            raise ParserError(message)
        multimap[key] = value  # Remembers order.
    return multimap
github buildinspace / peru / peru / parser.py View on Github external
def typesafe_pop(d, field, default=object()):
    if not isinstance(d, dict):
        raise ParserError('Error parsing peru file: {} is not a map.'.format(
            repr(d)))
    if default == typesafe_pop.__defaults__[0]:
        return d.pop(field)
    else:
        return d.pop(field, default)