# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import yaml
from .errors import LinterNotFound, LinterParseError from .types import supported_types
GLOBAL_SUPPORT_FILES = []
class Parser(object): """Reads and validates lint configuration files."""
missing_attrs = [] for attr in self.required_attributes: if attr notin linter:
missing_attrs.append(attr)
if missing_attrs: raise LinterParseError(
relpath, "Missing required attribute(s): ""{}".format(",".join(missing_attrs)),
)
if linter["type"] notin supported_types: raise LinterParseError(relpath, "Invalid type '{}'".format(linter["type"]))
for attr in ("include", "exclude", "support-files"): if attr notin linter: continue
ifnot isinstance(linter[attr], list) ornot all(
isinstance(a, str) for a in linter[attr]
): raise LinterParseError(
relpath, "The {} directive must be a ""list of strings!".format(attr),
)
invalid_paths = set() for path in linter[attr]: if"*"in path: if attr == "include": raise LinterParseError(
relpath, "Paths in the include directive cannot " "contain globs:\n {}".format(path),
) continue
if invalid_paths: raise LinterParseError(
relpath, "The {} directive contains the following " "paths that don't exist:\n{}".format(
attr, "\n".join(sorted(invalid_paths))
),
)
if"setup"in linter: if linter["setup"].count(":") != 1: raise LinterParseError(
relpath, "The setup attribute '{!r}' must have the " "form 'module:object'".format(linter["setup"]),
)
if"extensions"in linter and"exclude_extensions"in linter: raise LinterParseError(
relpath, "Can't have both 'extensions' and 'exclude_extensions'!",
)
for prop in ["extensions", "exclude_extensions"]: if prop in linter:
linter[prop] = [e.strip(".") for e in linter[prop]]
def parse(self, path): """Read a linter and return its LINTER definition.
:param path: Path to the linter.
:returns: List of linter definitions ([dict])
:raises: LinterNotFound, LinterParseError """ ifnot os.path.isfile(path): raise LinterNotFound(path)
ifnot path.endswith(".yml"): raise LinterParseError(
path, "Invalid filename, linters must end with '.yml'!"
)
with open(path) as fh:
configs = list(yaml.safe_load_all(fh))
ifnot configs: raise LinterParseError(path, "No lint definitions found!")
linters = [] for config in configs: for name, linter in config.items():
linter["name"] = name
linter["path"] = path
self._validate(linter)
linter.setdefault("support-files", []).extend(
GLOBAL_SUPPORT_FILES + [path]
)
linter.setdefault("include", ["."])
linters.append(linter)
return linters
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.