[Zope-dev] ZIdle, anyone?

Cees de Groot cg@cdegroot.com
1 Dec 2000 20:02:49 +0100


I've cooked up a quick IDLE extension that connects to ZEO and shows a
DB Browser, like the class/path browser. It's already great fun (quick
lookups through a Tkinter tree control beat the heck out of HTML), and
although my Tk is rusty, I think that Tk's HTML widget supports just
enough HTML to display management screens (that's step 2).

Now, before I go off and reinvent wheels, is this a new idea or not?

Code says more than 1000 words:

1. Startup script (change zope and idle locations)
--------------
#/bin/sh
PYTHONHOME=/opt/zope/current
export PYTHONHOME
PYTHONPATH=/opt/python/idle-0.5
PYTHONPATH=$PYTHONPATH:$PYTHONHOME
PYTHONPATH=$PYTHONPATH:$PYTHONHOME/lib/python
PYTHONPATH=$PYTHONPATH:$PYTHONHOME/Extensions
PYTHONPATH=$PYTHONPATH:$PYTHONHOME/ZServer/medusa
PYTHONPATH=$PYTHONPATH:$PYTHONHOME/ZServer
export PYTHONPATH
/usr/bin/python zidle.py
--------------
2. Zidle script
--------------
if __name__ == "__main__":
    from ZEO import ClientStorage
    from ZODB import DB
    from Zope import ClassFactory
    global App
    global Conn
    global Db

    storage = ClientStorage.ClientStorage(('', 8001))
    Db = DB(storage)
    Db.setClassFactory(ClassFactory.ClassFactory)
    Conn = Db.open()
    App = Conn.root()['Application']

    import PyShell, sys
    sys.argv = ['', '-t', 'ZIdle', '-c', 'from zidle import App, Conn']
    PyShell.main()
--------------
3. Browser
-------------
from TreeWidget import TreeNode, TreeItem, ScrolledCanvas
from WindowList import ListedToplevel
import PyShell

class DbBrowser:

    def __init__(self, dbnode):
        self.dbnode = dbnode
        self.init()

    def close(self, event=None):
        self.top.destroy()

    def init(self):
        self.top = ListedToplevel(PyShell.flist.root)
        self.top.protocol("WM_DELETE_WINDOW", self.close)
        self.top.bind("<Escape>", self.close)
        self.settitle()
        self.top.focus_set()
        sc = ScrolledCanvas(self.top, bg="white", highlightthickness=0,
                            takefocus=1)
        sc.frame.pack(expand=1, fill="both")
        item = self.rootnode()
        self.node = TreeNode(sc.canvas, None, item)
        self.node.update()
        self.node.expand()

    def settitle(self):
        self.top.wm_title("DB Browser")
        self.top.wm_iconname("DB Browser")

    def rootnode(self):
        return DbBrowserTreeItem(self.dbnode)

class DbBrowserTreeItem(TreeItem):

    def __init__(self, dbnode):
        self.dbnode = dbnode

    def GetText(self):
        try:
            return self.dbnode.title_and_id()
        except:
            return `self.dbnode`

    def GetIconName(self):
        try:
            if self.dbnode.isPrincipiaFolderish:
                return "folder"
            else:
                return "python"
        except:
            return "python"

    def GetSubList(self):
        sublist = []
        for i in self.dbnode.objectIds():
            sublist.append(DbBrowserTreeItem(self.dbnode[i]))
        return sublist

    def IsExpandable(self):
        try:
            return self.dbnode.isPrincipiaFolderish \
               and len(self.dbnode.objectMap()) > 0
        except:
            return 0

def main():
    from zidle import App
    DbBrowser(App)
    
if __name__ == "__main__":
    main()
----
4. README

Hit "dbbrowser.DbBrowser(App)" in the IDLE console and have fun.

-- 
Cees de Groot               http://www.cdegroot.com     <cg@cdegroot.com>
GnuPG 1024D/E0989E8B 0016 F679 F38D 5946 4ECD  1986 F303 937F E098 9E8B