Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add run_MODIFIER methods to the command object #386

Merged
merged 2 commits into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions plumbum/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import functools
from contextlib import contextmanager
from plumbum.commands.processes import run_proc, iter_lines
import plumbum.commands.modifiers
from plumbum.lib import six
from tempfile import TemporaryFile
from subprocess import PIPE, Popen
Expand Down Expand Up @@ -224,6 +225,57 @@ def run(self, args = (), **kwargs):
with self.bgrun(args, **kwargs) as p:
return p.run()

def _use_modifier(self, modifier, args):
"""
Applies a modifier to the current object (e.g. FG, NOHUP)
:param modifier: The modifier class to apply (e.g. FG)
:param args: A dictionary of arguments to pass to this modifier
:return:
"""
modifier_instance = modifier(**args)
return self & modifier_instance

def run_bg(self, **kwargs):
"""
Run this command in the background. Uses all arguments from the BG construct
:py:class: `plumbum.commands.modifiers.BG`
"""
return self._use_modifier(plumbum.commands.modifiers.BG, kwargs)

def run_fg(self, **kwargs):
"""
Run this command in the foreground. Uses all arguments from the FG construct
:py:class: `plumbum.commands.modifiers.FG`
"""
return self._use_modifier(plumbum.commands.modifiers.FG, kwargs)

def run_tee(self, **kwargs):
"""
Run this command using the TEE construct. Inherits all arguments from TEE
:py:class: `plumbum.commands.modifiers.TEE`
"""
return self._use_modifier(plumbum.commands.modifiers.TEE, kwargs)

def run_tf(self, **kwargs):
"""
Run this command using the TF construct. Inherits all arguments from TF
:py:class: `plumbum.commands.modifiers.TF`
"""
return self._use_modifier(plumbum.commands.modifiers.TF, kwargs)

def run_retcode(self, **kwargs):
"""
Run this command using the RETCODE construct. Inherits all arguments from RETCODE
:py:class: `plumbum.commands.modifiers.RETCODE`
"""
return self._use_modifier(plumbum.commands.modifiers.RETCODE, kwargs)

def run_nohup(self, **kwargs):
"""
Run this command using the NOHUP construct. Inherits all arguments from NOHUP
:py:class: `plumbum.commands.modifiers.NOHUP`
"""
return self._use_modifier(plumbum.commands.modifiers.NOHUP, kwargs)

class BoundCommand(BaseCommand):
__slots__ = ("cmd", "args")
Expand Down
6 changes: 3 additions & 3 deletions plumbum/commands/modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from itertools import chain

from plumbum.commands.processes import run_proc, ProcessExecutionError
from plumbum.commands.base import AppendingStdoutRedirection, StdoutRedirection
import plumbum.commands.base
from plumbum.lib import read_fd_decode_safely


Expand Down Expand Up @@ -310,11 +310,11 @@ def __init__(self, cwd='.', stdout='nohup.out', stderr=None, append=True):
self.append = append

def __rand__(self, cmd):
if isinstance(cmd, StdoutRedirection):
if isinstance(cmd, plumbum.commands.base.StdoutRedirection):
stdout = cmd.file
append = False
cmd = cmd.cmd
elif isinstance(cmd, AppendingStdoutRedirection):
elif isinstance(cmd, plumbum.commands.base.AppendingStdoutRedirection):
stdout = cmd.file
append = True
cmd = cmd.cmd
Expand Down
Loading