Module developer
[hide private]
[frames] | no frames]

Source Code for Module developer

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2004 Roger Binns <rogerb@rogerbinns.com> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: developer.py 3944 2007-01-27 06:18:36Z djpham $ 
  9   
 10  "The magic developer console" 
 11   
 12  import wx 
 13  import wx.html 
 14  import wx.py 
 15  import widgets 
 16   
17 -class DeveloperPanel(wx.Panel, widgets.BitPimWidget):
18
19 - def __init__(self, parent, locals=None):
20 wx.Panel.__init__(self, parent) 21 22 split=wx.SplitterWindow(self, style = wx.SP_3D| wx.SP_LIVE_UPDATE) 23 24 if locals is None: 25 self.locals={} 26 else: 27 self.locals=locals.copy() 28 self.locals.update(self.getlocals()) 29 30 cmd=wx.py.shell.Shell(split, locals=self.locals, introText=self.introtext) 31 32 self.htmlw=wx.html.HtmlWindow(split) 33 34 split.SetMinimumPaneSize(20) 35 split.SplitHorizontally(cmd, self.htmlw) 36 37 vbs=wx.BoxSizer(wx.VERTICAL) 38 vbs.Add(split, 1, wx.EXPAND) 39 self.SetSizer(vbs)
40 41 introtext=""" 42 Welcome to the BitPim developer shell 43 44 You can do any standard Python stuff here you want. For example you can import the 45 various modules, access functions and variables etc. The following shortcuts are 46 also available: 47 48 Database: 49 50 sql("your sql here", bindings=()) -- runs query and displays results 51 tables() -- displays list of all tables and their schema 52 rows("tablename") -- shows all rows in named table 53 54 Useful variables: 55 56 wx -- the wxPython module 57 app -- the application instance 58 mw -- the main window instance 59 """ 60
61 - def getlocals(self):
62 return { 63 'sql': self.sql, 64 'wx': wx, 65 'app': wx.GetApp(), 66 'mw': wx.GetApp().frame, 67 'tables': self.tables, 68 'rows': self.rows, 69 }
70
71 - def sql(self, cmd, bindings=()):
72 "Executes sql statement and prints result" 73 desc=False 74 for row in self.locals['db'].sql(cmd,bindings): 75 if not desc: 76 print "#",self.locals['db'].cursor.getdescription() 77 desc=True 78 print row
79
80 - def tables(self):
81 "Gets list of all tables" 82 cursor=self.locals['db'].cursor 83 html="<h1>All tables</h1>" 84 html+="<table>" 85 for name,s in cursor.execute("select name,sql from sqlite_master where type='table' order by name"): 86 html+="<tr><td valign=top>&nbsp;<br><b>%s</b><td valign=top><pre>%s</pre></tr>" % (name, htmlify(s)) 87 html+="</table>" 88 self.htmlw.SetPage(html)
89
90 - def rows(self, table, wheres=None):
91 "Shows rows from table" 92 cursor=self.locals['db'].cursor 93 html="<h1>All rows in %s</h1>" % (htmlify(table),) 94 statement="select * from [%s]"%table 95 if wheres: 96 statement+=" where "+wheres 97 cursor.execute(statement) 98 try: 99 cursor.getdescription() 100 except: 101 html+="<p>No data" 102 self.htmlw.SetPage(html) 103 return 104 105 html+="<table border=1 cellpadding=3>" 106 html+="<tr>" 107 for col in cursor.getdescription(): 108 html+="<th>%s<br>%s" % (htmlify(col[0]), `col[1]`) 109 html+="</tr>" 110 for vals in cursor: 111 html+="<tr>" 112 for v in vals: 113 try: 114 html+="<td>%s" % (htmlify(str(v)),) 115 except Exception,e: 116 html+="<td>Exception: %s"%htmlify(str(e)) 117 html+="</tr>" 118 html+="</table>" 119 self.htmlw.SetPage(html)
120
121 -def htmlify(s):
122 return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
123