PyXR

c:\projects\bitpim\src \ developer.py



0001 ### BITPIM
0002 ###
0003 ### Copyright (C) 2004 Roger Binns <rogerb@rogerbinns.com>
0004 ###
0005 ### This program is free software; you can redistribute it and/or modify
0006 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0007 ###
0008 ### $Id: developer.py 3944 2007-01-27 06:18:36Z djpham $
0009 
0010 "The magic developer console"
0011 
0012 import wx
0013 import wx.html
0014 import wx.py
0015 import widgets
0016 
0017 class DeveloperPanel(wx.Panel, widgets.BitPimWidget):
0018     
0019     def __init__(self, parent, locals=None):
0020         wx.Panel.__init__(self, parent)
0021 
0022         split=wx.SplitterWindow(self, style = wx.SP_3D| wx.SP_LIVE_UPDATE)
0023 
0024         if locals is None:
0025             self.locals={}
0026         else:
0027             self.locals=locals.copy()
0028         self.locals.update(self.getlocals())
0029         
0030         cmd=wx.py.shell.Shell(split, locals=self.locals, introText=self.introtext)
0031         
0032         self.htmlw=wx.html.HtmlWindow(split)
0033 
0034         split.SetMinimumPaneSize(20)
0035         split.SplitHorizontally(cmd, self.htmlw)
0036 
0037         vbs=wx.BoxSizer(wx.VERTICAL)
0038         vbs.Add(split, 1, wx.EXPAND)
0039         self.SetSizer(vbs)
0040 
0041     introtext="""
0042 Welcome to the BitPim developer shell
0043 
0044 You can do any standard Python stuff here you want.  For example you can import the
0045 various modules, access functions and variables etc.  The following shortcuts are
0046 also available:
0047 
0048 Database:
0049 
0050   sql("your sql here", bindings=())  -- runs query and displays results
0051   tables()                           -- displays list of all tables and their schema
0052   rows("tablename")                  -- shows all rows in named table
0053 
0054 Useful variables:
0055 
0056   wx                                 -- the wxPython module
0057   app                                -- the application instance
0058   mw                                 -- the main window instance
0059 """
0060 
0061     def getlocals(self):
0062         return {
0063             'sql': self.sql,
0064             'wx': wx,
0065             'app': wx.GetApp(),
0066             'mw': wx.GetApp().frame,
0067             'tables': self.tables,
0068             'rows': self.rows,
0069             }
0070 
0071     def sql(self, cmd, bindings=()):
0072         "Executes sql statement and prints result"
0073         desc=False
0074         for row in self.locals['db'].sql(cmd,bindings):
0075             if not desc:
0076                 print "#",self.locals['db'].cursor.getdescription()
0077                 desc=True
0078             print row
0079 
0080     def tables(self):
0081         "Gets list of all tables"
0082         cursor=self.locals['db'].cursor
0083         html="<h1>All tables</h1>"
0084         html+="<table>"
0085         for name,s in cursor.execute("select name,sql from sqlite_master where type='table' order by name"):
0086             html+="<tr><td valign=top>&nbsp;<br><b>%s</b><td valign=top><pre>%s</pre></tr>" % (name, htmlify(s))
0087         html+="</table>"
0088         self.htmlw.SetPage(html)
0089 
0090     def rows(self, table, wheres=None):
0091         "Shows rows from table"
0092         cursor=self.locals['db'].cursor
0093         html="<h1>All rows in %s</h1>" % (htmlify(table),)
0094         statement="select * from [%s]"%table
0095         if wheres:
0096             statement+=" where "+wheres
0097         cursor.execute(statement)
0098         try:
0099             cursor.getdescription()
0100         except:
0101             html+="<p>No data"
0102             self.htmlw.SetPage(html)
0103             return
0104 
0105         html+="<table border=1 cellpadding=3>"
0106         html+="<tr>"
0107         for col in cursor.getdescription():
0108             html+="<th>%s<br>%s" % (htmlify(col[0]), `col[1]`)
0109         html+="</tr>"
0110         for vals in cursor:
0111             html+="<tr>"
0112             for v in vals:
0113                 try:
0114                     html+="<td>%s" % (htmlify(str(v)),)
0115                 except Exception,e:
0116                     html+="<td>Exception: %s"%htmlify(str(e))
0117             html+="</tr>"
0118         html+="</table>"
0119         self.htmlw.SetPage(html)
0120 
0121 def htmlify(s):
0122     return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
0123 

Generated by PyXR 0.9.4