File: _enum.py

package info (click to toggle)
pygobject 3.54.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,864 kB
  • sloc: ansic: 40,281; python: 26,363; sh: 477; makefile: 81; xml: 35; cpp: 1
file content (81 lines) | stat: -rw-r--r-- 2,717 bytes parent folder | download
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
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
#
# Copyright (C) 2025 James Henstridge <james@jamesh.id.au>
#
#   _enum.py: base classes for enumeration and flags types.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

import enum
import warnings

from . import _gi


class GEnumMeta(enum.EnumMeta):
    def __new__(metacls, name, bases, classdict, /, **kwargs):
        enum_class = super().__new__(metacls, name, bases, classdict, **kwargs)

        # If __gtype__ is not set, this is a new enum or flags defined
        # from Python. Register a new GType for it.
        if "__gtype__" not in enum_class.__dict__:
            type_name = enum_class.__dict__.get("__gtype_name__")
            _gi.enum_register(enum_class, type_name)

        return enum_class

    @property
    def __enum_values__(self):
        from gi import PyGIDeprecationWarning

        warnings.warn(
            'Calling "__enum_values__" on enums is deprecated.', PyGIDeprecationWarning
        )
        return {m.value: m for m in self}


class GEnum(enum.IntEnum, metaclass=GEnumMeta):
    __module__ = _gi.__name__
    __gtype__ = None


class GFlagsMeta(enum.EnumMeta):
    def __new__(metacls, name, bases, classdict, /, **kwargs):
        flags_class = super().__new__(metacls, name, bases, classdict, **kwargs)

        # If __gtype__ is not set, this is a new enum or flags defined
        # from Python. Register a new GType for it.
        if "__gtype__" not in flags_class.__dict__:
            type_name = flags_class.__dict__.get("__gtype_name__")
            _gi.flags_register(flags_class, type_name)

        return flags_class

    @property
    def __flags_values__(self):
        from gi import PyGIDeprecationWarning

        warnings.warn(
            'Calling "__flags_values__" on flags is deprecated. ',
            PyGIDeprecationWarning,
        )
        return {m.value: m for m in self}


class GFlags(enum.IntFlag, metaclass=GFlagsMeta):
    __module__ = _gi.__name__
    __gtype__ = None