Skip to content

Commit

Permalink
Merged kfsone/tradedangerous into master
Browse files Browse the repository at this point in the history
  • Loading branch information
WombatFromHell committed Mar 7, 2015
2 parents a0c028d + b379508 commit 7282d9e
Show file tree
Hide file tree
Showing 7 changed files with 1,125 additions and 967 deletions.
10 changes: 10 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014
==============================================================================

v6.13.1 Mar 06 2015
. (kfsone) "shipvendor" command:
- Default action is now to list ships at given station, e.g.
trade.py shipvendor galileo
- Added "--name-sort" (--name) to sort list by ship name,
. (kfsone) Fixed some problems with maddavo's import,
. (kfsone) Added "WARNING" level events (use -qq to disable),
. (kfsone) Fixed #195 "local" ValueError when no stations listed,
+ Dry411S: ShipVendors

v6.13.0 Mar 03 2015
. (kfsone) Added "modified" column to ShipVendor table,
. (kfsone) "maddavo" import plugin:
Expand Down
30 changes: 16 additions & 14 deletions commands/local_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def run(results, cmdenv, tdb):
results.summary = ResultRow()
results.summary.near = srcSystem
results.summary.ly = ly
results.summary.stations = 0

distances = { srcSystem: 0.0 }

Expand Down Expand Up @@ -130,6 +131,7 @@ def station_filter(system):
row.dist = dist
row.stations = stations if showStations else []
results.rows.append(row)
results.summary.stations += len(row.stations)

return results

Expand All @@ -138,21 +140,21 @@ def station_filter(system):

def render(results, cmdenv, tdb):
if not results or not results.rows:
raise TradeException("No systems found within {}ly of {}.".format(
results.summary.ly,
results.summary.near.name(),
))
raise TradeException(
"No systems found within {}ly of {}."
.format(results.summary.ly, results.summary.near.name())
)

# Compare system names so we can tell
maxSysLen = max_len(results.rows, key=lambda row: row.system.name())

sysRowFmt = RowFormat().append(
ColumnFormat("System", '<', maxSysLen,
key=lambda row: row.system.name())
).append(
ColumnFormat("Dist", '>', '7', '.2f',
key=lambda row: row.dist)
)
ColumnFormat("System", '<', maxSysLen,
key=lambda row: row.system.name())
).append(
ColumnFormat("Dist", '>', '7', '.2f',
key=lambda row: row.dist)
)

showStations = cmdenv.detail
if showStations:
Expand Down Expand Up @@ -202,10 +204,10 @@ def render(results, cmdenv, tdb):
)

cmdenv.DEBUG0(
"Systems within {ly:<5.2f}ly of {sys}.\n",
sys=results.summary.near.name(),
ly=results.summary.ly,
)
"Systems within {ly:<5.2f}ly of {sys}.\n",
sys=results.summary.near.name(),
ly=results.summary.ly,
)

if not cmdenv.quiet:
heading, underline = sysRowFmt.heading()
Expand Down
89 changes: 78 additions & 11 deletions commands/shipvendor_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from commands.commandenv import ResultRow
from commands.exceptions import CommandLineError
from commands.parsing import MutuallyExclusiveGroup, ParseArgument
from formatting import RowFormat, ColumnFormat, max_len
from itertools import chain
from tradedb import AmbiguityError
from tradedb import System, Station
Expand All @@ -16,7 +17,7 @@
######################################################################
# Parser config

help='Add (or update) a ship vendor entry'
help='List, add or update available ships to a station'
name='shipvendor'
epilog=None
arguments = [
Expand All @@ -30,7 +31,7 @@
ParseArgument(
'ship',
help='Comma or space separated list of ship names.',
nargs='+',
nargs='*',
),
]
switches = [
Expand All @@ -45,6 +46,12 @@
help='Indicates you want to add a new station.',
action='store_true',
),
ParseArgument(
'--name-sort',
help='Sort listed ships by name.',
action='store_true',
dest='nameSort',
),
),
ParseArgument(
'--no-export',
Expand Down Expand Up @@ -73,7 +80,7 @@ def addShipVendor(tdb, cmdenv, station, ship):
cmdenv.NOTE("{} added to {} in local {} database.",
ship.name(), station.name(), tdb.dbPath)
return ship


def removeShipVendor(tdb, cmdenv, station, ship):
db = tdb.getDB()
Expand All @@ -94,6 +101,7 @@ def maybeExportToCSV(tdb, cmdenv):
lines, csvPath = csvexport.exportTableToFile(tdb, cmdenv, "ShipVendor")
cmdenv.NOTE("{} updated.", csvPath)


def checkShipPresent(tdb, station, ship):
# Ask the database how many rows it sees claiming
# this ship is sold at that station. The value will
Expand All @@ -112,27 +120,59 @@ def checkShipPresent(tdb, station, ship):
return (count > 0)


def listShipsPresent(tdb, cmdenv, station, results):
""" Populate results with a list of ships present at the given station """
cur = tdb.query("""
SELECT ship_id
FROM ShipVendor
WHERE station_id = ?
""", [station.ID]
)

results.summary = ResultRow()
results.summary.station = station
ships = tdb.shipByID
addShip = results.rows.append

for (ship_id,) in cur:
ship = ships.get(ship_id, None)
if ship:
addShip(ResultRow(ship=ship))

if cmdenv.nameSort:
results.rows.sort(key=lambda row: row.ship.name())
else:
results.rows.sort(key=lambda row: row.ship.cost, reverse=True)

return results


######################################################################
# Perform query and populate result set

def run(results, cmdenv, tdb):

station = cmdenv.startStation
if not isinstance(station, Station):
raise CommandLineError("{} is a system, not a station".format(
station.name()
))
raise CommandLineError(
"{} is a system, not a station"
.format(station.name())
)
if station.shipyard == 'N' and not cmdenv.remove:
raise CommandLineError(
"{} is flagged as having no shipyard."
.format(station.name())
)

if cmdenv.add:
action = addShipVendor
elif cmdenv.remove:
action = removeShipVendor
else:
###TODO:
### if not cmdenv.ship:
### List ships at this station.
return listShipsPresent(tdb, cmdenv, station, results)

if not cmdenv.ship:
raise CommandLineError(
"You must specify --add or --remove"
"No ship names specified."
)

ships = {}
Expand Down Expand Up @@ -171,3 +211,30 @@ def run(results, cmdenv, tdb):
maybeExportToCSV(tdb, cmdenv)

return None

######################################################################
# Transform result set into output

def render(results, cmdenv, tdb):
if not results or not results.rows:
raise CommandLineError(
"No ships available at {}"
.format(results.summary.station.name())
)

maxShipLen = max_len(results.rows, key=lambda row: row.ship.name())

rowFmt = RowFormat().append(
ColumnFormat("Ship", '<', maxShipLen,
key=lambda row: row.ship.name())
).append(
ColumnFormat("Cost", '>', 12, 'n',
key=lambda row: row.ship.cost)
)

if not cmdenv.quiet:
heading, underline = rowFmt.heading()
print(heading, underline, sep='\n')

for row in results.rows:
print(rowFmt.format(row))
Loading

0 comments on commit 7282d9e

Please sign in to comment.