summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/loaders.py
blob: c369c892aae7699e532bcacf3fc51914d491de09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Utilities for loading various files."""

from __future__ import annotations

import logging
import os
from collections import defaultdict
from functools import partial
from typing import TYPE_CHECKING, Any, NamedTuple

import yaml
from yaml import YAMLError

try:
    from yaml import CFullLoader as FullLoader
    from yaml import CSafeLoader as SafeLoader
except (ImportError, AttributeError):
    from yaml import FullLoader, SafeLoader  # type: ignore[assignment]

if TYPE_CHECKING:
    from pathlib import Path


class IgnoreFile(NamedTuple):
    """IgnoreFile n."""

    default: str
    alternative: str


IGNORE_FILE = IgnoreFile(".ansible-lint-ignore", ".config/ansible-lint-ignore.txt")

yaml_load = partial(yaml.load, Loader=FullLoader)
yaml_load_safe = partial(yaml.load, Loader=SafeLoader)
_logger = logging.getLogger(__name__)


def yaml_from_file(filepath: str | Path) -> Any:
    """Return a loaded YAML file."""
    with open(str(filepath), encoding="utf-8") as content:
        return yaml_load(content)


def load_ignore_txt(filepath: Path | None = None) -> dict[str, set[str]]:
    """Return a list of rules to ignore."""
    result = defaultdict(set)

    ignore_file = None

    if filepath:
        if os.path.isfile(filepath):
            ignore_file = str(filepath)
        else:
            _logger.error("Ignore file not found '%s'", ignore_file)
    elif os.path.isfile(IGNORE_FILE.default):
        ignore_file = IGNORE_FILE.default
    elif os.path.isfile(IGNORE_FILE.alternative):
        ignore_file = IGNORE_FILE.alternative

    if ignore_file:
        with open(ignore_file, encoding="utf-8") as _ignore_file:
            _logger.debug("Loading ignores from '%s'", ignore_file)
            for line in _ignore_file:
                entry = line.split("#")[0].rstrip()
                if entry:
                    try:
                        path, rule = entry.split()
                    except ValueError as exc:
                        msg = f"Unable to parse line '{line}' from {ignore_file} file."
                        raise RuntimeError(msg) from exc
                    result[path].add(rule)

    return result


__all__ = [
    "load_ignore_txt",
    "yaml_from_file",
    "yaml_load",
    "yaml_load_safe",
    "YAMLError",
    "IGNORE_FILE",
]