Last active
May 29, 2018 14:02
-
-
Save sirosen/8f0ffe71e50e5c87daab6b6ba8a3e8cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
python3.5+ ( uses subprocess.run(), added in 3.5 ) | |
USAGE: | |
>>> from cli_invoker import COMMAND, invoke | |
>>> cmd = COMMAND.globus('-F', 'JSON') | |
>>> invoke(cmd.whoami) | |
>>> invoke(cmd.ls('ddb59aef-6d04-11e5-ba46-22000b92c6ec')) | |
To print output from such a handle, it's just a CompletedProcess, so... | |
>>> print(invoke(COMMAND.ls).stdout.decode('unicode_escape')) | |
Something fancy: | |
>>> print(invoke(COMMAND.globus.task.list( | |
... '-F', 'unix', | |
... '--jmespath', 'DATA[*].[status, type, label]') | |
... ).stdout.decode('unicode_escape')) | |
""" | |
import subprocess | |
class _CMD(object): | |
def __init__(self, args): | |
self._args = args | |
def __call__(self, *args): | |
return _CMD(self._args + list(args)) | |
def __getattr__(self, attrname): | |
return _CMD(self._args + [attrname]) | |
def __repr__(self): | |
return str(self._args) | |
COMMAND = _CMD([]) | |
def invoke(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs): | |
return subprocess.run( | |
cmd._args, | |
stdout=stdout, stderr=stderr, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment