Mercurial > hg > TextShaper
annotate textshaper/tablify.py @ 40:e1832eeae084
from http://k0s.org/hg/config
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 22 Aug 2014 15:54:19 -0700 |
parents | 8cf9a26b9aaa |
children |
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 | 14 def format_cols(rows, header=None, right_align=()): |
15 if header: | |
16 raise NotImplementedError('TODO') # -> record TODO items | |
17 if not rows: | |
18 return # XXX header | |
19 assert len(set([len(row) for row in rows])) == 1 | |
20 rows = [[str(col) for col in row] | |
21 for row in rows] | |
22 lengths = [max([len(row[i]) for row in rows]) | |
23 for i in range(len(rows[0]))] | |
24 rows = [[col + ' '*(length-len(col)) for col, length in zip(row, lengths)] | |
25 for row in rows] | |
26 return rows | |
27 | |
28 | |
29 def tablify(table_lines, header=True): | |
30 """=> HTML table""" | |
31 table = '<table>\n' | |
32 if header: | |
33 tag, invtag = '<th> ', ' </th>' | |
34 else: | |
35 tag, invtag = '<td> ', ' </td>' | |
36 if not hasattr(table_lines, '__iter__'): | |
37 table_lines = ( table_lines, ) | |
38 for i in table_lines: | |
39 table += '<tr>' | |
40 if not hasattr(i, '__iter__'): | |
41 i = (i,) | |
42 for j in i: | |
43 table += tag + str(j) + invtag | |
44 table += '</tr>\n' | |
45 tag = '<td> ' | |
46 invtag = ' </td>' | |
47 table += '</table>' | |
48 return table |