Package gobject
[hide private]
[frames] | no frames]

Source Code for Package gobject

  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  from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \ 
 27       io_add_watch, source_remove, child_watch_add, markup_escape_text, \ 
 28       get_current_time, filename_display_name, filename_display_basename, \ 
 29       filename_from_utf8, get_application_name, set_application_name, \ 
 30       get_prgname, set_prgname, main_depth, Pid, GError, glib_version, \ 
 31       MainLoop, MainContext, main_context_default, IOChannel, Source, Idle, \ 
 32       Timeout, PollFD, OptionGroup, OptionContext, option, uri_list_extract_uris 
 33  from glib import SPAWN_LEAVE_DESCRIPTORS_OPEN, SPAWN_DO_NOT_REAP_CHILD, \ 
 34       SPAWN_SEARCH_PATH, SPAWN_STDOUT_TO_DEV_NULL, SPAWN_STDERR_TO_DEV_NULL, \ 
 35       SPAWN_CHILD_INHERITS_STDIN, SPAWN_FILE_AND_ARGV_ZERO, PRIORITY_HIGH, \ 
 36       PRIORITY_DEFAULT, PRIORITY_HIGH_IDLE, PRIORITY_DEFAULT_IDLE, \ 
 37       PRIORITY_LOW, IO_IN, IO_OUT, IO_PRI, IO_ERR, IO_HUP, IO_NVAL, \ 
 38       IO_STATUS_ERROR, IO_STATUS_NORMAL, IO_STATUS_EOF, IO_STATUS_AGAIN, \ 
 39       IO_FLAG_APPEND, IO_FLAG_NONBLOCK, IO_FLAG_IS_READABLE, \ 
 40       IO_FLAG_IS_WRITEABLE, IO_FLAG_IS_SEEKABLE, IO_FLAG_MASK, \ 
 41       IO_FLAG_GET_MASK, IO_FLAG_SET_MASK, OPTION_FLAG_HIDDEN, \ 
 42       OPTION_FLAG_IN_MAIN, OPTION_FLAG_REVERSE, OPTION_FLAG_NO_ARG, \ 
 43       OPTION_FLAG_FILENAME, OPTION_FLAG_OPTIONAL_ARG, OPTION_FLAG_NOALIAS, \ 
 44       OPTION_ERROR_UNKNOWN_OPTION, OPTION_ERROR_BAD_VALUE, \ 
 45       OPTION_ERROR_FAILED, OPTION_REMAINING, OPTION_ERROR 
 46   
 47  from gobject.constants import * 
 48  from gobject._gobject import * 
 49  _PyGObject_API = _gobject._PyGObject_API 
 50   
 51  from gobject.propertyhelper import property 
 52   
 53  sys.modules['gobject.option'] = option 
 54   
55 -class GObjectMeta(type):
56 "Metaclass for automatically registering GObject classes"
57 - def __init__(cls, name, bases, dict_):
58 type.__init__(cls, name, bases, dict_) 59 cls._install_properties() 60 cls._type_register(cls.__dict__)
61
62 - def _install_properties(cls):
63 gproperties = getattr(cls, '__gproperties__', {}) 64 65 props = [] 66 for name, prop in cls.__dict__.items(): 67 if isinstance(prop, property): # not same as the built-in 68 if name in gproperties: 69 raise ValueError 70 prop.name = name 71 gproperties[name] = prop.get_pspec_args() 72 props.append(prop) 73 74 if not props: 75 return 76 77 cls.__gproperties__ = gproperties 78 79 if ('do_get_property' in cls.__dict__ or 80 'do_set_property' in cls.__dict__): 81 for prop in props: 82 if (prop.getter != prop._default_getter or 83 prop.setter != prop._default_setter): 84 raise TypeError( 85 "GObject subclass %r defines do_get/set_property" 86 " and it also uses a property which a custom setter" 87 " or getter. This is not allowed" % ( 88 cls.__name__,)) 89 90 def obj_get_property(self, pspec): 91 name = pspec.name.replace('-', '_') 92 prop = getattr(cls, name, None) 93 if prop: 94 return prop.getter(self)
95 cls.do_get_property = obj_get_property 96 97 def obj_set_property(self, pspec, value): 98 name = pspec.name.replace('-', '_') 99 prop = getattr(cls, name, None) 100 if prop: 101 prop.setter(self, value)
102 cls.do_set_property = obj_set_property 103
104 - def _must_register_type(cls, namespace):
105 ## don't register the class if already registered 106 if '__gtype__' in namespace: 107 return False 108 109 return ('__gproperties__' in namespace or 110 '__gsignals__' in namespace or 111 '__gtype_name__' in namespace)
112
113 - def _type_register(cls, namespace):
114 if cls._must_register_type(namespace): 115 type_register(cls, namespace.get('__gtype_name__'))
116 117 _gobject._install_metaclass(GObjectMeta) 118 119 del _gobject 120