Skip to content

Commit

Permalink
Streamlined argument set for buildCache
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Dec 3, 2014
1 parent 470614b commit 7af37fa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
12 changes: 7 additions & 5 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ def generateStationLink(tdenv, db):

######################################################################

def buildCache(tdenv, dbPath, sqlPath, pricesPath, importTables, defaultZero=False):
def buildCache(tdb, tdenv):
"""
Rebuilds the SQlite database from source files.
Expand All @@ -801,7 +801,10 @@ def buildCache(tdenv, dbPath, sqlPath, pricesPath, importTables, defaultZero=Fal
are newer than the database.
"""

dbFilename = str(dbPath)
dbPath = tdb.dbPath
sqlPath = tdb.sqlPath
pricesPath = tdb.pricesPath

# Create an in-memory database to populate with our data.
tdenv.DEBUG0("Creating temporary database in memory")
tempPath = dbPath.with_suffix(".new")
Expand All @@ -819,15 +822,15 @@ def buildCache(tdenv, dbPath, sqlPath, pricesPath, importTables, defaultZero=Fal
tempDB.executescript(sqlScript)

# import standard tables
for (importName, importTable) in importTables:
for (importName, importTable) in tdb.importTables:
try:
processImportFile(tdenv, tempDB, Path(importName), importTable)
except FileNotFoundError:
tdenv.DEBUG0("WARNING: processImportFile found no {} file", importName)

# Parse the prices file
if pricesPath.exists():
processPricesFile(tdenv, tempDB, pricesPath, defaultZero=defaultZero)
processPricesFile(tdenv, tempDB, pricesPath)
elif not tdenv.quiet:
print("NOTE: Missing \"{}\" file - no price data".format(
str(pricesPath)
Expand Down Expand Up @@ -872,7 +875,6 @@ def importDataFromFile(tdb, tdenv, path, reset=False):
processPricesFile(tdenv,
db=tdb.getDB(),
pricesPath=path,
defaultZero=tdenv.forceNa,
)

# If everything worked, we may need to re-build the prices file.
Expand Down
17 changes: 7 additions & 10 deletions commands/buildcache_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,20 @@ def run(results, cmdenv, tdb):
pricesFilename = cmdenv.pricesFilename or TradeDB.defaultPrices
importTables = TradeDB.defaultTables

from pathlib import Path
dbPath = Path(dbFilename)
sqlPath = Path(sqlFilename)
pricesPath = Path(pricesFilename)
if not cmdenv.force:
if dbPath.exists():
if tdb.dbPath.exists():
raise CommandLineError(
"SQLite3 database '{}' already exists. "
"SQLite3 database '{}' already exists.\n"
"Either remove the file first or use the '-f' option."
.format(dbFilename))
.format(tdb.dbFilename))

if not sqlPath.exists():
if not tdb.sqlPath.exists():
raise CommandLineError(
"SQL File does not exist: {}".format(sqlFilename))
"SQL File does not exist: {}"
.format(tdb.sqlFilename))

from cache import buildCache
buildCache(cmdenv, dbPath, sqlPath, pricesPath, importTables)
buildCache(tdb, cmdenv)

return None

8 changes: 1 addition & 7 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,7 @@ def reloadCache(self):
else:
self.tdenv.DEBUG0("Building DB Cache")

cache.buildCache(
self.tdenv,
dbPath=self.dbPath,
sqlPath=self.sqlPath,
pricesPath=self.pricesPath,
importTables=self.importTables
)
cache.buildCache(self, self.tdenv)


############################################################
Expand Down

0 comments on commit 7af37fa

Please sign in to comment.