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

td.py: Further re-factoring to pass flake8 checks

The output of this was confirmed the 'same' as from before this work:

 1. Generate the output.
 2. `cut -c 1-68` to a copy.
 3. diff those files.

This is necessary so as to not have 'every' line be different due to the
timestamp on it.
This commit is contained in:
Athanasius 2022-12-03 16:27:22 +00:00
parent ca98549f68
commit c9555cce66
No known key found for this signature in database
GPG Key ID: 772697E181BB2767

42
td.py
View File

@ -5,7 +5,6 @@ import time
from collections import defaultdict from collections import defaultdict
from operator import itemgetter from operator import itemgetter
from platform import system from platform import system
from sys import platform
from companion import CAPIData from companion import CAPIData
from config import applongname, appversion, config from config import applongname, appversion, config
@ -20,6 +19,7 @@ stockbracketmap = {0: '-',
2: 'M', 2: 'M',
3: 'H', } 3: 'H', }
def export(data: CAPIData) -> None: def export(data: CAPIData) -> None:
"""Export market data in TD format.""" """Export market data in TD format."""
data_path = pathlib.Path(config.get_str('outdir')) data_path = pathlib.Path(config.get_str('outdir'))
@ -31,31 +31,35 @@ def export(data: CAPIData) -> None:
with open(data_path / data_filename, 'wb') as h: with open(data_path / data_filename, 'wb') as h:
# Format described here: https://bitbucket.org/kfsone/tradedangerous/wiki/Price%20Data # Format described here: https://bitbucket.org/kfsone/tradedangerous/wiki/Price%20Data
h.write('#! trade.py import -\n'.encode('utf-8')) h.write('#! trade.py import -\n'.encode('utf-8'))
this_platform = 'darwin' and "Mac OS" or system_name() this_platform = 'darwin' and "Mac OS" or system()
cmdr_name = data['commander']['name'].strip() cmdr_name = data['commander']['name'].strip()
h.write(f'# Created by {applongname} {appversion()} on {this_platform} for Cmdr {cmdr_name}.\n'.encode('utf-8')) h.write(
h.write('#\n# <item name> <sellCR> <buyCR> <demand> <stock> <timestamp>\n\n'.encode('utf-8')) f'# Created by {applongname} {appversion()} on {this_platform} for Cmdr {cmdr_name}.\n'.encode('utf-8')
)
h.write(
'#\n# <item name> <sellCR> <buyCR> <demand> <stock> <timestamp>\n\n'.encode('utf-8')
)
system_name = data['lastSystem']['name'].strip() system_name = data['lastSystem']['name'].strip()
starport_name = data['lastStarport']['name'].strip() starport_name = data['lastStarport']['name'].strip()
h.write(f'@ {system_name}/{starport_name}\n'.encode('utf-8')) h.write(f'@ {system_name}/{starport_name}\n'.encode('utf-8'))
# sort commodities by category # sort commodities by category
bycategory = defaultdict(list) by_category = defaultdict(list)
for commodity in data['lastStarport']['commodities']: for commodity in data['lastStarport']['commodities']:
bycategory[commodity['categoryname']].append(commodity) by_category[commodity['categoryname']].append(commodity)
timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(data['timestamp'], '%Y-%m-%dT%H:%M:%SZ')) timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(data['timestamp'], '%Y-%m-%dT%H:%M:%SZ'))
for category in sorted(bycategory): for category in sorted(by_category):
h.write(' + {}\n'.format(category).encode('utf-8')) h.write(f' + {format(category)}\n'.encode('utf-8'))
# corrections to commodity names can change the sort order # corrections to commodity names can change the sort order
for commodity in sorted(bycategory[category], key=itemgetter('name')): for commodity in sorted(by_category[category], key=itemgetter('name')):
h.write(' {:<23} {:7d} {:7d} {:9}{:1} {:8}{:1} {}\n'.format( h.write(
commodity['name'], f" {commodity['name']:<23}"
int(commodity['sellPrice']), f" {int(commodity['sellPrice']):7d}"
int(commodity['buyPrice']), f" {int(commodity['buyPrice']):7d}"
int(commodity['demand']) if commodity['demandBracket'] else '', f" {int(commodity['demand']) if commodity['demandBracket'] else '':9}"
demandbracketmap[commodity['demandBracket']], f"{demandbracketmap[commodity['demandBracket']]:1}"
int(commodity['stock']) if commodity['stockBracket'] else '', f" {int(commodity['stock']) if commodity['stockBracket'] else '':8}"
stockbracketmap[commodity['stockBracket']], f"{stockbracketmap[commodity['stockBracket']]:1}"
timestamp).encode('utf-8')) f" {timestamp}\n".encode('utf-8')
)