Skip to content

Commit

Permalink
Expose added_id on System object
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Feb 24, 2015
1 parent 6e775ad commit 4f05f45
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ class System(object):
"""

__slots__ = (
'ID', 'dbname', 'posX', 'posY', 'posZ', 'stations', '_rangeCache'
'ID',
'dbname', 'posX', 'posY', 'posZ', 'stations',
'addedID',
'_rangeCache'
)

class RangeCache(object):
Expand All @@ -142,10 +145,11 @@ def __init__(self):
self.systems = []
self.probedLy = 0.

def __init__(self, ID, dbname, posX, posY, posZ):
def __init__(self, ID, dbname, posX, posY, posZ, addedID):
self.ID = ID
self.dbname = dbname
self.posX, self.posY, self.posZ = posX, posY, posZ
self.addedID = addedID or 0
self.stations = []
self._rangeCache = None

Expand Down Expand Up @@ -656,6 +660,31 @@ def reloadCache(self):

cache.buildCache(self, self.tdenv)

############################################################
# Load "added" data.

def _loadAdded(self):
"""
Loads the Added table as a simple dictionary
"""
stmt = """
SELECT added_id, name
FROM Added
"""
self.cur.execute(stmt)
addedByID = {}
for ID, name in self.cur:
addedByID[ID] = name
self.addedByID = addedByID
self.tdenv.DEBUG1("Loaded {:n} Addeds", len(addedByID))

def lookupAdded(self, name):
name = name.lower()
for ID, added in self.addedByID.items():
if added.lower() == name:
return ID
raise KeyError(name)

############################################################
# Star system data.

Expand All @@ -669,13 +698,15 @@ def _loadSystems(self):
CAUTION: Will orphan previously loaded objects.
"""
stmt = """
SELECT system_id, name, pos_x, pos_y, pos_z
SELECT system_id,
name, pos_x, pos_y, pos_z,
added_id
FROM System
"""
self.cur.execute(stmt)
systemByID, systemByName = {}, {}
for (ID, name, posX, posY, posZ) in self.cur:
system = System(ID, name, posX, posY, posZ)
for (ID, name, posX, posY, posZ, addedID) in self.cur:
system = System(ID, name, posX, posY, posZ, addedID)
systemByID[ID] = systemByName[name.upper()] = system

self.systemByID, self.systemByName = systemByID, systemByName
Expand Down Expand Up @@ -713,7 +744,7 @@ def addLocalSystem(self, name, x, y, z, added="Local", modified='now'):
name, x, y, z, added, modified,
])
ID = cur.lastrowid
system = System(ID, name.upper(), x, y, z)
system = System(ID, name.upper(), x, y, z, 0)
self.systemByID[ID] = system
self.systemByName[system.dbname] = system
db.commit()
Expand Down Expand Up @@ -1897,7 +1928,7 @@ def load(self, maxSystemLinkLy=None):
self.conn = conn = self.getDB()
self.cur = conn.cursor()

# Load raw tables.
self._loadAdded()
self._loadSystems()
self._loadStations()
self._loadShips()
Expand Down

0 comments on commit 4f05f45

Please sign in to comment.