Skip to content

Commit 1230997

Browse files
committed
Improve coerce_context() documentation
1 parent 6f613f9 commit 1230997

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

linux_utils/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# linux-utils: Linux system administration tools for Python.
22
#
33
# Author: Peter Odding <[email protected]>
4-
# Last Change: July 3, 2018
4+
# Last Change: February 9, 2020
55
# URL: https://linux-utils.readthedocs.io
66

77
"""Linux system administration tools for Python."""
@@ -30,13 +30,26 @@
3030

3131
def coerce_context(value):
3232
"""
33-
Coerce a value to an execution context.
33+
Coerce the given value to an execution context.
3434
3535
:param value: The value to coerce (an execution context created
3636
by :mod:`executor.contexts` or :data:`None`).
3737
:returns: An execution context created by :mod:`executor.contexts`.
3838
:raises: :exc:`~exceptions.ValueError` when `value` isn't :data:`None`
3939
but also isn't a valid execution context.
40+
41+
This function is used throughout the modules in the :mod:`linux_utils`
42+
package to abstract away the details of external command execution using
43+
`dependency injection`_:
44+
45+
- If no execution context is provided (`value` is :data:`None`)
46+
:class:`executor.contexts.LocalContext` is used by default.
47+
48+
- Callers can provide :class:`executor.contexts.RemoteContext` in
49+
which case the :mod:`linux_utils` package seamlessly adapts to
50+
command execution on a remote system over :man:`ssh`.
51+
52+
.. _dependency injection: https://en.wikipedia.org/wiki/Dependency_injection
4053
"""
4154
if value is None:
4255
value = LocalContext()

linux_utils/crypttab.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# linux-utils: Linux system administration tools for Python.
22
#
33
# Author: Peter Odding <[email protected]>
4-
# Last Change: July 3, 2018
4+
# Last Change: February 9, 2020
55
# URL: https://linux-utils.readthedocs.io
66

77
"""
@@ -55,8 +55,7 @@ def parse_crypttab(filename='/etc/crypttab', context=None):
5555
5656
:param filename: The absolute pathname of the file to parse (a string,
5757
defaults to ``/etc/crypttab``).
58-
:param context: An execution context created by :mod:`executor.contexts`
59-
(coerced using :func:`.coerce_context()`).
58+
:param context: See :func:`.coerce_context()` for details.
6059
:returns: A generator of :class:`EncryptedFileSystemEntry` objects.
6160
6261
Here's an example:

linux_utils/fstab.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ def find_mounted_filesystems(filename='/proc/mounts', context=None):
3939
4040
:param filename: The absolute pathname of the file to parse (a string,
4141
defaults to ``/proc/mounts``).
42-
:param context: An execution context created by :mod:`executor.contexts`
43-
(coerced using :func:`.coerce_context()`).
42+
:param context: See :func:`.coerce_context()` for details.
4443
:returns: A generator of :class:`FileSystemEntry` objects.
4544
4645
This function is a trivial wrapper for :func:`parse_fstab()` that instructs
@@ -81,8 +80,7 @@ def parse_fstab(filename='/etc/fstab', context=None):
8180
8281
:param filename: The absolute pathname of the file to parse (a string,
8382
defaults to `/etc/fstab`_).
84-
:param context: An execution context created by :mod:`executor.contexts`
85-
(coerced using :func:`.coerce_context()`).
83+
:param context: See :func:`.coerce_context()` for details.
8684
:returns: A generator of :class:`FileSystemEntry` objects.
8785
8886
Here's an example:

linux_utils/luks.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ def create_image_file(filename, size, context=None):
8989
9090
:param filename: The pathname of the image file (a string).
9191
:param size: How large the image file should be (see :func:`.coerce_size()`).
92-
:param context: An execution context created by :mod:`executor.contexts`
93-
(coerced using :func:`.coerce_context()`).
92+
:param context: See :func:`.coerce_context()` for details.
9493
:raises: :exc:`~exceptions.ValueError` when `size` is invalid,
9594
:exc:`~executor.ExternalCommandFailed` when the command fails.
9695
"""
@@ -108,8 +107,7 @@ def generate_key_file(filename, size=DEFAULT_KEY_SIZE, context=None):
108107
:param filename: The pathname of the key file (a string).
109108
:param size: How large the key file should be (see :func:`.coerce_size()`,
110109
defaults to :data:`DEFAULT_KEY_SIZE`).
111-
:param context: An execution context created by :mod:`executor.contexts`
112-
(coerced using :func:`.coerce_context()`).
110+
:param context: See :func:`.coerce_context()` for details.
113111
:raises: :exc:`~executor.ExternalCommandFailed` when the command fails.
114112
"""
115113
context = coerce_context(context)
@@ -134,8 +132,7 @@ def create_encrypted_filesystem(device_file, key_file=None, context=None):
134132
:param device_file: The pathname of the block special device or file (a string).
135133
:param key_file: The pathname of the key file used to encrypt the
136134
filesystem (a string or :data:`None`).
137-
:param context: An execution context created by :mod:`executor.contexts`
138-
(coerced using :func:`.coerce_context()`).
135+
:param context: See :func:`.coerce_context()` for details.
139136
:raises: :exc:`~executor.ExternalCommandFailed` when the command fails.
140137
141138
If no `key_file` is given the operator is prompted to choose a password.
@@ -164,8 +161,7 @@ def unlock_filesystem(device_file, target, key_file=None, options=None, context=
164161
:data:`None` (in which case the default options are used).
165162
Currently 'discard', 'readonly' and 'tries' are the only
166163
supported options (other options are silently ignored).
167-
:param context: An execution context created by :mod:`executor.contexts`
168-
(coerced using :func:`.coerce_context()`).
164+
:param context: See :func:`.coerce_context()` for details.
169165
:raises: :exc:`~executor.ExternalCommandFailed` when the command fails.
170166
171167
If no `key_file` is given the operator is prompted to enter a password.
@@ -205,8 +201,7 @@ def lock_filesystem(target, context=None):
205201
Lock a currently unlocked LUKS filesystem.
206202
207203
:param target: The mapped device name (a string).
208-
:param context: An execution context created by :mod:`executor.contexts`
209-
(coerced using :func:`.coerce_context()`).
204+
:param context: See :func:`.coerce_context()` for details.
210205
:raises: :exc:`~executor.ExternalCommandFailed` when the command fails.
211206
"""
212207
context = coerce_context(context)
@@ -220,8 +215,7 @@ def cryptdisks_start(target, context=None):
220215
Execute :man:`cryptdisks_start` or emulate its functionality.
221216
222217
:param target: The mapped device name (a string).
223-
:param context: An execution context created by :mod:`executor.contexts`
224-
(coerced using :func:`.coerce_context()`).
218+
:param context: See :func:`.coerce_context()` for details.
225219
:raises: :exc:`~executor.ExternalCommandFailed` when a command fails,
226220
:exc:`~exceptions.ValueError` when no entry in `/etc/crypttab`_
227221
matches `target`.
@@ -255,8 +249,7 @@ def cryptdisks_stop(target, context=None):
255249
Execute :man:`cryptdisks_stop` or emulate its functionality.
256250
257251
:param target: The mapped device name (a string).
258-
:param context: An execution context created by :mod:`executor.contexts`
259-
(coerced using :func:`.coerce_context()`).
252+
:param context: See :func:`.coerce_context()` for details.
260253
:raises: :exc:`~executor.ExternalCommandFailed` when a command fails,
261254
:exc:`~exceptions.ValueError` when no entry in `/etc/crypttab`_
262255
matches `target`.

linux_utils/tabfile.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# linux-utils: Linux system administration tools for Python.
22
#
33
# Author: Peter Odding <[email protected]>
4-
# Last Change: June 24, 2017
4+
# Last Change: February 9, 2020
55
# URL: https://linux-utils.readthedocs.io
66

77
"""Generic parsing of Linux configuration files like ``/etc/fstab`` and ``/etc/crypttab``."""
@@ -27,8 +27,7 @@ def parse_tab_file(filename, context=None, encoding='UTF-8'):
2727
Parse a Linux configuration file like ``/etc/fstab`` or ``/etc/crypttab``.
2828
2929
:param filename: The absolute pathname of the file to parse (a string).
30-
:param context: An execution context created by :mod:`executor.contexts`
31-
(coerced using :func:`.coerce_context()`).
30+
:param context: See :func:`.coerce_context()` for details.
3231
:param encoding: The name of the text encoding of the file (a string).
3332
:returns: A generator of :class:`TabFileEntry` objects.
3433

0 commit comments

Comments
 (0)