[PyKDE] Problems with PyKDE2 again (long)

Henrik Motakef henrik.motakef at web.de
Sun Feb 10 23:48:26 GMT 2002


Hi.

I just tried to write a simple app using KParts with PyKDE2alpha3
(basically a file viewer that loads appropriate KParts for the
selected files). I had a few problems, that might be bugs (Note
however that I never wrote anything using KParts before, so I could
just be doing things wrong.)

Back on the KStdAction topic, I wrote a simple wrapper module that one
can use instead of KStdAction directly, that deals with Python
slots. Probably there is a more sane way to do it, but until someone
tells me what it is... I included a script that generates this module
at the end of this message.

As for the problems:

First, KCmlLineArgs.init1 doesn't seem to work. When I do something
like 

    aboutData = KAboutData("Foo", "foo", "0.1",
                           "Useless test program",
                           KAboutData.License_GPL,
                           "(c) 2002 Henrik Motakef")
    KCmdLineArgs.init1(sys.argv, aboutData)

I get a
    TypeError: Argument 2 of KCmdLineArgs.init1() has an invalid type

Second, I cannot get an instance of KIO.KTrader. (It's a singleton
with an accessor method self()). A call to
    KIO.KTrader.self()
results in a segfault.

The output is:
  Trying to load /usr/home/henrik/devel/pykde/browser/pyviewer.py  (text/x-java)
  Getting trader
  KCrash: crashing.... crashRecursionCounter = 2
  KCrash: Application Name =  path = <unknown> pid = 60365

(The first two lines are debugging output generated by me)

Unfortunatly I don't have debugging support in my KDE installation
(and to be honest, I don't plan to change this - KDE still isn't too
fast even without :), so Dr. Konqi doesn't give me a usable
backtrace. I have also included the script where it happens, so that
interested people can try to reproduce it.

(BTW: If this is not an appropriate place to send such stuff, I'd be
thankful for pointers. Sorry for the traffic)

Regards
Henrik

----make_pykstdaction.py-----

#!/usr/bin/env python
# Prints a Module mimicking (a subset of) the KStdAction class 
# to stdout.
#
# PyViewer.py uses this module, so to run it, use:
#   $ python make_pykstdaction.py > PyKStdAction.py
#
# BTW, the module lacks the "print" action, scince "print" is a 
# reserved word in Python

template = """
def %(action)s(*args):
    if callable(args[0]):
        if len(args) == 2:
            action = KStdAction.%(action)s(None, None, args[1])
        else:
            action = KStdAction.%(action)s(None, None, args[1], args[2])
        QObject.connect(action, SIGNAL("activated()"), args[0])
        return action
    else:
        return apply(KStdAction.%(action)s, args)
"""

actions = [ "openNew", "open", "openRecent", "save", "saveAs", 
            "revert", "close", "printPreview", "mail", "quit",
            "undo", "redo", "cut", "copy", "paste", "selectAll",
            "find", "findNext", "findPrev", "replace", "actualSize",
            "fitToPage", "fitToWidth", "fitToHeight", "zoomIn",
            "zoomOut", "zoom", "redisplay", "up", "back", "forward",
            "home", "prior", "next", "goTo", "gotoPage", "gotoLine",
            "firstPage", "lastPage", "addBookmark", "editBookmarks",
            "spelling", "showMenubar", "showToolbar", "showStatusbar",
            "saveOptions", "keyBindings", "preferences",
            "configureToolbars", "help", "helpContents", "whatsThis",
            "reportBug", "aboutApp", "aboutKDE" ]

print "from kdeui import KStdAction"
print "from qt import QObject, SIGNAL"

for action in actions:
    print template % {"action": action}


----pyviewer.py-----
#!/usr/bin/env python

from qt import *
from kdecore import *
from kdeui import *
from kparts import *
from kio import *
from kfile import *
import PyKStdAction

import sys
def debug(s):
    sys.stderr.write("%s\n" % s)
    sys.stderr.flush()
    
class PyViewer(KParts.MainWindow):
    def __init__(self, *args):
        apply(KParts.MainWindow.__init__, (self,) + args)

        self.part = None

        PyKStdAction.open(self.open, self.actionCollection())
        PyKStdAction.quit(kapp, SLOT("quit()"), self.actionCollection())

        self.setXMLFile("pyviewerui.rc")
        self.createGUI(None)

    def open(self):
        filename = KFileDialog.getOpenFileName()
        if filename != "":
            mimetype = KMimeMagic.self().findFileType(filename).mimeType()
            debug("Trying to load %s  (%s)" % (filename, mimetype))
            self.load(filename, mimetype)

    def load(self, filename, mimetype):
        debug("Getting trader")
        trader = KIO.KTrader.self()  # <- SIGSEGV

        debug( "Querying mimetype")
        trader.query(mimetype, "'KParts/ReadOnlyPart' in ServiceTypes")
        debug("Got %d offers" % len(offers))

        if len(offers) == 0:
            KMessageBox.error(self, "Don't know how to handle this file",
                              "Unsupported file type")
            return
        
        service = offers[0]

        debug("Loading library")
        library = KLibLoader.self().factory(service.library())

        debug("Creating part")
        self.part = library.create(self, service.name, "KParts::ReadOnlyPart")

        debug("Opening file")
        self.part.openURL(filename)

        debug("Setting view")
        self.setView(self.part)

        debug("Creating new GUI")
        self.createGUI(self.part)
        
if __name__=="__main__":
    import sys

    aboutData = KAboutData("pyviewer", "PyViewer", "0.1",
                           "Simple file viewer using PyKDE2",
                           KAboutData.License_GPL,
                           "(c) 2002 Henrik Motakef")
    #KCmdLineArgs.init1(sys.argv, aboutData)
    KCmdLineArgs.init(sys.argv, "pyviewer", "Simple viewer using PyKDE2", "0.1")

    app = KApplication()
    win = PyViewer(None, "PyViewer")
    win.show()
    app.setMainWidget(win)
    app.exec_loop()

----pyviewerui.rc-----
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<!-- This file should go to $KDEDIR/share/apps/pyviewer      -->
<!-- It is obviously useless, but keeps KDE from complaining -->
<!-- about parse errors in createGUI()                       -->
<kpartgui name="pyviewer" version="1"/>

________________________________________________________________
Keine verlorenen Lotto-Quittungen, keine vergessenen Gewinne mehr! 
Beim WEB.DE Lottoservice: http://tippen2.web.de/?x=13






More information about the PyQt mailing list