1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-14 08:17:13 +03:00

Handle sparse arrays in the Companion API's json data.

Fixes #13.
This commit is contained in:
Jonathan Harris 2015-07-05 18:58:13 +01:00
parent 7576e3331f
commit 8ce49faf3a
3 changed files with 30 additions and 7 deletions

View File

@ -71,6 +71,29 @@ ship_map = {
}
# Companion API sometimes returns an array as a json array, sometimes as a json object indexed by "int".
# This seems to depend on whether the there are 'gaps' in the Cmdr's data - i.e. whether the array is sparse.
# In practice these arrays aren't very sparse so just convert them to lists with any 'gaps' holding None.
def listify(thing):
if thing is None:
return [] # data is not present
elif isinstance(thing, list):
return thing # array is not sparse
elif isinstance(thing, dict):
retval = []
for k,v in thing.iteritems():
idx = int(k)
if idx >= len(retval):
retval.extend([None] * (idx - len(retval)))
retval.append(v)
else:
retval[idx] = v
return retval
else:
assert False, thing # we expect an array or a sparse array
return list(thing) # hope for the best
class ServerError(Exception):
def __str__(self):
return 'Error: Server is down'

View File

@ -97,6 +97,6 @@ def export(data):
'systemName' : data['lastSystem']['name'].strip(),
'stationName' : data['lastStarport']['name'].strip(),
'timestamp' : time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(querytime)),
'ships' : [ship_map.get(ship['name'],ship['name']) for ship in data['lastStarport']['ships'].get('shipyard_list', {}).values() + data['lastStarport']['ships'].get('unavailable_list', [])],
'ships' : [ship_map.get(ship,ship) for ship in (data['lastStarport']['ships'].get('shipyard_list') or {}).keys() + (data['lastStarport']['ships'].get('unavailable_list') or {}).keys()],
}
})

View File

@ -262,10 +262,10 @@ class StatsResults(tk.Toplevel):
try:
current = data['commander'].get('currentShipId')
# 'ships' can be an array or a dict indexed by str(int). Perhaps the latter if you've sold ships?
for key in (isinstance(data['ships'], list) and range(len(data['ships'])) or sorted(data['ships'].keys(), key=int)):
ship = data['ships'][key]
self.addpagerow(page, [companion.ship_map.get(ship['name'], ship['name']) + (int(key)==current and ' *' or ''),
ship['starsystem']['name'], ship['station']['name']], align=tk.W)
for key, ship in enumerate(companion.listify(data.get('ships'))):
if ship:
self.addpagerow(page, [companion.ship_map.get(ship['name'], ship['name']) + (key==current and ' *' or ''),
ship['starsystem']['name'], ship['station']['name']], align=tk.W)
except:
if __debug__: print_exc()
notebook.add(page, text='Ships')
@ -322,14 +322,14 @@ class StatsResults(tk.Toplevel):
def addranking(self, parent, data, category):
try:
rank = data['commander']['rank'].get(category)
progress = list(data['stats']['ranks'].get(category, [])) # shallow copy
progress = companion.listify(data['stats']['ranks'].get(category))
if not rank or not progress:
self.addpageheader(parent, ['Rank'])
self.addpagerow(parent, [self.ranktitle(category, rank)])
else:
self.addpageheader(parent, ['Rank', 'Achieved', 'Elapsed'])
while rank > 0:
if rank>=len(progress) or not progress[rank]['ts']:
if rank>=len(progress) or not progress[rank] or not progress[rank]['ts']:
self.addpagerow(parent, [self.ranktitle(category, rank)])
else:
self.addpagerow(parent, [self.ranktitle(category, rank),