Skip to content

Commit eed39e4

Browse files
authored
API update (#956)
1 parent 2b45e9b commit eed39e4

6 files changed

Lines changed: 41 additions & 10 deletions

File tree

docs/sections/user_guide/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ API
3535
ungrib
3636
upp
3737
upp_assets
38+
utils
3839
ww3
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``uwtools.api.utils``
2+
=====================
3+
4+
.. automodule:: uwtools.api.utils
5+
:members:

src/uwtools/api/driver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
DriverCycleLeadtimeBased,
1717
DriverTimeInvariant,
1818
)
19+
from uwtools.drivers.stager import FileStager
1920

2021

2122
def yaml_keys_to_classes() -> dict[str, type]:
@@ -46,5 +47,6 @@ def yaml_keys_to_classes() -> dict[str, type]:
4647
"DriverCycleBased",
4748
"DriverCycleLeadtimeBased",
4849
"DriverTimeInvariant",
50+
"FileStager",
4951
"yaml_keys_to_classes",
5052
]

src/uwtools/api/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
API access to utility functions.
3+
"""
4+
5+
from uwtools.utils.processing import run_shell_cmd
6+
7+
__all__ = ["run_shell_cmd"]

src/uwtools/drivers/stager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class FileStager:
1212
"""
13-
A base class for tasks that stage files.
13+
A mixin class providing tasks to stage files.
1414
"""
1515

1616
@collection

src/uwtools/utils/processing.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,44 @@
1616

1717
def run_shell_cmd(
1818
cmd: str | list[str],
19+
callback: Callable[[Popen], None] | None = None,
1920
cwd: Path | str | None = None,
2021
env: dict[str, str] | None = None,
21-
start_new_session: bool = False,
22-
callback: Callable[[Popen], None] | None = None,
2322
executable: str | None = None,
2423
log_output: bool = False,
2524
quiet: bool = False,
25+
start_new_session: bool = False,
2626
taskname: str | None = None,
2727
) -> tuple[bool, str]:
2828
"""
2929
Run a command in a shell.
3030
3131
:param cmd: The command to run.
32-
:param cwd: Change to this directory before running cmd.
33-
:param env: Environment variables to set before running cmd.
32+
:param callback: Called with the ``Popen`` process object.
33+
:param cwd: Change to this directory before running the command.
34+
:param env: Environment variables to set before running the command.
35+
:param executable: Interpreter to run command in (e.g. "/bin/bash").
36+
:param log_output: Log output from successful command? (Error output is always logged.)
37+
:param quiet: Log ``INFO`` messages as ``DEBUG``.
3438
:param start_new_session: Run process in a new session?
35-
:param callback: Optional callable, called with the Popen process object.
36-
:param executable: Interpreter to use (e.g. "/bin/bash")
37-
:param log_output: Log output from successful cmd? (Error output is always logged.)
38-
:param quiet: Log INFO messages as DEBUG.
3939
:param taskname: Name of task executing this command, for logging.
40-
:return: A result object providing combined stder/stdout output and success values.
40+
:return: Success indication and combined ``stdout`` / ``stderr``.
41+
42+
The ``Callable`` specified by the ``callback`` argument will be called once with the ``Popen``
43+
object as soon as it is available; i.e. it may be called while the command is running, and does
44+
not wait for the command to complete.
45+
46+
Use ``executable`` to use a shell other than the ``subprocess.Popen`` default, ``/bin/sh``.
47+
48+
It is assumed that ``uwtools.api.loggin.use_uwtools_logger`` has previously been called to set
49+
up logging if ``log_output`` is ``True``.
50+
51+
The ``start_new_session`` argument is connected directly to the argument of the same name when
52+
``subprocess.Popen`` is called. Among other potential use cases, it prevents the command from
53+
receiving a ``SIGINT`` generated by a user Ctrl-C, useful when the command is running in a
54+
thread and the main control thread should handle the interrupt.
55+
56+
If ``taskname`` is provided, log messages will be prefixed with ``[<taskname>]``.
4157
"""
4258
pre = f"[{taskname}] " if taskname else ""
4359
logfunc = log.debug if quiet else log.info

0 commit comments

Comments
 (0)