# Path: pump/vivopump.py
# def write_csv_fp(fp, data, delimiter='|'):
#     """
#     Write a CSV to a file pointer.  Used to support stdout.
#     :param fp: File pointer.  Could be stdout.
#     :param data: data to be written
#     :param delimiter: field delimiter for output
#     :return:
#     """
#     assert(len(data.keys()) > 0)
# 
#     # create a list of var_names from the first row
#     var_names = data[data.keys()[0]].keys()
#     fp.write(delimiter.join(var_names) + '\n')
# 
#     for key in sorted(data.keys()):
#         fp.write(delimiter.join([data[key][x] for x in var_names]) + '\n')


import sys
from bibtexparser import loads
from disambiguate.utils import print_err
from pump.vivopump import write_csv_fp
    as modeled by vivotools
    :param bib_data:
    :return: csv_data
    """
    csv_data = {}
    row = 0
    col_names = set(y for x in bib_data.entries for y in x.keys())

    for x in bib_data.entries:
        row += 1
        csv_data[row] = {}

        for col_name in col_names:
            v = x.get(col_name, '')
            v = v.replace('\n', ' ')
            v = v.replace('\r', ' ')
            v = v.replace('\t', ' ')
            csv_data[row][col_name] = v.encode('utf-8').strip()
    return csv_data

bib_str = ""
for line in sys.stdin:
    bib_str += line

bib_data = loads(bib_str)
csv_data = bib2csv(bib_data)

# @TODO: fail if the number of entries do not match
print_err("Entries: {}".format(len(bib_data.entries)))
print_err("Rows: {}".format(len(csv_data)))

# Path: pump/vivopump.py
# def write_csv_fp(fp, data, delimiter='|'):
#     """
#     Write a CSV to a file pointer.  Used to support stdout.
#     :param fp: File pointer.  Could be stdout.
#     :param data: data to be written
#     :param delimiter: field delimiter for output
#     :return:
#     """
#     assert(len(data.keys()) > 0)
# 
#     # create a list of var_names from the first row
#     var_names = data[data.keys()[0]].keys()
#     fp.write(delimiter.join(var_names) + '\n')
# 
#     for key in sorted(data.keys()):
#         fp.write(delimiter.join([data[key][x] for x in var_names]) + '\n')


# Path: uf_examples/publications/filters/bib2csv_filter.py
import sys
from bibtexparser import loads
from disambiguate.utils import print_err
from pump.vivopump import write_csv_fp

    as modeled by vivotools
    :param bib_data:
    :return: csv_data
    """
    csv_data = {}
    row = 0
    col_names = set(y for x in bib_data.entries for y in x.keys())

    for x in bib_data.entries:
        row += 1
        csv_data[row] = {}

        for col_name in col_names:
            v = x.get(col_name, '')
            v = v.replace('\n', ' ')
            v = v.replace('\r', ' ')
            v = v.replace('\t', ' ')
            csv_data[row][col_name] = v.encode('utf-8').strip()
    return csv_data

bib_str = ""
for line in sys.stdin:
    bib_str += line

bib_data = loads(bib_str)
csv_data = bib2csv(bib_data)

# @TODO: fail if the number of entries do not match
print_err("Entries: {}".format(len(bib_data.entries)))
print_err("Rows: {}".format(len(csv_data)))

write_csv_fp(sys.stdout, csv_data)