comparison query_tabular.py @ 22:bed5018e7ae3 draft

Uploaded
author jjohnson
date Mon, 17 Jul 2017 15:22:04 -0400
parents ab27c4bd14b9
children
comparison
equal deleted inserted replaced
21:357fe86f245d 22:bed5018e7ae3
64 if options.output is not None: 64 if options.output is not None:
65 try: 65 try:
66 outputPath = os.path.abspath(options.output) 66 outputPath = os.path.abspath(options.output)
67 outputFile = open(outputPath, 'w') 67 outputFile = open(outputPath, 'w')
68 except Exception as e: 68 except Exception as e:
69 print("failed: %s" % e, file=sys.stderr) 69 exit('Error: %s' % (e))
70 exit(3)
71 else: 70 else:
72 outputFile = sys.stdout 71 outputFile = sys.stdout
73 72
74 def _create_table(ti, table): 73 def _create_table(ti, table):
75 path = table['file_path'] 74 path = table['file_path']
107 fh = open(options.jsonfile) 106 fh = open(options.jsonfile)
108 tdef = json.load(fh) 107 tdef = json.load(fh)
109 if 'tables' in tdef: 108 if 'tables' in tdef:
110 for ti, table in enumerate(tdef['tables']): 109 for ti, table in enumerate(tdef['tables']):
111 _create_table(ti, table) 110 _create_table(ti, table)
112 except Exception as exc: 111 except Exception as e:
113 print("Error: %s" % exc, file=sys.stderr) 112 exit('Error: %s' % (e))
114 113
115 query = None 114 query = None
116 if (options.query_file is not None): 115 if (options.query_file is not None):
117 with open(options.query_file, 'r') as fh: 116 with open(options.query_file, 'r') as fh:
118 query = '' 117 query = ''
122 query = options.query 121 query = options.query
123 122
124 if (query is None): 123 if (query is None):
125 try: 124 try:
126 describe_tables(get_connection(options.sqlitedb), outputFile) 125 describe_tables(get_connection(options.sqlitedb), outputFile)
127 except Exception as exc: 126 except Exception as e:
128 print("Error: %s" % exc, file=sys.stderr) 127 exit('Error: %s' % (e))
129 else: 128 else:
130 try: 129 try:
131 run_query(get_connection(options.sqlitedb), query, outputFile, 130 run_query(get_connection(options.sqlitedb), query, outputFile,
132 no_header=options.no_header) 131 no_header=options.no_header)
133 except Exception as exc: 132 except Exception as e:
134 print("Error: %s" % exc, file=sys.stderr) 133 exit('Error: %s' % (e))
135 exit(1)
136 134
137 135
138 if __name__ == "__main__": 136 if __name__ == "__main__":
139 __main__() 137 __main__()