def _initChannel(self, filedesc, callbackflag, callback, isstderr):
     channel = gobject.IOChannel(filedesc)
     if sys.platform != "win32":
         channel.set_flags(gobject.IO_FLAG_NONBLOCK)
     if callback:
         tag = channel.add_watch(callbackflag, callback, isstderr)
         self.__channelTags.append(tag)
     return channel
Example #2
0
    def testPipe(self):
        #
        # Disable this test for win32, because it fails and warns:
        #
        # File "tests\test_tasklet.py", line 81, in pipe_reader
        #    assert chan.get_flags() & gobject.IO_FLAG_IS_READABLE
        #
        # ???:81: g_io_channel_get_flags: assertion `channel != NULL' failed
        # ???:95: giowin32.c:1669: 4 is neither a file descriptor or a socket
        # ???:96: g_io_channel_set_flags: assertion `channel != NULL' failed
        #
        if sys.platform == 'win32':
            return

        def pipe_reader(chan):
            assert chan.get_flags() & gobject.IO_FLAG_IS_READABLE
            yield tasklet.WaitForIO(chan, gobject.IO_IN)
            tasklet.get_event()
            c = chan.read(1)
            raise StopIteration(c)

        def pipe_writer(chan, c):
            assert chan.get_flags() & gobject.IO_FLAG_IS_WRITEABLE
            yield tasklet.WaitForIO(chan, gobject.IO_OUT)
            tasklet.get_event()
            chan.write(c)

        read_fd, write_fd = os.pipe()

        read_chan = gobject.IOChannel(read_fd)
        read_chan.set_flags(gobject.IO_FLAG_NONBLOCK)
        reader = tasklet.run(pipe_reader(read_chan))

        write_chan = gobject.IOChannel(write_fd)
        write_chan.set_flags(gobject.IO_FLAG_NONBLOCK)
        write_chan.set_encoding(None)
        write_chan.set_buffered(False)
        tasklet.run(pipe_writer(write_chan, chr(123)))

        mainloop = gobject.MainLoop()
        reader.add_join_callback(lambda task, retval: mainloop.quit())
        mainloop.run()

        self.assertEqual(reader.state, tasklet.Tasklet.STATE_ZOMBIE)
        self.assertEqual(reader.return_value, chr(123))
 def add_io_channel(self, sock):
     """Set up notification on the socket via a giochannel"""
     sock.setblocking(False)
     channel = gobject.IOChannel(sock.fileno())
     channel.set_flags(channel.get_flags() | gobject.IO_FLAG_NONBLOCK)
     self._sources.append(channel.add_watch(gobject.IO_IN,
         self._socket_connected))
     self._sources.append(channel.add_watch(gobject.IO_HUP | gobject.IO_ERR,
         self._socket_disconnected))
     return channel
Example #4
0
    def _pre_open(self, io_object):
        io_object.setblocking(False)
        channel = gobject.IOChannel(io_object.fileno())
        channel.set_flags(channel.get_flags() | gobject.IO_FLAG_NONBLOCK)
        channel.set_encoding(None)
        channel.set_buffered(False)

        self._transport = io_object
        self._channel = channel

        self._source_id = None
        self._source_condition = 0
        self._outgoing_queue = []
        AbstractClient._pre_open(self)
Example #5
0
    def __CreateChannel(fd):
        """Creates the inner gobject.IOChannel instance.

    Note: has side-effects on file descriptor by setting O_NONBLOCK.

    @type fd: int
    @param fd: File descriptor

    """
        channel = gobject.IOChannel(fd)
        channel.set_encoding(None)
        channel.set_buffered(0)
        channel.set_flags(gobject.IO_FLAG_NONBLOCK)
        return channel
Example #6
0
def main():
    parser = OptionParser(usage="usage: %prog command")
    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.print_help()
        sys.exit(1)

    win = gtk.Window()
    textview = gtk.TextView()
    textview.modify_font(pango.FontDescription("Monospace"))
    textview.show()
    sw = gtk.ScrolledWindow()
    sw.add(textview)
    sw.show()
    win.add(sw)
    win.set_default_size(gtk.gdk.screen_width() * 2 / 3,
                         gtk.gdk.screen_height() * 2 / 3)
    win.show()

    ## launch process
    proc = subprocess.Popen(args[0],
                            shell=True,
                            stdout=subprocess.PIPE,
                            bufsize=1,
                            close_fds=True)
    win.set_title("%s (running)" % args[0])
    # print proc.stdout, type(proc.stdout), dir(proc.stdout)
    chan = gobject.IOChannel(filedes=proc.stdout.fileno())
    chan.set_flags(gobject.IO_FLAG_NONBLOCK)
    sink = tasklet.run(
        process_stdout_sink(chan, textview.get_buffer(), textview))

    ## child watch
    yield (tasklet.WaitForProcess(proc.pid),
           tasklet.WaitForSignal(win, "destroy"))

    if isinstance(tasklet.get_event(), tasklet.WaitForSignal):
        killproc(proc.pid)
        gtk.main_quit()
    else:
        ## stop reader
        yield tasklet.Message("quit", dest=sink)
        win.set_title("%s (completed)" % args[0])
        yield tasklet.WaitForSignal(win, "destroy")
        tasklet.get_event()
        gtk.main_quit()
Example #7
0
 def read_and_close(io):
     t = gobject.IOChannel(io)
     out = t.read()
     t.close()
     return out
Example #8
0
data = CustomData()

#print usage map
print("USAGE: Choose one of the following options, then press enter:\n"
      " 'C' to increase contrast, 'c' to decrease contrast\n"
      " 'B' to increase brightness, 'b' to decrease brightness\n"
      " 'H' to increase hue, 'h' to decrease hue\n"
      " 'S' to increase saturation, 's' to decrease saturation\n"
      " 'Q' to quit")

#Build the pipeline
data.pipeline = gst.parse_launch(
    "playbin2 uri=http://docs.gstreamer.com/media/sintel_trailer-480p.webm")

#Add a keyboard watch so we get notified of keystrokes
io_stdin = gobject.IOChannel(sys.stdin.fileno())
io_stdin.add_watch(glib.IO_IN, handle_keyboard, data)

ret = data.pipeline.set_state(gst.STATE_PLAYING)
if (ret == gst.STATE_CHANGE_FAILURE):
    print >> sys.stderr, "Unable to set the pipeline to the playing state."
    exit(-1)

print_current_values(data.pipeline)

#Create a GLib Main loop and set it to run
data.loop = gobject.MainLoop(None, False)
data.loop.run()

data.pipeline.set_state(gst.STATE_NULL)