diff --git a/ndradex/__init__.py b/ndradex/__init__.py index 81f666a..071ee74 100644 --- a/ndradex/__init__.py +++ b/ndradex/__init__.py @@ -1,9 +1,8 @@ -__all__ = ["consts", "lamda", "nd", "radex", "run"] +__all__ = ["lamda", "nd", "radex", "run"] __version__ = "0.3.1" # dependencies -from . import consts from . import lamda from . import nd from . import radex diff --git a/ndradex/consts.py b/ndradex/consts.py deleted file mode 100644 index 92a312e..0000000 --- a/ndradex/consts.py +++ /dev/null @@ -1,86 +0,0 @@ -__all__ = [ - # ndradex-related - "NDRADEX", - "NDRADEX_CONFIG", - "NDRADEX_DIR", - # radex-related - "RADEX_BIN", - "RADEX_VERSION", -] - - -# standard library -from os import getenv -from pathlib import Path -from typing import Any, Optional, TypeVar, overload - - -# dependencies -from tomlkit import load - - -# type hints -T = TypeVar("T") - - -# helper functions -def ensure(toml: Path) -> Path: - """Create an empty TOML file if it does not exist.""" - if not toml.exists(): - toml.parent.mkdir(parents=True, exist_ok=True) - toml.touch() - - return toml - - -@overload -def getval(toml: Path, keys: str, default: type[T]) -> Optional[T]: ... - - -@overload -def getval(toml: Path, keys: str, default: T) -> T: ... - - -def getval(toml: Path, keys: str, default: Any) -> Any: - """Return the value of the keys in a TOML file.""" - if isinstance(default, type): - type_, default_ = default, None - else: - type_, default_ = type(default), default - - with open(toml) as file: - doc = load(file) - - for key in keys.split("."): - if (doc := doc.get(key)) is None: - return default_ - - return type_(doc.unwrap()) - - -# ndradex-related -NDRADEX = Path(__file__).parent -"""Path of the ndRADEX package.""" - -NDRADEX_CONFIG: Path -"""Path of the ndRADEX config.""" - -NDRADEX_DIR: Path -"""Path of the ndRADEX directory.""" - -if (env := getenv("NDRADEX_DIR")) is not None: - NDRADEX_DIR = Path(env) -elif (env := getenv("XDG_CONFIG_HOME")) is not None: - NDRADEX_DIR = Path(env) / "ndradex" -else: - NDRADEX_DIR = Path.home() / ".config" / "ndradex" - -NDRADEX_CONFIG = ensure(NDRADEX_DIR / "config.toml") - - -# radex-related -RADEX_BIN = getval(NDRADEX_CONFIG, "radex.bin", NDRADEX / "bin") -"""Default path for the RADEX binaries.""" - -RADEX_VERSION = "30nov2011" -"""Supported version of the RADEX binaries.""" diff --git a/ndradex/radex.py b/ndradex/radex.py index e223362..f052a62 100644 --- a/ndradex/radex.py +++ b/ndradex/radex.py @@ -1,4 +1,4 @@ -__all__ = ["build", "run", "runmap", "to_input"] +__all__ = ["RADEX_BIN", "build", "run"] # standard library @@ -14,10 +14,6 @@ from typing import Any, Iterable, Iterator, Optional, Union -# dependencies -from .consts import NDRADEX, RADEX_BIN, RADEX_VERSION - - # type hints Input = tuple[str, ...] Output = list[tuple[str, ...]] @@ -30,11 +26,13 @@ # constants FC = getenv("FC", "gfortran") NAN = str(float("nan")) -NDRADEX_BIN = NDRADEX / "bin" +NDRADEX_BIN = Path(__file__).parent / "bin" +RADEX_BIN = Path(getenv("RADEX_BIN", NDRADEX_BIN)).expanduser().resolve() RADEX_COLUMNS = 11 RADEX_LOGFILE = devnull RADEX_MAXITER = 1_000_000 RADEX_MINITER = 10 +RADEX_VERSION = "30nov2011" # module logger @@ -55,7 +53,7 @@ def build( set to the package's bin (``/path/to/ndradex/bin``): Otherwise, no build is run even if ``force=True``. - Keyword Args: + Args: force: Whether to forcibly rebuild the binaries. logfile: Path of the RADEX log file. miniter: Minimum number of iterations. @@ -72,7 +70,7 @@ def build( if force: sprun( args=["make", "clean"], - cwd=NDRADEX / "bin", + cwd=NDRADEX_BIN, stdout=PIPE, stderr=PIPE, check=True, diff --git a/tests/test_radex.py b/tests/test_radex.py index 9f21bdd..1955bb0 100644 --- a/tests/test_radex.py +++ b/tests/test_radex.py @@ -4,9 +4,8 @@ # dependencies -from ndradex.consts import RADEX_BIN from ndradex.lamda import query -from ndradex.radex import run, runmap, to_input +from ndradex.radex import RADEX_BIN, run, runmap, to_input # test data