mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-15 00:30:33 +03:00
Also collate module and ship symbolic IDs
This commit is contained in:
parent
cea4f292e3
commit
133e2be102
43
collate.py
43
collate.py
@ -9,7 +9,7 @@ import os
|
|||||||
from os.path import exists, isfile
|
from os.path import exists, isfile
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from companion import ship_map
|
import companion
|
||||||
import outfitting
|
import outfitting
|
||||||
|
|
||||||
|
|
||||||
@ -43,9 +43,8 @@ def addcommodities(data):
|
|||||||
if new['id'] != old['id'] or new['category'] != old['category']:
|
if new['id'] != old['id'] or new['category'] != old['category']:
|
||||||
raise AssertionError('%s: "%s"!="%s"' % (key, new, old))
|
raise AssertionError('%s: "%s"!="%s"' % (key, new, old))
|
||||||
elif new['average'] != old['average']:
|
elif new['average'] != old['average']:
|
||||||
commodities[key] = new
|
size_pre -= 1
|
||||||
else:
|
commodities[key] = new
|
||||||
commodities[key] = new
|
|
||||||
|
|
||||||
if len(commodities) > size_pre:
|
if len(commodities) > size_pre:
|
||||||
|
|
||||||
@ -71,7 +70,7 @@ def addmodules(data):
|
|||||||
|
|
||||||
outfile = 'outfitting.csv'
|
outfile = 'outfitting.csv'
|
||||||
modules = {}
|
modules = {}
|
||||||
fields = ['id', 'category', 'name', 'mount', 'guidance', 'ship', 'class', 'rating', 'entitlement']
|
fields = ['id', 'symbol', 'category', 'name', 'mount', 'guidance', 'ship', 'class', 'rating', 'entitlement']
|
||||||
|
|
||||||
# slurp existing
|
# slurp existing
|
||||||
if isfile(outfile):
|
if isfile(outfile):
|
||||||
@ -84,15 +83,17 @@ def addmodules(data):
|
|||||||
for key,module in data['lastStarport'].get('modules').iteritems():
|
for key,module in data['lastStarport'].get('modules').iteritems():
|
||||||
# sanity check
|
# sanity check
|
||||||
if int(key) != module.get('id'): raise AssertionError('id: %s!=%s' % (key, module['id']))
|
if int(key) != module.get('id'): raise AssertionError('id: %s!=%s' % (key, module['id']))
|
||||||
new = outfitting.lookup(module, ship_map, True)
|
new = outfitting.lookup(module, companion.ship_map, True)
|
||||||
if new:
|
if new:
|
||||||
old = modules.get(int(key))
|
old = modules.get(int(key))
|
||||||
if old:
|
if old:
|
||||||
# check consistency with existing data
|
# check consistency with existing data
|
||||||
for thing in fields:
|
for thing in fields:
|
||||||
if str(new.get(thing,'')) != old.get(thing): raise AssertionError('%s: %s "%s"!="%s"' % (key, thing, new.get(thing), old.get(thing)))
|
if not old.get(thing) and new.get(thing):
|
||||||
else:
|
size_pre -= 1
|
||||||
modules[int(key)] = new
|
elif str(new.get(thing,'')) != old.get(thing):
|
||||||
|
raise AssertionError('%s: %s "%s"!="%s"' % (key, thing, new.get(thing), old.get(thing)))
|
||||||
|
modules[int(key)] = new
|
||||||
|
|
||||||
if len(modules) > size_pre:
|
if len(modules) > size_pre:
|
||||||
|
|
||||||
@ -116,26 +117,31 @@ def addships(data):
|
|||||||
|
|
||||||
shipfile = 'shipyard.csv'
|
shipfile = 'shipyard.csv'
|
||||||
ships = {}
|
ships = {}
|
||||||
|
fields = ['id', 'symbol', 'name']
|
||||||
|
|
||||||
# slurp existing
|
# slurp existing
|
||||||
if isfile(shipfile):
|
if isfile(shipfile):
|
||||||
with open(shipfile) as csvfile:
|
with open(shipfile) as csvfile:
|
||||||
reader = csv.DictReader(csvfile)
|
reader = csv.DictReader(csvfile, restval='')
|
||||||
for row in reader:
|
for row in reader:
|
||||||
ships[int(row['id'])] = row['name'] # index by int for easier lookup and sorting
|
ships[int(row['id'])] = row # index by int for easier lookup and sorting
|
||||||
size_pre = len(ships)
|
size_pre = len(ships)
|
||||||
|
|
||||||
for ship in (data['lastStarport']['ships'].get('shipyard_list') or {}).values() + data['lastStarport']['ships'].get('unavailable_list'):
|
for ship in (data['lastStarport']['ships'].get('shipyard_list') or {}).values() + data['lastStarport']['ships'].get('unavailable_list'):
|
||||||
# sanity check
|
# sanity check
|
||||||
key = ship['id']
|
key = ship['id']
|
||||||
new = ship_map.get(ship['name'].lower())
|
new = { 'id': int(key), 'symbol': ship['name'], 'name': companion.ship_map.get(ship['name'].lower()) }
|
||||||
if new:
|
if new:
|
||||||
old = ships.get(int(key))
|
old = ships.get(int(key))
|
||||||
if old:
|
if old:
|
||||||
# check consistency with existing data
|
# check consistency with existing data
|
||||||
if new != old: raise AssertionError('%s: "%s"!="%s"' % (key, new, old))
|
for thing in fields:
|
||||||
else:
|
if not old.get(thing) and new.get(thing):
|
||||||
ships[int(key)] = new
|
ships[int(key)] = new
|
||||||
|
size_pre -= 1
|
||||||
|
elif str(new.get(thing,'')) != old.get(thing):
|
||||||
|
raise AssertionError('%s: %s "%s"!="%s"' % (key, thing, new.get(thing), old.get(thing)))
|
||||||
|
ships[int(key)] = new
|
||||||
|
|
||||||
if len(ships) > size_pre:
|
if len(ships) > size_pre:
|
||||||
|
|
||||||
@ -145,11 +151,10 @@ def addships(data):
|
|||||||
os.rename(shipfile, shipfile+'.bak')
|
os.rename(shipfile, shipfile+'.bak')
|
||||||
|
|
||||||
with open(shipfile, 'wb') as csvfile:
|
with open(shipfile, 'wb') as csvfile:
|
||||||
writer = csv.DictWriter(csvfile, ['id', 'name'])
|
writer = csv.DictWriter(csvfile, ['id', 'symbol', 'name'])
|
||||||
writer.writeheader()
|
writer.writeheader()
|
||||||
for key in sorted(ships):
|
for key in sorted(ships):
|
||||||
row = { 'id': key, 'name': ships[key] }
|
writer.writerow(ships[key])
|
||||||
writer.writerow(row)
|
|
||||||
|
|
||||||
print 'Added %d new ships' % (len(ships) - size_pre)
|
print 'Added %d new ships' % (len(ships) - size_pre)
|
||||||
|
|
||||||
@ -159,6 +164,7 @@ if __name__ == "__main__":
|
|||||||
print 'Usage: collate.py [dump.json]'
|
print 'Usage: collate.py [dump.json]'
|
||||||
else:
|
else:
|
||||||
# read from dumped json file(s)
|
# read from dumped json file(s)
|
||||||
|
session = companion.Session()
|
||||||
for f in sys.argv[1:]:
|
for f in sys.argv[1:]:
|
||||||
with open(f) as h:
|
with open(f) as h:
|
||||||
print f
|
print f
|
||||||
@ -169,6 +175,7 @@ if __name__ == "__main__":
|
|||||||
print 'No starport!'
|
print 'No starport!'
|
||||||
else:
|
else:
|
||||||
if data['lastStarport'].get('commodities'):
|
if data['lastStarport'].get('commodities'):
|
||||||
|
session.fixup(data['lastStarport']['commodities'])
|
||||||
addcommodities(data)
|
addcommodities(data)
|
||||||
else:
|
else:
|
||||||
print 'No market'
|
print 'No market'
|
||||||
|
@ -247,7 +247,7 @@ def lookup(module, ship_map, entitled=False):
|
|||||||
if not module.get('name'): raise AssertionError('%s: Missing name' % module['id'])
|
if not module.get('name'): raise AssertionError('%s: Missing name' % module['id'])
|
||||||
|
|
||||||
name = module['name'].lower().split('_')
|
name = module['name'].lower().split('_')
|
||||||
new = {}
|
new = { 'id': module['id'], 'symbol': module['name'] }
|
||||||
|
|
||||||
# Armour - e.g. Federation_Dropship_Armour_Grade2
|
# Armour - e.g. Federation_Dropship_Armour_Grade2
|
||||||
if name[-2] == 'armour':
|
if name[-2] == 'armour':
|
||||||
@ -375,8 +375,7 @@ def lookup(module, ship_map, entitled=False):
|
|||||||
new.update(moduledata.get(key, {}))
|
new.update(moduledata.get(key, {}))
|
||||||
|
|
||||||
# check we've filled out mandatory fields
|
# check we've filled out mandatory fields
|
||||||
new['id'] = module['id']
|
for thing in ['id', 'symbol', 'category', 'name', 'class', 'rating']: # Don't consider mass etc as mandatory
|
||||||
for thing in ['id', 'category', 'name', 'class', 'rating']: # Don't consider mass etc as mandatory
|
|
||||||
if not new.get(thing): raise AssertionError('%s: failed to set %s' % (module['id'], thing))
|
if not new.get(thing): raise AssertionError('%s: failed to set %s' % (module['id'], thing))
|
||||||
if new['category'] == 'hardpoint' and not new.get('mount'):
|
if new['category'] == 'hardpoint' and not new.get('mount'):
|
||||||
raise AssertionError('%s: failed to set %s' % (module['id'], 'mount'))
|
raise AssertionError('%s: failed to set %s' % (module['id'], 'mount'))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user