1"""Snippets of code able to convert a text rendered table to a CSV file.
2
3This was part of some old (working) code.
4I'll probably reuse this eventually.
5"""
6__copyright__ = "Copyright (C) 2016  Martin Blais"
7__license__ = "GNU GPLv2"
8
9import re
10
11
12def get_columns_from_underlines(line):
13    index = 0;
14    columns = []
15    for field in re.findall(r'\-+', line):
16        end = index + len(field)
17        columns.append((index, end))
18        index = end + 1
19    return columns
20
21
22def table_to_csv(table_string):
23    """Convert a formatted table to CSV rows."""
24    lines = iter(table_string.splitlines())
25    next(lines)
26    columns = get_columns_from_underlines(next(lines))
27    rows = []
28    for line in lines:
29        row = []
30        for start, end in columns:
31            field = line[start:end]
32            row.append(field.strip())
33        rows.append(row)
34    return rows
35