# HG changeset patch # User Jeff Hammel # Date 1512947880 28800 # Node ID 82cd4e0b66cffca8c642fce56c49eea4734721cb # Parent afc259799019d48175cdeea64031995d68c49f25 csv2sql diff -r afc259799019 -r 82cd4e0b66cf lemuriformes/csv2sql.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemuriformes/csv2sql.py Sun Dec 10 15:18:00 2017 -0800 @@ -0,0 +1,45 @@ +#!/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 + 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()