Coverage for src/hypra/config.py: 63.79%

40 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-26 22:12 +0000

1"""Configuration for an externally installed, licensed Wolfram/HypExp backend.""" 

2 

3from __future__ import annotations 

4 

5import os 

6import shutil 

7from dataclasses import dataclass 

8from pathlib import Path 

9 

10 

11@dataclass(frozen=True) 

12class BackendConfig: 

13 """Settings for the external Wolfram comparison backend. 

14 

15 Package paths identify trusted local source files (or package directories). 

16 These packages execute in the Wolfram kernel: do not configure untrusted files. 

17 

18 Attributes: 

19 executable: Path to the Wolfram executable. Omit for auto-discovery. 

20 hypexp_path: Local HypExp package file or directory. 

21 hpl_path: Local HPL package file or directory. 

22 timeout: Kernel subprocess timeout in seconds. 

23 cache_dir: Override the default external cache directory. 

24 use_cache: Reuse results from the local disk cache. 

25 """ 

26 

27 executable: str | None = None 

28 hypexp_path: str | None = None 

29 hpl_path: str | None = None 

30 timeout: float = 120.0 

31 cache_dir: str | None = None 

32 use_cache: bool = True 

33 

34 def __post_init__(self) -> None: 

35 if ( 

36 isinstance(self.timeout, bool) 

37 or not isinstance(self.timeout, (int, float)) 

38 or not 0 < self.timeout <= 86400 

39 ): 

40 raise ValueError("timeout must be a positive number of seconds, at most 86400") 

41 for name in ("executable", "hypexp_path", "hpl_path", "cache_dir"): 

42 value = getattr(self, name) 

43 if value is not None and (not isinstance(value, str) or not value or "\x00" in value): 43 ↛ 44line 43 didn't jump to line 44 because the condition on line 43 was never true

44 raise ValueError(f"{name} must be a nonempty path string or None") 

45 

46 @classmethod 

47 def from_env(cls) -> BackendConfig: 

48 """Load the external backend settings from Hypra environment variables.""" 

49 return cls( 

50 executable=os.environ.get("HYPRA_WOLFRAM_EXECUTABLE") or None, 

51 hypexp_path=os.environ.get("HYPRA_HYPEXP_PATH") or None, 

52 hpl_path=os.environ.get("HYPRA_HPL_PATH") or None, 

53 timeout=float(os.environ.get("HYPRA_TIMEOUT", "120")), 

54 cache_dir=os.environ.get("HYPRA_CACHE_DIR") or None, 

55 use_cache=os.environ.get("HYPRA_CACHE", "1") not in {"0", "false", "no"}, 

56 ) 

57 

58 def resolved_executable(self) -> str | None: 

59 """Find the configured or auto-discovered Wolfram executable.""" 

60 if self.executable: 60 ↛ 64line 60 didn't jump to line 64 because the condition on line 60 was always true

61 candidate = os.path.expanduser(self.executable) 

62 found = shutil.which(candidate) 

63 return str(Path(found).resolve()) if found else None 

64 for name in ("wolframscript", "WolframKernel", "MathKernel"): 

65 if found := shutil.which(name): 

66 return str(Path(found).resolve()) 

67 candidates = ( 

68 "/Applications/Mathematica.app/Contents/MacOS/WolframKernel", 

69 "/Applications/Wolfram Mathematica.app/Contents/MacOS/WolframKernel", 

70 "/Applications/Wolfram.app/Contents/MacOS/WolframKernel", 

71 "/usr/local/Wolfram/Mathematica/Executables/WolframKernel", 

72 ) 

73 for candidate in candidates: 

74 if os.path.isfile(candidate) and os.access(candidate, os.X_OK): 

75 return candidate 

76 return None 

77 

78 def resolved_cache_dir(self) -> Path: 

79 """Return the cache location after expanding user paths.""" 

80 if self.cache_dir: 80 ↛ 82line 80 didn't jump to line 82 because the condition on line 80 was always true

81 return Path(self.cache_dir).expanduser() 

82 return Path(os.environ.get("XDG_CACHE_HOME", str(Path.home() / ".cache"))) / "hypra"