Beispiel #1
0
    def make_text_csv(self):
        """
        Get the text representation of a report element as csv.
        """
        import csv
        import io

        try:
            import io
            out = io.StringIO()
        except ImportError:
            import io
            out = io.StringIO()

        writer = csv.writer(out,
                            delimiter='|',
                            lineterminator='\n',
                            quoting=csv.QUOTE_MINIMAL)

        if self.function == 'total':
            writer.writerows(self.results)

        elif self.function == 'top':
            rows = [['Value', self.headers.strip('"')]]
            if self.results[0] is not None:
                for res in self.results:
                    if res is not None:
                        rows.append(tuple([res[0], ','.join(res[1])]))
                writer.writerows(rows)

        elif self.function == 'table':
            rows = [[
                header.strip('"')
                for header in re.split('\s*,\s*', self.headers)
            ]]

            for res in sorted(self.results, key=lambda x: x[0]):
                row = list(res[:-1])
                lastcol = get_fmt_results(res[-1], limit=10)
                if lastcol[-1][0] == '[' and lastcol[-1][-1] == ']':
                    row.append('{0} {1}'.format(', '.join(lastcol[:-1]),
                                                lastcol[-1]))
                else:
                    row.append(', '.join(lastcol))
                rows.append(row)

            writer.writerows(rows)

        self.csv_text = out.getvalue()
Beispiel #2
0
    def make_text_csv(self):
        """
        Get the text representation of a report element as csv.
        """
        import csv
        import io

        try:
            import cStringIO
            out = cStringIO.StringIO()
        except ImportError:
            import io
            out = io.StringIO()            
            
        writer = csv.writer(out, delimiter='|', lineterminator='\n', quoting=csv.QUOTE_MINIMAL)

        if self.function == 'total':
            writer.writerows(self.results)                

        elif self.function == 'top':
            rows = [['Value', self.headers.strip('"')]]
            if self.results[0] is not None:
                for res in self.results:
                    if res is not None:
                        rows.append(tuple([res[0], ','.join(res[1])]))
                writer.writerows(rows)
                
        elif self.function == 'table':
            rows = [[header.strip('"') for header in re.split('\s*,\s*', self.headers)]]

            for res in sorted(self.results, key=lambda x: x[0]):
                row = list(res[:-1])
                lastcol = get_fmt_results(res[-1], limit=10)
                if lastcol[-1][0] == '[' and lastcol[-1][-1] == ']':
                    row.append(u'{0} {1}'.format(u', '.join(lastcol[:-1]), lastcol[-1]))
                else:
                    row.append(u', '.join(lastcol))
                rows.append(row)
                
            writer.writerows(rows)

        self.csv_text = out.getvalue()
Beispiel #3
0
    def make_text_html(self):
        """
        Make the text representation of a report element as html.
        """
        text = None
        if self.function == 'total':
            text = '<table border="0" width="100%" rules="cols" cellpadding="2">\n'\
                   '<tr><th colspan="2" align="left"><h3><font color="{1}">'\
                   '{0}</font></h3></th></tr>\n'\
                   .format(lograptor.utils.htmlsafe(self.title.strip()), self.color)

            for res in self.results:
                text = '{0}<tr><td valign="top" align="right">{1}</td>'\
                       '<td valign="top" width="90%">{2}</td></tr>'\
                       .format(text, res[0], res[1])

        elif self.function == 'top':
            text = '<table border="0" width="100%" rules="cols" cellpadding="2">\n'\
                   '<tr><th colspan="2" align="left"><h3><font color="{1}">'\
                   '{0}</font></h3></th></tr>\n'\
                   .format(lograptor.utils.htmlsafe(self.title.strip()), self.color)

            if self.results[0] is not None:
                for res in self.results:
                    if res is not None:
                        text = '{0}<tr><td valign="top" align="right">{1}</td>'\
                               '<td valign="top" width="90%">{2}</td></tr>'\
                               .format(text, res[0], ', '.join(res[1]))
            else:
                text = '{0}<tr><td valign="top" align="left">{1}</td>'\
                       .format(text, "None")

        elif self.function == 'table':
            text = '<h3><font color="{1}">{0}</font></h3>'\
                   '<table width="100%" rules="cols" cellpadding="2">\n'\
                   '<tr bgcolor="#aaaaaa">'\
                   .format(lograptor.utils.htmlsafe(self.title.strip()), self.color)

            headers = re.split('\s*,\s*', self.headers)
            for i in range(len(headers)):
                text = '{0}<th align="center" colspan="1">'\
                       '<font color="black">{1}</font></th>'\
                       .format(text, headers[i].strip('"'))

            text = '{0}</tr>\n'.format(text)

            oddflag = False
            lastval = ""
            for res in sorted(self.results, key=lambda x: x[0]):
                if lastval != res[0]:
                    oddflag = not oddflag
                    if oddflag:
                        text = '{0}<tr bgcolor="#dddddd">'.format(text)
                    else:
                        text = '{0}<tr>'.format(text)

                    text = '{0}<td valign="top" width="15%">{1}</td>'\
                           .format(text, res[0])
                else:
                    if oddflag:
                        text = '{0}<tr bgcolor="#dddddd">'.format(text)
                    else:
                        text = '{0}<tr>'.format(text)

                    text = '{0}<td valign="top" width="15%">&nbsp;</td>'.format(
                        text)
                lastval = res[0]

                for i in range(1, len(headers) - 1):
                    text = '{0}<td valign="top" width="15%">{1}</td>'.format(
                        text, res[i])
                lastcol = get_fmt_results(
                    res[-1], limit=10, fmt='<font color="darkred">{0}</font>')

                if lastcol[-1].find(" more skipped]") > -1:
                    text = '{0}<td valign="top" width="{1}%">{2} {3}</td></tr>\n'\
                           .format(text, 100-15*(len(headers)-1),
                                   ', '.join(lastcol[:-1]), lastcol[-1])
                else:
                    text = '{0}<td valign="top" width="{1}%">{2}</td></tr>\n'\
                           .format(text, 100-15*(len(headers)-1), ', '.join(lastcol))

        self.html_text = '{0}</table>\n<p>\n'.format(text)
Beispiel #4
0
    def make_text_plain(self, width):
        """
        Make the text representation of a report element as plain text.
        """
        def mformat(reslist):
            plaintext = ""
            _buffer = reslist[0]
            for j in range(1, len(reslist)):
                if (_buffer
                        == "") or (len(_buffer) +
                                   len(reslist[j])) <= (width - len(filling)):
                    if reslist[j][0] == '[' and reslist[j][-1] == ']':
                        _buffer = '{0} {1}'.format(_buffer, reslist[j])
                    else:
                        _buffer = '{0}, {1}'.format(_buffer, reslist[j])
                else:
                    plaintext = '{0}{1}\n{2}'.format(plaintext, _buffer,
                                                     filling)
                    _buffer = reslist[j]
            plaintext = '{0}{1}'.format(plaintext, _buffer)
            return plaintext

        text = '\n----- {0} -----\n\n'.format(self.title.strip())

        if self.function == 'total':
            width1 = max(
                len(res[0]) for res in self.results if res is not None)
            for res in self.results:
                padding = ' ' * (width1 - len(res[0]) + 1)
                text = '{0}{1}{2}| {3}\n'.format(text, res[0], padding, res[1])

        elif self.function == 'top':
            if self.results[0] is not None:
                width1 = max(
                    len(res[0]) for res in self.results if res is not None)
                width2 = min([
                    width - width1 - 4,
                    max(
                        len(', '.join(res[1])) for res in self.results
                        if res is not None)
                ])

                text = '{0}{1} | {2}\n'.format(text, ' ' * width1,
                                               self.headers.strip('"'))
                text = '{0}{1}-+-{2}-\n'.format(text, '-' * width1,
                                                '-' * width2)

                for res in self.results:
                    if res is not None:
                        padding = ' ' * (width1 - len(res[0]) + 1)
                        filling = '{0}| '.format(' ' * (width1 + 1))
                        lastcol = mformat(res[1])
                        text = '{0}{1}{2}| {3}\n'.format(
                            text, res[0], padding, lastcol)
            else:
                text = '{0} {1}\n'.format(text, 'None')

        elif self.function == 'table':
            headers = re.split('\s*,\s*', self.headers)

            colwidth = []
            for i in range(len(headers) - 1):
                colwidth.append(
                    max([
                        len(headers[i]),
                        max(len(res[i]) for res in self.results)
                    ]))

            for i in range(len(headers) - 1):
                text = '{0}{1}{2}| '\
                            .format(text, headers[i].strip('"'), ' ' * (colwidth[i]-len(headers[i])+2))

            text = '{0}{1}\n'.format(text, headers[-1].strip('"'))
            text = '{0}{1}\n'.format(text, '-' * (width - 1))

            filling = ""
            for i in range(len(headers) - 1):
                filling = '{0}{1}| '.format(filling, ' ' * colwidth[i])

            for res in sorted(self.results, key=lambda x: x[0]):
                for i in range(len(headers) - 1):
                    text = '{0}{1}{2}| '.format(
                        text, res[i], ' ' * (colwidth[i] - len(res[i])))
                lastcol = get_fmt_results(res[-1], limit=5)
                text = '{0}{1}\n'.format(text, mformat(lastcol))
        self.plain_text = text
Beispiel #5
0
    def make_text_html(self):
        """
        Make the text representation of a report element as html.
        """
        text = None
        if self.function == 'total':
            text = u'<table border="0" width="100%" rules="cols" cellpadding="2">\n'\
                   '<tr><th colspan="2" align="left"><h3><font color="{1}">'\
                   '{0}</font></h3></th></tr>\n'\
                   .format(lograptor.utils.htmlsafe(self.title.strip()), self.color)

            for res in self.results:
                text = u'{0}<tr><td valign="top" align="right">{1}</td>'\
                       '<td valign="top" width="90%">{2}</td></tr>'\
                       .format(text, res[0], res[1])

        elif self.function == 'top':
            text = u'<table border="0" width="100%" rules="cols" cellpadding="2">\n'\
                   '<tr><th colspan="2" align="left"><h3><font color="{1}">'\
                   '{0}</font></h3></th></tr>\n'\
                   .format(lograptor.utils.htmlsafe(self.title.strip()), self.color)

            if self.results[0] is not None:
                for res in self.results:
                    if res is not None:
                        text = u'{0}<tr><td valign="top" align="right">{1}</td>'\
                               '<td valign="top" width="90%">{2}</td></tr>'\
                               .format(text, res[0], ', '.join(res[1]))
            else:
                text = u'{0}<tr><td valign="top" align="left">{1}</td>'\
                       .format(text, "None")
                
        elif self.function == 'table':
            text = u'<h3><font color="{1}">{0}</font></h3>'\
                   '<table width="100%" rules="cols" cellpadding="2">\n'\
                   '<tr bgcolor="#aaaaaa">'\
                   .format(lograptor.utils.htmlsafe(self.title.strip()), self.color)
            
            headers = re.split('\s*,\s*', self.headers)
            for i in range(len(headers)):
                text = '{0}<th align="center" colspan="1">'\
                       '<font color="black">{1}</font></th>'\
                       .format(text, headers[i].strip('"'))

            text = u'{0}</tr>\n'.format(text)
            
            oddflag = False
            lastval = ""            
            for res in sorted(self.results, key=lambda x: x[0]):
                if lastval != res[0]:
                    oddflag = not oddflag
                    if oddflag:
                        text = u'{0}<tr bgcolor="#dddddd">'.format(text)
                    else:
                        text = u'{0}<tr>'.format(text)

                    text = u'{0}<td valign="top" width="15%">{1}</td>'\
                           .format(text, res[0])
                else:
                    if oddflag:
                        text = u'{0}<tr bgcolor="#dddddd">'.format(text)
                    else:
                        text = u'{0}<tr>'.format(text)

                    text = u'{0}<td valign="top" width="15%">&nbsp;</td>'.format(text)
                lastval = res[0]
                
                for i in range(1, len(headers)-1):
                    text = u'{0}<td valign="top" width="15%">{1}</td>'.format(text, res[i])
                lastcol = get_fmt_results(res[-1], limit=10, fmt=u'<font color="darkred">{0}</font>')

                if lastcol[-1].find(u" more skipped]") > -1:
                    text = u'{0}<td valign="top" width="{1}%">{2} {3}</td></tr>\n'\
                           .format(text, 100-15*(len(headers)-1),
                                   u', '.join(lastcol[:-1]), lastcol[-1])
                else:
                    text = u'{0}<td valign="top" width="{1}%">{2}</td></tr>\n'\
                           .format(text, 100-15*(len(headers)-1), u', '.join(lastcol))

        self.html_text = u'{0}</table>\n<p>\n'.format(text)
Beispiel #6
0
    def make_text_plain(self, width):
        """
        Make the text representation of a report element as plain text.
        """
        def mformat(reslist):
            plaintext = ""
            _buffer = reslist[0]
            for j in range(1, len(reslist)):
                if (_buffer == "") or (len(_buffer) + len(reslist[j])) <= (width - len(filling)):
                    if reslist[j][0] == '[' and reslist[j][-1] == ']':
                        _buffer = '{0} {1}'.format(_buffer, reslist[j])
                    else:
                        _buffer = '{0}, {1}'.format(_buffer, reslist[j])
                else:
                    plaintext = '{0}{1}\n{2}'.format(plaintext, _buffer, filling)
                    _buffer = reslist[j]
            plaintext = '{0}{1}'.format(plaintext, _buffer)
            return plaintext

        text = '\n----- {0} -----\n\n'.format(self.title.strip())

        if self.function == 'total':
            width1 = max(len(res[0]) for res in self.results if res is not None)
            for res in self.results:
                padding = ' ' * (width1 - len(res[0]) + 1)
                text = '{0}{1}{2}| {3}\n'.format(text, res[0], padding, res[1])
                
        elif self.function == 'top':
            if self.results[0] is not None:
                width1 = max(len(res[0]) for res in self.results if res is not None)
                width2 = min([width-width1-4,
                              max(len(', '.join(res[1])) for res in self.results if res is not None)])

                text = '{0}{1} | {2}\n'.format(text, ' ' * width1, self.headers.strip('"'))
                text = '{0}{1}-+-{2}-\n'.format(text, '-' * width1, '-' * width2)

                for res in self.results:
                    if res is not None:
                        padding = ' ' * (width1 - len(res[0]) + 1)
                        filling = '{0}| '.format(' ' * (width1 + 1))
                        lastcol = mformat(res[1])
                        text = '{0}{1}{2}| {3}\n'.format(text, res[0], padding, lastcol)
            else:
                text = '{0} {1}\n'.format(text, 'None')
                    
        elif self.function == 'table':
            headers = re.split('\s*,\s*', self.headers)

            colwidth = []
            for i in range(len(headers)-1):
                colwidth.append(max([len(headers[i]), max(len(res[i]) for res in self.results)]))

            for i in range(len(headers)-1):
                text = '{0}{1}{2}| '\
                            .format(text, headers[i].strip('"'), ' ' * (colwidth[i]-len(headers[i])+2))

            text = '{0}{1}\n'.format(text, headers[-1].strip('"'))
            text = '{0}{1}\n'.format(text, '-' * (width-1))

            filling = ""
            for i in range(len(headers)-1):
                filling = '{0}{1}| '.format(filling, ' ' * colwidth[i])
            
            for res in sorted(self.results, key=lambda x: x[0]):
                for i in range(len(headers)-1):
                    text = '{0}{1}{2}| '.format(text, res[i], ' ' * (colwidth[i]-len(res[i])))
                lastcol = get_fmt_results(res[-1], limit=5)
                text = '{0}{1}\n'.format(text, mformat(lastcol))
        self.plain_text = text