File: __init__.py

package info (click to toggle)
python-gssapi 1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 876 kB
  • sloc: python: 3,707; sh: 198; makefile: 154; ansic: 60
file content (41 lines) | stat: -rw-r--r-- 1,097 bytes parent folder | download | duplicates (3)
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
import typing as t

from enum import EnumMeta


_extra_values: t.Dict[str, t.Dict[str, t.Any]] = {}


def register_value(
    cl_str: str,
    name: str,
    value: t.Any,
) -> None:
    _extra_values[cl_str] = _extra_values.get(cl_str, {})
    _extra_values[cl_str][name] = value


class ExtendableEnum(EnumMeta):
    def __new__(
        metacl,
        name: str,
        bases: t.Tuple[t.Type],
        classdict: t.Dict[str, t.Any],
    ) -> "ExtendableEnum":
        extra_vals = _extra_values.get(name)

        if extra_vals is not None:
            for extra_name, extra_val in list(extra_vals.items()):
                if extra_name in classdict:
                    raise AttributeError(
                        "Enumeration extensions cannot override existing "
                        "enumeration members")
                else:
                    classdict[extra_name] = extra_val

        return super(ExtendableEnum, metacl).__new__(
            metacl,
            name,
            bases,
            classdict,  # type: ignore[arg-type] # Uses private explicit type
        )