# HG changeset patch # User catherine@Elli.myhome.westell.com # Date 1222374008 14400 # Node ID a2731c87b837cedd1370d94c3f8ed8621c1494b5 # Parent d5917f02ae838dd0b1f0f828a0dd34c3a711c9e2 lists to genshi diff -r d5917f02ae83 -r a2731c87b837 sqlpyPlus.py --- a/sqlpyPlus.py Thu Sep 25 15:40:20 2008 -0400 +++ b/sqlpyPlus.py Thu Sep 25 16:20:08 2008 -0400 @@ -408,43 +408,15 @@ for row in self.rows] return '\n'.join(result) - xml_template = genshi.template.MarkupTemplate(''' - ''') - def output_row_as_xml(self, row): - result = [' <%s>\n %s\n ' % - (colname.lower(), self.str_or_empty(itm), colname.lower()) - for (itm, colname) in zip(row, self.colnames)] - return '\n'.join(result) - ''' - xml_template = genshi.template.MarkupTemplate(""" - - - - Column Name - - - - - Value - - - - -""") -''' - def output_as_xml(self): - return self.xml_template(**self.__dict__) - result = ['<%s>\n%s\n' % - (self.tblname, self.output_row_as_xml(row), self.tblname) - for row in self.rows] - return '\n'.join(result) - + xml_template = genshi.template.NewTextTemplate(""" + + <${tblname}_resultset>{% for row in rows %} + <$tblname>{% for (colname, itm) in zip(colnames, row) %} + <${colname.lower()}>$itm{% end %} + {% end %} + +""") + html_template = genshi.template.MarkupTemplate(""" @@ -465,18 +437,19 @@ """) + + list_template = genshi.template.NewTextTemplate(""" +{% for (rowNum, row) in enumerate(rows) %} +**** Row: ${rowNum + 1} +{% for (colname, itm) in zip(colnames, row) %}$colname: $itm +{% end %}{% end %}""") + + aligned_list_template = genshi.template.NewTextTemplate(""" +{% for (rowNum, row) in enumerate(rows) %} +**** Row: ${rowNum + 1} +{% for (colname, itm) in zip(colnames, row) %}${colname.ljust(colnamelen)}: $itm +{% end %}{% end %}""") - def output_as_list(self, align): - result = [] - colnamelen = max(len(colname) for colname in self.colnames) + 1 - for (idx, row) in enumerate(self.rows): - result.append('\n**** Row: %d' % (idx+1)) - for (itm, colname) in zip(row, self.colnames): - if align: - colname = colname.ljust(colnamelen) - result.append('%s: %s' % (colname, itm)) - return '\n'.join(result) - tableNameFinder = re.compile(r'from\s+([\w$#_"]+)', re.IGNORECASE | re.MULTILINE | re.DOTALL) def output(self, outformat, rowlimit): self.tblname = self.tableNameFinder.search(self.curs.statement).group(1) @@ -484,11 +457,12 @@ if outformat == '\\i': result = self.output_as_insert_statements() elif outformat == '\\x': - result = self.output_as_xml() + result = self.xml_template.generate(**self.__dict__) elif outformat == '\\g': - result = self.output_as_list(align=False) + result = self.list_template.generate(**self.__dict__) elif outformat == '\\G': - result = self.output_as_list(align=True) + self.colnamelen = max(len(colname) for colname in self.colnames) + result = self.aligned_list_template.generate(**self.__dict__) elif outformat in ('\\s', '\\S', '\\c', '\\C'): #csv result = [] if outformat in ('\\s', '\\c'):