Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mth5 \ mth5 \ data \ paths.py: 73%
37 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-10 00:01 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-10 00:01 -0800
1"""
2Sets up paths for synthetic data testing.
4"""
5import errno
6import pathlib
7import tempfile
8from typing import Optional, Union
10from loguru import logger
13DEFAULT_SANDBOX_PATH = pathlib.Path(__file__).parent.resolve()
16class SyntheticTestPaths:
17 """
18 This class was created to workaround installations with read-only access to the folder containing mth5.
19 Normally, the mth5 data/ folder can be used to store mth5 test data generated when running tests or examples.
20 If data/ is read-only, then this class allows setting "sandbox_path", a writable folder for tests or examples.
22 """
24 def __init__(
25 self,
26 sandbox_path: Optional[Union[pathlib.Path, None]] = None,
27 ascii_data_path: Optional[Union[pathlib.Path, None]] = None,
28 ):
29 """
31 :type sandbox_path: Union[pathlib.Path, None]
32 :param sandbox_path: A writable path where test results are stored.
33 :type ascii_data_path: Union[pathlib.Path, None]
34 :param ascii_data_path: This is where the synthetic ascii data are loaded from.
37 """
38 # READ ONLY OK
39 if ascii_data_path is None:
40 self.ascii_data_path = pathlib.Path(__file__).parent.resolve()
42 # NEED WRITE ACCESS
43 # Consider using an environment variable for sandbox_path
44 if sandbox_path is None:
45 logger.debug(
46 f"synthetic sandbox path is being set to {DEFAULT_SANDBOX_PATH}"
47 )
48 self._sandbox_path = DEFAULT_SANDBOX_PATH
49 else:
50 self._sandbox_path = sandbox_path
52 self.mth5_path = self._sandbox_path.joinpath("mth5")
53 self.mkdirs()
54 self.writability_check()
56 def writability_check(self) -> None:
57 """
59 Check if the path is writable, and Placeholder
61 Tried adding the second solution from here:
62 https://stackoverflow.com/questions/2113427/determining-whether-a-directory-is-writeable
64 If dirs are not writeable, consider
65 HOME = pathlib.Path().home()
66 workaround_sandbox = HOME.joinpath(".cache", "aurora", "sandbox")
67 """
68 if not _is_writable(self.mth5_path):
69 msg = f"mth5_path {self.mth5_path} is not writable -- cannot make test data"
70 raise IOError(msg)
72 def mkdirs(self) -> None:
73 """
74 Makes the directories that the tests will write results to.
76 """
77 try:
78 self.mth5_path.mkdir(parents=True, exist_ok=True)
79 except Exception as e:
80 msg = "unable to create mth5 data folder -- check write access!"
81 raise FileNotFoundError(msg)
84def _is_writable(path: pathlib.Path) -> bool:
85 """
86 Checks a path to see if you can write to it.
88 :type path: pathlib.Path
89 :param path: a place you want to write data
90 :rtype: bool
91 :return: True if path is writable, else False
93 """
94 try:
95 testfile = tempfile.TemporaryFile(dir=path)
96 testfile.close()
97 except OSError as e:
98 if e.errno == errno.EACCES: # 13
99 return False
100 e.filename = path
101 raise
102 return True
105# def main():
106# print(DEFAULT_SANDBOX_PATH.absolute())
107#
108# if __name__ =="__main__":
109# main()