Skip to content

Commit

Permalink
Normalization pass on export_cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Dec 3, 2014
1 parent 3269e29 commit 0ef95e5
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions commands/export_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

0 comments on commit 0ef95e5

Please sign in to comment.