Skip to content

Commit

Permalink
Shortcut for context creation (executor.contexts.create_context())
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Apr 9, 2016
1 parent 3cb5171 commit f8b8ad8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions executor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Programmer friendly subprocess wrapper.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: April 7, 2016
# Last Change: April 9, 2016
# URL: https://executor.readthedocs.org

"""
Expand Down Expand Up @@ -64,7 +64,7 @@
unicode = str

# Semi-standard module versioning.
__version__ = '9.6.1'
__version__ = '9.7'

# Initialize a logger.
logger = logging.getLogger(__name__)
Expand Down
21 changes: 20 additions & 1 deletion executor/contexts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Programmer friendly subprocess wrapper.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: April 3, 2016
# Last Change: April 9, 2016
# URL: https://executor.readthedocs.org

r"""
Expand Down Expand Up @@ -76,6 +76,25 @@ def details_about_system(context):
logger = logging.getLogger(__name__)


def create_context(**options):
"""
Create an execution context.
:param options: Any keyword arguments are passed on to the context's initializer.
:returns: A :class:`LocalContext` or :class:`RemoteContext` object.
This function provides an easy to use shortcut for constructing context
objects: If the keyword argument ``ssh_alias`` is given (and not
:data:`None`) then a :class:`RemoteContext` object will be created,
otherwise a :class:`LocalContext` object is created.
"""
ssh_alias = options.pop('ssh_alias', None)
if ssh_alias is not None:
return RemoteContext(ssh_alias, **options)
else:
return LocalContext(**options)


class AbstractContext(object):

"""
Expand Down
13 changes: 11 additions & 2 deletions executor/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Automated tests for the `executor' module.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: April 3, 2016
# Last Change: April 9, 2016
# URL: https://executor.readthedocs.org

"""
Expand Down Expand Up @@ -73,7 +73,7 @@
)
from executor.cli import main
from executor.concurrent import CommandPool, CommandPoolFailed
from executor.contexts import LocalContext, RemoteContext
from executor.contexts import LocalContext, RemoteContext, create_context
from executor.ssh.client import (
DEFAULT_CONNECT_TIMEOUT,
RemoteCommand,
Expand Down Expand Up @@ -709,6 +709,15 @@ def test_foreach_with_logging(self):
assert len(log_files) == len(ssh_aliases)
assert all(os.path.getsize(os.path.join(directory, fn)) > 0 for fn in log_files)

def test_create_context(self):
"""Test context creation."""
assert isinstance(create_context(), LocalContext)
assert isinstance(create_context(ssh_alias=None), LocalContext)
assert isinstance(create_context(ssh_alias='whatever'), RemoteContext)
assert create_context(ssh_alias='whatever').ssh_alias == 'whatever'
assert create_context(sudo=True).options['sudo'] is True
assert create_context(sudo=False).options['sudo'] is False

def test_local_context(self):
"""Test a local command context."""
self.check_context(LocalContext())
Expand Down

0 comments on commit f8b8ad8

Please sign in to comment.