Mercurial > sqlpython
comparison sqlpyPlus.py @ 147:a2731c87b837
lists to genshi
author | catherine@Elli.myhome.westell.com |
---|---|
date | Thu, 25 Sep 2008 16:20:08 -0400 |
parents | d5917f02ae83 |
children | fcb19853cd94 |
comparison
equal
deleted
inserted
replaced
146:d5917f02ae83 | 147:a2731c87b837 |
---|---|
406 result = ['INSERT INTO %s (%s) VALUES (%s);' % | 406 result = ['INSERT INTO %s (%s) VALUES (%s);' % |
407 (self.tblname, ','.join(self.colnames), formatRow(row)) | 407 (self.tblname, ','.join(self.colnames), formatRow(row)) |
408 for row in self.rows] | 408 for row in self.rows] |
409 return '\n'.join(result) | 409 return '\n'.join(result) |
410 | 410 |
411 xml_template = genshi.template.MarkupTemplate(''' | 411 xml_template = genshi.template.NewTextTemplate(""" |
412 <ul xmlns:py="http://genshi.edgewall.org/"> | 412 <xml> |
413 <li py:for="item in items">${item}</li> | 413 <${tblname}_resultset>{% for row in rows %} |
414 </ul>''') | 414 <$tblname>{% for (colname, itm) in zip(colnames, row) %} |
415 def output_row_as_xml(self, row): | 415 <${colname.lower()}>$itm</${colname.lower()}>{% end %} |
416 result = [' <%s>\n %s\n </%s>' % | 416 </$tblname>{% end %} |
417 (colname.lower(), self.str_or_empty(itm), colname.lower()) | 417 </${tblname}_resultset> |
418 for (itm, colname) in zip(row, self.colnames)] | 418 </xml>""") |
419 return '\n'.join(result) | 419 |
420 ''' | |
421 xml_template = genshi.template.MarkupTemplate(""" | |
422 <xml xmlns:py="http://genshi.edgewall.org/"> | |
423 <relation py:attr="{'type':tblname}"> | |
424 <tuple py:for="row in rows" py:attr="{'type':> | |
425 </tuple> | |
426 </relation> | |
427 <tr> | |
428 <th py:for="colname in colnames"> | |
429 <span py:replace="colname">Column Name</span> | |
430 </th> | |
431 </tr> | |
432 <tr py:for="row in rows"> | |
433 <td py:for="itm in row"> | |
434 <span py:replace="str(itm)">Value</span> | |
435 </td> | |
436 </tr> | |
437 </table> | |
438 </body> | |
439 </html>""") | |
440 ''' | |
441 def output_as_xml(self): | |
442 return self.xml_template(**self.__dict__) | |
443 result = ['<%s>\n%s\n</%s>' % | |
444 (self.tblname, self.output_row_as_xml(row), self.tblname) | |
445 for row in self.rows] | |
446 return '\n'.join(result) | |
447 | |
448 html_template = genshi.template.MarkupTemplate(""" | 420 html_template = genshi.template.MarkupTemplate(""" |
449 <html xmlns:py="http://genshi.edgewall.org/"> | 421 <html xmlns:py="http://genshi.edgewall.org/"> |
450 <head> | 422 <head> |
451 <title py:content="tblname">Table Name</title> | 423 <title py:content="tblname">Table Name</title> |
452 </head> | 424 </head> |
463 </td> | 435 </td> |
464 </tr> | 436 </tr> |
465 </table> | 437 </table> |
466 </body> | 438 </body> |
467 </html>""") | 439 </html>""") |
440 | |
441 list_template = genshi.template.NewTextTemplate(""" | |
442 {% for (rowNum, row) in enumerate(rows) %} | |
443 **** Row: ${rowNum + 1} | |
444 {% for (colname, itm) in zip(colnames, row) %}$colname: $itm | |
445 {% end %}{% end %}""") | |
446 | |
447 aligned_list_template = genshi.template.NewTextTemplate(""" | |
448 {% for (rowNum, row) in enumerate(rows) %} | |
449 **** Row: ${rowNum + 1} | |
450 {% for (colname, itm) in zip(colnames, row) %}${colname.ljust(colnamelen)}: $itm | |
451 {% end %}{% end %}""") | |
468 | 452 |
469 def output_as_list(self, align): | |
470 result = [] | |
471 colnamelen = max(len(colname) for colname in self.colnames) + 1 | |
472 for (idx, row) in enumerate(self.rows): | |
473 result.append('\n**** Row: %d' % (idx+1)) | |
474 for (itm, colname) in zip(row, self.colnames): | |
475 if align: | |
476 colname = colname.ljust(colnamelen) | |
477 result.append('%s: %s' % (colname, itm)) | |
478 return '\n'.join(result) | |
479 | |
480 tableNameFinder = re.compile(r'from\s+([\w$#_"]+)', re.IGNORECASE | re.MULTILINE | re.DOTALL) | 453 tableNameFinder = re.compile(r'from\s+([\w$#_"]+)', re.IGNORECASE | re.MULTILINE | re.DOTALL) |
481 def output(self, outformat, rowlimit): | 454 def output(self, outformat, rowlimit): |
482 self.tblname = self.tableNameFinder.search(self.curs.statement).group(1) | 455 self.tblname = self.tableNameFinder.search(self.curs.statement).group(1) |
483 self.colnames = [d[0] for d in self.curs.description] | 456 self.colnames = [d[0] for d in self.curs.description] |
484 if outformat == '\\i': | 457 if outformat == '\\i': |
485 result = self.output_as_insert_statements() | 458 result = self.output_as_insert_statements() |
486 elif outformat == '\\x': | 459 elif outformat == '\\x': |
487 result = self.output_as_xml() | 460 result = self.xml_template.generate(**self.__dict__) |
488 elif outformat == '\\g': | 461 elif outformat == '\\g': |
489 result = self.output_as_list(align=False) | 462 result = self.list_template.generate(**self.__dict__) |
490 elif outformat == '\\G': | 463 elif outformat == '\\G': |
491 result = self.output_as_list(align=True) | 464 self.colnamelen = max(len(colname) for colname in self.colnames) |
465 result = self.aligned_list_template.generate(**self.__dict__) | |
492 elif outformat in ('\\s', '\\S', '\\c', '\\C'): #csv | 466 elif outformat in ('\\s', '\\S', '\\c', '\\C'): #csv |
493 result = [] | 467 result = [] |
494 if outformat in ('\\s', '\\c'): | 468 if outformat in ('\\s', '\\c'): |
495 result.append(','.join('"%s"' % colname for colname in self.colnames)) | 469 result.append(','.join('"%s"' % colname for colname in self.colnames)) |
496 for row in self.rows: | 470 for row in self.rows: |