annotate sqlpython/output_templates.py @ 269:e4869b369e12

json now works with numbers, etc
author catherine@dellzilla
date Wed, 18 Mar 2009 09:44:25 -0400
parents 7aa24643e358
children dd80e46b5ca2
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
268
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
17
266
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
18 '\\j': genshi.template.NewTextTemplate("""
268
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
19 {"${tblname}_resultset": [
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
20 ${',\\n '.join('{%s}' % (
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
21 ','.join('"%s": "%s"\\n ' % ((colname, itm) for (colname, itm) in zip(colnames, row)))
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
22 for row in rows))}
267
95195ec92c6c comma separation in genshi is hard
catherine@dellzilla
parents: 266
diff changeset
23 ]
266
342e96de6de6 json not quite there
catherine@Elli.myhome.westell.com
parents: 189
diff changeset
24 }"""),
267
95195ec92c6c comma separation in genshi is hard
catherine@dellzilla
parents: 266
diff changeset
25
268
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
26 '\\j': genshi.template.NewTextTemplate("""
269
e4869b369e12 json now works with numbers, etc
catherine@dellzilla
parents: 268
diff changeset
27 {"${tblname}": [
e4869b369e12 json now works with numbers, etc
catherine@dellzilla
parents: 268
diff changeset
28 ${',\\n'.join(' {%s}' % ', '.join('"%s": %s' % (colname,
e4869b369e12 json now works with numbers, etc
catherine@dellzilla
parents: 268
diff changeset
29 ((isinstance(itm,int) or isinstance(itm,float)) and '%s' or '"%s"') % str(itm)
e4869b369e12 json now works with numbers, etc
catherine@dellzilla
parents: 268
diff changeset
30 ) for (colname, itm) in zip(colnames, row)) for row in rows)}
268
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
31 ]
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
32 }"""),
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
33
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
34
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
35 '\\h': genshi.template.MarkupTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
36 <!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
37 <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
38 <head>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
39 <title py:content="tblname">Table Name</title>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
40 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
41 </head>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
42 <body>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
43 <table py:attrs="{'id':tblname,
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
44 'summary':'Result set from query on table ' + tblname}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
45 <tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
46 <th py:for="colname in colnames"
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
47 py:attrs="{'id':'header_' + colname.lower()}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
48 <span py:replace="colname.lower()">Column Name</span>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
49 </th>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
50 </tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
51 <tr py:for="row in rows">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
52 <td py:for="(colname, itm) in zip(colnames, row)" py:attrs="{'headers':'header_' + colname.lower()}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
53 <span py:replace="str(itm)">Value</span>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
54 </td>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
55 </tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
56 </table>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
57 </body>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
58 </html>"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
59
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
60 '\\g': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
61 {% for (rowNum, row) in enumerate(rows) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
62 **** Row: ${rowNum + 1}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
63 {% for (colname, itm) in zip(colnames, row) %}$colname: $itm
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
64 {% end %}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
65
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
66 '\\G': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
67 {% for (rowNum, row) in enumerate(rows) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
68 **** Row: ${rowNum + 1}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
69 {% for (colname, itm) in zip(colnames, row) %}${colname.ljust(colnamelen)}: $itm
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
70 {% end %}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
71
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
72 '\\i': genshi.template.NewTextTemplate("""{% for (rowNum, row) in enumerate(rows) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
73 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
74
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
75 '\\c': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
76 ${','.join(colnames)}{% for row in rows %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
77 ${','.join('"%s"' % val for val in row)}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
78
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
79 '\\C': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
80 {% for row in rows %}${','.join('"%s"' % val for val in row)}{% end %}""")
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
81
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
82 }
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
83
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
84 output_templates['\\s'] = output_templates['\\c']
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
85 output_templates['\\S'] = output_templates['\\C']