Skip to content

Commit

Permalink
Complete standardization on pathlib Path (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
maddenp-noaa authored Feb 14, 2024
1 parent 666880f commit ea8e7e8
Show file tree
Hide file tree
Showing 29 changed files with 143 additions and 236 deletions.
34 changes: 17 additions & 17 deletions src/uwtools/api/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path
from typing import List, Optional, Union

from uwtools.config.formats.fieldtable import FieldTableConfig as _FieldTableConfig
Expand All @@ -10,15 +11,14 @@
from uwtools.config.tools import compare_configs as _compare
from uwtools.config.tools import realize_config as _realize
from uwtools.config.validator import validate_yaml as _validate_yaml
from uwtools.types import DefinitePath, OptionalPath
from uwtools.utils.file import FORMAT as _FORMAT

# Public


def compare(
config_1_path: DefinitePath,
config_2_path: DefinitePath,
config_1_path: Path,
config_2_path: Path,
config_1_format: Optional[str] = None,
config_2_format: Optional[str] = None,
) -> bool:
Expand All @@ -33,7 +33,7 @@ def compare(
)


def get_fieldtable_config(config: Union[dict, OptionalPath] = None) -> _FieldTableConfig:
def get_fieldtable_config(config: Union[dict, Optional[Path]] = None) -> _FieldTableConfig:
"""
Get a ``FieldTableConfig`` object.
Expand All @@ -44,7 +44,7 @@ def get_fieldtable_config(config: Union[dict, OptionalPath] = None) -> _FieldTab
return _FieldTableConfig(config=config)


def get_ini_config(config: Union[dict, OptionalPath] = None) -> _INIConfig:
def get_ini_config(config: Union[dict, Optional[Path]] = None) -> _INIConfig:
"""
Get an ``INIConfig`` object.
Expand All @@ -54,7 +54,7 @@ def get_ini_config(config: Union[dict, OptionalPath] = None) -> _INIConfig:
return _INIConfig(config=config)


def get_nml_config(config: Union[dict, OptionalPath] = None) -> _NMLConfig:
def get_nml_config(config: Union[dict, Optional[Path]] = None) -> _NMLConfig:
"""
Get an ``NMLConfig`` object.
Expand All @@ -65,7 +65,7 @@ def get_nml_config(config: Union[dict, OptionalPath] = None) -> _NMLConfig:
return _NMLConfig(config=config)


def get_sh_config(config: Union[dict, OptionalPath] = None) -> _SHConfig:
def get_sh_config(config: Union[dict, Optional[Path]] = None) -> _SHConfig:
"""
Get an ``SHConfig`` object.
Expand All @@ -76,7 +76,7 @@ def get_sh_config(config: Union[dict, OptionalPath] = None) -> _SHConfig:
return _SHConfig(config=config)


def get_yaml_config(config: Union[dict, OptionalPath] = None) -> _YAMLConfig:
def get_yaml_config(config: Union[dict, Optional[Path]] = None) -> _YAMLConfig:
"""
Get a ``YAMLConfig`` object.
Expand All @@ -88,11 +88,11 @@ def get_yaml_config(config: Union[dict, OptionalPath] = None) -> _YAMLConfig:


def realize(
input_config: Union[dict, _Config, OptionalPath] = None,
input_config: Union[dict, _Config, Optional[Path]] = None,
input_format: Optional[str] = None,
output_file: OptionalPath = None,
output_file: Optional[Path] = None,
output_format: Optional[str] = None,
supplemental_configs: Optional[List[Union[dict, _Config, DefinitePath]]] = None,
supplemental_configs: Optional[List[Union[dict, _Config, Path]]] = None,
values_needed: bool = False,
dry_run: bool = False,
) -> bool:
Expand All @@ -112,9 +112,9 @@ def realize(


def realize_to_dict(
input_config: Union[dict, _Config, OptionalPath] = None,
input_config: Union[dict, _Config, Optional[Path]] = None,
input_format: Optional[str] = None,
supplemental_configs: Optional[List[Union[dict, _Config, DefinitePath]]] = None,
supplemental_configs: Optional[List[Union[dict, _Config, Path]]] = None,
values_needed: bool = False,
dry_run: bool = False,
) -> dict:
Expand All @@ -124,7 +124,7 @@ def realize_to_dict(
return _realize(
input_config=_ensure_config_arg_type(input_config),
input_format=input_format,
output_file=os.devnull,
output_file=Path(os.devnull),
output_format=None,
supplemental_configs=supplemental_configs,
values_needed=values_needed,
Expand All @@ -133,7 +133,7 @@ def realize_to_dict(


def validate(
schema_file: DefinitePath, config: Optional[Union[dict, _YAMLConfig, OptionalPath]] = None
schema_file: Path, config: Optional[Union[dict, _YAMLConfig, Optional[Path]]] = None
) -> bool:
"""
Check whether the specified config conforms to the specified JSON Schema spec.
Expand All @@ -152,8 +152,8 @@ def validate(


def _ensure_config_arg_type(
config: Union[dict, _Config, OptionalPath]
) -> Union[_Config, OptionalPath]:
config: Union[dict, _Config, Optional[Path]]
) -> Union[_Config, Optional[Path]]:
"""
Encapsulate a dict in a Config; return a Config or path argument as-is.
Expand Down
4 changes: 2 additions & 2 deletions src/uwtools/api/fv3.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import datetime as dt
from pathlib import Path
from typing import Dict

import iotaa

from uwtools.drivers.fv3 import FV3
from uwtools.types import DefinitePath


def execute(
task: str,
config_file: DefinitePath,
config_file: Path,
cycle: dt.datetime,
batch: bool = False,
dry_run: bool = False,
Expand Down
8 changes: 4 additions & 4 deletions src/uwtools/api/rocoto.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Union
from pathlib import Path
from typing import Optional, Union

from uwtools.config.formats.yaml import YAMLConfig
from uwtools.rocoto import realize_rocoto_xml as _realize
from uwtools.rocoto import validate_rocoto_xml_file as _validate
from uwtools.types import OptionalPath


def realize(config: Union[YAMLConfig, OptionalPath], output_file: OptionalPath = None) -> bool:
def realize(config: Union[YAMLConfig, Optional[Path]], output_file: Optional[Path] = None) -> bool:
"""
Realize the Rocoto workflow defined in the given YAML as XML.
Expand All @@ -24,7 +24,7 @@ def realize(config: Union[YAMLConfig, OptionalPath], output_file: OptionalPath =
return True


def validate(xml_file: OptionalPath = None) -> bool:
def validate(xml_file: Optional[Path] = None) -> bool:
"""
Validate purported Rocoto XML file against its schema.
Expand Down
12 changes: 6 additions & 6 deletions src/uwtools/api/template.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from pathlib import Path
from typing import Dict, Optional, Union

from uwtools.config.atparse_to_jinja2 import convert as _convert_atparse_to_jinja2
from uwtools.config.jinja2 import render as _render
from uwtools.types import DefinitePath, OptionalPath


def render(
values: Union[dict, DefinitePath],
values: Union[dict, Path],
values_format: Optional[str] = None,
input_file: OptionalPath = None,
output_file: OptionalPath = None,
input_file: Optional[Path] = None,
output_file: Optional[Path] = None,
overrides: Optional[Dict[str, str]] = None,
values_needed: bool = False,
dry_run: bool = False,
Expand Down Expand Up @@ -46,8 +46,8 @@ def render(


def translate(
input_file: OptionalPath = None,
output_file: OptionalPath = None,
input_file: Optional[Path] = None,
output_file: Optional[Path] = None,
dry_run: bool = False,
) -> bool:
"""
Expand Down
3 changes: 2 additions & 1 deletion src/uwtools/apps/srw.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import shutil
from pathlib import Path

from uwtools.drivers.facade import Facade
from uwtools.exceptions import UWError
Expand All @@ -24,7 +25,7 @@ def __init__(self):
Initialize the facade driver.
"""

def load_config(self, config_file: str) -> None:
def load_config(self, config_file: Path) -> None:
"""
Load the configuration file.
"""
Expand Down
6 changes: 4 additions & 2 deletions src/uwtools/apps/uw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
This file contains the specific drivers for a particular app, using the facade pattern base class.
"""

from pathlib import Path

from uwtools.config.formats.yaml import YAMLConfig
from uwtools.drivers.facade import Facade

Expand All @@ -16,12 +18,12 @@ def __init__(self):
Initialize the facade driver.
"""

def load_config(self, config_file: str) -> None:
def load_config(self, config_file: Path) -> None:
"""
Load the configuration file.
"""
config_obj = YAMLConfig(config_file)
config_obj.dump("config.yaml")
config_obj.dump(Path("config.yaml"))

def validate_config(self, config_file: str) -> None:
"""
Expand Down
6 changes: 4 additions & 2 deletions src/uwtools/config/atparse_to_jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"""

import re
from pathlib import Path
from typing import Optional

from uwtools.logging import log
from uwtools.utils.file import OptionalPath, readable, writable
from uwtools.utils.file import readable, writable


def convert(
input_file: OptionalPath = None, output_file: OptionalPath = None, dry_run: bool = False
input_file: Optional[Path] = None, output_file: Optional[Path] = None, dry_run: bool = False
) -> None:
"""
Replaces atparse @[] tokens with Jinja2 {{}} equivalents.
Expand Down
16 changes: 8 additions & 8 deletions src/uwtools/config/formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from abc import ABC, abstractmethod
from collections import UserDict
from copy import deepcopy
from pathlib import Path
from typing import List, Optional, Tuple, Union

import yaml
Expand All @@ -14,7 +15,6 @@
from uwtools.config.support import INCLUDE_TAG, depth, log_and_error
from uwtools.exceptions import UWConfigError
from uwtools.logging import log
from uwtools.types import OptionalPath


class Config(ABC, UserDict):
Expand All @@ -23,7 +23,7 @@ class Config(ABC, UserDict):
several configuration-file formats.
"""

def __init__(self, config: Union[dict, OptionalPath] = None) -> None:
def __init__(self, config: Optional[Union[dict, Path]] = None) -> None:
"""
Construct a Config object.
Expand All @@ -34,7 +34,7 @@ def __init__(self, config: Union[dict, OptionalPath] = None) -> None:
self._config_file = None
self.update(config)
else:
self._config_file = str(config) if config else None
self._config_file = config if config else None
self.data = self._load(self._config_file)
if self.get_depth_threshold() and self.depth != self.get_depth_threshold():
raise UWConfigError(
Expand All @@ -51,7 +51,7 @@ def __repr__(self) -> str:
# Private methods

@abstractmethod
def _load(self, config_file: OptionalPath) -> dict:
def _load(self, config_file: Optional[Path]) -> dict:
"""
Reads and parses a config file.
Expand All @@ -61,7 +61,7 @@ def _load(self, config_file: OptionalPath) -> dict:
:param config_file: Path to config file to load.
"""

def _load_paths(self, config_files: List[str]) -> dict:
def _load_paths(self, config_files: List[Path]) -> dict:
"""
Merge and return the contents of a collection of config files.
Expand All @@ -71,7 +71,7 @@ def _load_paths(self, config_files: List[str]) -> dict:
for config_file in config_files:
if not os.path.isabs(config_file):
if self._config_file:
config_file = os.path.join(os.path.dirname(self._config_file), config_file)
config_file = self._config_file.parent / config_file
else:
raise log_and_error(
"Reading from stdin, a relative path was encountered: %s" % config_file
Expand Down Expand Up @@ -179,7 +179,7 @@ def logstate(state: str) -> None:
logstate("final")

@abstractmethod
def dump(self, path: OptionalPath) -> None:
def dump(self, path: Optional[Path]) -> None:
"""
Dumps the config to stdout or a file.
Expand All @@ -195,7 +195,7 @@ def get_depth_threshold() -> Optional[int]:

@staticmethod
@abstractmethod
def dump_dict(cfg: dict, path: OptionalPath = None) -> None:
def dump_dict(cfg: dict, path: Optional[Path] = None) -> None:
"""
Dumps a provided config dictionary to stdout or a file.
Expand Down
7 changes: 4 additions & 3 deletions src/uwtools/config/formats/fieldtable.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pathlib import Path
from typing import Optional

from uwtools.config.formats.yaml import YAMLConfig
from uwtools.utils.file import FORMAT, OptionalPath, writable
from uwtools.utils.file import FORMAT, writable


class FieldTableConfig(YAMLConfig):
Expand All @@ -12,7 +13,7 @@ class FieldTableConfig(YAMLConfig):

# Public methods

def dump(self, path: OptionalPath = None) -> None:
def dump(self, path: Optional[Path] = None) -> None:
"""
Dumps the config in Field Table format.
Expand All @@ -21,7 +22,7 @@ def dump(self, path: OptionalPath = None) -> None:
self.dump_dict(self.data, path)

@staticmethod
def dump_dict(cfg: dict, path: OptionalPath = None) -> None:
def dump_dict(cfg: dict, path: Optional[Path] = None) -> None:
"""
Dumps a provided config dictionary in Field Table format.
Expand Down
Loading

0 comments on commit ea8e7e8

Please sign in to comment.