diff --git a/src/pyfabm/__init__.py b/src/pyfabm/__init__.py index 0337b690..00d265e9 100644 --- a/src/pyfabm/__init__.py +++ b/src/pyfabm/__init__.py @@ -23,6 +23,7 @@ # typing.Final not available in Python 3.7 from typing import Any + try: from typing import Final, SupportsIndex except ImportError: @@ -42,7 +43,6 @@ sys.exit(1) import numpy.typing as npt - LOG_CALLBACK = ctypes.CFUNCTYPE(None, ctypes.c_char_p) @@ -1068,7 +1068,7 @@ def __init__(self, model: "Model", name: str): class Model(object): def __init__( self, - path: Union[str, dict] = "fabm.yaml", + path: Union[str, dict, os.PathLike] = "fabm.yaml", shape: Tuple[int, ...] = (), libname: Optional[str] = None, start: Optional[Tuple[int, ...]] = None, @@ -1095,6 +1095,8 @@ def _none_representer(self: Dumper, _: None) -> Any: yaml.dump(path, wrapper, Dumper=Dumper) path = f.name delete = True + else: + path = os.fspath(path) if libname is None: # Pick one of the built-in FABM libraries (0D or 1D) @@ -1292,9 +1294,11 @@ def setCellThickness(self, value: npt.ArrayLike): def getSubModel(self, name: str) -> SubModel: return SubModel(self, name) - def save_settings(self, path: str, display: int = DISPLAY_NORMAL): + def save_settings( + self, path: Union[str, os.PathLike], display: int = DISPLAY_NORMAL + ): """Write model configuration to yaml file""" - self.fabm.save_settings(self.pmodel, path.encode("ascii"), display) + self.fabm.save_settings(self.pmodel, os.fspath(path).encode("ascii"), display) def _save_state(self) -> Tuple[Mapping[str, np.ndarray], Mapping[str, np.ndarray]]: environment = {} diff --git a/src/pyfabm/complete_yaml.py b/src/pyfabm/complete_yaml.py index b9e142a0..eab5f054 100644 --- a/src/pyfabm/complete_yaml.py +++ b/src/pyfabm/complete_yaml.py @@ -1,9 +1,12 @@ +from typing import Union +import os + import pyfabm def processFile( - infile: str, - outfile: str, + infile: Union[str, os.PathLike], + outfile: Union[str, os.PathLike], subtract_background: bool = False, add_missing: bool = False, ): diff --git a/src/pyfabm/utils/fabm_complete_yaml.py b/src/pyfabm/utils/fabm_complete_yaml.py index 266e1a36..2f96db2d 100755 --- a/src/pyfabm/utils/fabm_complete_yaml.py +++ b/src/pyfabm/utils/fabm_complete_yaml.py @@ -7,6 +7,7 @@ """ import sys +from pathlib import Path try: import pyfabm.complete_yaml @@ -21,6 +22,7 @@ def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "path", + type=Path, help=( "Path to a YAML file with the model configuration that needs to" " be completed." @@ -28,6 +30,7 @@ def main() -> None: ) parser.add_argument( "output_path", + type=Path, help=( "Path to save the completed YAML file to. If not provided," " this defaults to the file from which the model configuration is read." diff --git a/src/pyfabm/utils/fabm_configuration_gui.py b/src/pyfabm/utils/fabm_configuration_gui.py index 3fe5085f..c4eaeb89 100755 --- a/src/pyfabm/utils/fabm_configuration_gui.py +++ b/src/pyfabm/utils/fabm_configuration_gui.py @@ -5,6 +5,7 @@ """ import sys +from pathlib import Path try: import pyfabm @@ -26,7 +27,8 @@ def main() -> None: "path", help="Path to a YAML file with the model configuration", nargs="?", - default="fabm.yaml", + type=Path, + default=Path("fabm.yaml"), ) args = parser.parse_args() diff --git a/src/pyfabm/utils/fabm_describe_model.py b/src/pyfabm/utils/fabm_describe_model.py index b42e5320..b694b6c8 100755 --- a/src/pyfabm/utils/fabm_describe_model.py +++ b/src/pyfabm/utils/fabm_describe_model.py @@ -6,6 +6,7 @@ """ import sys +from pathlib import Path try: import pyfabm @@ -22,7 +23,8 @@ def main() -> None: "path", help="Path to a YAML file with the model configuration", nargs="?", - default="fabm.yaml", + type=Path, + default=Path("fabm.yaml"), ) parser.add_argument( "--all",