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

[1268] Handover to Path.is_reserved()

This commit is contained in:
David Sangrey 2024-05-02 15:02:43 -04:00
parent 22e4b9604f
commit 88bfd8ca8b
No known key found for this signature in database
GPG Key ID: 3AEADBB0186884BC

View File

@ -5,18 +5,21 @@ Copyright (c) EDCD, All Rights Reserved
Licensed under the GNU General Public License. Licensed under the GNU General Public License.
See LICENSE file. See LICENSE file.
""" """
from pathlib import Path
from edmc_data import ship_name_map from edmc_data import ship_name_map
def ship_file_name(ship_name: str, ship_type: str) -> str: def ship_file_name(ship_name: str, ship_type: str) -> str:
"""Return a ship name suitable for a filename.""" """Return a ship name suitable for a filename."""
name = str(ship_name or ship_name_map.get(ship_type.lower(), ship_type)).strip() name = str(ship_name or ship_name_map.get(ship_type.lower(), ship_type)).strip()
if name.endswith('.'):
name = name[:-2]
if name.lower() in ('con', 'prn', 'aux', 'nul', # Handle suffix using Pathlib's with_suffix method
'com0', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', name = Path(name).with_suffix("").name
'lpt0', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9'):
name += '_'
return name.translate({ord(x): '_' for x in ('\0', '<', '>', ':', '"', '/', '\\', '|', '?', '*')}) # Check if the name is a reserved filename
if Path(name).is_reserved():
name += "_"
return name.translate(
{ord(x): "_" for x in ("\0", "<", ">", ":", '"', "/", "\\", "|", "?", "*")}
)