Skip to content

Commit

Permalink
mincmorph
Browse files Browse the repository at this point in the history
  • Loading branch information
jennydaman committed Mar 4, 2022
1 parent 2b747ed commit 19f30c2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions civet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
'memoization',
'abstract_data',
'memoization',
'shells'
]
5 changes: 3 additions & 2 deletions civet/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from typing import Sequence, TypeVar, Generic, Callable
from dataclasses import dataclass
from civet.abstract_data import AbstractDataCommand
from civet.memoization import Session, subprocess_run
from civet.memoization import Session
from civet.shells import Shell, subprocess_run


@dataclass(frozen=True)
Expand All @@ -17,7 +18,7 @@ class DataSource(AbstractDataCommand, abc.ABC):
"""
def save(self, output: str | PathLike,
require_output: bool = True,
shell: Callable[[Sequence[str | PathLike]], None] = subprocess_run) -> None:
shell: Shell = subprocess_run) -> None:
r"""
Save the result of this command to the given output path.
Expand Down
18 changes: 6 additions & 12 deletions civet/memoization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@
Manual control of memoization features.
"""

from dataclasses import dataclass, field
from os import PathLike
from pathlib import Path
from typing import ContextManager, Sequence, Callable, NewType
from dataclasses import dataclass, field
from tempfile import NamedTemporaryFile, TemporaryDirectory
from typing import ContextManager, Sequence, Callable, NewType

from civet.abstract_data import AbstractDataCommand
import subprocess
from civet.shells import Shell, subprocess_run

_IntermediatePath = NewType('IntermediatePath', Path)


def subprocess_run(cmd: Sequence[str | PathLike]) -> None:
"""
Alias for `subprocess.run(cmd, check=True)`
"""
subprocess.run(cmd, check=True)


@dataclass(frozen=True)
class Memoizer:
"""
Expand All @@ -41,7 +35,7 @@ class Memoizer:
"""

temp_dir: Path
shell: Callable[[Sequence[str | PathLike[str]]], None]
shell: Shell
require_output: bool = True
_cache: dict[AbstractDataCommand, _IntermediatePath] = field(init=False, default_factory=dict)

Expand Down Expand Up @@ -111,7 +105,7 @@ class Session(ContextManager[Memoizer]):
"""
If True, raise `NoOutputError` if a command fails to produce output to its given path.
"""
shell: Callable[[Sequence[str | PathLike]], None] = subprocess_run
shell: Shell = subprocess_run
"""
A function which executes its parameters as a subprocess.
"""
Expand Down
24 changes: 24 additions & 0 deletions civet/shells.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Wrappers around
[`subprocess.run`](https://docs.python.org/3/library/subprocess.html#subprocess.run).
"""

import subprocess as sp
from os import PathLike
from typing import Sequence, Callable

Shell = Callable[[Sequence[str | PathLike]], None]


def subprocess_run(cmd: Sequence[str | PathLike]) -> None:
"""
Alias for `subprocess.run(cmd, check=True)`
"""
sp.run(cmd, check=True)


def quiet(cmd: Sequence[str | PathLike]) -> None:
"""
Similar to `subprocess_run` but output streams are piped to `/dev/null`.
"""
sp.run(cmd, check=True, stdout=sp.DEVNULL, stderr=sp.STDOUT)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name='pycivet',
version='0.0.4',
version='0.0.5',
description='Object-oriented CIVET bindings for Python',
long_description=readme,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 19f30c2

Please sign in to comment.