comparison query_tabular.py @ 4:fe7be5634ab3 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/query_tabular commit 6a362345c31764c28bb6328da1f0d81ef8f35d40
author iuc
date Thu, 03 May 2018 10:17:06 -0400
parents 98b40ecad32b
children
comparison
equal deleted inserted replaced
3:98b40ecad32b 4:fe7be5634ab3
57 help='Include a column headers line') 57 help='Include a column headers line')
58 parser.add_option('-c', '--comment_char', dest='comment_char', default='', 58 parser.add_option('-c', '--comment_char', dest='comment_char', default='',
59 help='comment character to prefix column header line') 59 help='comment character to prefix column header line')
60 parser.add_option('-o', '--output', dest='output', default=None, 60 parser.add_option('-o', '--output', dest='output', default=None,
61 help='Output file for query results') 61 help='Output file for query results')
62 parser.add_option('-d', '--debug', dest='debug', default=False,
63 action='store_true',
64 help='Output info to stderr')
62 (options, args) = parser.parse_args() 65 (options, args) = parser.parse_args()
63 66
64 # determine output destination 67 # determine output destination
65 if options.output is not None: 68 if options.output is not None:
66 try: 69 try:
107 110
108 if options.jsonfile: 111 if options.jsonfile:
109 try: 112 try:
110 with open(options.jsonfile) as fh: 113 with open(options.jsonfile) as fh:
111 tdef = json.load(fh) 114 tdef = json.load(fh)
115 if options.debug:
116 print('JSON: %s' % tdef, file=sys.stderr)
112 if 'tables' in tdef: 117 if 'tables' in tdef:
113 for ti, table in enumerate(tdef['tables']): 118 for ti, table in enumerate(tdef['tables']):
114 _create_table(ti, table) 119 _create_table(ti, table)
120 if 'sql_stmts' in tdef:
121 for si, stmt in enumerate(tdef['sql_stmts']):
122 rowcount = run_query(get_connection(options.sqlitedb), stmt, None)
123 if options.debug:
124 print('\nDB modification: %s \nrowcount: %s' %
125 (stmt, rowcount), file=sys.stderr)
126 if 'queries' in tdef:
127 for qi, qstmt in enumerate(tdef['queries']):
128 if 'header' in qstmt:
129 no_header = False
130 comment_char = qstmt['header']
131 else:
132 no_header = True
133 comment_char = None
134 with open(qstmt['result_file'], 'w') as fh:
135 query = qstmt['query']
136 rowcount = run_query(get_connection(options.sqlitedb),
137 query,
138 fh,
139 no_header=no_header,
140 comment_char=comment_char)
141 if options.debug:
142 print('\nSQL: %s \nrowcount: %s' %
143 (query, rowcount), file=sys.stderr)
115 except Exception as e: 144 except Exception as e:
116 exit('Error: %s' % (e)) 145 exit('Error: %s' % (e))
117 146
118 query = None 147 query = None
119 if options.query_file is not None: 148 if options.query_file is not None:
129 describe_tables(get_connection(options.sqlitedb), outputFile) 158 describe_tables(get_connection(options.sqlitedb), outputFile)
130 except Exception as e: 159 except Exception as e:
131 exit('Error: %s' % (e)) 160 exit('Error: %s' % (e))
132 else: 161 else:
133 try: 162 try:
134 run_query(get_connection(options.sqlitedb), query, outputFile, 163 rowcount = run_query(get_connection(options.sqlitedb),
135 no_header=options.no_header, 164 query, outputFile,
136 comment_char=options.comment_char) 165 no_header=options.no_header,
166 comment_char=options.comment_char)
167 if options.debug:
168 print('\nSQL: %s \nrowcount: %s' %
169 (query, rowcount), file=sys.stderr)
137 except Exception as e: 170 except Exception as e:
138 exit('Error: %s' % (e)) 171 exit('Error: %s' % (e))
139 172
140 173
141 if __name__ == "__main__": 174 if __name__ == "__main__":