Skip to content

Commit

Permalink
Added removeLocalSystem and removeLocalStation to TradeDB
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Mar 2, 2015
1 parent 2805428 commit bceffb6
Showing 1 changed file with 83 additions and 5 deletions.
88 changes: 83 additions & 5 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,14 @@ def lookupSystem(self, key):
"System", key, self.systems(), key=lambda system: system.dbname
)

def addLocalSystem(self, name, x, y, z, added="Local", modified='now'):
def addLocalSystem(
self,
name,
x, y, z,
added="Local",
modified='now',
commit=True,
):
"""
Add a system to the local cache and memory copy.
"""
Expand All @@ -750,7 +757,8 @@ def addLocalSystem(self, name, x, y, z, added="Local", modified='now'):
system = System(ID, name.upper(), x, y, z, 0)
self.systemByID[ID] = system
self.systemByName[system.dbname] = system
db.commit()
if commit:
db.commit()
self.tdenv.NOTE(
"Added new system #{}: {} [{},{},{}]",
ID, name, x, y, z
Expand All @@ -761,6 +769,7 @@ def updateLocalSystem(
self, system,
name, x, y, z, added="Local", modified='now',
force=False,
commit=True,
):
"""
Updates an entry for a local system.
Expand All @@ -784,7 +793,8 @@ def updateLocalSystem(
""", [
dbname, x, y, z, added, modified
])
db.commit()
if commit:
db.commit()
self.tdenv.NOTE(
"{} (#{}) updated in {}: {}, {}, {}, {}, {}, {}",
oldname, system.ID,
Expand All @@ -797,6 +807,33 @@ def updateLocalSystem(

return True

def removeLocalSystem(
self, system,
commit=True,
):
""" Removes a system and it's stations from the local DB. """
for stn in self.stations:
self.removeLocalStation(stn, commit=False)
db = self.getDB()
db.execute("""
DELETE FROM System WHERE system_id = ?
""", [
system.ID
])
if commit:
db.commit()
del self.systemByName[system.dbname]
del self.systemByID[system.ID]

self.tdenv.NOTE(
"{} (#{}) deleted from {}",
system.name(), system.ID,
self.dbPath if self.tdenv.detail > 1 else "local db",
)

system.dbname = "DELETED " + system.dbname
del system

def __buildStellarGrid(self):
"""
Divides the galaxy into a fixed-sized grid allowing us to
Expand Down Expand Up @@ -1098,6 +1135,7 @@ def addLocalStation(
shipyard='?',
maxPadSize='?',
modified='now',
commit=True,
):
"""
Add a station to the local cache and memory copy.
Expand Down Expand Up @@ -1152,7 +1190,8 @@ def addLocalStation(
itemCount=0, dataAge=0,
)
self.stationByID[ID] = station
db.commit()
if commit:
db.commit()
self.tdenv.NOTE(
"{} (#{}) added to {}: "
"ls={}, mkt={}, bm={}, yard={}, pad={}, mod={}",
Expand All @@ -1163,6 +1202,7 @@ def addLocalStation(
)
return station


def updateLocalStation(
self, station,
name=None,
Expand All @@ -1173,6 +1213,7 @@ def updateLocalStation(
maxPadSize=None,
modified='now',
force=False,
commit=True,
):
"""
Alter the properties of a station in-memory and in the DB.
Expand Down Expand Up @@ -1252,7 +1293,8 @@ def _changed(label, old, new):
modified,
station.ID
])
db.commit()
if commit:
db.commit()

self.tdenv.NOTE(
"{} (#{}) updated in {}: {}",
Expand All @@ -1263,6 +1305,42 @@ def _changed(label, old, new):

return True


def removeLocalStation(self, station, commit=True):
"""
Removes a station from the local database and memory image.
Becareful of any references to the station you may still have
after this.
"""

# Remove reference from my system
system = station.system
system.stations.remove(station)

# Remove the ID lookup
del self.stationByID[station.ID]

# Delete database entry
db = self.getDB()
db.execute("""
DELETE FROM Station
WHERE system_id = ? AND station_id = ?
""", [system.ID, station.ID]
)
if commit:
db.commit()

self.tdenv.NOTE(
"{} (#{}) deleted from {}",
station.name(), station.ID,
self.dbPath if self.tdenv.detail > 1 else "local db",
)

station.dbname = "DELETED "+station.dbname
del station


def lookupPlace(self, name):
"""
Lookup the station/system specified by 'name' which can be the
Expand Down

0 comments on commit bceffb6

Please sign in to comment.