| 1 |
# SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
|
| 4 |
"""Constant definitions for the feature-check Python library.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import abc |
| 9 |
import dataclasses |
| 10 |
import typing |
| 11 |
|
| 12 |
|
| 13 |
if typing.TYPE_CHECKING: |
| 14 |
from . import version as fver |
| 15 |
|
| 16 |
|
| 17 |
DEFAULT_OPTION = "--features" |
| 18 |
"""The default command-line option to pass to a program to query for supported features.""" |
| 19 |
|
| 20 |
DEFAULT_PREFIX = "Features: " |
| 21 |
"""The default prefix of the program's features output line.""" |
| 22 |
|
| 23 |
DEFAULT_OUTPUT_FMT = "tsv" |
| 24 |
"""The default output format for the `feature-check` command-line tool.""" |
| 25 |
|
| 26 |
VERSION = "2.3.2" |
| 27 |
"""The feature-check library version, SemVer-style.""" |
| 28 |
|
| 29 |
VERSION_STRING = VERSION |
| 30 |
|
| 31 |
|
| 32 |
class FCError(Exception): |
| 33 |
"""A base class for errors in handling a feature-check request.""" |
| 34 |
|
| 35 |
def __init__(self, code: int, msg: str) -> None: |
| 36 |
"""Initialize an error object.""" |
| 37 |
super().__init__(msg) |
| 38 |
self._code = code |
| 39 |
self._msg = msg |
| 40 |
|
| 41 |
@property |
| 42 |
def code(self) -> int: |
| 43 |
"""Return the numeric error code.""" |
| 44 |
return self._code |
| 45 |
|
| 46 |
@property |
| 47 |
def message(self) -> str: |
| 48 |
"""Return a human-readable error message.""" |
| 49 |
return self._msg |
| 50 |
|
| 51 |
|
| 52 |
@dataclasses.dataclass(frozen=True) |
| 53 |
class Result: |
| 54 |
"""The base class for an expression result.""" |
| 55 |
|
| 56 |
|
| 57 |
@dataclasses.dataclass(frozen=True) |
| 58 |
class Expr(abc.ABC): |
| 59 |
"""The (pretty much abstract) base class for an expression.""" |
| 60 |
|
| 61 |
@abc.abstractmethod |
| 62 |
def evaluate(self, data: dict[str, fver.Version]) -> Result: |
| 63 |
"""Evaluate the expression and return a Result object. |
| 64 |
|
| 65 |
Overridden in actual expression classes. |
| 66 |
""" |
| 67 |
raise NotImplementedError( |
| 68 |
f"{type(self).__name__}.evaluate() must be overridden", # noqa: EM102 |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
@dataclasses.dataclass(frozen=True) |
| 73 |
class Mode: |
| 74 |
"""Base class for the feature-check operating modes.""" |
| 75 |
|
| 76 |
|
| 77 |
@dataclasses.dataclass(frozen=True) |
| 78 |
class ModeList(Mode): |
| 79 |
"""List the features supported by the program.""" |
| 80 |
|
| 81 |
|
| 82 |
@dataclasses.dataclass(frozen=True) |
| 83 |
class ModeSingle(Mode): |
| 84 |
"""Query for the presence or the version of a single feature.""" |
| 85 |
|
| 86 |
feature: str |
| 87 |
ast: Expr |
| 88 |
|
| 89 |
|
| 90 |
@dataclasses.dataclass(frozen=True) |
| 91 |
class ModeSimple(Mode): |
| 92 |
"""Verify whether a simple 'feature op version' expression holds true.""" |
| 93 |
|
| 94 |
ast: Expr |