Add batch command-context manager on top of run-context manager #532
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is a Draft-PR because it is against another PR.
This PR adds a
batchcommand
-context manager. Abatchcommand
-context manager starts a subprocess and supports batch-style communication with it. That means, the user can send a request to the subprocess and receive the response from the subprocess.How bytes that are read from the subprocess are assembled into a response is determined by the protocol that is used when the
batchcommand
-context manager is created. Typical responses would be single lines of text or JSON-encoded data.The PR contains three convenience functions that provide context-manager for common interaction patterns (common in
datalad
anddatalad_next
anyways):stdout_batchcommand
: this context-manager writes tostdin
of the subprocess and returns the next, yet unread, chunk of bytes that was written tostdout
of the subprocess.stdoutline_batchcommand
: this context-manager writes tostdin
of the subprocess and returns the next, yet unread, line that was written tostdout
of the subprocess. Line-separators and whether they are included in the output can be chosen when creating the context-manager.annexjson_batchcommand
: this context-mananger writes tostdin
of the subprocess and returns a JSON-object, that is created from the next, yet unread, line that is written tostdout
of the subprocess. This context-mananger is geared towards git-annex batch commands with JSON-result output, i.e. git-annex invocations with the parameters--batch
,--json
, or--json-error-messages
.The tests contain an example for a protocol,
PythonProtocol
that handles output from an interactive python interpreter, which varies in size.