annotate textshaper/tablify.py @ 35:5ffa0dbcb7fe

from k0s.org/hg/config
author Jeff Hammel <k0scist@gmail.com>
date Thu, 13 Mar 2014 13:53:41 -0700
parents
children 8cf9a26b9aaa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 def format_cols(rows, header=None, right_align=()):
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 if header:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 raise NotImplementedError('TODO') # -> record TODO items
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 if not rows:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 return # XXX header
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 assert len(set([len(row) for row in rows])) == 1
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 rows = [[str(col) for col in row]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 for row in rows]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 lengths = [max([len(row[i]) for row in rows])
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 for i in range(len(rows[0]))]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 rows = [[col + ' '*(length-len(col)) for col, length in zip(row, lengths)]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 for row in rows]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 return rows
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 def tablify(table_lines, header=True):
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 """=> HTML table"""
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 table = '<table>\n'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 if header:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 tag, invtag = '<th> ', ' </th>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 else:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 tag, invtag = '<td> ', ' </td>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 if not hasattr(table_lines, '__iter__'):
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 table_lines = ( table_lines, )
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 for i in table_lines:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 table += '<tr>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 if not hasattr(i, '__iter__'):
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 i = (i,)
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 for j in i:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 table += tag + str(j) + invtag
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 table += '</tr>\n'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 tag = '<td> '
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 invtag = ' </td>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 table += '</table>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 return table