1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-12 23:37: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

@ -5,7 +5,6 @@ import time
from collections import defaultdict
from operator import itemgetter
from platform import system
from sys import platform
from companion import CAPIData
from config import applongname, appversion, config
@ -20,6 +19,7 @@ stockbracketmap = {0: '-',
2: 'M',
3: 'H', }
def export(data: CAPIData) -> None:
"""Export market data in TD format."""
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:
# Format described here: https://bitbucket.org/kfsone/tradedangerous/wiki/Price%20Data
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()
h.write(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'))
h.write(
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()
starport_name = data['lastStarport']['name'].strip()
h.write(f'@ {system_name}/{starport_name}\n'.encode('utf-8'))
# sort commodities by category
bycategory = defaultdict(list)
by_category = defaultdict(list)
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'))
for category in sorted(bycategory):
h.write(' + {}\n'.format(category).encode('utf-8'))
for category in sorted(by_category):
h.write(f' + {format(category)}\n'.encode('utf-8'))
# corrections to commodity names can change the sort order
for commodity in sorted(bycategory[category], key=itemgetter('name')):
h.write(' {:<23} {:7d} {:7d} {:9}{:1} {:8}{:1} {}\n'.format(
commodity['name'],
int(commodity['sellPrice']),
int(commodity['buyPrice']),
int(commodity['demand']) if commodity['demandBracket'] else '',
demandbracketmap[commodity['demandBracket']],
int(commodity['stock']) if commodity['stockBracket'] else '',
stockbracketmap[commodity['stockBracket']],
timestamp).encode('utf-8'))
for commodity in sorted(by_category[category], key=itemgetter('name')):
h.write(
f" {commodity['name']:<23}"
f" {int(commodity['sellPrice']):7d}"
f" {int(commodity['buyPrice']):7d}"
f" {int(commodity['demand']) if commodity['demandBracket'] else '':9}"
f"{demandbracketmap[commodity['demandBracket']]:1}"
f" {int(commodity['stock']) if commodity['stockBracket'] else '':8}"
f"{stockbracketmap[commodity['stockBracket']]:1}"
f" {timestamp}\n".encode('utf-8')
)