Skip to content

Commit

Permalink
More tightly integrated update_gui
Browse files Browse the repository at this point in the history
Improved startup times by taking tdb and cmdenv parameters and avoiding having to double-open the DB and re-read various tables.
  • Loading branch information
kfsone committed Nov 22, 2014
1 parent bbfb091 commit 8828c89
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ v6.0.4 Nov 21 2014 (Beta 3.9.1)
. (kfsone) Update GUI now supports up/down key inputs and has a default size
. (kfsone) Issue#57 Fixed extra cache rebuilds after doing an 'update'
. (kfsone) Changing .prices won't force a full rebuild of the cache
. (kfsone) Better integrated update gui (improved startup times)

v6.0.3 Nov 21 2014
. (kfsone) Added a GUI to the "update" sub-command, it's experimental:
Expand Down
5 changes: 2 additions & 3 deletions commands/update_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,14 @@ def editUpdate(tdb, cmdenv, stationID):


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

from commands.update_gui import render
try:
render(tdb.dbPath, stationID, tmpPath)
render(tdb, cmdenv, tmpPath)
cmdenv.DEBUG0("Got results, importing")
importDataFromFile(cmdenv, tdb, tmpPath, stationID, dbFilename)
cache.importDataFromFile(cmdenv, tdb, tmpPath)
finally:
saveTemporaryFile(tmpPath)

Expand Down
60 changes: 26 additions & 34 deletions commands/update_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ class UpdateGUI(tk.Canvas):
for TradeDangerous Price Updates
"""

def __init__(self, root, dbPath, stationID):
super().__init__(root, borderwidth=0, width=464, height=400)
def __init__(self, root, tdb, cmdenv):
super().__init__(root, borderwidth=0, width=500, height=440)

self.root = root
self.tdb = tdb
self.cmdenv = cmdenv

self.rowNo = 0
self.colNo = 0
self.items = {}
Expand All @@ -39,7 +42,7 @@ def __init__(self, root, dbPath, stationID):
self.frame.rowconfigure(0, weight=1)
self.frame.columnconfigure(0, weight=1)

self.createWidgets(dbPath, stationID)
self.createWidgets()

self.focusOn(0, 0)

Expand Down Expand Up @@ -227,7 +230,7 @@ def addItemRow(self, ID, catID, itemName, paying, asking, demand, stock):
self.items[itemName] = item
self.itemList.append(item)

self.addLabel(itemName)
self.addLabel(itemName.upper())
self.addInput(itemName, paying, row)
self.addInput(itemName, asking, row)
self.addInput(itemName, demand, row)
Expand All @@ -238,32 +241,17 @@ def addItemRow(self, ID, catID, itemName, paying, asking, demand, stock):
self.endRow()


def createWidgets(self, dbPath, stationID):
def createWidgets(self):
self.addHeadings()

db = sqlite3.connect(str(dbPath))
tdb, cmdenv = self.tdb, self.cmdenv
station = cmdenv.startStation
self.root.title(station.name())

db = tdb.getDB()
db.row_factory = sqlite3.Row
cur = db.cursor()

cur.execute("""
SELECT sys.name, stn.name
FROM Station AS stn
INNER JOIN System AS sys
USING (system_id)
WHERE stn.station_id = ?
LIMIT 1
""", [stationID])
(self.sysName, self.stnName) = (cur.fetchone())

self.root.title("{}/{}".format(self.sysName.upper(), self.stnName))

cur.execute("""
SELECT cat.category_id AS ID,
cat.name AS name
FROM Category AS cat
""")
self.categories = { row["ID"]: row["name"] for row in cur }

cur.execute("""
SELECT item.category_id AS catID,
item.item_id AS ID,
Expand All @@ -286,6 +274,7 @@ def createWidgets(self, dbPath, stationID):
WHERE si.station_id = ?
ORDER BY cat.name, si.ui_order
""", [stationID])
self.categories = self.tdb.categoryByID

def describeSupply(units, level):
if not level:
Expand All @@ -300,7 +289,7 @@ def describeSupply(units, level):
for row in cur:
cat = row["catID"]
if cat != lastCat:
self.addSection(self.categories[cat])
self.addSection(self.categories[cat].name())
lastCat = cat
itemName = row["name"]
paying, asking = row["paying"], row["asking"]
Expand All @@ -315,15 +304,14 @@ def getResults(self):
txt = (
"# Generated by TDGUI\n"
"\n"
"@ {sys}/{stn}\n".format(
sys=self.sysName.upper(),
stn=self.stnName
"@ {stn}\n".format(
stn=self.cmdenv.startStation.name(),
)
)
for item in self.itemList:
if item.catID != lastCat:
lastCat = item.catID
txt += (" + {}\n".format(self.categories[lastCat]))
txt += (" + {}\n".format(self.categories[lastCat].dbname))

row = self.itemDisplays[item.displayNo]
rowvals = [ val[1].get() for val in row ]
Expand All @@ -334,10 +322,14 @@ def getResults(self):
demand = rowvals[2]
stock = rowvals[3]

if not paying and not asking:
continue

if paying and not demand:
demand = "?"

if asking == 0:
stock = "-"
elif asking > 0 and not demand:
demand = "?"

txt += (" {item:<30s} "
"{paying:>10} "
Expand All @@ -353,9 +345,9 @@ def getResults(self):
self.results = txt


def render(dbPath, stationID, tmpPath):
def render(tdb, cmdenv, tmpPath):
root = tk.Tk()
gui = UpdateGUI(root, dbPath, stationID)
gui = UpdateGUI(root, tdb, cmdenv)
gui.mainloop()
if not gui.results:
gui.getResults()
Expand Down

0 comments on commit 8828c89

Please sign in to comment.