diff sqlex/main.py @ 6:22fbe50d92e8

can now export a table
author Jeff Hammel <k0scist@gmail.com>
date Sat, 01 Apr 2017 12:35:28 -0700
parents 3a7f515571dc
children adf056d67c01
line wrap: on
line diff
--- a/sqlex/main.py	Sat Apr 01 11:11:28 2017 -0700
+++ b/sqlex/main.py	Sat Apr 01 12:35:28 2017 -0700
@@ -40,6 +40,8 @@
         self.add_argument('--columns', '--list-columns', dest='list_columns',
                           action='store_true', default=False,
                           help="list columns in `table` and exit")
+        self.add_argument('-o', '--output',
+                          help="output to directory (if `table` not given), or filename or stdout by default")
         self.options = None
 
     def parse_args(self, *args, **kw):
@@ -56,6 +58,11 @@
         except Exception as e:
             self.error("Could not open '{}': {}".format(options.db, e))
 
+        if not any((options.output,
+                    options.list_tables,
+                    options.table)):
+            self.error("`--output` directory must be specified to output entire database")
+
         if options.list_columns and not options.table:
             self.error("`--list-columns` requires `table`")
 
@@ -90,5 +97,18 @@
         print ('\n'.join(db.columns(options.table).keys()))
         return
 
+    if options.table:
+        # output table
+
+        if options.output:
+            with open(options.output, 'w') as f:
+                db.table2csv(options.table, f)
+        else:
+            db.table2csv(options.table, sys.stdout)
+            sys.stdout.flush()
+    else:
+        # output entire db to CSV files in directory
+        raise NotImplementedError('TODO')
+
 if __name__ == '__main__':
     main()