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