Skip to content

Commit

Permalink
Merged kfsone/tradedangerous into master
Browse files Browse the repository at this point in the history
  • Loading branch information
maddavo committed Dec 4, 2014
2 parents 2309b01 + b4f411d commit ff0f0df
Show file tree
Hide file tree
Showing 11 changed files with 654 additions and 211 deletions.
27 changes: 15 additions & 12 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pathlib import Path
from collections import namedtuple
from tradeexcept import TradeException
from data import corrections
import corrections

######################################################################
# Regular expression patterns. Here be draegons.
Expand Down 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,17 +801,19 @@ 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")
tempDBName = dbFilename + ".new"
backupDBName = dbFilename + ".prev"
tempPath, backupPath = Path(tempDBName), Path(backupDBName)
tempPath = dbPath.with_suffix(".new")
backupPath = dbPath.with_suffix(".prev")

if tempPath.exists():
tempPath.unlink()

tempDB = sqlite3.connect(tempDBName)
tempDB = sqlite3.connect(str(tempPath))
tempDB.execute("PRAGMA foreign_keys=ON")
# Read the SQL script so we are ready to populate structure, etc.
tdenv.DEBUG0("Executing SQL Script '{}' from '{}'", sqlPath, os.getcwd())
Expand All @@ -820,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 @@ -858,6 +860,8 @@ def importDataFromFile(tdb, tdenv, path, reset=False):
existing records for that station in the database.
"""

assert isinstance(path, Path)

if not path.exists():
raise TradeException("No such file: {}".format(
str(path)
Expand All @@ -871,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 All @@ -880,12 +883,12 @@ def importDataFromFile(tdb, tdenv, path, reset=False):
tdenv.DEBUG0("Update complete, regenerating .prices file")
with tdb.pricesPath.open("w") as pricesFile:
prices.dumpPrices(
tdb.dbURI,
tdb.dbFilename,
prices.Element.full,
file=pricesFile,
debug=tdenv.debug)

# Update the DB file so we don't regenerate it.
os.utime(tdb.dbURI)
os.utime(tdb.dbFilename)


22 changes: 7 additions & 15 deletions commands/buildcache_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,20 @@ def run(results, cmdenv, tdb):
from tradedb import TradeDB

# Check that the file doesn't already exist.
dbFilename = cmdenv.dbFilename or TradeDB.defaultDB
sqlFilename = cmdenv.sqlFilename or TradeDB.defaultSQL
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

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
4 changes: 2 additions & 2 deletions commands/update_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def editUpdate(tdb, cmdenv, stationID):
tmpPath = getTemporaryPath(cmdenv)

absoluteFilename = None
dbFilename = cmdenv.dbFilename or tdb.defaultDB
dbFilename = tdb.dbFilename
try:
elementMask = prices.Element.basic
if cmdenv.supply: elementMask |= prices.Element.supply
Expand Down Expand Up @@ -327,7 +327,7 @@ def editUpdate(tdb, cmdenv, stationID):


def guidedUpdate(tdb, cmdenv):
dbFilename = cmdenv.dbFilename or tdb.defaultDB
dbFilename = tdb.dbFilename
stationID = cmdenv.startStation.ID
tmpPath = getTemporaryPath(cmdenv)

Expand Down
65 changes: 65 additions & 0 deletions corrections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Provides an interface for correcting star/station names that
# have changed in recent versions.

from __future__ import absolute_import, with_statement, print_function, division, unicode_literals

# Arbitrary, negative value to denote something that's been removed.
DELETED = -111

systems = {
'MANTOAC': "Mantóac",
"NANTOAC": "Nantóac",
"LIFTHRUTI": "Lífthruti",

#ADD_SYSTEMS_HERE
}

stations = {
"CHEMAKU/BARTOE PLATFORM": DELETED,

#ADD_STATIONS_HERE
}

categories = {
'DRUGS': 'Legal Drugs',
}

items = {
'HYDROGEN FUELS': 'Hydrogen Fuel',
'MARINE SUPPLIES': 'Marine Equipment',
'TERRAIN ENRICH SYS': 'Land Enrichment Systems',
'HEL-STATIC FURNACES': 'Microbial Furnaces',
'REACTIVE ARMOR': 'Reactive Armour',
}

def correctSystem(oldName):
try:
return systems[oldName.upper()]
except KeyError:
return oldName


def correctStation(systemName, oldName):
try:
return stations[systemName.upper() + "/" + oldName.upper()]
except KeyError:
pass
try:
return stations[oldName.upper()]
except KeyError:
return oldName


def correctCategory(oldName):
try:
return categories[oldName.upper()]
except KeyError:
return oldName


def correctItem(oldName):
try:
return items[oldName.upper()]
except KeyError:
return oldName

Loading

0 comments on commit ff0f0df

Please sign in to comment.