From b70d201605b145e3dbd5f6a3c7c9d7a07188e283 Mon Sep 17 00:00:00 2001 From: Mike Boyle Date: Fri, 6 Sep 2024 01:04:09 -0400 Subject: [PATCH] Add __file__ attributes to everything on load Closes #96 and #103 --- sxs/handlers.py | 9 +++++++-- sxs/metadata/metadata.py | 2 +- sxs/simulations/simulation.py | 9 ++++++--- sxs/simulations/simulations.py | 4 +++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/sxs/handlers.py b/sxs/handlers.py index b3e6f55f..b67149a1 100644 --- a/sxs/handlers.py +++ b/sxs/handlers.py @@ -282,7 +282,7 @@ def load(location, download=None, cache=None, progress=None, truepath=None, **kw elif location == "simulations": return Simulations.load(download=download) - + elif sxs_id_version_lev_exact_re.match(location): return Simulation(location, download=download, cache=cache, progress=progress, **kwargs) @@ -311,7 +311,12 @@ def load(location, download=None, cache=None, progress=None, truepath=None, **kw loader = sxs_loader(path, kwargs.get("group", None)) - return loader(path, **kwargs) + loaded = loader(path, **kwargs) + try: + loaded.__file__ = str(path) + except: + pass + return loaded def load_via_sxs_id(sxsid, location, *, download=None, cache=None, progress=None, truepath=None, **kwargs): diff --git a/sxs/metadata/metadata.py b/sxs/metadata/metadata.py index 2aa9df84..358e8a18 100644 --- a/sxs/metadata/metadata.py +++ b/sxs/metadata/metadata.py @@ -90,7 +90,7 @@ def from_file(cls, file_name, ignore_invalid_lines=False, cache_json=True): else: return cls.from_json_file(json_path) else: - raise ValueError(f"Could not find file named '{file_name}', '{json_path}', or '{txt_path}'") + raise ValueError(f"Could not find file named '{json_path}' or '{txt_path}'") load = from_file diff --git a/sxs/simulations/simulation.py b/sxs/simulations/simulation.py index ec9eafd5..da4fa54f 100644 --- a/sxs/simulations/simulation.py +++ b/sxs/simulations/simulation.py @@ -95,7 +95,8 @@ def Simulation(location, *args, **kwargs): arguments other than those listed above. """ - from .. import load + from .. import load, sxs_directory + from ..utilities import sxs_path_to_system_path # Extract the simulation ID, version, and Lev from the location string simulation_id, input_version = sxs_id_and_version(location) @@ -202,15 +203,17 @@ def Simulation(location, *args, **kwargs): # Finally, figure out which version of the simulation to load and dispatch version_number = float(version[1:]) if 1 <= version_number < 2.0: - return Simulation_v1( + sim = Simulation_v1( metadata, series, version, sxs_id_stem, sxs_id, url, files, lev_numbers, output_lev_number, location, *args, **kwargs ) elif 2 <= version_number < 3.0: - return Simulation_v2( + sim = Simulation_v2( metadata, series, version, sxs_id_stem, sxs_id, url, files, lev_numbers, output_lev_number, location, *args, **kwargs ) else: raise ValueError(f"Version '{version}' not yet supported") + sim.__file__ = str(sxs_directory("cache") / sxs_path_to_system_path(sim.sxs_id)) + return sim class SimulationBase: diff --git a/sxs/simulations/simulations.py b/sxs/simulations/simulations.py index 6d78f64b..b9730a34 100644 --- a/sxs/simulations/simulations.py +++ b/sxs/simulations/simulations.py @@ -157,7 +157,9 @@ def load(cls, download=None): except Exception as e: raise ValueError(f"Failed to open '{cache_path}' as a ZIP file") from e - return cls(simulations) + sims = cls(simulations) + sims.__file__ = str(cache_path) + return sims @classmethod def reload(cls, download=True):