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

Add newlines to separate scopes

Newlines between scope changes help you to not get lost when reading
source
This commit is contained in:
A_D 2020-07-13 05:42:55 +02:00 committed by Athanasius
parent 7e422c73c4
commit a546ecb735

View File

@ -17,8 +17,8 @@ import outfitting
# keep a summary of commodities found using in-game names # keep a summary of commodities found using in-game names
# Assumes that the commodity data has already been 'fixed up' # Assumes that the commodity data has already been 'fixed up'
def addcommodities(data): def addcommodities(data):
if not data['lastStarport'].get('commodities'):
if not data['lastStarport'].get('commodities'): return return
commodityfile = 'commodity.csv' commodityfile = 'commodity.csv'
commodities = {} commodities = {}
@ -29,6 +29,7 @@ def addcommodities(data):
reader = csv.DictReader(csvfile) reader = csv.DictReader(csvfile)
for row in reader: for row in reader:
commodities[int(row['id'])] = row # index by int for easier lookup and sorting commodities[int(row['id'])] = row # index by int for easier lookup and sorting
size_pre = len(commodities) size_pre = len(commodities)
for commodity in data['lastStarport'].get('commodities'): for commodity in data['lastStarport'].get('commodities'):
@ -39,22 +40,26 @@ def addcommodities(data):
'category' : companion.category_map.get(commodity['categoryname']) or commodity['categoryname'], 'category' : companion.category_map.get(commodity['categoryname']) or commodity['categoryname'],
'name' : commodity.get('locName') or 'Limpets', 'name' : commodity.get('locName') or 'Limpets',
} }
old = commodities.get(key) old = commodities.get(key)
if old and companion.category_map.get(commodity['categoryname'], True): if old and companion.category_map.get(commodity['categoryname'], True):
if new['symbol'] != old['symbol'] or new['name'] != old['name']: if new['symbol'] != old['symbol'] or new['name'] != old['name']:
raise AssertionError('%s: "%s"!="%s"' % (key, new, old)) raise AssertionError('%s: "%s"!="%s"' % (key, new, old))
commodities[key] = new commodities[key] = new
if len(commodities) > size_pre: if len(commodities) > size_pre:
if isfile(commodityfile): if isfile(commodityfile):
if isfile(commodityfile+'.bak'): if isfile(commodityfile+'.bak'):
os.unlink(commodityfile+'.bak') os.unlink(commodityfile+'.bak')
os.rename(commodityfile, commodityfile+'.bak') os.rename(commodityfile, commodityfile+'.bak')
with open(commodityfile, 'w') as csvfile: with open(commodityfile, 'w') as csvfile:
writer = csv.DictWriter(csvfile, ['id', 'symbol', 'category', 'name']) writer = csv.DictWriter(csvfile, ['id', 'symbol', 'category', 'name'])
writer.writeheader() writer.writeheader()
for key in sorted(commodities): for key in sorted(commodities):
writer.writerow(commodities[key]) writer.writerow(commodities[key])
@ -62,7 +67,6 @@ def addcommodities(data):
# keep a summary of modules found # keep a summary of modules found
def addmodules(data): def addmodules(data):
if not data['lastStarport'].get('modules'): return if not data['lastStarport'].get('modules'): return
outfile = 'outfitting.csv' outfile = 'outfitting.csv'
@ -75,17 +79,21 @@ def addmodules(data):
reader = csv.DictReader(csvfile, restval='') reader = csv.DictReader(csvfile, restval='')
for row in reader: for row in reader:
modules[int(row['id'])] = row # index by int for easier lookup and sorting modules[int(row['id'])] = row # index by int for easier lookup and sorting
size_pre = len(modules) size_pre = len(modules)
for key,module in data['lastStarport'].get('modules').items(): for key,module in data['lastStarport'].get('modules').items():
# 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']))
try: try:
new = outfitting.lookup(module, companion.ship_map, True) new = outfitting.lookup(module, companion.ship_map, True)
except: except:
print('%d, %s:' % (module['id'], module['name'])) print('%d, %s:' % (module['id'], module['name']))
print_exc(0) print_exc(0)
new = None new = None
if new: if new:
old = modules.get(int(key)) old = modules.get(int(key))
if old: if old:
@ -93,20 +101,23 @@ def addmodules(data):
for thing in fields: for thing in fields:
if not old.get(thing) and new.get(thing): if not old.get(thing) and new.get(thing):
size_pre -= 1 size_pre -= 1
elif str(new.get(thing,'')) != old.get(thing): elif str(new.get(thing,'')) != old.get(thing):
raise AssertionError('%s: %s "%s"!="%s"' % (key, thing, new.get(thing), old.get(thing))) raise AssertionError('%s: %s "%s"!="%s"' % (key, thing, new.get(thing), old.get(thing)))
modules[int(key)] = new modules[int(key)] = new
if len(modules) > size_pre: if len(modules) > size_pre:
if isfile(outfile): if isfile(outfile):
if isfile(outfile+'.bak'): if isfile(outfile+'.bak'):
os.unlink(outfile+'.bak') os.unlink(outfile+'.bak')
os.rename(outfile, outfile+'.bak') os.rename(outfile, outfile+'.bak')
with open(outfile, 'w') as csvfile: with open(outfile, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fields, extrasaction='ignore') writer = csv.DictWriter(csvfile, fields, extrasaction='ignore')
writer.writeheader() writer.writeheader()
for key in sorted(modules): for key in sorted(modules):
writer.writerow(modules[key]) writer.writerow(modules[key])
@ -114,7 +125,6 @@ def addmodules(data):
# keep a summary of ships found # keep a summary of ships found
def addships(data): def addships(data):
if not data['lastStarport'].get('ships'): return if not data['lastStarport'].get('ships'): return
shipfile = 'shipyard.csv' shipfile = 'shipyard.csv'
@ -127,6 +137,7 @@ def addships(data):
reader = csv.DictReader(csvfile, restval='') reader = csv.DictReader(csvfile, restval='')
for row in reader: for row in reader:
ships[int(row['id'])] = row # 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 list((data['lastStarport']['ships'].get('shipyard_list') or {}).values()) + data['lastStarport']['ships'].get('unavailable_list'): for ship in list((data['lastStarport']['ships'].get('shipyard_list') or {}).values()) + data['lastStarport']['ships'].get('unavailable_list'):
@ -141,8 +152,10 @@ def addships(data):
if not old.get(thing) and new.get(thing): if not old.get(thing) and new.get(thing):
ships[int(key)] = new ships[int(key)] = new
size_pre -= 1 size_pre -= 1
elif str(new.get(thing,'')) != old.get(thing): elif str(new.get(thing,'')) != old.get(thing):
raise AssertionError('%s: %s "%s"!="%s"' % (key, thing, new.get(thing), old.get(thing))) raise AssertionError('%s: %s "%s"!="%s"' % (key, thing, new.get(thing), old.get(thing)))
ships[int(key)] = new ships[int(key)] = new
if len(ships) > size_pre: if len(ships) > size_pre:
@ -150,11 +163,13 @@ def addships(data):
if isfile(shipfile): if isfile(shipfile):
if isfile(shipfile+'.bak'): if isfile(shipfile+'.bak'):
os.unlink(shipfile+'.bak') os.unlink(shipfile+'.bak')
os.rename(shipfile, shipfile+'.bak') os.rename(shipfile, shipfile+'.bak')
with open(shipfile, 'w') as csvfile: with open(shipfile, 'w') as csvfile:
writer = csv.DictWriter(csvfile, ['id', 'symbol', 'name']) writer = csv.DictWriter(csvfile, ['id', 'symbol', 'name'])
writer.writeheader() writer.writeheader()
for key in sorted(ships): for key in sorted(ships):
writer.writerow(ships[key]) writer.writerow(ships[key])
@ -173,18 +188,25 @@ if __name__ == "__main__":
data = json.load(h) data = json.load(h)
if not data['commander'].get('docked'): if not data['commander'].get('docked'):
print('Not docked!') print('Not docked!')
elif not data.get('lastStarport'): elif not data.get('lastStarport'):
print('No starport!') print('No starport!')
else: else:
if data['lastStarport'].get('commodities'): if data['lastStarport'].get('commodities'):
addcommodities(data) addcommodities(data)
else: else:
print('No market') print('No market')
if data['lastStarport'].get('modules'): if data['lastStarport'].get('modules'):
addmodules(data) addmodules(data)
else: else:
print('No outfitting') print('No outfitting')
if data['lastStarport'].get('ships'): if data['lastStarport'].get('ships'):
addships(data) addships(data)
else: else:
print('No shipyard') print('No shipyard')