diff --git a/.gitignore b/.gitignore index 80f5a99..59da8e3 100644 --- a/.gitignore +++ b/.gitignore @@ -160,4 +160,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/src/g4edgetestdata/core.py b/src/g4edgetestdata/core.py index 6bac64d..204f9c6 100644 --- a/src/g4edgetestdata/core.py +++ b/src/g4edgetestdata/core.py @@ -3,7 +3,7 @@ import logging import os from getpass import getuser -from pathlib import Path +from pathlib import Path, PurePath from tempfile import gettempdir from git import GitCommandError, InvalidGitRepositoryError, Repo @@ -12,12 +12,27 @@ class G4EdgeTestData: + """ + Class to access all test data. Data can be accessed via the path using the + [] operator. A full list of available files (built dynamically) is given in + the member `files`. + + >>> d = G4EdgeTestData() + >>> d.files + ['convert/T001_geant4Box2Fluka.gdml', + 'convert/T001_geant4Box2Fluka.inp', + 'convert/T001_geant4Box2Fluka_baked.inp', + ... + >>> abs_path = d['convert/T001_geant4Box2Fluka.inp'] + """ + def __init__(self): self._default_git_ref = "main" self._repo_path = Path( os.getenv("G4EDGE_TESTDATA", gettempdir() + "/g4edge-testdata-" + getuser()) ) self._repo: Repo = self._init_testdata_repo() + self._build_list_of_available_data() def _init_testdata_repo(self) -> None: if not self._repo_path.is_dir(): @@ -64,3 +79,17 @@ def __getitem__(self, filename: str | Path) -> Path: raise FileNotFoundError(msg) return full_path + + def _build_list_of_available_data(self): + """ + Build a list of all available data dynamically. From python 3.12 we could use + `Path.walk`, but we use down to 3.7, therefore use the `os.walk` method instead. + """ + self.files = [] + root = Path(self._repo_path / "data") + for dirpath, _dirnames, filenames in os.walk(root): + for f in filenames: + common = os.path.relpath(dirpath, root) + rp = PurePath(common) / f + self.files.append(str(rp)) + self.files = sorted(self.files)