Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
31 changes: 30 additions & 1 deletion src/g4edgetestdata/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand Down Expand Up @@ -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)
Loading