annotate sqlpython/output_templates.py @ 266:342e96de6de6

json not quite there
author catherine@Elli.myhome.westell.com
date Mon, 16 Mar 2009 23:30:41 -0400
parents c5398d87498e
children 95195ec92c6c
rev   line source
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
1 import genshi.template
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
2
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
3 # To make more output formats available to sqlpython, just edit this
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
4 # file, or place a copy in your local directory and edit that.
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
5
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
6 output_templates = {
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
7
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
8 '\\x': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
9 <xml>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
10 <${tblname}_resultset>{% for row in rows %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
11 <$tblname>{% for (colname, itm) in zip(colnames, row) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
12 <${colname.lower()}>$itm</${colname.lower()}>{% end %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
13 </$tblname>{% end %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
14 </${tblname}_resultset>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
15 </xml>"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
16
266
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
17 '\\j': genshi.template.NewTextTemplate("""
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
18 {"${tblname}_resultset: [{% for row in rows %}
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
19 "${tblname}": {{% for (colname, itm) in zip(colnames, row) %}
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
20 "${colname.lower()}": "${itm}",{% end %} },
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
21 {% end %}]
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
22 }"""),
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
23
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
24 '\\h': genshi.template.MarkupTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
25 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
26 <html xmlns:py="http://genshi.edgewall.org/" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
27 <head>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
28 <title py:content="tblname">Table Name</title>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
29 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
30 </head>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
31 <body>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
32 <table py:attrs="{'id':tblname,
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
33 'summary':'Result set from query on table ' + tblname}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
34 <tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
35 <th py:for="colname in colnames"
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
36 py:attrs="{'id':'header_' + colname.lower()}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
37 <span py:replace="colname.lower()">Column Name</span>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
38 </th>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
39 </tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
40 <tr py:for="row in rows">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
41 <td py:for="(colname, itm) in zip(colnames, row)" py:attrs="{'headers':'header_' + colname.lower()}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
42 <span py:replace="str(itm)">Value</span>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
43 </td>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
44 </tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
45 </table>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
46 </body>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
47 </html>"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
48
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
49 '\\g': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
50 {% for (rowNum, row) in enumerate(rows) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
51 **** Row: ${rowNum + 1}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
52 {% for (colname, itm) in zip(colnames, row) %}$colname: $itm
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
53 {% end %}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
54
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
55 '\\G': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
56 {% for (rowNum, row) in enumerate(rows) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
57 **** Row: ${rowNum + 1}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
58 {% for (colname, itm) in zip(colnames, row) %}${colname.ljust(colnamelen)}: $itm
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
59 {% end %}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
60
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
61 '\\i': genshi.template.NewTextTemplate("""{% for (rowNum, row) in enumerate(rows) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
62 INSERT INTO $tblname (${', '.join(colnames)}) VALUES (${', '.join(f % r for (r,f) in zip(row, formatters))});{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
63
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
64 '\\c': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
65 ${','.join(colnames)}{% for row in rows %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
66 ${','.join('"%s"' % val for val in row)}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
67
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
68 '\\C': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
69 {% for row in rows %}${','.join('"%s"' % val for val in row)}{% end %}""")
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
70
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
71 }
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
72
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
73 output_templates['\\s'] = output_templates['\\c']
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
74 output_templates['\\S'] = output_templates['\\C']