batchquery.py
changeset 30 a8b7cd92f39f
parent 29 8636719a30f6
child 39 0cef3540b69f
--- a/batchquery.py	Tue Feb 14 18:15:56 2012 +0100
+++ b/batchquery.py	Mon Feb 27 15:12:40 2012 +0100
@@ -11,6 +11,7 @@
         self.parser.add_argument('--file', dest='file', type=str, help='CSV file with data to use as arguments.')
         self.parser.add_argument('--init', dest='init', type=str, help='Query which initialize database session (eg. temporary function).')
         self.parser.add_argument('--output', dest='output', type=str, help='File name for results.')
+        self.parser.add_argument('--outputfunc', dest='outputfunc', type=str, help='Python function which will format results (format_row(args, rows)).')
         self.parser.add_argument('--header', dest='header', action='store_true', help='First line of CSV is header with names for columns. These name can be used in query.')
         self.init()
 
@@ -46,11 +47,21 @@
             curs.connection.commit()
         # write results to output file
         if self.args.output:
+            format_row = None
+            if self.args.outputfunc:
+                with open(self.args.outputfunc, 'r', encoding='utf8') as f:
+                    d = dict()
+                    exec(f.read(), d)
+                    format_row = d['format_row']
+            
             with open(self.args.output, 'w', encoding='utf8') as f:
                 for args, rows in results:
-                    f.write(repr(args))
-                    f.write(' -> ')
-                    f.write(repr(rows))
+                    if format_row:
+                        f.write(format_row(args, rows))
+                    else:
+                        f.write(repr(args))
+                        f.write(' -> ')
+                        f.write(repr(rows))
                     f.write('\n')