1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # pygobject - Python bindings for the GObject library
3 # Copyright (C) 2006 Johan Dahlin
5 # gobject/__init__.py: initialisation file for gobject module
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22 # this can go when things are a little further along
26 # we can't have pygobject 2 loaded at the same time we load the internal _gobject
27 if 'gobject' in sys.modules:
28 raise ImportError('When using gi.repository you must not import static modules like "gobject". Please change all occurrences of "import gobject" to "from gi.repository import GObject".')
30 from .._glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \
31 io_add_watch, source_remove, child_watch_add, markup_escape_text, \
32 get_current_time, filename_display_name, filename_display_basename, \
33 filename_from_utf8, get_application_name, set_application_name, \
34 get_prgname, set_prgname, main_depth, Pid, GError, glib_version, \
35 MainLoop, MainContext, main_context_default, IOChannel, Source, Idle, \
36 Timeout, PollFD, OptionGroup, OptionContext, option, uri_list_extract_uris
37 from .._glib import SPAWN_LEAVE_DESCRIPTORS_OPEN, SPAWN_DO_NOT_REAP_CHILD, \
38 SPAWN_SEARCH_PATH, SPAWN_STDOUT_TO_DEV_NULL, SPAWN_STDERR_TO_DEV_NULL, \
39 SPAWN_CHILD_INHERITS_STDIN, SPAWN_FILE_AND_ARGV_ZERO, PRIORITY_HIGH, \
40 PRIORITY_DEFAULT, PRIORITY_HIGH_IDLE, PRIORITY_DEFAULT_IDLE, \
41 PRIORITY_LOW, IO_IN, IO_OUT, IO_PRI, IO_ERR, IO_HUP, IO_NVAL, \
42 IO_STATUS_ERROR, IO_STATUS_NORMAL, IO_STATUS_EOF, IO_STATUS_AGAIN, \
43 IO_FLAG_APPEND, IO_FLAG_NONBLOCK, IO_FLAG_IS_READABLE, \
44 IO_FLAG_IS_WRITEABLE, IO_FLAG_IS_SEEKABLE, IO_FLAG_MASK, \
45 IO_FLAG_GET_MASK, IO_FLAG_SET_MASK, OPTION_FLAG_HIDDEN, \
46 OPTION_FLAG_IN_MAIN, OPTION_FLAG_REVERSE, OPTION_FLAG_NO_ARG, \
47 OPTION_FLAG_FILENAME, OPTION_FLAG_OPTIONAL_ARG, OPTION_FLAG_NOALIAS, \
48 OPTION_ERROR_UNKNOWN_OPTION, OPTION_ERROR_BAD_VALUE, \
49 OPTION_ERROR_FAILED, OPTION_REMAINING, OPTION_ERROR
51 from .constants import *
52 from ._gobject import *
54 _PyGObject_API = _gobject._PyGObject_API
56 from .propertyhelper import property
58 sys.modules['gi._gobject.option'] = option
60 class GObjectMeta(type):
61 "Metaclass for automatically registering GObject classes"
62 def __init__(cls, name, bases, dict_):
63 type.__init__(cls, name, bases, dict_)
64 cls._install_properties()
65 cls._type_register(cls.__dict__)
67 def _install_properties(cls):
68 gproperties = getattr(cls, '__gproperties__', {})
71 for name, prop in cls.__dict__.items():
72 if isinstance(prop, property): # not same as the built-in
73 if name in gproperties:
76 gproperties[name] = prop.get_pspec_args()
82 cls.__gproperties__ = gproperties
84 if ('do_get_property' in cls.__dict__ or
85 'do_set_property' in cls.__dict__):
87 if (prop.getter != prop._default_getter or
88 prop.setter != prop._default_setter):
90 "GObject subclass %r defines do_get/set_property"
91 " and it also uses a property which a custom setter"
92 " or getter. This is not allowed" % (
95 def obj_get_property(self, pspec):
96 name = pspec.name.replace('-', '_')
97 prop = getattr(cls, name, None)
99 return prop.getter(self)
100 cls.do_get_property = obj_get_property
102 def obj_set_property(self, pspec, value):
103 name = pspec.name.replace('-', '_')
104 prop = getattr(cls, name, None)
106 prop.setter(self, value)
107 cls.do_set_property = obj_set_property
109 def _type_register(cls, namespace):
110 ## don't register the class if already registered
111 if '__gtype__' in namespace:
114 # Do not register a new GType for the overrides, as this would sort of
115 # defeat the purpose of overrides...
116 if cls.__module__.startswith('gi.overrides.'):
119 type_register(cls, namespace.get('__gtype_name__'))
121 _gobject._install_metaclass(GObjectMeta)