comparison sqlpyPlus.py @ 146:d5917f02ae83

html output switched to genshi
author catherine@Elli.myhome.westell.com
date Thu, 25 Sep 2008 15:40:20 -0400
parents 7e5105efa15d
children a2731c87b837
comparison
equal deleted inserted replaced
145:7e5105efa15d 146:d5917f02ae83
184 and c1.r_owner = c2.owner 184 and c1.r_owner = c2.owner
185 and c1.owner = :owner 185 and c1.owner = :owner
186 """ 186 """
187 } 187 }
188 188
189 import sys, os, re, sqlpython, cx_Oracle, pyparsing, re, completion 189 import sys, os, re, sqlpython, cx_Oracle, pyparsing, re, completion, genshi
190 from cmd2 import Cmd, make_option, options, Statekeeper 190 from cmd2 import Cmd, make_option, options, Statekeeper
191 191
192 if float(sys.version[:3]) < 2.3: 192 if float(sys.version[:3]) < 2.3:
193 def enumerate(lst): 193 def enumerate(lst):
194 return zip(range(len(lst)), lst) 194 return zip(range(len(lst)), lst)
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('''
412 <ul xmlns:py="http://genshi.edgewall.org/">
413 <li py:for="item in items">${item}</li>
414 </ul>''')
411 def output_row_as_xml(self, row): 415 def output_row_as_xml(self, row):
412 result = [' <%s>\n %s\n </%s>' % 416 result = [' <%s>\n %s\n </%s>' %
413 (colname.lower(), self.str_or_empty(itm), colname.lower()) 417 (colname.lower(), self.str_or_empty(itm), colname.lower())
414 for (itm, colname) in zip(row, self.colnames)] 418 for (itm, colname) in zip(row, self.colnames)]
415 return '\n'.join(result) 419 return '\n'.join(result)
416 def output_as_xml(self): 420 '''
417 result = ['<%s>\n%s\n</%s>' % 421 xml_template = genshi.template.MarkupTemplate("""
418 (self.tblname, self.output_row_as_xml(row), self.tblname) 422 <xml xmlns:py="http://genshi.edgewall.org/">
419 for row in self.rows] 423 <relation py:attr="{'type':tblname}">
420 return '\n'.join(result) 424 <tuple py:for="row in rows" py:attr="{'type':>
421 425 </tuple>
422 html_template = """<html> 426 </relation>
423 <head>
424 <title py:content="tblname">Table Name</title>
425 </head>
426 <body>
427 <table py:attr="{'id':tblname}">
428 <tr> 427 <tr>
429 <th py:for="colname in colnames"> 428 <th py:for="colname in colnames">
430 <span py:replace="colname">Column Name</span> 429 <span py:replace="colname">Column Name</span>
431 </th> 430 </th>
432 </tr> 431 </tr>
433 <tr py:for="row in rows"> 432 <tr py:for="row in rows">
434 <td py:for="itm in row"> 433 <td py:for="itm in row">
435 <span py:replace="str_or_empty(itm)">Value</span> 434 <span py:replace="str(itm)">Value</span>
436 </td> 435 </td>
437 </tr> 436 </tr>
438 </table> 437 </table>
439 </body> 438 </body>
440 </html>""" 439 </html>""")
441 def output_as_html_table(self): 440 '''
442 result = ''.join('<th>%s</th>' % c for c in self.colnames) 441 def output_as_xml(self):
443 result = [' <tr>\n %s\n </tr>' % result] 442 return self.xml_template(**self.__dict__)
444 for row in self.rows: 443 result = ['<%s>\n%s\n</%s>' %
445 result.append(' <tr>\n %s\n </tr>' % 444 (self.tblname, self.output_row_as_xml(row), self.tblname)
446 (''.join('<td>%s</td>' % 445 for row in self.rows]
447 self.str_or_empty(itm) 446 return '\n'.join(result)
448 for itm in row))) 447
449 result = '''<table id="%s"> 448 html_template = genshi.template.MarkupTemplate("""
450 %s 449 <html xmlns:py="http://genshi.edgewall.org/">
451 </table>''' % (self.tblname, '\n'.join(result)) 450 <head>
452 return result 451 <title py:content="tblname">Table Name</title>
453 452 </head>
454 #TODO: use serious templating to make these user-tweakable 453 <body>
455 454 <table py:attrs="{'id':tblname}">
456 def output_as_markup(self, genshi_template): 455 <tr>
457 return None 456 <th py:for="colname in colnames">
458 #self.tblname, self.colnames, self.rows 457 <span py:replace="colname.lower()">Column Name</span>
459 458 </th>
459 </tr>
460 <tr py:for="row in rows">
461 <td py:for="itm in row">
462 <span py:replace="str(itm)">Value</span>
463 </td>
464 </tr>
465 </table>
466 </body>
467 </html>""")
468
460 def output_as_list(self, align): 469 def output_as_list(self, align):
461 result = [] 470 result = []
462 colnamelen = max(len(colname) for colname in self.colnames) + 1 471 colnamelen = max(len(colname) for colname in self.colnames) + 1
463 for (idx, row) in enumerate(self.rows): 472 for (idx, row) in enumerate(self.rows):
464 result.append('\n**** Row: %d' % (idx+1)) 473 result.append('\n**** Row: %d' % (idx+1))
486 result.append(','.join('"%s"' % colname for colname in self.colnames)) 495 result.append(','.join('"%s"' % colname for colname in self.colnames))
487 for row in self.rows: 496 for row in self.rows:
488 result.append(','.join('"%s"' % self.str_or_empty(itm) for itm in row)) 497 result.append(','.join('"%s"' % self.str_or_empty(itm) for itm in row))
489 result = '\n'.join(result) 498 result = '\n'.join(result)
490 elif outformat == '\\h': 499 elif outformat == '\\h':
491 result = self.output_as_html_table() 500 result = self.html_template.generate(**self.__dict__)
492 elif outformat == '\\t': 501 elif outformat == '\\t':
493 rows = [self.colnames] 502 rows = [self.colnames]
494 rows.extend(list(self.rows)) 503 rows.extend(list(self.rows))
495 transpr = [[rows[y][x] for y in range(len(rows))]for x in range(len(rows[0]))] # matrix transpose 504 transpr = [[rows[y][x] for y in range(len(rows))]for x in range(len(rows[0]))] # matrix transpose
496 newdesc = [['ROW N.'+str(y),10] for y in range(len(rows))] 505 newdesc = [['ROW N.'+str(y),10] for y in range(len(rows))]
557 rowlimitPattern = pyparsing.Word(pyparsing.nums)('rowlimit') 566 rowlimitPattern = pyparsing.Word(pyparsing.nums)('rowlimit')
558 terminatorPattern = (pyparsing.oneOf('; \\s \\S \\c \\C \\t \\x \\h \\g \\G \\i') 567 terminatorPattern = (pyparsing.oneOf('; \\s \\S \\c \\C \\t \\x \\h \\g \\G \\i')
559 ^ pyparsing.Literal('\n/') ^ \ 568 ^ pyparsing.Literal('\n/') ^ \
560 (pyparsing.Literal('\nEOF') + pyparsing.stringEnd)) \ 569 (pyparsing.Literal('\nEOF') + pyparsing.stringEnd)) \
561 ('terminator') + \ 570 ('terminator') + \
562 pyparsing.Optional(rowlimitPattern) + \ 571 pyparsing.Optional(rowlimitPattern) #+ \
563 pyparsing.FollowedBy(pyparsing.LineEnd()) 572 #pyparsing.FollowedBy(pyparsing.LineEnd())
564 def do_select(self, arg, bindVarsIn=None, override_terminator=None): 573 def do_select(self, arg, bindVarsIn=None, override_terminator=None):
565 """Fetch rows from a table. 574 """Fetch rows from a table.
566 575
567 Limit the number of rows retrieved by appending 576 Limit the number of rows retrieved by appending
568 an integer after the terminator 577 an integer after the terminator