mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-12 23:37:14 +03:00
loadout.py: flake8 & mypy pass
* Catch if an empty string (or other not-None Falsey value) is passed in. * Use pathlib for constructed filename.
This commit is contained in:
parent
37ca1c3c19
commit
485a1e3bb4
41
loadout.py
41
loadout.py
@ -1,48 +1,67 @@
|
|||||||
# Export ship loadout in Companion API json format
|
"""Export ship loadout in Companion API json format."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from os.path import join
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
from os.path import join
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from config import config
|
|
||||||
import companion
|
import companion
|
||||||
import util_ships
|
import util_ships
|
||||||
|
from config import config
|
||||||
|
from EDMCLogging import get_main_logger
|
||||||
|
|
||||||
|
logger = get_main_logger()
|
||||||
|
|
||||||
|
|
||||||
def export(data, filename=None):
|
def export(data: companion.CAPIData, requested_filename: Optional[str] = None) -> None:
|
||||||
|
"""
|
||||||
|
Write Ship Loadout in Companion API JSON format.
|
||||||
|
|
||||||
|
:param data: CAPI data containing ship loadout.
|
||||||
|
:param requested_filename: Name of file to write to.
|
||||||
|
"""
|
||||||
string = json.dumps(
|
string = json.dumps(
|
||||||
companion.ship(data), cls=companion.CAPIDataEncoder,
|
companion.ship(data), cls=companion.CAPIDataEncoder,
|
||||||
ensure_ascii=False, indent=2, sort_keys=True, separators=(',', ': ')
|
ensure_ascii=False, indent=2, sort_keys=True, separators=(',', ': ')
|
||||||
) # pretty print
|
) # pretty print
|
||||||
|
|
||||||
if filename:
|
if requested_filename is not None and requested_filename:
|
||||||
with open(filename, 'wt') as h:
|
with open(requested_filename, 'wt') as h:
|
||||||
h.write(string)
|
h.write(string)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
elif not requested_filename:
|
||||||
|
logger.error(f"{requested_filename=} is not valid")
|
||||||
|
return
|
||||||
|
|
||||||
# Look for last ship of this type
|
# Look for last ship of this type
|
||||||
ship = util_ships.ship_file_name(data['ship'].get('shipName'), data['ship']['name'])
|
ship = util_ships.ship_file_name(data['ship'].get('shipName'), data['ship']['name'])
|
||||||
regexp = re.compile(re.escape(ship) + '\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
|
regexp = re.compile(re.escape(ship) + r'\.\d\d\d\d\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
|
||||||
oldfiles = sorted([x for x in os.listdir(config.get_str('outdir')) if regexp.match(x)])
|
oldfiles = sorted([x for x in os.listdir(config.get_str('outdir')) if regexp.match(x)])
|
||||||
if oldfiles:
|
if oldfiles:
|
||||||
with open(join(config.get_str('outdir'), oldfiles[-1]), 'rU') as h:
|
with open(join(config.get_str('outdir'), oldfiles[-1]), 'rU') as h:
|
||||||
if h.read() == string:
|
if h.read() == string:
|
||||||
return # same as last time - don't write
|
return # same as last time - don't write
|
||||||
|
|
||||||
querytime = config.get_int('querytime', default=int(time.time()))
|
query_time = config.get_int('querytime', default=int(time.time()))
|
||||||
|
|
||||||
# Write
|
# Write
|
||||||
#
|
#
|
||||||
# When this is refactored into multi-line CHECK IT WORKS, avoiding the
|
# When this is refactored into multi-line CHECK IT WORKS, avoiding the
|
||||||
# brainfart we had with dangling commas in commodity.py:export() !!!
|
# brainfart we had with dangling commas in commodity.py:export() !!!
|
||||||
#
|
#
|
||||||
filename = join(config.get_str('outdir'), '%s.%s.txt' % (ship, time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(querytime))))
|
output_path = pathlib.Path(config.get_str('outdir'))
|
||||||
|
output_filename = pathlib.Path(
|
||||||
|
ship + '.' + time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime(query_time)) + '.txt'
|
||||||
|
)
|
||||||
|
# Can't re-use `filename`, different type
|
||||||
|
file_name = output_path / output_filename
|
||||||
#
|
#
|
||||||
# When this is refactored into multi-line CHECK IT WORKS, avoiding the
|
# When this is refactored into multi-line CHECK IT WORKS, avoiding the
|
||||||
# brainfart we had with dangling commas in commodity.py:export() !!!
|
# brainfart we had with dangling commas in commodity.py:export() !!!
|
||||||
#
|
#
|
||||||
with open(filename, 'wt') as h:
|
with open(file_name, 'wt') as h:
|
||||||
h.write(string)
|
h.write(string)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user