#! /usr/bin/env python
# -*- Python -*-

####################################################################################################
# 
# PySpice - A Spice Package for Python
# Copyright (C) 2014 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>. 
# 
####################################################################################################

####################################################################################################

import argparse
import sys
    
####################################################################################################
#
# Options
#

argument_parser = argparse.ArgumentParser(description='Format a RST table')

argument_parser.add_argument('-s',
                             dest='column_separator',
                             default='|',
                             help='CSV column separator')

argument_parser.add_argument('-rst',
                             action='store_true', default=False,
                             help='input is rst else CSV')

args = argument_parser.parse_args()

####################################################################################################

if args.rst:
    column_separator = ' + '
else:
    column_separator = args.column_separator

####################################################################################################

column_widths = []
lines = []
for line in sys.stdin:

    line = line.strip()

    if args.rst:
        if line.startswith('+-'):
            continue
        if line.startswith('|'):
            line = line[1:]
        if line.endswith('|'):
            line = line[:-1]

    columns = [x for x in [x.strip() for x in line.split(column_separator)] if x]
    lines.append(columns)
    if len(columns) > len(column_widths):
        column_widths.extend([0]*(len(columns) - len(column_widths)))
    for i, text in enumerate(columns):
        column_widths[i] = max(column_widths[i], len(text))

####################################################################################################

hline = '+' + '+'.join(['-'*(width+2) for width in column_widths]) + '+'
line_format = '|' + '+'.join([' {:<' + str(width) + '} ' for width in column_widths]) + '|'
for line in lines:
    print hline
    if len(line) < len(column_widths):
        line.extend(['']*(len(column_widths) - len(line)))
    print line_format.format(*line)
print hline

####################################################################################################
# 
# End
# 
####################################################################################################
