File: compat.py

package info (click to toggle)
pg-activity 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,144 kB
  • sloc: python: 3,902; sql: 1,067; sh: 5; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,177 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

import operator
from importlib.metadata import version
from typing import Any

import attr
import attr.validators
import blessed

ATTR_VERSION = tuple(int(x) for x in version("attrs").split(".", 2)[:2])
BLESSED_VERSION = tuple(int(x) for x in version("blessed").split(".", 2)[:2])

if ATTR_VERSION < (18, 1):

    def fields_dict(cls: Any) -> dict[str, Any]:
        return {a.name: a for a in cls.__attrs_attrs__}

else:
    fields_dict = attr.fields_dict

if ATTR_VERSION < (21, 3):

    @attr.s(auto_attribs=True, frozen=True, slots=True)
    class gt:
        bound: int

        def __call__(self, instance: Any, attribute: Any, value: Any) -> None:
            if not operator.gt(value, self.bound):
                raise ValueError(f"'{attribute.name}' must be > {self.bound}: {value}")

else:
    gt = attr.validators.gt  # type: ignore[assignment,misc]


if BLESSED_VERSION < (1, 17):

    def link(term: blessed.Terminal, url: str, text: str, url_id: str = "") -> str:
        return url

else:

    def link(term: blessed.Terminal, url: str, text: str, url_id: str = "") -> str:
        return term.link(url, text, url_id=url_id)