diff lemuriformes/csv2sql.py @ 12:82cd4e0b66cf

csv2sql
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 15:18:00 -0800
parents
children
line wrap: on
line diff
--- /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 </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()