Changeset 990


Ignore:
Timestamp:
Jul 6, 2012 11:14:12 AM (10 years ago)
Author:
jemian
Message:

refs #8, refactor the table maker

File:
1 edited

Legend:

Unmodified
Added
Removed
  • specdomain/trunk/src/specdomain/sphinxcontrib/specmacrofileparser.py

    r989 r990  
    426426        return '\n'.join(s)
    427427   
    428     def _report_table(self, title, itemlist, col_keys = ('start_line', 'line',)):
     428    def old_report_table(self, title, itemlist, col_keys = ('start_line', 'line',)):
    429429        """ return the itemlist as a reST table """
    430430        s = []
     
    450450        s.append( separator )
    451451        return s
     452   
     453    def _report_table(self, title, itemlist, col_keys = ('start_line', 'line',)):
     454        """ return the itemlist as a reST table """
     455        if len(itemlist) == 0:
     456            return []
     457        rows = []
     458        last_line = None
     459        for d in itemlist:
     460            if d['start_line'] != last_line:
     461                rows.append( tuple([str(d[key]).strip() for key in col_keys]) )
     462            last_line = d['start_line']
     463        return _make_table(title, col_keys, rows, '=')
     464
     465
     466def _make_table( title, labels, rows, titlechar = '='):
     467    """
     468    build a reST table (internal routine)
     469   
     470    :param str title: placed in a section heading above the table
     471    :param [str] labels: columns labels
     472    :param [[str]] rows: 2-D grid of data, len(labels) == len(data[i]) for all i
     473    :param str titlechar: character to use when underlining title as reST section heading
     474    :returns [str]: each list item is reST
     475    """
     476    s = []
     477    if len(rows) == 0:
     478        return s
     479    if len(labels) > 0:
     480        columns = zip(labels, *rows)
     481    else:
     482        columns = zip(*rows)
     483    widths = [max([len(item) for item in row]) for row in columns]
     484    separator = " ".join( ['='*key for key in widths] )
     485    fmt = " ".join( '%%-%ds' % key for key in widths )
     486    s.append( '' )
     487    s.append( title )
     488    s.append( titlechar*len(title) )
     489    s.append( '' )
     490    s.append( separator )
     491    if len(labels) > 0:
     492        s.append( fmt % labels )
     493        s.append( separator )
     494    s.extend( fmt % row for row in rows )
     495    s.append( separator )
     496    return s
    452497
    453498
Note: See TracChangeset for help on using the changeset viewer.