Skip to content

Commit 31bf59b

Browse files
authored
Fix documentation (docstrings and RST) (#71)
* Fix documentation (docstrings and RST) * cleanup last part of py2.7 support
1 parent 06e8de5 commit 31bf59b

File tree

12 files changed

+88
-86
lines changed

12 files changed

+88
-86
lines changed

README.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ This methods are almost the same for `SSHCleint` and `Subprocess`, except specif
129129
result = helper.execute(
130130
command, # type: str
131131
verbose=False, # type: bool
132-
timeout=1 * 60 * 60, # type: typing.Optional[int]
132+
timeout=1 * 60 * 60, # type: typing.Union[int, float, None]
133133
**kwargs
134134
)
135135
@@ -139,7 +139,7 @@ This methods are almost the same for `SSHCleint` and `Subprocess`, except specif
139139
result = helper.check_call(
140140
command, # type: str
141141
verbose=False, # type: bool
142-
timeout=1 * 60 * 60, # type: typing.Optional[int]
142+
timeout=1 * 60 * 60, # type: type: typing.Union[int, float, None]
143143
error_info=None, # type: typing.Optional[str]
144144
expected=None, # type: typing.Optional[typing.Iterable[int]]
145145
raise_on_err=True, # type: bool
@@ -151,7 +151,7 @@ This methods are almost the same for `SSHCleint` and `Subprocess`, except specif
151151
result = helper.check_stderr(
152152
command, # type: str
153153
verbose=False, # type: bool
154-
timeout=1 * 60 * 60, # type: typing.Optional[int]
154+
timeout=1 * 60 * 60, # type: type: typing.Union[int, float, None]
155155
error_info=None, # type: typing.Optional[str]
156156
raise_on_err=True, # type: bool
157157
)
@@ -187,10 +187,10 @@ Execution result object has a set of useful properties:
187187
* `stderr` -> `typing.Tuple[bytes]`. Raw stderr output.
188188
* `stdout_bin` -> `bytearray`. Binary stdout output.
189189
* `stderr_bin` -> `bytearray`. Binary stderr output.
190-
* `stdout_str` -> `six.text_types`. Text representation of output.
191-
* `stderr_str` -> `six.text_types`. Text representation of output.
192-
* `stdout_brief` -> `six.text_types`. Up to 7 lines from stdout (3 first and 3 last if >7 lines).
193-
* `stderr_brief` -> `six.text_types`. Up to 7 lines from stderr (3 first and 3 last if >7 lines).
190+
* `stdout_str` -> `str`. Text representation of output.
191+
* `stderr_str` -> `str`. Text representation of output.
192+
* `stdout_brief` -> `str`. Up to 7 lines from stdout (3 first and 3 last if >7 lines).
193+
* `stderr_brief` -> `str`. Up to 7 lines from stderr (3 first and 3 last if >7 lines).
194194

195195
* `stdout_json` - STDOUT decoded as JSON.
196196

@@ -211,7 +211,7 @@ Possible to call commands in parallel on multiple hosts if it's not produce huge
211211
results = SSHClient.execute_together(
212212
remotes, # type: typing.Iterable[SSHClient]
213213
command, # type: str
214-
timeout=1 * 60 * 60, # type: typing.Optional[int]
214+
timeout=1 * 60 * 60, # type: type: typing.Union[int, float, None]
215215
expected=None, # type: typing.Optional[typing.Iterable[int]]
216216
raise_on_err=True # type: bool
217217
)
@@ -229,7 +229,7 @@ For execute through SSH host can be used `execute_through_host` method:
229229
command, # type: str
230230
auth=None, # type: typing.Optional[SSHAuth]
231231
target_port=22, # type: int
232-
timeout=1 * 60 * 60, # type: typing.Optional[int]
232+
timeout=1 * 60 * 60, # type: type: typing.Union[int, float, None]
233233
verbose=False, # type: bool
234234
get_pty=False, # type: bool
235235
)

doc/source/ExecResult.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ API: ExecResult
1515
:param cmd: command
1616
:type cmd: ``str``
1717
:param stdin: STDIN
18-
:type stdin: ``typing.Optional[str]``
18+
:type stdin: ``typing.Union[bytes, str, bytearray, None]``
1919
:param stdout: binary STDOUT
2020
:type stdout: ``typing.Optional[typing.Iterable[bytes]]``
2121
:param stderr: binary STDERR
@@ -40,17 +40,17 @@ API: ExecResult
4040

4141
.. py:attribute:: stdin
4242
43-
``typing.Text``
43+
``typing.Optional[str]``
4444
Stdin input as string.
4545

4646
.. py:attribute:: stdout
4747
48-
``typing.Tuple[bytes]``
48+
``typing.Tuple[bytes, ...]``
4949
Stdout output as list of binaries.
5050

5151
.. py:attribute:: stderr
5252
53-
``typing.Tuple[bytes]``
53+
``typing.Tuple[bytes, ...]``
5454
Stderr output as list of binaries.
5555

5656
.. py:attribute:: stdout_bin
@@ -65,22 +65,22 @@ API: ExecResult
6565

6666
.. py:attribute:: stdout_str
6767
68-
``typing.Text``
68+
``str``
6969
Stdout output as string.
7070

7171
.. py:attribute:: stderr_str
7272
73-
``typing.Text``
73+
``str``
7474
Stderr output as string.
7575

7676
.. py:attribute:: stdout_brief
7777
78-
``typing.Text``
78+
``str``
7979
Brief stdout output (mostly for exceptions).
8080

8181
.. py:attribute:: stderr_brief
8282
83-
``typing.Text``
83+
``str``
8484
Brief stderr output (mostly for exceptions).
8585

8686
.. py:attribute:: exit_code

doc/source/SSHClient.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ API: SSHClient and SSHAuth.
128128
:param command: Command for execution
129129
:type command: ``str``
130130
:param stdin: pass STDIN text to the process
131-
:type stdin: ``typing.Union[typing.AnyStr, bytearray, None]``
131+
:type stdin: ``typing.Union[str, bytes, bytearray, None]``
132132
:param open_stdout: open STDOUT stream for read
133133
:type open_stdout: bool
134134
:param open_stderr: open STDERR stream for read
@@ -153,7 +153,7 @@ API: SSHClient and SSHAuth.
153153
:param verbose: Produce log.info records for command call and output
154154
:type verbose: ``bool``
155155
:param timeout: Timeout for command execution.
156-
:type timeout: ``typing.Union[int, None]``
156+
:type timeout: ``typing.Union[int, float, None]``
157157
:rtype: ExecResult
158158
:raises ExecHelperTimeoutError: Timeout exceeded
159159

@@ -168,7 +168,7 @@ API: SSHClient and SSHAuth.
168168
:param verbose: Produce log.info records for command call and output
169169
:type verbose: ``bool``
170170
:param timeout: Timeout for command execution.
171-
:type timeout: ``typing.Union[int, None]``
171+
:type timeout: ``typing.Union[int, float, None]``
172172
:param error_info: Text for error details, if fail happens
173173
:type error_info: ``typing.Optional[str]``
174174
:param expected: expected return codes (0 by default)
@@ -190,7 +190,7 @@ API: SSHClient and SSHAuth.
190190
:param verbose: Produce log.info records for command call and output
191191
:type verbose: ``bool``
192192
:param timeout: Timeout for command execution.
193-
:type timeout: ``typing.Union[int, None]``
193+
:type timeout: ``typing.Union[int, float, None]``
194194
:param error_info: Text for error details, if fail happens
195195
:type error_info: ``typing.Optional[str]``
196196
:param raise_on_err: Raise exception on unexpected return code
@@ -217,7 +217,7 @@ API: SSHClient and SSHAuth.
217217
:param verbose: Produce log.info records for command call and output
218218
:type verbose: ``bool``
219219
:param timeout: Timeout for command execution.
220-
:type timeout: ``typing.Union[int, None]``
220+
:type timeout: ``typing.Union[int, float, None]``
221221
:param get_pty: open PTY on target machine
222222
:type get_pty: ``bool``
223223
:rtype: ExecResult
@@ -234,7 +234,7 @@ API: SSHClient and SSHAuth.
234234
:param command: Command for execution
235235
:type command: ``str``
236236
:param timeout: Timeout for command execution.
237-
:type timeout: ``typing.Union[int, None]``
237+
:type timeout: ``typing.Union[int, float, None]``
238238
:param expected: expected return codes (0 by default)
239239
:type expected: ``typing.Optional[typing.Iterable[]]``
240240
:param raise_on_err: Raise exception on unexpected return code

doc/source/Subprocess.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ API: Subprocess
4646
:param command: Command for execution
4747
:type command: str
4848
:param stdin: pass STDIN text to the process
49-
:type stdin: ``typing.Union[typing.AnyStr, bytearray, None]``
49+
:type stdin: ``typing.Union[str, bytes, bytearray, None]``
5050
:param open_stdout: open STDOUT stream for read
5151
:type open_stdout: ``bool``
5252
:param open_stderr: open STDERR stream for read
@@ -69,7 +69,7 @@ API: Subprocess
6969
:param verbose: Produce log.info records for command call and output
7070
:type verbose: ``bool``
7171
:param timeout: Timeout for command execution.
72-
:type timeout: ``typing.Union[int, None]``
72+
:type timeout: ``typing.Union[int, float, None]``
7373
:rtype: ExecResult
7474
:raises ExecHelperTimeoutError: Timeout exceeded
7575

@@ -88,7 +88,7 @@ API: Subprocess
8888
:param verbose: Produce log.info records for command call and output
8989
:type verbose: ``bool``
9090
:param timeout: Timeout for command execution.
91-
:type timeout: ``typing.Union[int, None]``
91+
:type timeout: ``typing.Union[int, float, None]``
9292
:param error_info: Text for error details, if fail happens
9393
:type error_info: ``typing.Optional[str]``
9494
:param expected: expected return codes (0 by default)
@@ -111,7 +111,7 @@ API: Subprocess
111111
:param verbose: Produce log.info records for command call and output
112112
:type verbose: ``bool``
113113
:param timeout: Timeout for command execution.
114-
:type timeout: ``typing.Union[int, None]``
114+
:type timeout: ``typing.Union[int, float, None]``
115115
:param error_info: Text for error details, if fail happens
116116
:type error_info: ``typing.Optional[str]``
117117
:param raise_on_err: Raise exception on unexpected return code

doc/source/exceptions.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ API: exceptions
3636

3737
.. py:attribute:: timeout
3838
39-
``int``
39+
``typing.Union[int, float]``
4040

4141
.. py:attribute:: result
4242
@@ -46,12 +46,12 @@ API: exceptions
4646

4747
.. py:attribute:: stdout
4848
49-
``typing.Text``
49+
``str``
5050
stdout string or brief string
5151

5252
.. py:attribute:: stderr
5353
54-
``typing.Text``
54+
``str``
5555
stdout string or brief string
5656

5757
.. py:exception:: CalledProcessError(ExecCalledProcessError)
@@ -92,12 +92,12 @@ API: exceptions
9292

9393
.. py:attribute:: stdout
9494
95-
``typing.Text``
95+
``str``
9696
stdout string or brief string
9797

9898
.. py:attribute:: stderr
9999
100-
``typing.Text``
100+
``str``
101101
stdout string or brief string
102102

103103
.. py:exception:: ParallelCallExceptions(ExecCalledProcessError)

exec_helpers/_ssh_client_base.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
"""SSH client helper based on Paramiko. Base class."""
1818

19+
import abc
1920
import base64
2021
import collections
2122
import concurrent.futures
@@ -58,7 +59,7 @@
5859
CPYTHON = 'CPython' == platform.python_implementation()
5960

6061

61-
class _MemorizedSSH(type):
62+
class _MemorizedSSH(abc.ABCMeta):
6263
"""Memorize metaclass for SSHClient.
6364
6465
This class implements caching and managing of SSHClient connections.
@@ -112,16 +113,23 @@ def __call__( # type: ignore
112113
auth: typing.Optional[ssh_auth.SSHAuth] = None,
113114
verbose: bool = True,
114115
) -> 'SSHClientBase':
115-
"""Main memorize method: check for cached instance and return it.
116+
"""Main memorize method: check for cached instance and return it. API follows target __init__.
116117
118+
:param host: remote hostname
117119
:type host: str
120+
:param port: remote ssh port
118121
:type port: int
119-
:type username: str
120-
:type password: str
121-
:type private_keys: list
122-
:type auth: ssh_auth.SSHAuth
122+
:param username: remote username.
123+
:type username: typing.Optional[str]
124+
:param password: remote password
125+
:type password: typing.Optional[str]
126+
:param private_keys: private keys for connection
127+
:type private_keys: typing.Optional[typing.Iterable[paramiko.RSAKey]]
128+
:param auth: credentials for connection
129+
:type auth: typing.Optional[ssh_auth.SSHAuth]
130+
:param verbose: show additional error/warning messages
123131
:type verbose: bool
124-
:rtype: SSHClient
132+
:rtype: SSHClientBase
125133
"""
126134
if (host, port) in cls.__cache:
127135
key = host, port
@@ -164,7 +172,7 @@ def __call__( # type: ignore
164172
def clear_cache(mcs: typing.Type['_MemorizedSSH']) -> None:
165173
"""Clear cached connections for initialize new instance on next call.
166174
167-
getrefcount is used to check for usage.
175+
getrefcount is used to check for usage, so connections closed on CPYTHON only.
168176
"""
169177
n_count = 3
170178
# PY3: cache, ssh, temporary
@@ -210,8 +218,8 @@ def __init__(
210218
) -> None:
211219
"""Context manager for call commands with sudo.
212220
213-
:type ssh: SSHClient
214-
:type enforce: bool
221+
:type ssh: SSHClientBase
222+
:type enforce: typing.Optional[bool]
215223
"""
216224
self.__ssh = ssh
217225
self.__sudo_status = ssh.sudo_mode
@@ -241,7 +249,7 @@ def __init__(
241249
) -> None:
242250
"""Context manager for keepalive management.
243251
244-
:type ssh: SSHClient
252+
:type ssh: SSHClientBase
245253
:type enforce: bool
246254
:param enforce: Keep connection alive after context manager exit
247255
"""
@@ -465,8 +473,8 @@ def _clear_cache(cls: typing.Type['SSHClientBase']) -> None:
465473
def __del__(self) -> None:
466474
"""Destructor helper: close channel and threads BEFORE closing others.
467475
468-
Due to threading in paramiko, default destructor could generate asserts
469-
on close, so we calling channel close before closing main ssh object.
476+
Due to threading in paramiko, default destructor could generate asserts on close,
477+
so we calling channel close before closing main ssh object.
470478
"""
471479
try:
472480
self.__ssh.close()
@@ -540,6 +548,7 @@ def sudo(
540548
541549
:param enforce: Enforce sudo enabled or disabled. By default: None
542550
:type enforce: typing.Optional[bool]
551+
:rtype: typing.ContextManager
543552
"""
544553
return self.__get_sudo(ssh=self, enforce=enforce)
545554

@@ -551,6 +560,7 @@ def keepalive(
551560
552561
:param enforce: Enforce keepalive enabled or disabled.
553562
:type enforce: bool
563+
:rtype: typing.ContextManager
554564
555565
.. Note:: Enter and exit ssh context manager is produced as well.
556566
.. versionadded:: 1.2.1
@@ -572,7 +582,7 @@ def execute_async(
572582
:param command: Command for execution
573583
:type command: str
574584
:param stdin: pass STDIN text to the process
575-
:type stdin: typing.Union[str, bytes, bytearray, None]
585+
:type stdin: typing.Union[bytes, str, bytearray, None]
576586
:param open_stdout: open STDOUT stream for read
577587
:type open_stdout: bool
578588
:param open_stderr: open STDERR stream for read
@@ -623,6 +633,7 @@ def execute_async(
623633
cmd = "sudo -S bash -c 'eval \"$(base64 -d <(echo \"{0}\"))\"'".format(encoded_cmd)
624634
chan.exec_command(cmd) # nosec # Sanitize on caller side
625635
if stdout.channel.closed is False:
636+
# noinspection PyTypeChecker
626637
self.auth.enter_password(_stdin)
627638
_stdin.flush()
628639
else:
@@ -716,6 +727,7 @@ def poll_pipes(stop: threading.Event) -> None:
716727
stop_event = threading.Event()
717728

718729
# pylint: disable=assignment-from-no-return
730+
# noinspection PyNoneFunctionAssignment
719731
future = poll_pipes(stop=stop_event) # type: concurrent.futures.Future
720732
# pylint: enable=assignment-from-no-return
721733

0 commit comments

Comments
 (0)