Imported Upstream version 2.28.6
[platform/upstream/pygobject2.git] / gi / _gobject / __init__.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # pygobject - Python bindings for the GObject library
3 # Copyright (C) 2006  Johan Dahlin
4 #
5 #   gobject/__init__.py: initialisation file for gobject module
6 #
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.
11 #
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.
16 #
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
20 # USA
21
22 # this can go when things are a little further along
23
24 import sys
25
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".')
29
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
50
51 from .constants import *
52 from ._gobject import *
53
54 _PyGObject_API = _gobject._PyGObject_API
55
56 from .propertyhelper import property
57
58 sys.modules['gi._gobject.option'] = option
59
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__)
66
67     def _install_properties(cls):
68         gproperties = getattr(cls, '__gproperties__', {})
69
70         props = []
71         for name, prop in cls.__dict__.items():
72             if isinstance(prop, property): # not same as the built-in
73                 if name in gproperties:
74                     raise ValueError
75                 prop.name = name
76                 gproperties[name] = prop.get_pspec_args()
77                 props.append(prop)
78
79         if not props:
80             return
81
82         cls.__gproperties__ = gproperties
83
84         if ('do_get_property' in cls.__dict__ or
85             'do_set_property' in cls.__dict__):
86             for prop in props:
87                 if (prop.getter != prop._default_getter or
88                     prop.setter != prop._default_setter):
89                     raise TypeError(
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" % (
93                         cls.__name__,))
94
95         def obj_get_property(self, pspec):
96             name = pspec.name.replace('-', '_')
97             prop = getattr(cls, name, None)
98             if prop:
99                 return prop.getter(self)
100         cls.do_get_property = obj_get_property
101
102         def obj_set_property(self, pspec, value):
103             name = pspec.name.replace('-', '_')
104             prop = getattr(cls, name, None)
105             if prop:
106                 prop.setter(self, value)
107         cls.do_set_property = obj_set_property
108
109     def _type_register(cls, namespace):
110         ## don't register the class if already registered
111         if '__gtype__' in namespace:
112             return
113
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.'):
117             return
118
119         type_register(cls, namespace.get('__gtype_name__'))
120
121 _gobject._install_metaclass(GObjectMeta)
122
123 #del _gobject