Skip to content

Commit ca1531e

Browse files
committed
Add enum helper
To get all enum values for a specific enum.
1 parent b33ef40 commit ca1531e

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

pyvips/base.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# basic defs and link to ffi
22

33

4-
from pyvips import ffi, vips_lib, gobject_lib, _to_string, _to_bytes, Error
4+
from pyvips import ffi, vips_lib, glib_lib, gobject_lib, _to_string, _to_bytes, Error
55

66

77
def leak_set(leak):
@@ -89,6 +89,24 @@ def type_map(gtype, fn):
8989
return vips_lib.vips_type_map(gtype, cb, ffi.NULL, ffi.NULL)
9090

9191

92+
def values_for_enum(gtype):
93+
"""Get all values for a enum (gtype)."""
94+
95+
g_type_class = gobject_lib.g_type_class_ref(gtype)
96+
g_enum_class = ffi.cast('GEnumClass *', g_type_class)
97+
98+
values = []
99+
100+
# -1 since we always have a "last" member.
101+
for i in range(0, g_enum_class.n_values - 1):
102+
value = _to_string(ffi.string(g_enum_class.values[i].value_nick))
103+
values.append(value)
104+
105+
glib_lib.g_free(g_enum_class)
106+
107+
return values
108+
109+
92110
__all__ = [
93111
'leak_set',
94112
'version',
@@ -100,4 +118,5 @@ def type_map(gtype, fn):
100118
'type_name',
101119
'type_map',
102120
'type_from_name',
121+
'values_for_enum'
103122
]

pyvips/decls.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,15 @@ def cdefs(features):
120120
GType vips_interpretation_get_type (void);
121121
GType vips_operation_flags_get_type (void);
122122
GType vips_band_format_get_type (void);
123+
GType vips_token_get_type (void);
124+
GType vips_saveable_get_type (void);
125+
GType vips_image_type_get_type (void);
123126
124127
typedef struct _GData GData;
125128
126129
typedef struct _GTypeClass GTypeClass;
127130
128-
typedef struct _GTypeInstance
129-
{
131+
typedef struct _GTypeInstance {
130132
GTypeClass *g_class;
131133
} GTypeInstance;
132134
@@ -153,6 +155,24 @@ def cdefs(features):
153155
unsigned int param_id;
154156
} GParamSpec;
155157
158+
typedef struct _GEnumValue {
159+
int value;
160+
161+
const char *value_name;
162+
const char *value_nick;
163+
} GEnumValue;
164+
165+
typedef struct _GEnumClass {
166+
GTypeClass *g_type_class;
167+
168+
int minimum;
169+
int maximum;
170+
unsigned int n_values;
171+
GEnumValue *values;
172+
} GEnumClass;
173+
174+
void* g_type_class_ref (GType type);
175+
156176
void g_object_ref (void* object);
157177
void g_object_unref (void* object);
158178

0 commit comments

Comments
 (0)