annotate textshaper/tablify.py @ 43:21b6a9569f21

onelineit
author Jeff Hammel <k0scist@gmail.com>
date Wed, 03 Sep 2014 18:13:15 -0700
parents 8cf9a26b9aaa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
1 """
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
2 functions having to do with tables
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
3 """
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
4
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
5 import csv
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
6 from .decorator import lines
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
7 from StringIO import StringIO
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
8
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
9 @lines
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
10 def make_csv(text):
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
11 """make space-separated text into comma-separated"""
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
12 return [','.join(line.strip().split()) for line in text]
8cf9a26b9aaa STUB: textshaper/main.py textshaper/tablify.py
Jeff Hammel <k0scist@gmail.com>
parents: 35
diff changeset
13
35
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 def format_cols(rows, header=None, right_align=()):
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 if header:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 raise NotImplementedError('TODO') # -> record TODO items
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 if not rows:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 return # XXX header
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 assert len(set([len(row) for row in rows])) == 1
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 rows = [[str(col) for col in row]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 for row in rows]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 lengths = [max([len(row[i]) for row in rows])
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 for i in range(len(rows[0]))]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 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
25 for row in rows]
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 return rows
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 def tablify(table_lines, header=True):
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 """=> HTML table"""
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 table = '<table>\n'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 if header:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 tag, invtag = '<th> ', ' </th>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 else:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 tag, invtag = '<td> ', ' </td>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 if not hasattr(table_lines, '__iter__'):
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 table_lines = ( table_lines, )
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 for i in table_lines:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 table += '<tr>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 if not hasattr(i, '__iter__'):
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 i = (i,)
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 for j in i:
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 table += tag + str(j) + invtag
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 table += '</tr>\n'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 tag = '<td> '
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 invtag = ' </td>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 table += '</table>'
5ffa0dbcb7fe from k0s.org/hg/config
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 return table