annotate sqlpython/output_templates.py @ 348:c652478be4fd

migrated \dt to gerald
author catherine@cordelia
date Fri, 24 Apr 2009 15:09:29 -0400
parents c2e3223703f2
children
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 '\\j': genshi.template.NewTextTemplate("""
269
e4869b369e12 json now works with numbers, etc
catherine@dellzilla
parents: 268
diff changeset
18 {"${tblname}": [
e4869b369e12 json now works with numbers, etc
catherine@dellzilla
parents: 268
diff changeset
19 ${',\\n'.join(' {%s}' % ', '.join('"%s": %s' % (colname,
e4869b369e12 json now works with numbers, etc
catherine@dellzilla
parents: 268
diff changeset
20 ((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
21 ) for (colname, itm) in zip(colnames, row)) for row in rows)}
268
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
22 ]
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
23 }"""),
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
24
7aa24643e358 json almost working
catherine@dellzilla
parents: 267
diff changeset
25
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
26 '\\h': genshi.template.MarkupTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
27 <!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
28 <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
29 <head>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
30 <title py:content="tblname">Table Name</title>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
31 <meta http-equiv="content-type" content="text/html;charset=utf-8" />
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
32 </head>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
33 <body>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
34 <table py:attrs="{'id':tblname,
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
35 'summary':'Result set from query on table ' + tblname}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
36 <tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
37 <th py:for="colname in colnames"
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
38 py:attrs="{'id':'header_' + colname.lower()}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
39 <span py:replace="colname.lower()">Column Name</span>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
40 </th>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
41 </tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
42 <tr py:for="row in rows">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
43 <td py:for="(colname, itm) in zip(colnames, row)" py:attrs="{'headers':'header_' + colname.lower()}">
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
44 <span py:replace="str(itm)">Value</span>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
45 </td>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
46 </tr>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
47 </table>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
48 </body>
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
49 </html>"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
50
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
51 '\\g': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
52 {% for (rowNum, row) in enumerate(rows) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
53 **** Row: ${rowNum + 1}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
54 {% for (colname, itm) in zip(colnames, row) %}$colname: $itm
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
55 {% end %}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
56
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
57 '\\G': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
58 {% for (rowNum, row) in enumerate(rows) %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
59 **** Row: ${rowNum + 1}
344
c2e3223703f2 finally desc works in postgres
Catherine Devlin <catherine.devlin@gmail.com>
parents: 322
diff changeset
60 {% for (colname, itm) in zip(colnames, row) %}${colname.ljust(max(colnamelengths))}: $itm
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
61 {% end %}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
62
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
63 '\\i': genshi.template.NewTextTemplate("""{% for (rowNum, row) in enumerate(rows) %}
322
d791333902f7 handle NULLs and apostrophes in \i inserts
Catherine Devlin <catherine.devlin@gmail.com>
parents: 270
diff changeset
64 INSERT INTO $tblname (${', '.join(colnames)}) VALUES (${', '.join(formattedForSql(r) for r in row)});{% end %}"""),
189
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
65
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
66 '\\c': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
67 ${','.join(colnames)}{% for row in rows %}
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
68 ${','.join('"%s"' % val for val in row)}{% end %}"""),
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
69
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
70 '\\C': genshi.template.NewTextTemplate("""
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
71 {% for row in rows %}${','.join('"%s"' % val for val in row)}{% end %}""")
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
72
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
73 }
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
74
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
75 output_templates['\\s'] = output_templates['\\c']
c5398d87498e cat bug
catherine@dellzilla
parents:
diff changeset
76 output_templates['\\S'] = output_templates['\\C']