OILS / web / table / schema2sqlite.py View on Github | oils.pub

45 lines, 28 significant
1#!/usr/bin/env python2
2"""
3schema2sqlite.py
4"""
5from __future__ import print_function
6
7import csv
8import optparse
9import sys
10
11import csv2html
12
13
14def CreateOptionsParser():
15 p = optparse.OptionParser()
16
17 return p
18
19
20def main(argv):
21 (opts, argv) = CreateOptionsParser().parse_args(argv[1:])
22
23 try:
24 schema_path = argv[0]
25 except IndexError:
26 raise RuntimeError('Expected schema TSV filename.')
27
28 try:
29 schema_f = open(schema_path)
30 except IOError as e:
31 raise RuntimeError('Error opening schema: %s' % e)
32
33 r = csv.reader(schema_f, delimiter='\t', doublequote=False,
34 quoting=csv.QUOTE_NONE)
35 schema = csv2html.Schema(list(r))
36
37 print(schema.ToSqlite())
38
39
40if __name__ == '__main__':
41 try:
42 main(sys.argv)
43 except RuntimeError as e:
44 print('FATAL: %s' % e, file=sys.stderr)
45 sys.exit(1)