#!/usr/bin/python2.6

## This file is a part of PyGG: PyGTK and Glade
## (C) 2005 Franck Pommereau <pommereau@univ-paris12.fr>
##
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public
## License as published by the Free Software Foundation; either
## version 2.1 of the License, or (at your option) any later version.
##
## This library is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public
## License along with this library; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import sys, xml.parsers.expat
import gtk, gobject

def arg (name) :
    if name.startswith("Gtk") or name.startswith("Gdk") :
        return name[3:].lower()
    else :
        return name.lower()

_ret = { "gboolean" : "bool()",
         "gchararray" : "str()",
         "gdouble" : "float()",
         "gfloat" : "float()",
         "gint" : "int()",
         "gpointer" : "object()",
         "guint" : "abs(int())" }

def ret (name) :
    return _ret.get(name, name + "()")

class Handler :
    parent = { "GtkWindow" : "Window",
               "GtkDialog" : "Dialog" }
    def __init__ (self) :
        self.done = set()
    def startElement (self, name, attrs) :
        if name == "widget" :
            if not hasattr(self, "widget") :
                self.widget = attrs.get("class", "Widget")
                print "class %s (pygg.%s) :" % (attrs.get("id"),
                                                self.parent.get(self.widget, "Widget"))
            self.widget = attrs.get("class", "Widget")
        elif name == "signal" :
            handler = attrs.get("handler")
            if handler in self.done :
                return
            self.done.add(handler)
            signal = attrs.get("name")
            args = ["self"]
            _gtype = gobject.type_from_name(self.widget)
            _id, _name, _type, flags, _ret, _params = gobject.signal_query(signal, _gtype)
            args.extend([_type.name] + [t.name for t in _params])
            print "    def %s (%s) :" % (handler, ", ".join([arg(a) for a in args]))
            print '        print "%s"' % handler
            if _ret.name != "void" :
                print '        return %s' % ret(_ret.name)

parser = xml.parsers.expat.ParserCreate()
handler = Handler()

parser.StartElementHandler = handler.startElement

for src in sys.argv[1:] :
    parser.Parse(open(src).read())
    print
