class Source(GLib.Source): def __new__(cls, *args, **kwargs): # use our custom pyg_source_new() here as g_source_new() is not # bindable source = source_new() source.__class__ = cls setattr(source, '__pygi_custom_source', True) return source def __init__(self, *args, **kwargs): return super(Source, self).__init__() def set_callback(self, fn, user_data=None): if hasattr(self, '__pygi_custom_source'): # use our custom pyg_source_set_callback() if for a GSource object # with custom functions source_set_callback(self, fn, user_data) else: # otherwise, for Idle and Timeout, use the standard method super(Source, self).set_callback(fn, user_data) def get_current_time(self): return GLib.get_real_time() * 0.000001 get_current_time = deprecated( get_current_time, 'GLib.Source.get_time() or GLib.get_real_time()') """get_current_time() :returns: Time in seconds since the Epoch :rtype: float {{ docs }} """ # as get/set_priority are introspected, we can't use the static # property(get_priority, ..) here def __get_priority(self): return self.get_priority() def __set_priority(self, value): self.set_priority(value) priority = property(__get_priority, __set_priority) def __get_can_recurse(self): return self.get_can_recurse() def __set_can_recurse(self, value): self.set_can_recurse(value) can_recurse = property(__get_can_recurse, __set_can_recurse)
class IOChannel(GLib.IOChannel): def __new__(cls, filedes=None, filename=None, mode=None, hwnd=None): if filedes is not None: return GLib.IOChannel.unix_new(filedes) if filename is not None: return GLib.IOChannel.new_file(filename, mode or 'r') if hwnd is not None: return GLib.IOChannel.win32_new_fd(hwnd) raise TypeError('either a valid file descriptor, file name, or window handle must be supplied') def __init__(self, *args, **kwargs): return super(IOChannel, self).__init__() def read(self, max_count=-1): return io_channel_read(self, max_count) def readline(self, size_hint=-1): # note, size_hint is just to maintain backwards compatible API; the # old static binding did not actually use it (status, buf, length, terminator_pos) = self.read_line() if buf is None: return '' return buf def readlines(self, size_hint=-1): # note, size_hint is just to maintain backwards compatible API; # the old static binding did not actually use it lines = [] status = GLib.IOStatus.NORMAL while status == GLib.IOStatus.NORMAL: (status, buf, length, terminator_pos) = self.read_line() # note, this appends an empty line after EOF; this is # bug-compatible with the old static bindings if buf is None: buf = '' lines.append(buf) return lines def write(self, buf, buflen=-1): if not isinstance(buf, bytes): buf = buf.encode('UTF-8') if buflen == -1: buflen = len(buf) (status, written) = self.write_chars(buf, buflen) return written def writelines(self, lines): for line in lines: self.write(line) _whence_map = {0: GLib.SeekType.SET, 1: GLib.SeekType.CUR, 2: GLib.SeekType.END} def seek(self, offset, whence=0): try: w = self._whence_map[whence] except KeyError: raise ValueError("invalid 'whence' value") return self.seek_position(offset, w) def add_watch(self, condition, callback, *user_data, **kwargs): priority = kwargs.get('priority', GLib.PRIORITY_DEFAULT) return io_add_watch(self, priority, condition, callback, *user_data) add_watch = deprecated(add_watch, 'GLib.io_add_watch()') def __iter__(self): return self def __next__(self): (status, buf, length, terminator_pos) = self.read_line() if status == GLib.IOStatus.NORMAL: return buf raise StopIteration # Python 2.x compatibility next = __next__
# we need this to be accessible for unit testing __all__.append('_child_watch_add_get_args') def child_watch_add(*args, **kwargs): """child_watch_add(priority, pid, function, *data)""" priority, pid, function, data = _child_watch_add_get_args(*args, **kwargs) return GLib.child_watch_add(priority, pid, function, *data) __all__.append('child_watch_add') def get_current_time(): return GLib.get_real_time() * 0.000001 get_current_time = deprecated(get_current_time, 'GLib.get_real_time()') __all__.append('get_current_time') # backwards compatible API with default argument, and ignoring bytes_read # output argument def filename_from_utf8(utf8string, len=-1): return GLib.filename_from_utf8(utf8string, len)[0] __all__.append('filename_from_utf8') # backwards compatible API for renamed function if not hasattr(GLib, 'unix_signal_add_full'): def add_full_compat(*args):
# API aliases for backwards compatibility for name in [ 'markup_escape_text', 'get_application_name', 'set_application_name', 'get_prgname', 'set_prgname', 'main_depth', 'filename_display_basename', 'filename_display_name', 'filename_from_utf8', 'uri_list_extract_uris', 'MainLoop', 'MainContext', 'main_context_default', 'source_remove', 'Source', 'Idle', 'Timeout', 'PollFD', 'idle_add', 'timeout_add', 'timeout_add_seconds', 'io_add_watch', 'child_watch_add', 'get_current_time', 'spawn_async' ]: # ubuntu 12.04 glib if name == "uri_list_extract_uris" and not hasattr(GLib, name): continue globals()[name] = deprecated(getattr(GLib, name), 'GLib.' + name) __all__.append(name) # constants are also deprecated, but cannot mark them as such for name in [ 'PRIORITY_DEFAULT', 'PRIORITY_DEFAULT_IDLE', 'PRIORITY_HIGH', 'PRIORITY_HIGH_IDLE', 'PRIORITY_LOW', 'IO_IN', 'IO_OUT', 'IO_PRI', 'IO_ERR', 'IO_HUP', 'IO_NVAL', 'IO_STATUS_ERROR', 'IO_STATUS_NORMAL', 'IO_STATUS_EOF', 'IO_STATUS_AGAIN', 'IO_FLAG_APPEND', 'IO_FLAG_NONBLOCK', 'IO_FLAG_IS_READABLE', 'IO_FLAG_IS_WRITEABLE', 'IO_FLAG_IS_SEEKABLE', 'IO_FLAG_MASK', 'IO_FLAG_GET_MASK', 'IO_FLAG_SET_MASK', 'SPAWN_LEAVE_DESCRIPTORS_OPEN', 'SPAWN_DO_NOT_REAP_CHILD', 'SPAWN_SEARCH_PATH', 'SPAWN_STDOUT_TO_DEV_NULL', 'SPAWN_STDERR_TO_DEV_NULL', 'SPAWN_CHILD_INHERITS_STDIN', 'SPAWN_FILE_AND_ARGV_ZERO', 'OPTION_FLAG_HIDDEN', 'OPTION_FLAG_IN_MAIN', 'OPTION_FLAG_REVERSE',
# API aliases for backwards compatibility for name in ['markup_escape_text', 'get_application_name', 'set_application_name', 'get_prgname', 'set_prgname', 'main_depth', 'filename_display_basename', 'filename_display_name', 'filename_from_utf8', 'uri_list_extract_uris', 'MainLoop', 'MainContext', 'main_context_default', 'source_remove', 'Source', 'Idle', 'Timeout', 'PollFD', 'idle_add', 'timeout_add', 'timeout_add_seconds', 'io_add_watch', 'child_watch_add', 'get_current_time', 'spawn_async']: # ubuntu 12.04 glib if name == "uri_list_extract_uris" and not hasattr(GLib, name): continue globals()[name] = deprecated(getattr(GLib, name), 'GLib.' + name) __all__.append(name) # constants are also deprecated, but cannot mark them as such for name in ['PRIORITY_DEFAULT', 'PRIORITY_DEFAULT_IDLE', 'PRIORITY_HIGH', 'PRIORITY_HIGH_IDLE', 'PRIORITY_LOW', 'IO_IN', 'IO_OUT', 'IO_PRI', 'IO_ERR', 'IO_HUP', 'IO_NVAL', 'IO_STATUS_ERROR', 'IO_STATUS_NORMAL', 'IO_STATUS_EOF', 'IO_STATUS_AGAIN', 'IO_FLAG_APPEND', 'IO_FLAG_NONBLOCK', 'IO_FLAG_IS_READABLE', 'IO_FLAG_IS_WRITEABLE', 'IO_FLAG_IS_SEEKABLE', 'IO_FLAG_MASK', 'IO_FLAG_GET_MASK', 'IO_FLAG_SET_MASK', 'SPAWN_LEAVE_DESCRIPTORS_OPEN', 'SPAWN_DO_NOT_REAP_CHILD', 'SPAWN_SEARCH_PATH', 'SPAWN_STDOUT_TO_DEV_NULL', 'SPAWN_STDERR_TO_DEV_NULL', 'SPAWN_CHILD_INHERITS_STDIN', 'SPAWN_FILE_AND_ARGV_ZERO',