mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-06-05 18:03:17 +03:00
Make export_ship() paranoid about possible encoding issues.
We had a report of a UnicodeDecodeError trying to read an old file. So try utf-8 first, if it fails try the default, and if nothing else try to write a new file so the next call should actually work as expected.
This commit is contained in:
parent
4398e7a85e
commit
69bc90f5b4
18
monitor.py
18
monitor.py
@ -1085,7 +1085,7 @@ class EDLogs(FileSystemEventHandler): # type: ignore # See below
|
|||||||
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def export_ship(self, filename=None) -> None:
|
def export_ship(self, filename=None) -> None: # noqa: C901, CCR001
|
||||||
"""
|
"""
|
||||||
Export ship loadout as a Loadout event.
|
Export ship loadout as a Loadout event.
|
||||||
|
|
||||||
@ -1124,10 +1124,24 @@ class EDLogs(FileSystemEventHandler): # type: ignore # See below
|
|||||||
regexp = re.compile(re.escape(ship) + r'\.\d{4}\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
|
regexp = re.compile(re.escape(ship) + r'\.\d{4}\-\d\d\-\d\dT\d\d\.\d\d\.\d\d\.txt')
|
||||||
oldfiles = sorted((x for x in listdir(config.get_str('outdir')) if regexp.match(x))) # type: ignore
|
oldfiles = sorted((x for x in listdir(config.get_str('outdir')) if regexp.match(x))) # type: ignore
|
||||||
if oldfiles:
|
if oldfiles:
|
||||||
with open(join(config.get_str('outdir'), oldfiles[-1]), 'rU') as h: # type: ignore
|
try:
|
||||||
|
with open(join(config.get('outdir'), oldfiles[-1]), 'r', encoding='utf-8') as h: # type: ignore
|
||||||
if h.read() == string:
|
if h.read() == string:
|
||||||
return # same as last time - don't write
|
return # same as last time - don't write
|
||||||
|
|
||||||
|
except UnicodeError:
|
||||||
|
logger.exception("UnicodeError reading old ship loadout with utf-8 encoding, trying without...")
|
||||||
|
try:
|
||||||
|
with open(join(config.get('outdir'), oldfiles[-1]), 'rU') as h: # type: ignore
|
||||||
|
if h.read() == string:
|
||||||
|
return # same as last time - don't write
|
||||||
|
|
||||||
|
except OSError:
|
||||||
|
logger.exception("OSError reading old ship loadout default encoding.")
|
||||||
|
|
||||||
|
except OSError:
|
||||||
|
logger.exception("OSError reading old ship loadout with default encoding")
|
||||||
|
|
||||||
# Write
|
# Write
|
||||||
ts = strftime('%Y-%m-%dT%H.%M.%S', localtime(time()))
|
ts = strftime('%Y-%m-%dT%H.%M.%S', localtime(time()))
|
||||||
filename = join( # type: ignore
|
filename = join( # type: ignore
|
||||||
|
Loading…
x
Reference in New Issue
Block a user