| 1 |
# -*- Mode: Python; py-indent-offset: 4 -*- |
| 2 |
# vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
# |
| 4 |
# Copyright (C) 2025 James Henstridge <[email protected]> |
| 5 |
# |
| 6 |
# _enum.py: base classes for enumeration and flags types. |
| 7 |
# |
| 8 |
# This library is free software; you can redistribute it and/or |
| 9 |
# modify it under the terms of the GNU Lesser General Public |
| 10 |
# License as published by the Free Software Foundation; either |
| 11 |
# version 2.1 of the License, or (at your option) any later version. |
| 12 |
# |
| 13 |
# This library is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
# Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public |
| 19 |
# License along with this library; if not, write to the Free Software |
| 20 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 21 |
# USA |
| 22 |
|
| 23 |
import enum |
| 24 |
import warnings |
| 25 |
|
| 26 |
from . import _gi |
| 27 |
|
| 28 |
|
| 29 |
class GEnumMeta(enum.EnumMeta): |
| 30 |
def __new__(metacls, name, bases, classdict, /, **kwargs): |
| 31 |
enum_class = super().__new__(metacls, name, bases, classdict, **kwargs) |
| 32 |
|
| 33 |
# If __gtype__ is not set, this is a new enum or flags defined |
| 34 |
# from Python. Register a new GType for it. |
| 35 |
if "__gtype__" not in enum_class.__dict__: |
| 36 |
type_name = enum_class.__dict__.get("__gtype_name__") |
| 37 |
_gi.enum_register(enum_class, type_name) |
| 38 |
|
| 39 |
return enum_class |
| 40 |
|
| 41 |
@property |
| 42 |
def __enum_values__(self): |
| 43 |
from gi import PyGIDeprecationWarning |
| 44 |
|
| 45 |
warnings.warn( |
| 46 |
'Calling "__enum_values__" on enums is deprecated.', PyGIDeprecationWarning |
| 47 |
) |
| 48 |
return {m.value: m for m in self} |
| 49 |
|
| 50 |
|
| 51 |
class GEnum(enum.IntEnum, metaclass=GEnumMeta): |
| 52 |
__module__ = _gi.__name__ |
| 53 |
__gtype__ = None |
| 54 |
|
| 55 |
|
| 56 |
class GFlagsMeta(enum.EnumMeta): |
| 57 |
def __new__(metacls, name, bases, classdict, /, **kwargs): |
| 58 |
flags_class = super().__new__(metacls, name, bases, classdict, **kwargs) |
| 59 |
|
| 60 |
# If __gtype__ is not set, this is a new enum or flags defined |
| 61 |
# from Python. Register a new GType for it. |
| 62 |
if "__gtype__" not in flags_class.__dict__: |
| 63 |
type_name = flags_class.__dict__.get("__gtype_name__") |
| 64 |
_gi.flags_register(flags_class, type_name) |
| 65 |
|
| 66 |
return flags_class |
| 67 |
|
| 68 |
@property |
| 69 |
def __flags_values__(self): |
| 70 |
from gi import PyGIDeprecationWarning |
| 71 |
|
| 72 |
warnings.warn( |
| 73 |
'Calling "__flags_values__" on flags is deprecated. ', |
| 74 |
PyGIDeprecationWarning, |
| 75 |
) |
| 76 |
return {m.value: m for m in self} |
| 77 |
|
| 78 |
|
| 79 |
class GFlags(enum.IntFlag, metaclass=GFlagsMeta): |
| 80 |
__module__ = _gi.__name__ |
| 81 |
__gtype__ = None |