From 4360a9cf5c4c75bef2c6edfe1a15279205394a6d Mon Sep 17 00:00:00 2001 From: grwth0 Date: Mon, 24 Oct 2022 19:14:52 -0400 Subject: [PATCH] Made `parser.write` and `parser.parse`'s file-seeking approach case-insensitive on all systems. --- src/pyfomod/parser.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/pyfomod/parser.py b/src/pyfomod/parser.py index 7fa1b8c..7c046da 100644 --- a/src/pyfomod/parser.py +++ b/src/pyfomod/parser.py @@ -254,16 +254,27 @@ def _iterparse(file_path, target): target.end(element.tag) return target.close() +def get_case_insensitive_path(parent_path: Path, file: str): + """ + Returns a `Path` object matching `parent_path/file`, using `file`'s actual casing "on disk". + Works on case-sensitive systems where `file`'s casing may differs from the provided value. + + Returns a `Path` object for `parent_path/file` if no match could be found. + """ + for candidate_file in os.listdir(parent_path): + if candidate_file.lower() == file.lower(): + return parent_path / candidate_file + return parent_path / file def parse(source, warnings=None, lineno=False): if isinstance(source, (tuple, list)): info, conf = source else: - path = Path(source) / "fomod" + path = get_case_insensitive_path(source, "fomod") if not path.is_dir(): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), "fomod") - info = path / "info.xml" - conf = path / "moduleconfig.xml" + info = get_case_insensitive_path(path, "info.xml") + conf = get_case_insensitive_path(path, "moduleconfig.xml") if not info.is_file(): info = None else: @@ -302,10 +313,10 @@ def write(root, path): info = Path(info) conf = Path(path[1]) else: - path = Path(path) / "fomod" + path = get_case_insensitive_path(Path(path), "fomod") path.mkdir(parents=True, exist_ok=True) - info = path / "info.xml" - conf = path / "moduleconfig.xml" + info = get_case_insensitive_path(path, "info.xml") + conf = get_case_insensitive_path(path, "moduleconfig.xml") if info is not None: with info.open("w") as info_f: info_f.write(root._info.to_string())