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 30, 2014
2 parents 3bb9f2b + 8b770ff commit f865874
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 62 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014
==============================================================================

WIP:
. (kfsone) Added experimental "upload-to-maddavo" (misc/madupload.py)
. (kfsone) Improved the feedback from run when routes go wrong.
. (kfsone) fixed "--link-ly" causing a 'TypeError' in various places,
. (kfsone) "nav" now supports "--via"; destinations are processed in-order.
. (kfsone) buy, sell, nav and local now have consistent presentation
Expand Down
15 changes: 6 additions & 9 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,12 @@ def processItemLine(matches):

######################################################################

def processPricesFile(tdenv, db, pricesPath, defaultZero=False):
def processPricesFile(tdenv, db, pricesPath, pricesFh=None, defaultZero=False):
tdenv.DEBUG0("Processing Prices file '{}'", pricesPath)

assert isinstance(pricesPath, Path)

with pricesPath.open('rU') as pricesFile:
with pricesFh or pricesPath.open('rU') as pricesFh:
warnings, items, buys, sells = processPrices(
tdenv, pricesFile, db, defaultZero
tdenv, pricesFh, db, defaultZero
)

if items:
Expand Down Expand Up @@ -883,16 +881,14 @@ def regeneratePricesFile(tdb, tdenv):

######################################################################

def importDataFromFile(tdb, tdenv, path, reset=False):
def importDataFromFile(tdb, tdenv, path, pricesFh=None, reset=False):
"""
Import price data from a file on a per-station basis,
that is when a new station is encountered, delete any
existing records for that station in the database.
"""

assert isinstance(path, Path)

if not path.exists():
if not pricesFh and not path.exists():
raise TradeException("No such file: {}".format(
str(path)
))
Expand All @@ -905,6 +901,7 @@ def importDataFromFile(tdb, tdenv, path, reset=False):
processPricesFile(tdenv,
db=tdb.getDB(),
pricesPath=path,
pricesFh=pricesFh,
)

# If everything worked, we may need to re-build the prices file.
Expand Down
25 changes: 12 additions & 13 deletions commands/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ class NoDataError(TradeException):
def __init__(self, errorStr):
self.errorStr = errorStr
def __str__(self):
return "Error: {}\n".format(self.errorStr) + (
"This can happen when there are no profitable trades"
" matching your criteria, or if you have not yet entered"
" any price data for the station(s) involved.\n"
"\n"
"See '{} update -h' for help entering/updating prices, or"
" obtain a '.prices' file from the web, such as maddavo's:"
" http://www.davek.com.au/td/\n"
"\n"
"See https://bitbucket.org/kfsone/tradedangerous/wiki/"
"Price%20Data"
" for more help."
).format(sys.argv[0])
return "Error: {}\n".format(self.errorStr) + ("""
This can either indicate a lack of data (such as missing
price information, station data, etc) or that there was no
data matching your criteria.
See '{} update -h' for help entering/updating prices, or
obtain a crowd-sourced '.prices' file from the web, such
as maddavo's (http://www.davek.com.au/td/)".
For more help, see the TradeDangerous Wiki:
http://kfs.org/td/wiki
""").format(sys.argv[0])


18 changes: 12 additions & 6 deletions commands/import_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,23 @@ def run(results, cmdenv, tdb):
cmdenv.filename = filename

# check the file exists.
filePath = Path(cmdenv.filename)
if not filePath.is_file():
raise CommandLineError("File not found: {}".format(
str(filePath)
))
if cmdenv.filename != "-":
fh = None
filePath = Path(cmdenv.filename)
if not filePath.is_file():
raise CommandLineError("File not found: {}".format(
str(filePath)
))
else:
filePath = "stdin"
fh = sys.stdin

if cmdenv.plug:
if not plugin.finish():
cache.regeneratePricesFile()
return None

cache.importDataFromFile(tdb, cmdenv, filePath)
cache.importDataFromFile(tdb, cmdenv, filePath, pricesFh=fh)

return None

43 changes: 36 additions & 7 deletions commands/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def filterByVia(routes, viaSet, viaStartPos):
)

return partialRoutes[maxMet], (
"No runs visited all of your via destinations. "
"SORRY: No runs visited all of your via destinations. "
"Listing runs that matched at least {}.".format(
maxMet
)
Expand Down Expand Up @@ -543,6 +543,9 @@ def run(results, cmdenv, tdb):
cmdenv.DEBUG1("numHops {}, vias {}, adhocHops {}",
numHops, len(viaSet), cmdenv.adhocHops)

results.summary = ResultRow()
results.summary.exception = ""

for hopNo in range(numHops):
cmdenv.DEBUG1("Hop {}", hopNo)

Expand All @@ -552,18 +555,41 @@ def run(results, cmdenv, tdb):
elif len(viaSet) > cmdenv.adhocHops:
restrictTo = viaSet

routes = calc.getBestHops(routes, restrictTo=restrictTo)

results.summary = ResultRow()
results.summary.exception = None
newRoutes = calc.getBestHops(routes, restrictTo=restrictTo)
if not newRoutes and hopNo > 0:
if restrictTo:
restrictions = list(restrictTo)
if len(restrictions) == 1:
dests = restrictions[0].name()
elif len(set(stn.system for stn in restrictions)) == 1:
dests = restrictions[0].system.name()
else:
dests = ", ".join([
stn.name() for stn in restrictions[0:-1]
])
dests += " or " + restrictions[-1].name()
results.summary.exception += (
"SORRY: Could not find any routes that "
"delivered a profit to {} at hop #{}\n"
"You may need to add more hops to your route.\n"
.format(
dests, hopNo + 1
)
)
break
results.summary.exception += (
"SORRY: Could not find routes beyond hop #%d\n" % (hopNo + 1)
)
break
routes = newRoutes

if not routes:
raise NoDataError("No profitable trades matched your critera, or price data along the route is missing.")

if viaSet:
routes, caution = filterByVia(routes, viaSet, viaStartPos)
if caution:
results.summary.exception = caution
results.summary.exception += caution + "\n"

routes.sort()
results.data = routes
Expand All @@ -579,7 +605,10 @@ def render(results, cmdenv, tdb):

exception = results.summary.exception
if exception:
print("\aCAUTION: {}\n".format(exception))
print('#' * 76)
print("\a{}".format(exception), end="")
print('#' * 76)
print()

routes = results.data

Expand Down
24 changes: 0 additions & 24 deletions commands/update_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,30 +371,6 @@ def guidedUpdate(tdb, cmdenv):
saveTemporaryFile(tmpPath)


def uploadUpdated():
try:
import requests
except ImportError:
if platform.system() == "Windows":
prompt = "C:\ThisDir\>"
else:
prompt = "$"
raise SystemExit("""Missing 'requests' module:
You don't appear to have the Python module "requests" installed.
It can be installed with Python's package installer, e.g:
{prompt} pip install requests
For additional help, consult:
Bitbucket Wiki http://kfs.org/td/wiki
Facebook Group http://kfs.org/td/group
ED Forum Thread http://kfs.org/td/thread
""".format(
prompt=prompt
))


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

Expand Down
12 changes: 12 additions & 0 deletions data/ShipVendor.csv
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ unq:[email protected]_id,unq:[email protected]_id,unq:[email protected]_id
'LTT 15574','Haxel Port','Lakon Type 9'
'LTT 15574','Haxel Port','Sidewinder'
'LTT 15574','Haxel Port','Viper'
'Neto','Ising Vision','Adder'
'Neto','Ising Vision','Anaconda'
'Neto','Ising Vision','Asp Explorer'
'Neto','Ising Vision','Cobra'
'Neto','Ising Vision','Dropship'
'Neto','Ising Vision','Eagle'
'Neto','Ising Vision','Hauler'
'Neto','Ising Vision','Lakon Type 9'
'Neto','Ising Vision','Orca'
'Neto','Ising Vision','Python'
'Neto','Ising Vision','Sidewinder'
'Neto','Ising Vision','Viper'
'Obambivas','Clebsch Terminal','Asp Explorer'
'Obambivas','Clebsch Terminal','Orca'
'Obambivas','Clebsch Terminal','Sidewinder'
Expand Down
10 changes: 7 additions & 3 deletions data/Station.csv
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ unq:[email protected]_id,unq:name,ls_from_star,blackmarket,max_pad_size
'Algreit','Smith Exchange',0,'?','?'
'Alicarl','Birdseye Port',0,'?','L'
'Alicarl','Parise Ring',0,'?','L'
'Alino','Hadfield Orbital',0,'?','?'
'Alit','Albitzky Market',0,'?','?'
'Alit','Bond Port',0,'?','?'
'Alit','Morrow Station',0,'?','?'
Expand All @@ -154,9 +155,9 @@ unq:[email protected]_id,unq:name,ls_from_star,blackmarket,max_pad_size
'Alpha Centauri','Al-Din Prospect',0,'?','M'
'Alpha Centauri','Hutton Orbital',6937920,'?','M'
'Alrai','Bounds Hub',0,'?','?'
'Altair','Grandin Gateway',0,'?','?'
'Altair','Solo Orbiter',0,'?','?'
'Altair','Tereshkova Dock',0,'?','?'
'Altair','Grandin Gateway',284,'?','M'
'Altair','Solo Orbiter',670,'?','L'
'Altair','Tereshkova Dock',414,'?','M'
'Alyuto','Levinson Vision',0,'?','?'
'Amait','Lopez de Villalobos Prospect',0,'?','?'
'Amait','Oramus Legacy',0,'?','?'
Expand Down Expand Up @@ -1047,6 +1048,7 @@ unq:[email protected]_id,unq:name,ls_from_star,blackmarket,max_pad_size
'Lave','Lave Station',303,'?','?'
'Lave','Warinus',863,'?','?'
'LAWD 52','Guerrero Horizons',0,'?','?'
'LAWD 52','Leichhardt Enterprise',0,'?','?'
'LDS 2314','McDonald Colony',0,'?','?'
'Leesti','George Lucas',255,'?','?'
'Leesti','Kolmogorov Hub',2964,'?','?'
Expand Down Expand Up @@ -2055,6 +2057,8 @@ unq:[email protected]_id,unq:name,ls_from_star,blackmarket,max_pad_size
'Uszaa','Guest Installation',0,'?','?'
'Uszaa','Patterson Dock',0,'?','?'
'Utgaroar','Fort Klarix',0,'?','?'
'V1285 Aquilae','Kendrick Platform',1322,'?','M'
'V1285 Aquilae','Tanner Plant',3538,'?','M'
'V1688 Aquilae','Blaylock Enterprise',516,'?','?'
'V1688 Aquilae','Caselberg Orbital',705,'?','?'
'V1688 Aquilae','Crown Enterprise',140,'?','?'
Expand Down
72 changes: 72 additions & 0 deletions misc/madupload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#! /usr/bin/env python

import pathlib
import platform
import re

try:
import requests
except ImportError:
if platform.system() == "Windows":
prompt = "C:\ThisDir\>"
else:
prompt = "$"
raise Exception("""Missing 'requests' module:
----------------------------------------------------------------
You don't appear to have the Python module "requests" installed.
It can be installed with Python's package installer, e.g:
{prompt} pip install requests
For additional help, see:
Bitbucket Wiki http://kfs.org/td/wiki
Facebook Group http://kfs.org/td/group
ED Forum Thread http://kfs.org/td/thread
----------------------------------------------------------------
""".format(
prompt=prompt
))


############################################################################


upload_url = 'http://www.davek.com.au/td/uploaddata.asp'
upfile = "updated.prices"


############################################################################


if not pathlib.Path(upfile).is_file():
raise SystemExit("ERROR: File not found: {}".format(upfile))

files = {
}
r = requests.post(
upload_url,
files={
'Filename': (
'updated.prices',
open('updated.prices', 'rb'),
'text/plain',
{
"Expires": '300',
}
),
}
)

response = r.text
m = re.search(r'UPLOAD RESULT:\s*(.*?)<br', response, re.IGNORECASE)
if not m:
raise Exception("Unexpected result:\n" + r.text)

resultCode = m.group(1)
if resultCode.startswith("SUCCESS"):
raise SystemExit("Upload complete")

print("Upload failed: {}".format(resultCode))

0 comments on commit f865874

Please sign in to comment.