Skip to content

Commit

Permalink
Add __file__ attributes to everything on load
Browse files Browse the repository at this point in the history
Closes #96 and #103
  • Loading branch information
moble committed Sep 6, 2024
1 parent 4c41014 commit b70d201
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
9 changes: 7 additions & 2 deletions sxs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion sxs/metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions sxs/simulations/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion sxs/simulations/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b70d201

Please sign in to comment.