diff --git a/td.py b/td.py index f75ddf93..cd2c0d3e 100644 --- a/td.py +++ b/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# \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# \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') + )