1
2
3
4
5
6
7
8
9
10 "The magic developer console"
11
12 import wx
13 import wx.html
14 import wx.py
15 import widgets
16
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
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
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> <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
123