Idea: Can we modify Devel::REPL or combine with Perl Object Environment to have programmatic interaction with a gtk2-perl process like in other languages?




Hi,

I like perl and gtk2  and   would rather use it than  xyz (other languages :)). 

Can we set up a working interactive REPL with gtk2-perl as with pygtk for instance. I can use Devel::REPL to 
start and  build the gui
but then I lose the interaction control once I start Gtk2->main;

I would like to redefine callbacks, change widgets on the fly etc as we continue the interaction using 
Devel::REPL. I have done it with pygtk of course using the program
pygtkconsole.py. Do we modify Devel::REPL or set up something with perl POE?


I think we should be able to do this with gtk2-perl, although I am still learnining some of this stuff so I 
wanted your opinions on the details. Could we use perl POE?


Hw to keep access to the working program? Now I know that there are issues with gui threads. However can you 
imagine doing something with perl POE?

Thanks

Mitchell

Here for instance is what they do in pygtk:

In the pygtk tutorial: 

http://www.pygtk.org/pygtk2tutorial/ch-Introduction.html#sec-ExploringPygtk

you can set up a pygtk window and dynamically from the interactive shell drop child widgets
and add new ones to replace them and then change callbacks etc.

here is pygtkconsole.py

#!/usr/bin/env python
# -*- Mode: python; c-basic-offset: 4 -*-
#
# Interactive PyGtk Console, Johan Dahlin 2002
#

import os
import signal
import sys
import string
import socket, select
from code import InteractiveInterpreter, InteractiveConsole

import gtk
import gobject

# For compatibility, instead of using GDK.INPUT_READ or
# gtk.gdk.INPUT_READ depending on the PyGtk version
GDK_INPUT_READ = 1

class Mainloop(InteractiveInterpreter):
    def __init__(self, read_fd, sock):
        InteractiveInterpreter.__init__(self)
        self._rfd = os.fdopen(read_fd, 'r')
        self._sock = sock
        gobject.io_add_watch(read_fd, GDK_INPUT_READ, self.input_func)

    def read_message(self):
        length = ord(self._rfd.read(1))
        return self._rfd.read(length)

    def input_func(self, fd, cond):
        data = self.read_message()
        more = self.runsource(data)
        self._sock.send(chr(more))
        return True

    def run(self):
        gtk.main()
        
class Console(InteractiveConsole):
    def __init__(self, write_fd, sock, pid):
        InteractiveConsole.__init__(self)
        self._wfd = os.fdopen(write_fd, 'w')
        self._sock = sock
        self.pid = pid

    def send_message(self, message):
        self._wfd.write('%c%s' % (len(message), message))
        self._wfd.flush()

    def interact(self, banner=None):
        InteractiveConsole.interact(self, banner)
        # Die child die
        os.kill(self.pid, 9)
        
    def runsource(self, source, filename):
        self.send_message(source)
        # wait for notification from parent
        select.select([self._sock],[],[])
        more = ord(self._sock.recv(1))
        return more
        
class GtkInterpreter(Console):
    def __init__(self):
        rfd, wfd = os.pipe()
        # set up socket for returning command result
        sigsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock_addr = None
        for port in range(4321,5321):
            try:
                sigsock.bind(('', port))
                sock_addr = ('', port)
            except:
                pass
        if not sock_addr:
            print "Can't open socket"
        sigsock.listen(1)

        parent_pid = os.getpid()
        child_pid = os.fork()
        if not child_pid:
            # connect to command return socket
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect(sock_addr)
            g = Mainloop(rfd, sock)
            g.run()
        else:
            # Wait for command return socket connection
            sock, addr = sigsock.accept()
            Console.__init__(self, wfd, sock, child_pid)

def interact():
    try:
        import readline
        import rlcompleter
        readline.parse_and_bind('tab: complete')
    except ImportError:
        pass
    
    gi = GtkInterpreter()
    gi.push("from gtk import *")

    python_version = string.split(sys.version)[0]
    try:
    pygtk_version = string.join(map(str, gtk.pygtk_version), '.')
    gtk_version = string.join(map(str, gtk.gtk_version), '.')
    except:
    pygtk_version = '0.6.x'
    gtk_version = '1.2.x'

    banner = """Python %s, PyGTK %s (Gtk+ %s)
Interactive console to manipulate GTK+ widgets.""" % (python_version,
       pygtk_version,
       gtk_version)
    gi.interact(banner)
    
if __name__ == '__main__':
    interact()








I would like to be able to interact programmatically with a working gtk2-perl program through a REPL, as one 
might in lisp or in python. Thus redefine callbacks etc.
change baby widgets whatever one wanted to do. It seems like a cool thing to be able to do. I read a story 
somewhere about progammers who attached to a live 
lisp process to debug a satellite in orbit, and this would be nice for us to do with perl too.








[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]