| 1 |
# -*- Mode: Python; py-indent-offset: 4 -*- |
| 2 |
# vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
# |
| 4 |
# Copyright (C) 2012 Canonical Ltd. |
| 5 |
# Author: Martin Pitt <[email protected]> |
| 6 |
# Copyright (C) 2012-2013 Simon Feltman <[email protected]> |
| 7 |
# Copyright (C) 2012 Bastian Winkler <[email protected]> |
| 8 |
# |
| 9 |
# This library is free software; you can redistribute it and/or |
| 10 |
# modify it under the terms of the GNU Lesser General Public |
| 11 |
# License as published by the Free Software Foundation; either |
| 12 |
# version 2.1 of the License, or (at your option) any later version. |
| 13 |
# |
| 14 |
# This library is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
# Lesser General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU Lesser General Public |
| 20 |
# License along with this library; if not, write to the Free Software |
| 21 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 22 |
# USA |
| 23 |
|
| 24 |
from __future__ import annotations |
| 25 |
|
| 26 |
import functools |
| 27 |
import warnings |
| 28 |
import typing |
| 29 |
from collections import namedtuple |
| 30 |
|
| 31 |
from gi.overrides import override, deprecated_attr |
| 32 |
from gi.repository import GLib |
| 33 |
from gi import PyGIDeprecationWarning |
| 34 |
from gi import _propertyhelper as propertyhelper |
| 35 |
from gi import _signalhelper as signalhelper |
| 36 |
from gi import _gi |
| 37 |
|
| 38 |
# Typing relies on https://github.com/pygobject/pygobject-stubs. |
| 39 |
if typing.TYPE_CHECKING: |
| 40 |
# Import stubs for type checking this file. |
| 41 |
# FIXME: For IDE typing support, you need to copy |
| 42 |
# pygobject-stubs/src/gi-stubs/repository/*.pyi to gi/repository/ |
| 43 |
from gi.repository import GObject as GObjectModule |
| 44 |
|
| 45 |
# Type annotations cannot have `GObject.` prefix because they are copied into |
| 46 |
# GObject stubs module which cannot refer to itself. Use type aliases. |
| 47 |
GType: typing.TypeAlias = GObjectModule.GType |
| 48 |
Object = GObjectModule.Object |
| 49 |
ConnectFlags = GObjectModule.ConnectFlags |
| 50 |
else: |
| 51 |
from gi.module import get_introspection_module |
| 52 |
|
| 53 |
GObjectModule = get_introspection_module("GObject") |
| 54 |
|
| 55 |
|
| 56 |
__all__ = [] |
| 57 |
|
| 58 |
|
| 59 |
# API aliases for backwards compatibility |
| 60 |
for name in [ |
| 61 |
"markup_escape_text", |
| 62 |
"get_application_name", |
| 63 |
"set_application_name", |
| 64 |
"get_prgname", |
| 65 |
"set_prgname", |
| 66 |
"main_depth", |
| 67 |
"filename_display_basename", |
| 68 |
"filename_display_name", |
| 69 |
"filename_from_utf8", |
| 70 |
"uri_list_extract_uris", |
| 71 |
"MainLoop", |
| 72 |
"MainContext", |
| 73 |
"main_context_default", |
| 74 |
"source_remove", |
| 75 |
"Source", |
| 76 |
"Idle", |
| 77 |
"Timeout", |
| 78 |
"PollFD", |
| 79 |
"idle_add", |
| 80 |
"timeout_add", |
| 81 |
"timeout_add_seconds", |
| 82 |
"io_add_watch", |
| 83 |
"child_watch_add", |
| 84 |
"get_current_time", |
| 85 |
"spawn_async", |
| 86 |
]: |
| 87 |
globals()[name] = getattr(GLib, name) |
| 88 |
deprecated_attr("GObject", name, "GLib." + name) |
| 89 |
__all__.append(name) |
| 90 |
|
| 91 |
# deprecated constants |
| 92 |
for name in [ |
| 93 |
"PRIORITY_DEFAULT", |
| 94 |
"PRIORITY_DEFAULT_IDLE", |
| 95 |
"PRIORITY_HIGH", |
| 96 |
"PRIORITY_HIGH_IDLE", |
| 97 |
"PRIORITY_LOW", |
| 98 |
"IO_IN", |
| 99 |
"IO_OUT", |
| 100 |
"IO_PRI", |
| 101 |
"IO_ERR", |
| 102 |
"IO_HUP", |
| 103 |
"IO_NVAL", |
| 104 |
"IO_STATUS_ERROR", |
| 105 |
"IO_STATUS_NORMAL", |
| 106 |
"IO_STATUS_EOF", |
| 107 |
"IO_STATUS_AGAIN", |
| 108 |
"IO_FLAG_APPEND", |
| 109 |
"IO_FLAG_NONBLOCK", |
| 110 |
"IO_FLAG_IS_READABLE", |
| 111 |
"IO_FLAG_IS_WRITEABLE", |
| 112 |
"IO_FLAG_IS_SEEKABLE", |
| 113 |
"IO_FLAG_MASK", |
| 114 |
"IO_FLAG_GET_MASK", |
| 115 |
"IO_FLAG_SET_MASK", |
| 116 |
"SPAWN_LEAVE_DESCRIPTORS_OPEN", |
| 117 |
"SPAWN_DO_NOT_REAP_CHILD", |
| 118 |
"SPAWN_SEARCH_PATH", |
| 119 |
"SPAWN_STDOUT_TO_DEV_NULL", |
| 120 |
"SPAWN_STDERR_TO_DEV_NULL", |
| 121 |
"SPAWN_CHILD_INHERITS_STDIN", |
| 122 |
"SPAWN_FILE_AND_ARGV_ZERO", |
| 123 |
"OPTION_FLAG_HIDDEN", |
| 124 |
"OPTION_FLAG_IN_MAIN", |
| 125 |
"OPTION_FLAG_REVERSE", |
| 126 |
"OPTION_FLAG_NO_ARG", |
| 127 |
"OPTION_FLAG_FILENAME", |
| 128 |
"OPTION_FLAG_OPTIONAL_ARG", |
| 129 |
"OPTION_FLAG_NOALIAS", |
| 130 |
"OPTION_ERROR_UNKNOWN_OPTION", |
| 131 |
"OPTION_ERROR_BAD_VALUE", |
| 132 |
"OPTION_ERROR_FAILED", |
| 133 |
"OPTION_REMAINING", |
| 134 |
"glib_version", |
| 135 |
]: |
| 136 |
with warnings.catch_warnings(): |
| 137 |
# TODO: this uses deprecated Glib attributes, silence for now |
| 138 |
warnings.simplefilter("ignore", PyGIDeprecationWarning) |
| 139 |
globals()[name] = getattr(GLib, name) |
| 140 |
deprecated_attr("GObject", name, "GLib." + name) |
| 141 |
__all__.append(name) |
| 142 |
|
| 143 |
|
| 144 |
for name in [ |
| 145 |
"G_MININT8", |
| 146 |
"G_MAXINT8", |
| 147 |
"G_MAXUINT8", |
| 148 |
"G_MININT16", |
| 149 |
"G_MAXINT16", |
| 150 |
"G_MAXUINT16", |
| 151 |
"G_MININT32", |
| 152 |
"G_MAXINT32", |
| 153 |
"G_MAXUINT32", |
| 154 |
"G_MININT64", |
| 155 |
"G_MAXINT64", |
| 156 |
"G_MAXUINT64", |
| 157 |
]: |
| 158 |
new_name = name.split("_", 1)[-1] |
| 159 |
globals()[name] = getattr(GLib, new_name) |
| 160 |
deprecated_attr("GObject", name, "GLib." + new_name) |
| 161 |
__all__.append(name) |
| 162 |
|
| 163 |
# these are not currently exported in GLib gir, presumably because they are |
| 164 |
# platform dependent; so get them from our static bindings |
| 165 |
for name in [ |
| 166 |
"G_MINFLOAT", |
| 167 |
"G_MAXFLOAT", |
| 168 |
"G_MINDOUBLE", |
| 169 |
"G_MAXDOUBLE", |
| 170 |
"G_MINSHORT", |
| 171 |
"G_MAXSHORT", |
| 172 |
"G_MAXUSHORT", |
| 173 |
"G_MININT", |
| 174 |
"G_MAXINT", |
| 175 |
"G_MAXUINT", |
| 176 |
"G_MINLONG", |
| 177 |
"G_MAXLONG", |
| 178 |
"G_MAXULONG", |
| 179 |
"G_MAXSIZE", |
| 180 |
"G_MINSSIZE", |
| 181 |
"G_MAXSSIZE", |
| 182 |
"G_MINOFFSET", |
| 183 |
"G_MAXOFFSET", |
| 184 |
]: |
| 185 |
new_name = name.split("_", 1)[-1] |
| 186 |
globals()[name] = getattr(GLib, new_name) |
| 187 |
deprecated_attr("GObject", name, "GLib." + new_name) |
| 188 |
__all__.append(name) |
| 189 |
|
| 190 |
|
| 191 |
TYPE_INVALID: GType = GObjectModule.type_from_name("invalid") |
| 192 |
TYPE_NONE: GType = GObjectModule.type_from_name("void") |
| 193 |
TYPE_INTERFACE: GType = GObjectModule.type_from_name("GInterface") |
| 194 |
TYPE_CHAR: GType = GObjectModule.type_from_name("gchar") |
| 195 |
TYPE_UCHAR: GType = GObjectModule.type_from_name("guchar") |
| 196 |
TYPE_BOOLEAN: GType = GObjectModule.type_from_name("gboolean") |
| 197 |
TYPE_INT: GType = GObjectModule.type_from_name("gint") |
| 198 |
TYPE_UINT: GType = GObjectModule.type_from_name("guint") |
| 199 |
TYPE_LONG: GType = GObjectModule.type_from_name("glong") |
| 200 |
TYPE_ULONG: GType = GObjectModule.type_from_name("gulong") |
| 201 |
TYPE_INT64: GType = GObjectModule.type_from_name("gint64") |
| 202 |
TYPE_UINT64: GType = GObjectModule.type_from_name("guint64") |
| 203 |
TYPE_ENUM: GType = GObjectModule.type_from_name("GEnum") |
| 204 |
TYPE_FLAGS: GType = GObjectModule.type_from_name("GFlags") |
| 205 |
TYPE_FLOAT: GType = GObjectModule.type_from_name("gfloat") |
| 206 |
TYPE_DOUBLE: GType = GObjectModule.type_from_name("gdouble") |
| 207 |
TYPE_STRING: GType = GObjectModule.type_from_name("gchararray") |
| 208 |
TYPE_POINTER: GType = GObjectModule.type_from_name("gpointer") |
| 209 |
TYPE_BOXED: GType = GObjectModule.type_from_name("GBoxed") |
| 210 |
TYPE_PARAM: GType = GObjectModule.type_from_name("GParam") |
| 211 |
TYPE_OBJECT: GType = GObjectModule.type_from_name("GObject") |
| 212 |
TYPE_PYOBJECT: GType = GObjectModule.type_from_name("PyObject") |
| 213 |
TYPE_GTYPE: GType = GObjectModule.type_from_name("GType") |
| 214 |
TYPE_STRV: GType = GObjectModule.type_from_name("GStrv") |
| 215 |
TYPE_VARIANT: GType = GObjectModule.type_from_name("GVariant") |
| 216 |
TYPE_GSTRING: GType = GObjectModule.type_from_name("GString") |
| 217 |
TYPE_VALUE: GType = GObjectModule.Value.__gtype__ |
| 218 |
TYPE_UNICHAR: GType = TYPE_UINT |
| 219 |
__all__ += [ |
| 220 |
"TYPE_BOOLEAN", |
| 221 |
"TYPE_BOXED", |
| 222 |
"TYPE_CHAR", |
| 223 |
"TYPE_DOUBLE", |
| 224 |
"TYPE_ENUM", |
| 225 |
"TYPE_FLAGS", |
| 226 |
"TYPE_FLOAT", |
| 227 |
"TYPE_GSTRING", |
| 228 |
"TYPE_GTYPE", |
| 229 |
"TYPE_INT", |
| 230 |
"TYPE_INT64", |
| 231 |
"TYPE_INTERFACE", |
| 232 |
"TYPE_INVALID", |
| 233 |
"TYPE_LONG", |
| 234 |
"TYPE_NONE", |
| 235 |
"TYPE_OBJECT", |
| 236 |
"TYPE_PARAM", |
| 237 |
"TYPE_POINTER", |
| 238 |
"TYPE_PYOBJECT", |
| 239 |
"TYPE_STRING", |
| 240 |
"TYPE_STRV", |
| 241 |
"TYPE_UCHAR", |
| 242 |
"TYPE_UINT", |
| 243 |
"TYPE_UINT64", |
| 244 |
"TYPE_ULONG", |
| 245 |
"TYPE_UNICHAR", |
| 246 |
"TYPE_VALUE", |
| 247 |
"TYPE_VARIANT", |
| 248 |
] |
| 249 |
|
| 250 |
|
| 251 |
# Deprecated, use GLib directly |
| 252 |
for name in ["Pid", "GError", "OptionGroup", "OptionContext"]: |
| 253 |
globals()[name] = getattr(GLib, name) |
| 254 |
deprecated_attr("GObject", name, "GLib." + name) |
| 255 |
__all__.append(name) |
| 256 |
|
| 257 |
|
| 258 |
# Deprecated, use: GObject.ParamFlags.* directly |
| 259 |
for name in [ |
| 260 |
"PARAM_CONSTRUCT", |
| 261 |
"PARAM_CONSTRUCT_ONLY", |
| 262 |
"PARAM_LAX_VALIDATION", |
| 263 |
"PARAM_READABLE", |
| 264 |
"PARAM_WRITABLE", |
| 265 |
]: |
| 266 |
new_name = name.split("_", 1)[-1] |
| 267 |
globals()[name] = getattr(GObjectModule.ParamFlags, new_name) |
| 268 |
deprecated_attr("GObject", name, "GObject.ParamFlags." + new_name) |
| 269 |
__all__.append(name) |
| 270 |
|
| 271 |
# PARAM_READWRITE should come from the gi module but cannot due to: |
| 272 |
# https://gitlab.gnome.org/GNOME/gobject-introspection/issues/75 |
| 273 |
PARAM_READWRITE = GObjectModule.ParamFlags.READABLE | GObjectModule.ParamFlags.WRITABLE |
| 274 |
deprecated_attr("GObject", "PARAM_READWRITE", "GObject.ParamFlags.READWRITE") |
| 275 |
__all__.append("PARAM_READWRITE") |
| 276 |
|
| 277 |
|
| 278 |
# Deprecated, use: GObject.SignalFlags.* directly |
| 279 |
for name in [ |
| 280 |
"SIGNAL_ACTION", |
| 281 |
"SIGNAL_DETAILED", |
| 282 |
"SIGNAL_NO_HOOKS", |
| 283 |
"SIGNAL_NO_RECURSE", |
| 284 |
"SIGNAL_RUN_CLEANUP", |
| 285 |
"SIGNAL_RUN_FIRST", |
| 286 |
"SIGNAL_RUN_LAST", |
| 287 |
]: |
| 288 |
new_name = name.split("_", 1)[-1] |
| 289 |
globals()[name] = getattr(GObjectModule.SignalFlags, new_name) |
| 290 |
deprecated_attr("GObject", name, "GObject.SignalFlags." + new_name) |
| 291 |
__all__.append(name) |
| 292 |
|
| 293 |
# Static types |
| 294 |
GBoxed = _gi.GBoxed |
| 295 |
GEnum = _gi.GEnum |
| 296 |
GFlags = _gi.GFlags |
| 297 |
GInterface = _gi.GInterface |
| 298 |
GObject = _gi.GObject |
| 299 |
GObjectWeakRef = _gi.GObjectWeakRef |
| 300 |
GPointer = _gi.GPointer |
| 301 |
GType = _gi.GType |
| 302 |
Warning = _gi.Warning |
| 303 |
__all__ += [ |
| 304 |
"GBoxed", |
| 305 |
"GEnum", |
| 306 |
"GFlags", |
| 307 |
"GInterface", |
| 308 |
"GObject", |
| 309 |
"GObjectWeakRef", |
| 310 |
"GPointer", |
| 311 |
"GType", |
| 312 |
"Warning", |
| 313 |
] |
| 314 |
|
| 315 |
|
| 316 |
features = {"generic-c-marshaller": True} |
| 317 |
list_properties = _gi.list_properties |
| 318 |
new = _gi.new |
| 319 |
pygobject_version = _gi.pygobject_version |
| 320 |
threads_init = GLib.threads_init |
| 321 |
type_register = _gi.type_register |
| 322 |
__all__ += [ |
| 323 |
"features", |
| 324 |
"list_properties", |
| 325 |
"new", |
| 326 |
"pygobject_version", |
| 327 |
"threads_init", |
| 328 |
"type_register", |
| 329 |
] |
| 330 |
|
| 331 |
|
| 332 |
class Value(GObjectModule.Value): |
| 333 |
def __init__( |
| 334 |
self, value_type: GType | None = None, py_value: typing.Any | None = None |
| 335 |
) -> None: |
| 336 |
GObjectModule.Value.__init__(self) |
| 337 |
if value_type is not None: |
| 338 |
self.init(value_type) |
| 339 |
if py_value is not None: |
| 340 |
self.set_value(py_value) |
| 341 |
|
| 342 |
@property |
| 343 |
def __g_type(self): |
| 344 |
# XXX: This is the same as self.g_type, but the field marshalling |
| 345 |
# code is currently very slow. |
| 346 |
return _gi._gvalue_get_type(self) |
| 347 |
|
| 348 |
def set_boxed(self, boxed: GBoxed): |
| 349 |
if not self.__g_type.is_a(TYPE_BOXED): |
| 350 |
warnings.warn( |
| 351 |
"Calling set_boxed() on a non-boxed type deprecated", |
| 352 |
PyGIDeprecationWarning, |
| 353 |
stacklevel=2, |
| 354 |
) |
| 355 |
# Workaround the introspection marshalers inability to know |
| 356 |
# these methods should be marshaling boxed types. This is because |
| 357 |
# the type information is stored on the GValue. |
| 358 |
_gi._gvalue_set(self, boxed) |
| 359 |
|
| 360 |
def get_boxed(self) -> GBoxed: |
| 361 |
if not self.__g_type.is_a(TYPE_BOXED): |
| 362 |
warnings.warn( |
| 363 |
"Calling get_boxed() on a non-boxed type deprecated", |
| 364 |
PyGIDeprecationWarning, |
| 365 |
stacklevel=2, |
| 366 |
) |
| 367 |
return _gi._gvalue_get(self) |
| 368 |
|
| 369 |
def set_value(self, py_value: typing.Any) -> None: |
| 370 |
gtype = self.__g_type |
| 371 |
|
| 372 |
if gtype == TYPE_CHAR: |
| 373 |
self.set_char(py_value) |
| 374 |
elif gtype == TYPE_UCHAR: |
| 375 |
self.set_uchar(py_value) |
| 376 |
elif gtype == TYPE_STRING: |
| 377 |
if not isinstance(py_value, str) and py_value is not None: |
| 378 |
raise TypeError(f"Expected string but got {py_value}{type(py_value)}") |
| 379 |
_gi._gvalue_set(self, py_value) |
| 380 |
elif gtype == TYPE_PARAM: |
| 381 |
self.set_param(py_value) |
| 382 |
elif gtype.is_a(TYPE_FLAGS): |
| 383 |
self.set_flags(py_value) |
| 384 |
elif gtype == TYPE_POINTER: |
| 385 |
self.set_pointer(py_value) |
| 386 |
elif gtype == TYPE_GTYPE: |
| 387 |
self.set_gtype(py_value) |
| 388 |
elif gtype == TYPE_VARIANT: |
| 389 |
self.set_variant(py_value) |
| 390 |
else: |
| 391 |
# Fall back to _gvalue_set which handles some more cases |
| 392 |
# like fundamentals for which a converter is registered |
| 393 |
try: |
| 394 |
_gi._gvalue_set(self, py_value) |
| 395 |
except TypeError: |
| 396 |
if gtype == TYPE_INVALID: |
| 397 |
raise TypeError("GObject.Value needs to be initialized first") |
| 398 |
raise |
| 399 |
|
| 400 |
def get_value(self) -> typing.Any: |
| 401 |
gtype = self.__g_type |
| 402 |
|
| 403 |
if gtype == TYPE_CHAR: |
| 404 |
return self.get_char() |
| 405 |
if gtype == TYPE_UCHAR: |
| 406 |
return self.get_uchar() |
| 407 |
if gtype == TYPE_PARAM: |
| 408 |
return self.get_param() |
| 409 |
if gtype.is_a(TYPE_ENUM): |
| 410 |
return self.get_enum() |
| 411 |
if gtype.is_a(TYPE_FLAGS): |
| 412 |
return self.get_flags() |
| 413 |
if gtype == TYPE_POINTER: |
| 414 |
return self.get_pointer() |
| 415 |
if gtype == TYPE_GTYPE: |
| 416 |
return self.get_gtype() |
| 417 |
if gtype == TYPE_VARIANT: |
| 418 |
# get_variant was missing annotations |
| 419 |
# https://gitlab.gnome.org/GNOME/glib/merge_requests/492 |
| 420 |
return self.dup_variant() |
| 421 |
try: |
| 422 |
return _gi._gvalue_get(self) |
| 423 |
except TypeError: |
| 424 |
if gtype == TYPE_INVALID: |
| 425 |
return None |
| 426 |
raise |
| 427 |
|
| 428 |
def __repr__(self) -> str: |
| 429 |
return f"<Value ({self.__g_type.name}) {self.get_value()}>" |
| 430 |
|
| 431 |
|
| 432 |
Value = override(Value) |
| 433 |
__all__.append("Value") |
| 434 |
|
| 435 |
|
| 436 |
def type_from_name(name: str) -> GType: |
| 437 |
type_ = GObjectModule.type_from_name(name) |
| 438 |
if type_ == TYPE_INVALID: |
| 439 |
raise RuntimeError(f"unknown type name: {name}") |
| 440 |
return type_ |
| 441 |
|
| 442 |
|
| 443 |
__all__.append("type_from_name") |
| 444 |
|
| 445 |
|
| 446 |
def type_parent(type_: GType | Object) -> GType: |
| 447 |
parent = GObjectModule.type_parent(type_) |
| 448 |
if parent == TYPE_INVALID: |
| 449 |
raise RuntimeError("no parent for type") |
| 450 |
return parent |
| 451 |
|
| 452 |
|
| 453 |
__all__.append("type_parent") |
| 454 |
|
| 455 |
|
| 456 |
def _validate_type_for_signal_method(type_: GType | Object) -> None: |
| 457 |
if hasattr(type_, "__gtype__"): |
| 458 |
type_ = type_.__gtype__ |
| 459 |
if not type_.is_instantiatable() and not type_.is_interface(): |
| 460 |
raise TypeError(f"type must be instantiable or an interface, got {type_}") |
| 461 |
|
| 462 |
|
| 463 |
def signal_list_ids(type_: GType | Object) -> tuple[int, ...]: |
| 464 |
_validate_type_for_signal_method(type_) |
| 465 |
return GObjectModule.signal_list_ids(type_) |
| 466 |
|
| 467 |
|
| 468 |
__all__.append("signal_list_ids") |
| 469 |
|
| 470 |
|
| 471 |
def signal_list_names(type_: GType | Object) -> tuple[str, ...]: |
| 472 |
ids = signal_list_ids(type_) |
| 473 |
return tuple(GObjectModule.signal_name(i) for i in ids) |
| 474 |
|
| 475 |
|
| 476 |
__all__.append("signal_list_names") |
| 477 |
|
| 478 |
|
| 479 |
def signal_lookup(name: str, type_: GType | Object) -> int: |
| 480 |
_validate_type_for_signal_method(type_) |
| 481 |
return GObjectModule.signal_lookup(name, type_) |
| 482 |
|
| 483 |
|
| 484 |
__all__.append("signal_lookup") |
| 485 |
|
| 486 |
|
| 487 |
SignalQuery = namedtuple( |
| 488 |
"SignalQuery", |
| 489 |
[ |
| 490 |
"signal_id", |
| 491 |
"signal_name", |
| 492 |
"itype", |
| 493 |
"signal_flags", |
| 494 |
"return_type", |
| 495 |
# n_params', |
| 496 |
"param_types", |
| 497 |
], |
| 498 |
) |
| 499 |
|
| 500 |
|
| 501 |
def signal_query( |
| 502 |
id_or_name: int | str, type_: GType | Object | None = None |
| 503 |
) -> SignalQuery | None: |
| 504 |
if isinstance(id_or_name, str): |
| 505 |
assert type_ is not None, "type_ is required when looking up signal by name" |
| 506 |
signal_id = signal_lookup(id_or_name, type_) |
| 507 |
else: |
| 508 |
signal_id = id_or_name |
| 509 |
|
| 510 |
res = GObjectModule.signal_query(signal_id) |
| 511 |
assert res is not None |
| 512 |
|
| 513 |
if res.signal_id == 0: |
| 514 |
return None |
| 515 |
|
| 516 |
# Return a named tuple to allows indexing which is compatible with the |
| 517 |
# static bindings along with field like access of the gi struct. |
| 518 |
# Note however that the n_params was not returned from the static bindings |
| 519 |
# so we must skip over it. |
| 520 |
return SignalQuery( |
| 521 |
res.signal_id, |
| 522 |
res.signal_name, |
| 523 |
res.itype, |
| 524 |
res.signal_flags, |
| 525 |
res.return_type, |
| 526 |
tuple(res.param_types), |
| 527 |
) |
| 528 |
|
| 529 |
|
| 530 |
__all__.append("signal_query") |
| 531 |
|
| 532 |
|
| 533 |
class _HandlerBlockManager: |
| 534 |
def __init__(self, obj: Object, handler_id: int): |
| 535 |
self.obj = obj |
| 536 |
self.handler_id = handler_id |
| 537 |
|
| 538 |
def __enter__(self) -> None: |
| 539 |
pass |
| 540 |
|
| 541 |
def __exit__(self, exc_type, exc_value, traceback) -> None: |
| 542 |
GObjectModule.signal_handler_unblock(self.obj, self.handler_id) |
| 543 |
|
| 544 |
|
| 545 |
def signal_handler_block(obj: Object, handler_id: int) -> typing.ContextManager[None]: |
| 546 |
"""Blocks the signal handler from being invoked until |
| 547 |
handler_unblock() is called. |
| 548 |
|
| 549 |
:param GObject.Object obj: |
| 550 |
Object instance to block handlers for. |
| 551 |
:param int handler_id: |
| 552 |
Id of signal to block. |
| 553 |
:returns: |
| 554 |
A context manager which optionally can be used to |
| 555 |
automatically unblock the handler: |
| 556 |
|
| 557 |
.. code-block:: python |
| 558 |
|
| 559 |
with GObject.signal_handler_block(obj, id): |
| 560 |
pass |
| 561 |
""" |
| 562 |
GObjectModule.signal_handler_block(obj, handler_id) |
| 563 |
return _HandlerBlockManager(obj, handler_id) |
| 564 |
|
| 565 |
|
| 566 |
__all__.append("signal_handler_block") |
| 567 |
|
| 568 |
|
| 569 |
def signal_parse_name( |
| 570 |
detailed_signal: str, itype: GType | Object, force_detail_quark: bool |
| 571 |
) -> tuple[int, int]: |
| 572 |
"""Parse a detailed signal name into (signal_id, detail). |
| 573 |
|
| 574 |
:param str detailed_signal: |
| 575 |
Signal name which can include detail. |
| 576 |
For example: "notify:prop_name" |
| 577 |
:returns: |
| 578 |
Tuple of (signal_id, detail) |
| 579 |
:raises ValueError: |
| 580 |
If the given signal is unknown. |
| 581 |
""" |
| 582 |
res, signal_id, detail = GObjectModule.signal_parse_name( |
| 583 |
detailed_signal, itype, force_detail_quark |
| 584 |
) |
| 585 |
if res: |
| 586 |
return signal_id, detail |
| 587 |
raise ValueError(f"{itype}: unknown signal name: {detailed_signal}") |
| 588 |
|
| 589 |
|
| 590 |
__all__.append("signal_parse_name") |
| 591 |
|
| 592 |
|
| 593 |
def remove_emission_hook(obj: Object, detailed_signal: str, hook_id: int) -> None: |
| 594 |
signal_id, _detail = signal_parse_name(detailed_signal, obj, True) |
| 595 |
GObjectModule.signal_remove_emission_hook(signal_id, hook_id) |
| 596 |
|
| 597 |
|
| 598 |
__all__.append("remove_emission_hook") |
| 599 |
|
| 600 |
|
| 601 |
# GObject accumulators with pure Python implementations |
| 602 |
# These return a tuple of (continue_emission, accumulation_result) |
| 603 |
|
| 604 |
|
| 605 |
def signal_accumulator_first_wins(ihint, return_accu, handler_return, user_data=None): |
| 606 |
# Stop emission but return the result of the last handler |
| 607 |
return (False, handler_return) |
| 608 |
|
| 609 |
|
| 610 |
__all__.append("signal_accumulator_first_wins") |
| 611 |
|
| 612 |
|
| 613 |
def signal_accumulator_true_handled(ihint, return_accu, handler_return, user_data=None): |
| 614 |
# Stop emission if the last handler returns True |
| 615 |
return (not handler_return, handler_return) |
| 616 |
|
| 617 |
|
| 618 |
__all__.append("signal_accumulator_true_handled") |
| 619 |
|
| 620 |
|
| 621 |
# Statically bound signal functions which need to clobber GI (for now) |
| 622 |
|
| 623 |
add_emission_hook = _gi.add_emission_hook |
| 624 |
signal_new = _gi.signal_new |
| 625 |
|
| 626 |
__all__ += ["add_emission_hook", "signal_new"] |
| 627 |
|
| 628 |
|
| 629 |
class _FreezeNotifyManager: |
| 630 |
def __init__(self, obj: Object): |
| 631 |
self.obj = obj |
| 632 |
|
| 633 |
def __enter__(self) -> None: |
| 634 |
pass |
| 635 |
|
| 636 |
def __exit__(self, exc_type, exc_value, traceback) -> None: |
| 637 |
self.obj.thaw_notify() |
| 638 |
|
| 639 |
|
| 640 |
def _signalmethod(func): |
| 641 |
# Function wrapper for signal functions used as instance methods. |
| 642 |
# This is needed when the signal functions come directly from GI. |
| 643 |
# (they are not already wrapped) |
| 644 |
@functools.wraps(func) |
| 645 |
def meth(*args, **kwargs): |
| 646 |
return func(*args, **kwargs) |
| 647 |
|
| 648 |
return meth |
| 649 |
|
| 650 |
|
| 651 |
class Object(GObjectModule.Object): |
| 652 |
def _unsupported_method(self, *args, **kargs): |
| 653 |
raise RuntimeError("This method is currently unsupported.") |
| 654 |
|
| 655 |
def _unsupported_data_method(self, *args, **kargs): |
| 656 |
raise RuntimeError( |
| 657 |
"Data access methods are unsupported. Use normal Python attributes instead" |
| 658 |
) |
| 659 |
|
| 660 |
# Generic data methods are not needed in python as it can be handled |
| 661 |
# with standard attribute access: https://bugzilla.gnome.org/show_bug.cgi?id=641944 |
| 662 |
get_data = _unsupported_data_method |
| 663 |
get_qdata = _unsupported_data_method |
| 664 |
set_data = _unsupported_data_method |
| 665 |
steal_data = _unsupported_data_method |
| 666 |
steal_qdata = _unsupported_data_method |
| 667 |
replace_data = _unsupported_data_method |
| 668 |
replace_qdata = _unsupported_data_method |
| 669 |
|
| 670 |
# The following methods as unsupported until we verify |
| 671 |
# they work as gi methods. |
| 672 |
bind_property_full = _unsupported_method |
| 673 |
compat_control = _unsupported_method |
| 674 |
interface_find_property = _unsupported_method |
| 675 |
interface_install_property = _unsupported_method |
| 676 |
interface_list_properties = _unsupported_method |
| 677 |
notify_by_pspec = _unsupported_method |
| 678 |
watch_closure = _unsupported_method |
| 679 |
|
| 680 |
# Make all reference management methods private but still accessible. |
| 681 |
def _ref(self): |
| 682 |
"""Deprecated, do not explicitly reference GObjects.""" |
| 683 |
warnings.warn(self._ref.__doc__, PyGIDeprecationWarning, stacklevel=2) |
| 684 |
return GObjectModule.Object.ref(self) |
| 685 |
|
| 686 |
def _ref_sink(self): |
| 687 |
"""Deprecated, do not explicitly reference GObjects.""" |
| 688 |
warnings.warn(self._ref_sink.__doc__, PyGIDeprecationWarning, stacklevel=2) |
| 689 |
return GObjectModule.Object.ref_sink(self) |
| 690 |
|
| 691 |
def _unref(self): |
| 692 |
"""Deprecated, do not explicitly reference GObjects.""" |
| 693 |
warnings.warn(self._unref.__doc__, PyGIDeprecationWarning, stacklevel=2) |
| 694 |
return GObjectModule.Object.unref(self) |
| 695 |
|
| 696 |
def _force_floating(self): |
| 697 |
"""Deprecated, do not explicitly float GObjects.""" |
| 698 |
warnings.warn( |
| 699 |
self._force_floating.__doc__, PyGIDeprecationWarning, stacklevel=2 |
| 700 |
) |
| 701 |
return GObjectModule.Object.force_floating(self) |
| 702 |
|
| 703 |
ref = _unsupported_method |
| 704 |
ref_sink = _unsupported_method |
| 705 |
unref = _unsupported_method |
| 706 |
force_floating = _unsupported_method |
| 707 |
|
| 708 |
# The following methods are static APIs which need to leap frog the |
| 709 |
# gi methods until we verify the gi methods can replace them. |
| 710 |
get_property = _gi.GObject.get_property |
| 711 |
get_properties = _gi.GObject.get_properties |
| 712 |
set_property = _gi.GObject.set_property |
| 713 |
set_properties = _gi.GObject.set_properties |
| 714 |
bind_property = _gi.GObject.bind_property |
| 715 |
connect = _gi.GObject.connect |
| 716 |
connect_after = _gi.GObject.connect_after |
| 717 |
connect_object = _gi.GObject.connect_object |
| 718 |
connect_object_after = _gi.GObject.connect_object_after |
| 719 |
disconnect_by_func = _gi.GObject.disconnect_by_func |
| 720 |
handler_block_by_func = _gi.GObject.handler_block_by_func |
| 721 |
handler_unblock_by_func = _gi.GObject.handler_unblock_by_func |
| 722 |
emit = _gi.GObject.emit |
| 723 |
chain = _gi.GObject.chain |
| 724 |
weak_ref = _gi.GObject.weak_ref |
| 725 |
__copy__ = _gi.GObject.__copy__ |
| 726 |
__deepcopy__ = _gi.GObject.__deepcopy__ |
| 727 |
|
| 728 |
def freeze_notify(self) -> typing.ContextManager[None]: |
| 729 |
"""Freezes the object's property-changed notification queue. |
| 730 |
|
| 731 |
:returns: |
| 732 |
A context manager which optionally can be used to |
| 733 |
automatically thaw notifications. |
| 734 |
|
| 735 |
This will freeze the object so that "notify" signals are blocked until |
| 736 |
the thaw_notify() method is called. |
| 737 |
|
| 738 |
.. code-block:: python |
| 739 |
|
| 740 |
with obj.freeze_notify(): |
| 741 |
pass |
| 742 |
""" |
| 743 |
super().freeze_notify() |
| 744 |
return _FreezeNotifyManager(self) |
| 745 |
|
| 746 |
def connect_data( |
| 747 |
self, |
| 748 |
detailed_signal: str, |
| 749 |
handler: typing.Callable, |
| 750 |
*data: typing.Any, |
| 751 |
connect_flags: ConnectFlags = 0, |
| 752 |
) -> int: |
| 753 |
"""Connect a callback to the given signal with optional user data. |
| 754 |
|
| 755 |
:param str detailed_signal: |
| 756 |
A detailed signal to connect to. |
| 757 |
:param callable handler: |
| 758 |
Callback handler to connect to the signal. |
| 759 |
:param *data: |
| 760 |
Variable data which is passed through to the signal handler. |
| 761 |
:param GObject.ConnectFlags connect_flags: |
| 762 |
Flags used for connection options. |
| 763 |
:returns: |
| 764 |
A signal id which can be used with disconnect. |
| 765 |
""" |
| 766 |
if connect_flags & GObjectModule.ConnectFlags.AFTER: |
| 767 |
connect_func = _gi.GObject.connect_after |
| 768 |
else: |
| 769 |
connect_func = _gi.GObject.connect |
| 770 |
|
| 771 |
if connect_flags & GObjectModule.ConnectFlags.SWAPPED: |
| 772 |
if len(data) != 1: |
| 773 |
raise ValueError( |
| 774 |
"Using GObject.ConnectFlags.SWAPPED requires exactly " |
| 775 |
"one argument for user data, got: %s" % [data] |
| 776 |
) |
| 777 |
|
| 778 |
def new_handler(obj, *args): |
| 779 |
# Swap obj with the last element in args which will be the user |
| 780 |
# data passed to the connect function. |
| 781 |
args = list(args) |
| 782 |
swap = args.pop() |
| 783 |
args = [*args, obj] |
| 784 |
return handler(swap, *args) |
| 785 |
else: |
| 786 |
new_handler = handler |
| 787 |
|
| 788 |
return connect_func(self, detailed_signal, new_handler, *data) |
| 789 |
|
| 790 |
# |
| 791 |
# Aliases |
| 792 |
# |
| 793 |
|
| 794 |
handler_block = signal_handler_block |
| 795 |
handler_unblock = _signalmethod(GObjectModule.signal_handler_unblock) |
| 796 |
disconnect = _signalmethod(GObjectModule.signal_handler_disconnect) |
| 797 |
handler_disconnect = _signalmethod(GObjectModule.signal_handler_disconnect) |
| 798 |
handler_is_connected = _signalmethod(GObjectModule.signal_handler_is_connected) |
| 799 |
stop_emission_by_name = _signalmethod(GObjectModule.signal_stop_emission_by_name) |
| 800 |
|
| 801 |
# |
| 802 |
# Deprecated Methods |
| 803 |
# |
| 804 |
|
| 805 |
def stop_emission(self, detailed_signal: str) -> None: |
| 806 |
"""Deprecated, please use stop_emission_by_name.""" |
| 807 |
warnings.warn(self.stop_emission.__doc__, PyGIDeprecationWarning, stacklevel=2) |
| 808 |
self.stop_emission_by_name(detailed_signal) |
| 809 |
|
| 810 |
emit_stop_by_name = stop_emission |
| 811 |
|
| 812 |
|
| 813 |
Object = override(Object) |
| 814 |
GObject = Object |
| 815 |
__all__ += ["GObject", "Object"] |
| 816 |
|
| 817 |
|
| 818 |
class Binding(GObjectModule.Binding): |
| 819 |
def __call__(self): |
| 820 |
warnings.warn( |
| 821 |
"Using parentheses (binding()) to retrieve the Binding object is no " |
| 822 |
'longer needed because the binding is returned directly from "bind_property.', |
| 823 |
PyGIDeprecationWarning, |
| 824 |
stacklevel=2, |
| 825 |
) |
| 826 |
return self |
| 827 |
|
| 828 |
|
| 829 |
Binding = override(Binding) |
| 830 |
__all__.append("Binding") |
| 831 |
|
| 832 |
|
| 833 |
Property = propertyhelper.Property |
| 834 |
Signal = signalhelper.Signal |
| 835 |
SignalOverride = signalhelper.SignalOverride |
| 836 |
# Deprecated naming "property" available for backwards compatibility. |
| 837 |
# Keep this at the end of the file to avoid clobbering the builtin. |
| 838 |
property = Property |
| 839 |
deprecated_attr("GObject", "property", "GObject.Property") |
| 840 |
__all__ += ["Property", "Signal", "SignalOverride", "property"] |
| 841 |
|
| 842 |
|
| 843 |
@override |
| 844 |
class ParamSpec(GObjectModule.ParamSpec): |
| 845 |
@property |
| 846 |
def nick(self) -> str: |
| 847 |
return self._nick |
| 848 |
|
| 849 |
@nick.setter |
| 850 |
def nick(self, nick: str) -> None: |
| 851 |
self._nick = nick |
| 852 |
|
| 853 |
@property |
| 854 |
def blurb(self) -> str: |
| 855 |
return self._blurb |
| 856 |
|
| 857 |
@blurb.setter |
| 858 |
def blurb(self, blurb: str) -> None: |
| 859 |
self._blurb = blurb |
| 860 |
|
| 861 |
|
| 862 |
GParamSpec = ParamSpec |
| 863 |
deprecated_attr("GObject", "GParamSpec", "GObject.ParamSpec") |
| 864 |
__all__ += ["GParamSpec", "ParamSpec"] |
| 865 |
|
| 866 |
|
| 867 |
@override |
| 868 |
class ParamSpecEnum(GObjectModule.ParamSpecEnum): |
| 869 |
@property |
| 870 |
def enum_class(self): |
| 871 |
gtype = super().enum_class.g_type_class.g_type |
| 872 |
if pytype := gtype.pytype: |
| 873 |
return pytype |
| 874 |
return _gi.enum_add(None, gtype.name, gtype, None) |
| 875 |
|
| 876 |
|
| 877 |
__all__.append("ParamSpecEnum") |
| 878 |
|
| 879 |
|
| 880 |
@override |
| 881 |
class ParamSpecFlags(GObjectModule.ParamSpecFlags): |
| 882 |
@property |
| 883 |
def flags_class(self): |
| 884 |
gtype = super().flags_class.g_type_class.g_type |
| 885 |
if pytype := gtype.pytype: |
| 886 |
return pytype |
| 887 |
return _gi.flags_add(None, gtype.name, gtype, None) |
| 888 |
|
| 889 |
|
| 890 |
__all__.append("ParamSpecFlags") |
| 891 |
|
| 892 |
|
| 893 |
class Float(float): |
| 894 |
"""A wrapper to force conversion to G_TYPE_FLOAT instead of G_TYPE_DOUBLE when |
| 895 |
used in GValue APIs. |
| 896 |
""" |
| 897 |
|
| 898 |
__gtype__ = TYPE_FLOAT |
| 899 |
|
| 900 |
|
| 901 |
__all__.append("Float") |