Skip to content

Commit 0fb37ee

Browse files
rename execute_command to __call__
This commit addresses reviewer comment: <datalad#596 (comment)> It renames `ShellCommandExecutor.execute_command` to `ShellCommandExecutor.__call__`. With that change the following code becomes possible: >>> with shell_connection(['bash']) as shell: ... r = shell(b'uname')
1 parent d08a859 commit 0fb37ee

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

datalad_next/utils/shell_connection.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def train(queue: Queue):
7272
cmd_executor = ShellCommandExecutor(subprocess_inputs, shell_output)
7373
try:
7474
# Skip initial login messages
75-
result_0 = cmd_executor.execute_command(b'test')
75+
result_0 = cmd_executor(b'test')
7676
for line in result_0:
7777
lgr.debug('skipped login message line: %s', line)
7878
if result_0.returncode != 1:
@@ -118,11 +118,11 @@ def __init__(self,
118118
self.stdout = stdout
119119
self.end_marker, self.command_postfix = self._get_marker()
120120

121-
def execute_command(self,
122-
command: bytes,
123-
*,
124-
stdin: Iterable[bytes] | None = None,
125-
) -> ShellCommandResponseGenerator:
121+
def __call__(self,
122+
command: bytes,
123+
*,
124+
stdin: Iterable[bytes] | None = None,
125+
) -> ShellCommandResponseGenerator:
126126
"""Execute a command in the connected shell
127127
128128
Parameters
@@ -181,7 +181,7 @@ def upload(self,
181181
file_size = local_path.stat().st_size
182182
cmd_line = f'dd bs=1 of="{remote_path.as_posix()}" count={file_size}'
183183
with local_path.open('rb') as local_file:
184-
result = self.execute_command(
184+
result = self(
185185
cmd_line.encode(),
186186
stdin=iter(local_file.read, b''))
187187
consume(result)
@@ -213,7 +213,7 @@ def download(self,
213213
# wire. That ensures that the end-marker is not in the downloaded data,
214214
# even if it is contained in the remote file.
215215
cmd_line = f'7z a dummy -tgzip -so "{remote_path.as_posix()}"'
216-
result = self.execute_command(cmd_line.encode())
216+
result = self(cmd_line.encode())
217217
dco = decompressobj(wbits=31)
218218
with local_path.open('wb') as local_file:
219219
for chunk in result:
@@ -247,7 +247,7 @@ def delete(self,
247247
'rm ' \
248248
+ ('-f ' if force else '') \
249249
+ ' '.join(f'"{f.as_posix()}"' for f in files)
250-
result = self.execute_command(cmd_line.encode())
250+
result = self(cmd_line.encode())
251251
consume(result)
252252
return result.returncode, b''.join(result.stderr_deque)
253253

datalad_next/utils/tests/test_shell_connection.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ def test_basic_functionality_multi(sshserver):
4343

4444

4545
def _check_ls_result(ssh_executor, file_name: bytes):
46-
results = ssh_executor.execute_command(b'ls ' + file_name)
46+
results = ssh_executor(b'ls ' + file_name)
4747
assert b''.join(results) == file_name + b'\n'
4848
assert results.returncode == 0
4949

5050

5151
def test_return_code_functionality(sshserver):
5252
ssh_url = sshserver[0]
5353
with shell_connection(_get_cmdline(ssh_url)[0]) as ssh:
54-
results = ssh.execute_command(b'bash -c "exit 123"')
54+
results = ssh(b'bash -c "exit 123"')
5555
consume(results)
5656
assert results.returncode == 123
5757

@@ -77,7 +77,7 @@ def test_stdout_forwarding_multi(sshserver):
7777

7878

7979
def _check_echo_result(ssh: ShellCommandExecutor, cmd: bytes, expected: bytes):
80-
results = ssh.execute_command(cmd)
80+
results = ssh(cmd)
8181
assert b''.join(results) == expected
8282
assert results.returncode == 0
8383

@@ -94,7 +94,7 @@ def test_exit_if_unlimited_stdin_is_closed(sshserver):
9494
shell_connection(ssh_args) as ssh_executor, \
9595
iter_subproc([sys.executable, '-c', 'print("0123456789")']) as cat_feed:
9696

97-
results = ssh_executor.execute_command(b'cat >' + ssh_path, stdin=cat_feed)
97+
results = ssh_executor(b'cat >' + ssh_path, stdin=cat_feed)
9898
ssh_executor.close()
9999
consume(results)
100100
assert results.returncode == 0
@@ -115,7 +115,7 @@ def test_continuation_after_stdin_reading(sshserver):
115115

116116
for file_name, feed in (('dd-123', dd_feed_1), ('dd-456', dd_feed_2)):
117117
server_path = (ssh_path + '/' + file_name).encode()
118-
results = ssh_executor.execute_command(
118+
results = ssh_executor(
119119
b'dd bs=1 count=10 of=' + server_path,
120120
stdin=feed)
121121
consume(results)

0 commit comments

Comments
 (0)