From 0ef95e5fe8428ce7d30cbe72767d41fdf7abb271 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Tue, 2 Dec 2014 20:47:56 -0800 Subject: [PATCH] Normalization pass on export_cmd --- commands/export_cmd.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/commands/export_cmd.py b/commands/export_cmd.py index 34c396b7..e0ad878b 100644 --- a/commands/export_cmd.py +++ b/commands/export_cmd.py @@ -138,20 +138,19 @@ def run(results, cmdenv, tdb): from tradedb import TradeDB # check database exists - dbFilename = cmdenv.dbFilename or TradeDB.defaultDB - if not Path(dbFilename).is_file(): + if not tdb.dbPath.is_file(): raise CommandLineError("Database '{}' not found.".format(dbFilename)) # check export path exists - if not Path(cmdenv.path).is_dir(): + exportDir = Path(cmdenv.path) + if not exportDir.is_dir(): raise CommandLineError("Save location '{}' not found.".format(cmdenv.path)) # connect to the database if not cmdenv.quiet: - print("Using database '{}'".format(dbFilename)) - conn = sqlite3.connect(dbFilename) + print("Using database '{}'".format(tdb.dbFilename)) + conn = tdb.getDB() conn.row_factory = sqlite3.Row - conn.execute("PRAGMA foreign_keys=ON") # extract tables from command line if cmdenv.tables: @@ -184,12 +183,14 @@ def run(results, cmdenv, tdb): continue # create CSV files - exportName = Path(cmdenv.path).joinpath("{table}.csv".format(table=tableName)) + exportPath = (exportDir / Path(tableName)).with_suffix(".csv") if not cmdenv.quiet: - print("Export Table '{table}' to '{file}'".format(table=tableName, file=exportName)) + print("Export Table '{table}' to '{file}'".format( + table=tableName, file=str(exportPath) + )) lineCount = 0 - with exportName.open("w", encoding='utf-8', newline="\n") as exportFile: + with exportPath.open("w", encoding='utf-8', newline="\n") as exportFile: exportOut = csv.writer(exportFile, delimiter=",", quotechar="'", doublequote=True, quoting=csv.QUOTE_NONNUMERIC, lineterminator="\n") cur = conn.cursor() @@ -276,8 +277,8 @@ def run(results, cmdenv, tdb): cmdenv.DEBUG1("{count} {table}s exported".format(count=lineCount, table=tableName)) if cmdenv.deleteEmpty and lineCount == 0: # delete file if emtpy - exportName.unlink() + exportPath.unlink() if not cmdenv.quiet: - print("Delete empty file {file}'".format(file=exportName)) + print("Delete empty file {file}'".format(file=exportPath)) return None