Mercurial > hg > Lemuriformes
annotate lemuriformes/csv2sqlite.py @ 16:9b1bb9eee962
misc functions related to filesystem paths
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 17:19:30 -0800 |
parents | afc259799019 |
children |
rev | line source |
---|---|
11
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 """ |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 convert CSV files to a SQLite DB file |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 """ |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 # For SQLite types see |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 # http://www.sqlite.org/datatype3.html |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 import argparse |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 import csv |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 import os |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 import sqlite3 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 import sys |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 from collections import OrderedDict |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 from .cast import string |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 from .sql import SQLConnection |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 def read_csv(csv_file): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 """read csv file with header and return list of dicts""" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 reader = csv.DictReader(csv_file) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 return [row for row in reader] |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 def path_root(path): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 """return basename file root sans extension""" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 return os.path.splitext(os.path.basename(path))[0] |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
31 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 class SQLiteConnection(SQLConnection): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 """connection class to SQLite database""" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
35 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
36 # mapping of python types to SQLite types |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
37 types = {int: "INTEGER", |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
38 float: "REAL", |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
39 str: "TEXT"} |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
40 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
41 def __init__(self, db_file): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
42 if not os.path.exists(db_file): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
43 with open(db_file, 'wb') as f: |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
44 # touch file |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
45 pass |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
46 self.db_file = db_file |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
47 self.conn = sqlite3.connect(self.db_file) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
48 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
49 def __call__(self, sql, *args): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
50 c = self.conn.cursor() |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
51 c.execute(sql, args) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
52 retval = c.fetchall() |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
53 self.conn.commit() |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
54 return retval |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
55 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
56 def __del__(self): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
57 self.conn.close() |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
58 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
59 def tables(self): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
60 """ |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
61 return tables in the SQLite database |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
62 Ref: https://stackoverflow.com/questions/82875/how-to-list-the-tables-in-an-sqlite-database-file-that-was-opened-with-attach |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
63 """ |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
64 sql = "SELECT name FROM sqlite_master WHERE type='table';" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
65 results = self(sql) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
66 return [row[0] for row in results] |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
67 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
68 def create_table(self, table, **columns): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
69 """add a table to the database""" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
70 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
71 columns = ', '.join(["{key} {value}".format(key=key, value=value) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
72 for key, value in columns.items()]) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
73 self("CREATE TABLE IF NOT EXISTS {table} ({columns});".format(table=table, |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
74 columns=columns)) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
75 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
76 def schema(self, table): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
77 """get dictionary of typing for a table""" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
78 # http://www.sqlite.org/pragma.html#pragma_table_info |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
79 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
80 sql = "PRAGMA table_info('{}')".format(table) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
81 results = self(sql) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
82 # TODO: get data types from e.g. |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
83 # [(0, u'how', u'', 0, None, 0), (1, u'are', u'', 0, None, 0), (2, u'you', u'', 0, None, 0)] |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
84 # ... now |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
85 # [(0, u'how', u'TEXT', 0, None, 0), (1, u'you', u'TEXT', 0, None, 0), (2, u'are', u'TEXT', 0, None, 0)] |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
86 return OrderedDict([(result[1], result[2]) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
87 for result in results]) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
88 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
89 def columns(self, table): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
90 """return ordered list of column names""" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
91 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
92 return self.schema(table).keys() |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
93 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
94 def select(self, table): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
95 """just `select *` for now""" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
96 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
97 sql = "SELECT * FROM {table};" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
98 sql = sql.format(table=table) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
99 data = self(sql) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
100 columns = self.columns(table) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
101 return [OrderedDict(zip(columns, row)) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
102 for row in data] |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
103 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
104 select_all = select |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
105 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
106 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
107 class CSV2SQLite(object): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
108 """ |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
109 convert Comma Separated Value input to SQLite |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
110 """ |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
111 # TODO: allow conversion to arbitrary SQL |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
112 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
113 def __init__(self, output): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
114 self.conn = SQLiteConnection(output) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
115 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
116 def __call__(self, *csv_files, **csv_dict): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
117 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
118 # allow tables of default(ing) name |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
119 csv_dict.update({csv_file: None |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
120 for csv_file in csv_files}) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
121 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
122 for csv_file, tablename in csv_dict.items(): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
123 self.csv2table(csv_file, tablename=tablename) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
124 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
125 def csv2table(self, csv_file, tablename=None): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
126 if isinstance(csv_file, string): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
127 with open(csv_file) as f: |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
128 return self.csv2table(f, tablename=tablename) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
129 if tablename is None: |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
130 # TODO: allow lookup from scheme |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
131 tablename = path_root(csv_file.name) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
132 # read csv |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
133 data = read_csv(csv_file) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
134 assert data |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
135 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
136 # infer schema from data |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
137 # TODO: allow lookup from scheme |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
138 columns = {column: "TEXT" for column in data[0].keys()} |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
139 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
140 # create table |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
141 self.conn.create_table(tablename, **columns) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
142 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
143 # inseert data |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
144 for row in data: |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
145 self.conn.insert_row(tablename, **row) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
146 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
147 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
148 def main(args=sys.argv[1:]): |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
149 """CLI""" |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
150 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
151 # parse command line |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
152 parser = argparse.ArgumentParser(description=__doc__) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
153 parser.add_argument('input', nargs='+', |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
154 type=argparse.FileType('r'), |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
155 help="input CSV files; table names taken from file names") |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
156 parser.add_argument('-o', '--output', dest='output', |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
157 required=True, |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
158 help="output SQLite file") |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
159 options = parser.parse_args(args) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
160 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
161 # overwrite the file |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
162 # TODO: deprecate and allow appending |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
163 with open(options.output, 'w') as f: |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
164 pass |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
165 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
166 # instantiate converter |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
167 conn = CSV2SQLite(options.output) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
168 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
169 # convert input CSV to SQLite tables |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
170 conn(*options.input) |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
171 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
172 |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
173 if __name__ == '__main__': |
afc259799019
[CSV] add script to convert to SQL
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
174 main() |