comparison sqlite_to_tabular.py @ 0:859064f07be4 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 74915fc9cee746bbce1c4b507e13231259de177d
author iuc
date Tue, 18 Jul 2017 09:07:26 -0400
parents
children c1b700bc0150
comparison
equal deleted inserted replaced
-1:000000000000 0:859064f07be4
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4
5 import optparse
6 import os.path
7 import sys
8
9 from query_db import describe_tables, get_connection, run_query
10
11
12 def __main__():
13 # Parse Command Line
14 parser = optparse.OptionParser()
15 parser.add_option('-s', '--sqlitedb', dest='sqlitedb', default=None,
16 help='The SQLite Database')
17 parser.add_option('-q', '--query', dest='query', default=None,
18 help='SQL query')
19 parser.add_option('-Q', '--query_file', dest='query_file', default=None,
20 help='SQL query file')
21 parser.add_option('-n', '--no_header', dest='no_header', default=False,
22 action='store_true',
23 help='Include a column headers line')
24 parser.add_option('-o', '--output', dest='output', default=None,
25 help='Output file for query results')
26 (options, args) = parser.parse_args()
27
28 # determine output destination
29 if options.output is not None:
30 try:
31 outputPath = os.path.abspath(options.output)
32 outputFile = open(outputPath, 'w')
33 except Exception as e:
34 exit('Error: %s' % (e))
35 else:
36 outputFile = sys.stdout
37
38 query = None
39 if options.query_file is not None:
40 with open(options.query_file, 'r') as fh:
41 query = fh.read()
42 elif options.query is not None:
43 query = options.query
44
45 if query is None:
46 try:
47 describe_tables(get_connection(options.sqlitedb), outputFile)
48 except Exception as e:
49 exit('Error: %s' % (e))
50 exit(0)
51 else:
52 try:
53 run_query(get_connection(options.sqlitedb), query, outputFile,
54 no_header=options.no_header)
55 except Exception as e:
56 exit('Error: %s' % (e))
57
58
59 if __name__ == "__main__":
60 __main__()