view lemuriformes/csv2sql.py @ 15:0d1b8bb1d97b

SQL + data related functionality
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:16:52 -0800
parents 82cd4e0b66cf
children
line wrap: on
line source

#!/usr/bin/env python

"""
convert CSV to SQL
"""
# (whitepaper)

import sys
from .cast import infer
from .cli import ConfigurationParser
from .columns import read_columns
from .url2sql import url2sql

def main(args=sys.argv[1:]):
    """CLI"""

    # parse command line
    parser = ConfigurationParser(description=__doc__)
    parser.add_argument('csv',
                        type=read_columns,
                        help="CSV source to read")
    parser.add_argument('sql',
                        type=url2sql,
                        help="SQL connection URL")
    parser.add_argument('table',  # TODO: this would be nice to have as part of the URL </sugar>
                        help="SQL table to create")
    options = parser.parse_args(args)

    # infer column types
    columns = options.csv.keys()
    column_types = {column: infer(options.csv[column])
                    for column in columns}

    # cast columns
    for column in columns:
        _type = column_types[column]
        options.csv[column] = [_type(v) for v in options.csv[column]]

    # TODO:
    # - create table(s)
    # - populate data


if __name__ == '__main__':
    main()