Skip to content

Commit

Permalink
Merge branch 'master' into beta3
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Oct 29, 2014
2 parents f77dab7 + 9618748 commit a816c16
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
6 changes: 3 additions & 3 deletions trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ def parseAvoids(tdb, args):
raise CommandLineError("Unknown item/system/station: %s" % avoid)

# But if it matched more than once, whine about ambiguity
if item and system: raise AmbiguityError('Avoidance', avoid, item, system.str())
if item and station: raise AmbiguityError('Avoidance', avoid, item, station.str())
if system and station and station.system != system: raise AmbiguityError('Avoidance', avoid, system.str(), station.str())
if item and system: raise AmbiguityError('Avoidance', avoid, [ item, system.str() ])
if item and station: raise AmbiguityError('Avoidance', avoid, [ item, station.str() ])
if system and station and station.system != system: raise AmbiguityError('Avoidance', avoid, [ system.str(), station.str() ])

if args.debug:
print("Avoiding items %s, systems %s, stations %s" % (
Expand Down
43 changes: 35 additions & 8 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ class AmbiguityError(TradeException):
Attributes:
lookupType - description of what was being queried,
searchKey - the key given to the search routine,
first - the first potential match,
second - the alternate match
candidates - list of candidates
key - retrieve the display string for a candidate
"""
def __init__(self, lookupType, searchKey, first, second):
self.lookupType, self.searchKey, self.first, self.second = lookupType, searchKey, first, second
def __init__(self, lookupType, searchKey, candidates, key=lambda item:item):
self.lookupType = lookupType
self.searchKey = searchKey
self.candidates = candidates
self.key = key


def __str__(self):
return '%s lookup: "%s" could match either "%s" or "%s"' % (self.lookupType, str(self.searchKey), str(self.first), str(self.second))
return '{} lookup: "{}" could match either "{}" or "{}"'.format(
self.lookupType, str(self.searchKey),
str(key(self.candiates[0])),
str(key(self.candidates[1])),
)


class SystemNotStationError(TradeException):
Expand Down Expand Up @@ -481,6 +488,26 @@ def lookupSystem(self, key):
return TradeDB.listSearch("System", key, self.systems(), key=lambda system: system.dbname)


def lookupSystemRelaxed(self, key):
"""
Lookup a System object by it's name or by the name of any of it's stations.
"""
try:
return self.lookupSystem(key)
except LookupError:
pass
try:
nameList = self.stationByID.values()
station = TradeDB.listSearch("System or station", key, nameList, key=lambda entry: entry.dbname)
return station.system
except AmbiguityError as e:
# Check the ambiguities to see if they share a system
system = e.candidates[0].value.system
for i in range(1, len(e.candidates)):
if e.candidates[i].value.system != system:
raise
return system

############################################################
# Station data.

Expand Down Expand Up @@ -542,7 +569,7 @@ def lookupStation(self, name, system=None):
# the same station otherwise we have an ambiguity. Some stations have the
# same name as their star system (Aulin/Aulin Enterprise)
if system and station and system != station.system:
raise AmbiguityError('Station', name, system.name(), station.name())
raise AmbiguityError('Station', name, [ system.name(), station.name() ])

if station:
return station
Expand Down Expand Up @@ -876,12 +903,12 @@ def listSearch(listType, lookup, values, key=lambda item: item, val=lambda item:
# Whole word matches trump partial matches
if wordMatches:
if len(wordMatches) > 1:
raise AmbiguityError(listType, lookup, wordMatches[0].key, wordMatches[1].key)
raise AmbiguityError(listType, lookup, wordMatches, key=lambda item: item.key)
return wordMatches[0].value
# Fuzzy matches
if partialMatches:
if len(partialMatches) > 1:
raise AmbiguityError(listType, lookup, partialMatches[0].key, partialMatches[1].key)
raise AmbiguityError(listType, lookup, partialMatches, key=lambda item: item.key)
return partialMatches[0].value
# No matches
raise LookupError("Error: '%s' doesn't match any %s" % (lookup, listType))
Expand Down

0 comments on commit a816c16

Please sign in to comment.