diff --git a/src/openfecli/commands/gather.py b/src/openfecli/commands/gather.py index 9692b2fbc..7a2315e1e 100644 --- a/src/openfecli/commands/gather.py +++ b/src/openfecli/commands/gather.py @@ -8,6 +8,7 @@ import click import pandas as pd +from gufe.utils import magic_open from openfecli import OFECommandPlugin from openfecli.clicktypes import HyphenAwareChoice @@ -178,7 +179,10 @@ def format_df_with_precision( def is_results_json(fpath: os.PathLike | str) -> bool: """Sanity check that file is a result json before we try to deserialize""" - return "estimate" in open(fpath, "r").read(20) + + with open(fpath, "rb") as raw: + with magic_open(raw) as f: + return "estimate" in f.read(20) def load_json(fpath: os.PathLike | str) -> dict: @@ -201,7 +205,9 @@ def load_json(fpath: os.PathLike | str) -> dict: from gufe.tokenization import JSON_HANDLER - return json.load(open(fpath, "r"), cls=JSON_HANDLER.decoder) + with open(fpath, "rb") as raw: + with magic_open(raw) as f: + return json.load(f, cls=JSON_HANDLER.decoder) def _get_names(result: dict) -> tuple[str, str]: @@ -609,17 +615,13 @@ def _collect_result_jsons(results: List[os.PathLike | str]) -> List[pathlib.Path def collect_jsons(results: List[os.PathLike]): all_jsons = [] for p in results: - if str(p).endswith("json"): + if ".json" in str(p): all_jsons.append(p) elif p.is_dir(): - all_jsons.extend(glob.glob(f"{p}/**/*json", recursive=True)) + all_jsons.extend(glob.glob(f"{p}/**/*.json*", recursive=True)) return all_jsons - def is_results_json(fpath: os.PathLike | str) -> bool: - """Sanity check that file is a result json before we try to deserialize""" - return "estimate" in open(fpath, "r").read(20) - results = sorted(results) # ensures reproducible output order regardless of input order # 1) find all possible jsons @@ -774,7 +776,7 @@ def gather( RESULTS is the path(s) to JSON files or directories of JSON files containing RBFE protocol results as generated by ``openfe quickrun``. All directories will be walked recursively and any valid JSON results files will be gathered. - Files must end in .json to be collected, and invalid files will be ignored. + Files must contain `".json"` to be collected, and invalid files will be ignored. The results reported depends on ``--report`` flag: diff --git a/src/openfecli/tests/commands/test_gather.py b/src/openfecli/tests/commands/test_gather.py index 6722c440c..1a45bd9c7 100644 --- a/src/openfecli/tests/commands/test_gather.py +++ b/src/openfecli/tests/commands/test_gather.py @@ -515,13 +515,6 @@ def septop_result_dir(tmp_path_factory) -> pathlib.Path: ZENODO_SEPTOP_DATA.fetch("septop_results.zip", processor=pooch.Unzip()) result_dir = pathlib.Path(POOCH_CACHE) / "septop_results.zip.unzip/septop_results/" - for gz_file in result_dir.rglob("*.json.gz"): - json_file = gz_file.with_suffix("") # removes .gz, leaving .json - with gzip.open(gz_file, "rb") as f_in: - with open(json_file, "wb") as f_out: - f_out.write(f_in.read()) - gz_file.unlink() # remove the .gz file - return result_dir