Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made parser.write and parser.parse's file-seeking approach case-insensitive for every systems. #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/pyfomod/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand Down