repl-session 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- repl_session-0.1.0/LICENSE +201 -0
- repl_session-0.1.0/PKG-INFO +182 -0
- repl_session-0.1.0/README.md +164 -0
- repl_session-0.1.0/pyproject.toml +34 -0
- repl_session-0.1.0/setup.cfg +4 -0
- repl_session-0.1.0/src/repl_session/__init__.py +159 -0
- repl_session-0.1.0/src/repl_session/py.typed +0 -0
- repl_session-0.1.0/src/repl_session.egg-info/PKG-INFO +182 -0
- repl_session-0.1.0/src/repl_session.egg-info/SOURCES.txt +11 -0
- repl_session-0.1.0/src/repl_session.egg-info/dependency_links.txt +1 -0
- repl_session-0.1.0/src/repl_session.egg-info/entry_points.txt +2 -0
- repl_session-0.1.0/src/repl_session.egg-info/requires.txt +3 -0
- repl_session-0.1.0/src/repl_session.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: repl-session
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Runs a session with a Read-Eval-Print-Loop (REPL). Takes YAML as input gives JSON as output.
|
|
5
|
+
Classifier: Development Status :: 3 - Alpha
|
|
6
|
+
Classifier: Environment :: Console
|
|
7
|
+
Classifier: Intended Audience :: Developers
|
|
8
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
9
|
+
Classifier: Topic :: Software Development
|
|
10
|
+
Classifier: Topic :: Utilities
|
|
11
|
+
Requires-Python: >=3.11
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: argh>=0.30.5
|
|
15
|
+
Requires-Dist: msgspec>=0.19.0
|
|
16
|
+
Requires-Dist: pexpect>=4.9.0
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# REPL Session
|
|
20
|
+
|
|
21
|
+
[](https://github.com/entangled/repl-session/actions/workflows/pages.yml)
|
|
22
|
+
[](https://entangled.github.io/repl-session)
|
|
23
|
+
|
|
24
|
+
This is script that runs a session on any REPL following a description in a JSON file. The output contains the commands entered and the results given. This can be useful to drive documentation tests or literate programming tasks. This way we decouple running the commands from rendering or presenting the corresponding results, leading to better reproducibility, caching output and modularity w.r.t. any other tools that you may use.
|
|
25
|
+
|
|
26
|
+
This is very similar to running a Jupyter notebook from the console, with the benefit that you don't need a Jupyter kernel available for the language you're using. The downside is that REPLs can be messy to interact with.
|
|
27
|
+
|
|
28
|
+
## Is this for you?
|
|
29
|
+
|
|
30
|
+
This is only really useful if you're hacking together a literate programming environment similar to [Entangled](https://entangled.github.io). Suppose you have your documentation written in Markdown, ready for rendering with MkDocs or Pandoc. You want to automatically evaluate some expressions in this document as if they're entered in a REPL and process the results for inclusion in the document generator. Here `repl-session` is a nicely confined command-line tool, so its easy to integrate into a build pipeline.
|
|
31
|
+
|
|
32
|
+
## How does it work?
|
|
33
|
+
|
|
34
|
+
The REPL of your choice is started and interacted with through the `pexpect` library. All I/O is dealt with through `msgspec`.
|
|
35
|
+
|
|
36
|
+
The preferred way to solve this is by using `jupyter-client` in combination with an existing Jupyter kernel. However, not all languages have a Jupyter kernel available, and developing one takes a bit more than configuring `pexpect` for an existing REPL. I may still include Jupyter support in this application at a later stage.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
Install with,
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install repl-session
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or equivalent Poetry, Astral Uv, Hatch or Conda commands.
|
|
47
|
+
|
|
48
|
+
## Documentation
|
|
49
|
+
|
|
50
|
+
The full documentation is available at [entangled.github.io/repl-session](https://entangled.github.io/repl-session).
|
|
51
|
+
|
|
52
|
+
## Examples
|
|
53
|
+
|
|
54
|
+
Here are some examples where we could interact with a REPL successfully. In general, the less intricate the REPL the better the results.
|
|
55
|
+
|
|
56
|
+
### Chez Scheme
|
|
57
|
+
|
|
58
|
+
I like to work with [Chez Scheme](https://cisco.github.io/ChezScheme/). Suppose I want to document an interactive session. This can be done:
|
|
59
|
+
|
|
60
|
+
```yaml
|
|
61
|
+
#| file: test/scheme.yml
|
|
62
|
+
config:
|
|
63
|
+
command: "scheme --eedisable"
|
|
64
|
+
first_prompt: "> "
|
|
65
|
+
change_prompt: "(waiter-prompt-string \"{key}>\")"
|
|
66
|
+
next_prompt: "{key}> "
|
|
67
|
+
strip_command: true
|
|
68
|
+
commands:
|
|
69
|
+
- command: (* 6 7)
|
|
70
|
+
- command: |
|
|
71
|
+
(define (fac n init)
|
|
72
|
+
(if (zero? n)
|
|
73
|
+
init
|
|
74
|
+
(fac (- n 1) (* init n)))))
|
|
75
|
+
- command: (fac 10 1)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Passing this to `repl-session`, it will start the Chez Scheme interpreter, waiting for the `>` prompt to appear. It then changes the prompt to a generated `uuid4` code, for instance `27e87a8a-742c-4501-b05d-b05814f5a010> `. This will make sure that we can't accidentally match something else for an interactive prompt (imagine we're generating some XML!). Since commands are also echoed to standard out, we need to strip them from the resulting output. Running this should give:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
repl-session < test/scheme.yml | jq '.commands.[].output'
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
"42"
|
|
86
|
+
"(define (fac n init)\n (if (zero? n)\n init\n (fac (- n 1) (* init n)))))"
|
|
87
|
+
"3628800"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Lua
|
|
91
|
+
|
|
92
|
+
This looks very similar to the previous example:
|
|
93
|
+
|
|
94
|
+
```yaml
|
|
95
|
+
#| file: test/lua.yml
|
|
96
|
+
config:
|
|
97
|
+
command: "lua"
|
|
98
|
+
first_prompt: "> "
|
|
99
|
+
change_prompt: "_PROMPT = \"{key}> \""
|
|
100
|
+
next_prompt: "{key}> "
|
|
101
|
+
strip_command: true
|
|
102
|
+
strip_ansi: true
|
|
103
|
+
commands:
|
|
104
|
+
- command: 6 * 7
|
|
105
|
+
- command: "\"Hello\" .. \", \" .. \"World!\""
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The Lua REPL is not so nice. It sends ANSI escape codes and those need to be filtered out.
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
repl-session < test/lua.yml | jq '.commands.[].output'
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
"42"
|
|
116
|
+
"Hello, World!"
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Input/Output structure
|
|
120
|
+
|
|
121
|
+
The user can configure how the REPL is called and interpreted.
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
#| id: input-data
|
|
125
|
+
class ReplConfig(msgspec.Struct):
|
|
126
|
+
"""Configuration
|
|
127
|
+
|
|
128
|
+
Attributes:
|
|
129
|
+
command (str): Command to start the REPL
|
|
130
|
+
first_prompt (str): Regex to match the first prompt
|
|
131
|
+
change_prompt (str): Command to change prompt; should contain '{key}' as an
|
|
132
|
+
argument.
|
|
133
|
+
next_prompt (str): Regex to match the changed prompts; should contain '{key}'
|
|
134
|
+
as an argument.
|
|
135
|
+
append_newline (bool): Whether to append a newline to given commands.
|
|
136
|
+
strip_command (bool): Whether to strip the original command from the gotten
|
|
137
|
+
output; useful if the REPL echoes your input before answering.
|
|
138
|
+
timeout (float): Command timeout for this session in seconds.
|
|
139
|
+
"""
|
|
140
|
+
command: str
|
|
141
|
+
first_prompt: str
|
|
142
|
+
change_prompt: str
|
|
143
|
+
next_prompt: str
|
|
144
|
+
append_newline: bool = True
|
|
145
|
+
strip_command: bool = False
|
|
146
|
+
strip_ansi: bool = False
|
|
147
|
+
timeout: float = 5.0
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Then, a session is a list of commands. Each command should be a UTF-8 string, and we allow to attach some meta-data like expected MIME type for the output. We can also pass an expected output in the case of a documentation test. If `output` was already given on the input, it is moved to `expected`. This way it becomes really easy to setup regression tests on your documentation. Just rerun on the generated output file.
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
#| id: input-data
|
|
154
|
+
class ReplCommand(msgspec.Struct):
|
|
155
|
+
"""A command to be sent to the REPL.
|
|
156
|
+
|
|
157
|
+
Attributes:
|
|
158
|
+
command (str): the command.
|
|
159
|
+
output_type (str): MIME type of expected output.
|
|
160
|
+
output (str | None): evaluated output.
|
|
161
|
+
expected (str | None): expected output.
|
|
162
|
+
"""
|
|
163
|
+
command: str
|
|
164
|
+
output_type: str = "text/plain"
|
|
165
|
+
output: str | None = None
|
|
166
|
+
expected: str | None = None
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
class ReplSession(msgspec.Struct):
|
|
170
|
+
"""A REPL session.
|
|
171
|
+
|
|
172
|
+
Attributes:
|
|
173
|
+
config (ReplConfig): Config for setting up a REPL session.
|
|
174
|
+
commands (list[ReplCommand]): List of commands in the session.
|
|
175
|
+
"""
|
|
176
|
+
config: ReplConfig
|
|
177
|
+
commands: list[ReplCommand]
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## License and contribution
|
|
181
|
+
|
|
182
|
+
Licensed under the Apache 2.0 license. Contributions are welcome: if you've succesfully applied `repl-session` to a REPL not listed in the documentation, consider contributing your configuration to the documentation. If your contribution fixes a bug, please first file an issue.
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# REPL Session
|
|
2
|
+
|
|
3
|
+
[](https://github.com/entangled/repl-session/actions/workflows/pages.yml)
|
|
4
|
+
[](https://entangled.github.io/repl-session)
|
|
5
|
+
|
|
6
|
+
This is script that runs a session on any REPL following a description in a JSON file. The output contains the commands entered and the results given. This can be useful to drive documentation tests or literate programming tasks. This way we decouple running the commands from rendering or presenting the corresponding results, leading to better reproducibility, caching output and modularity w.r.t. any other tools that you may use.
|
|
7
|
+
|
|
8
|
+
This is very similar to running a Jupyter notebook from the console, with the benefit that you don't need a Jupyter kernel available for the language you're using. The downside is that REPLs can be messy to interact with.
|
|
9
|
+
|
|
10
|
+
## Is this for you?
|
|
11
|
+
|
|
12
|
+
This is only really useful if you're hacking together a literate programming environment similar to [Entangled](https://entangled.github.io). Suppose you have your documentation written in Markdown, ready for rendering with MkDocs or Pandoc. You want to automatically evaluate some expressions in this document as if they're entered in a REPL and process the results for inclusion in the document generator. Here `repl-session` is a nicely confined command-line tool, so its easy to integrate into a build pipeline.
|
|
13
|
+
|
|
14
|
+
## How does it work?
|
|
15
|
+
|
|
16
|
+
The REPL of your choice is started and interacted with through the `pexpect` library. All I/O is dealt with through `msgspec`.
|
|
17
|
+
|
|
18
|
+
The preferred way to solve this is by using `jupyter-client` in combination with an existing Jupyter kernel. However, not all languages have a Jupyter kernel available, and developing one takes a bit more than configuring `pexpect` for an existing REPL. I may still include Jupyter support in this application at a later stage.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
Install with,
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install repl-session
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Or equivalent Poetry, Astral Uv, Hatch or Conda commands.
|
|
29
|
+
|
|
30
|
+
## Documentation
|
|
31
|
+
|
|
32
|
+
The full documentation is available at [entangled.github.io/repl-session](https://entangled.github.io/repl-session).
|
|
33
|
+
|
|
34
|
+
## Examples
|
|
35
|
+
|
|
36
|
+
Here are some examples where we could interact with a REPL successfully. In general, the less intricate the REPL the better the results.
|
|
37
|
+
|
|
38
|
+
### Chez Scheme
|
|
39
|
+
|
|
40
|
+
I like to work with [Chez Scheme](https://cisco.github.io/ChezScheme/). Suppose I want to document an interactive session. This can be done:
|
|
41
|
+
|
|
42
|
+
```yaml
|
|
43
|
+
#| file: test/scheme.yml
|
|
44
|
+
config:
|
|
45
|
+
command: "scheme --eedisable"
|
|
46
|
+
first_prompt: "> "
|
|
47
|
+
change_prompt: "(waiter-prompt-string \"{key}>\")"
|
|
48
|
+
next_prompt: "{key}> "
|
|
49
|
+
strip_command: true
|
|
50
|
+
commands:
|
|
51
|
+
- command: (* 6 7)
|
|
52
|
+
- command: |
|
|
53
|
+
(define (fac n init)
|
|
54
|
+
(if (zero? n)
|
|
55
|
+
init
|
|
56
|
+
(fac (- n 1) (* init n)))))
|
|
57
|
+
- command: (fac 10 1)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Passing this to `repl-session`, it will start the Chez Scheme interpreter, waiting for the `>` prompt to appear. It then changes the prompt to a generated `uuid4` code, for instance `27e87a8a-742c-4501-b05d-b05814f5a010> `. This will make sure that we can't accidentally match something else for an interactive prompt (imagine we're generating some XML!). Since commands are also echoed to standard out, we need to strip them from the resulting output. Running this should give:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
repl-session < test/scheme.yml | jq '.commands.[].output'
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
"42"
|
|
68
|
+
"(define (fac n init)\n (if (zero? n)\n init\n (fac (- n 1) (* init n)))))"
|
|
69
|
+
"3628800"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Lua
|
|
73
|
+
|
|
74
|
+
This looks very similar to the previous example:
|
|
75
|
+
|
|
76
|
+
```yaml
|
|
77
|
+
#| file: test/lua.yml
|
|
78
|
+
config:
|
|
79
|
+
command: "lua"
|
|
80
|
+
first_prompt: "> "
|
|
81
|
+
change_prompt: "_PROMPT = \"{key}> \""
|
|
82
|
+
next_prompt: "{key}> "
|
|
83
|
+
strip_command: true
|
|
84
|
+
strip_ansi: true
|
|
85
|
+
commands:
|
|
86
|
+
- command: 6 * 7
|
|
87
|
+
- command: "\"Hello\" .. \", \" .. \"World!\""
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The Lua REPL is not so nice. It sends ANSI escape codes and those need to be filtered out.
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
repl-session < test/lua.yml | jq '.commands.[].output'
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
"42"
|
|
98
|
+
"Hello, World!"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Input/Output structure
|
|
102
|
+
|
|
103
|
+
The user can configure how the REPL is called and interpreted.
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
#| id: input-data
|
|
107
|
+
class ReplConfig(msgspec.Struct):
|
|
108
|
+
"""Configuration
|
|
109
|
+
|
|
110
|
+
Attributes:
|
|
111
|
+
command (str): Command to start the REPL
|
|
112
|
+
first_prompt (str): Regex to match the first prompt
|
|
113
|
+
change_prompt (str): Command to change prompt; should contain '{key}' as an
|
|
114
|
+
argument.
|
|
115
|
+
next_prompt (str): Regex to match the changed prompts; should contain '{key}'
|
|
116
|
+
as an argument.
|
|
117
|
+
append_newline (bool): Whether to append a newline to given commands.
|
|
118
|
+
strip_command (bool): Whether to strip the original command from the gotten
|
|
119
|
+
output; useful if the REPL echoes your input before answering.
|
|
120
|
+
timeout (float): Command timeout for this session in seconds.
|
|
121
|
+
"""
|
|
122
|
+
command: str
|
|
123
|
+
first_prompt: str
|
|
124
|
+
change_prompt: str
|
|
125
|
+
next_prompt: str
|
|
126
|
+
append_newline: bool = True
|
|
127
|
+
strip_command: bool = False
|
|
128
|
+
strip_ansi: bool = False
|
|
129
|
+
timeout: float = 5.0
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Then, a session is a list of commands. Each command should be a UTF-8 string, and we allow to attach some meta-data like expected MIME type for the output. We can also pass an expected output in the case of a documentation test. If `output` was already given on the input, it is moved to `expected`. This way it becomes really easy to setup regression tests on your documentation. Just rerun on the generated output file.
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
#| id: input-data
|
|
136
|
+
class ReplCommand(msgspec.Struct):
|
|
137
|
+
"""A command to be sent to the REPL.
|
|
138
|
+
|
|
139
|
+
Attributes:
|
|
140
|
+
command (str): the command.
|
|
141
|
+
output_type (str): MIME type of expected output.
|
|
142
|
+
output (str | None): evaluated output.
|
|
143
|
+
expected (str | None): expected output.
|
|
144
|
+
"""
|
|
145
|
+
command: str
|
|
146
|
+
output_type: str = "text/plain"
|
|
147
|
+
output: str | None = None
|
|
148
|
+
expected: str | None = None
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class ReplSession(msgspec.Struct):
|
|
152
|
+
"""A REPL session.
|
|
153
|
+
|
|
154
|
+
Attributes:
|
|
155
|
+
config (ReplConfig): Config for setting up a REPL session.
|
|
156
|
+
commands (list[ReplCommand]): List of commands in the session.
|
|
157
|
+
"""
|
|
158
|
+
config: ReplConfig
|
|
159
|
+
commands: list[ReplCommand]
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## License and contribution
|
|
163
|
+
|
|
164
|
+
Licensed under the Apache 2.0 license. Contributions are welcome: if you've succesfully applied `repl-session` to a REPL not listed in the documentation, consider contributing your configuration to the documentation. If your contribution fixes a bug, please first file an issue.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "repl-session"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Runs a session with a Read-Eval-Print-Loop (REPL). Takes YAML as input gives JSON as output."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
classifiers = [
|
|
7
|
+
"Development Status :: 3 - Alpha",
|
|
8
|
+
"Environment :: Console",
|
|
9
|
+
"Intended Audience :: Developers",
|
|
10
|
+
"License :: OSI Approved :: Apache Software License",
|
|
11
|
+
"Topic :: Software Development",
|
|
12
|
+
"Topic :: Utilities"
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.11"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"argh>=0.30.5",
|
|
17
|
+
"msgspec>=0.19.0",
|
|
18
|
+
"pexpect>=4.9.0",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[dependency-groups]
|
|
22
|
+
dev = [
|
|
23
|
+
"basedpyright>=1.31.4",
|
|
24
|
+
"entangled-cli>=2.1.13",
|
|
25
|
+
"mkdocs-macros-plugin>=1.3.9",
|
|
26
|
+
"mkdocs-material>=9.6.20",
|
|
27
|
+
"ty>=0.0.1a21",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.scripts]
|
|
31
|
+
repl-session = "repl_session:main"
|
|
32
|
+
|
|
33
|
+
[tool.uv]
|
|
34
|
+
package = true
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# ~/~ begin <<docs/index.md#src/repl_session/__init__.py>>[init]
|
|
2
|
+
#| file: src/repl_session/__init__.py
|
|
3
|
+
"""
|
|
4
|
+
`repl-session` is a command-line tool to evaluate a given session
|
|
5
|
+
in any REPL, and store the results.
|
|
6
|
+
"""
|
|
7
|
+
# ~/~ begin <<docs/index.md#imports>>[init]
|
|
8
|
+
#| id: imports
|
|
9
|
+
# from datetime import datetime, tzinfo
|
|
10
|
+
from typing import IO
|
|
11
|
+
from collections.abc import Generator, Callable
|
|
12
|
+
# import re
|
|
13
|
+
from contextlib import contextmanager
|
|
14
|
+
import uuid
|
|
15
|
+
import sys
|
|
16
|
+
import re
|
|
17
|
+
|
|
18
|
+
import pexpect
|
|
19
|
+
import msgspec
|
|
20
|
+
import argh
|
|
21
|
+
import importlib.metadata
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
__version__ = importlib.metadata.version("repl-session")
|
|
25
|
+
# ~/~ end
|
|
26
|
+
|
|
27
|
+
# ~/~ begin <<README.md#input-data>>[init]
|
|
28
|
+
#| id: input-data
|
|
29
|
+
class ReplConfig(msgspec.Struct):
|
|
30
|
+
"""Configuration
|
|
31
|
+
|
|
32
|
+
Attributes:
|
|
33
|
+
command (str): Command to start the REPL
|
|
34
|
+
first_prompt (str): Regex to match the first prompt
|
|
35
|
+
change_prompt (str): Command to change prompt; should contain '{key}' as an
|
|
36
|
+
argument.
|
|
37
|
+
next_prompt (str): Regex to match the changed prompts; should contain '{key}'
|
|
38
|
+
as an argument.
|
|
39
|
+
append_newline (bool): Whether to append a newline to given commands.
|
|
40
|
+
strip_command (bool): Whether to strip the original command from the gotten
|
|
41
|
+
output; useful if the REPL echoes your input before answering.
|
|
42
|
+
timeout (float): Command timeout for this session in seconds.
|
|
43
|
+
"""
|
|
44
|
+
command: str
|
|
45
|
+
first_prompt: str
|
|
46
|
+
change_prompt: str
|
|
47
|
+
next_prompt: str
|
|
48
|
+
append_newline: bool = True
|
|
49
|
+
strip_command: bool = False
|
|
50
|
+
strip_ansi: bool = False
|
|
51
|
+
timeout: float = 5.0
|
|
52
|
+
# ~/~ end
|
|
53
|
+
# ~/~ begin <<README.md#input-data>>[1]
|
|
54
|
+
#| id: input-data
|
|
55
|
+
class ReplCommand(msgspec.Struct):
|
|
56
|
+
"""A command to be sent to the REPL.
|
|
57
|
+
|
|
58
|
+
Attributes:
|
|
59
|
+
command (str): the command.
|
|
60
|
+
output_type (str): MIME type of expected output.
|
|
61
|
+
output (str | None): evaluated output.
|
|
62
|
+
expected (str | None): expected output.
|
|
63
|
+
"""
|
|
64
|
+
command: str
|
|
65
|
+
output_type: str = "text/plain"
|
|
66
|
+
output: str | None = None
|
|
67
|
+
expected: str | None = None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class ReplSession(msgspec.Struct):
|
|
71
|
+
"""A REPL session.
|
|
72
|
+
|
|
73
|
+
Attributes:
|
|
74
|
+
config (ReplConfig): Config for setting up a REPL session.
|
|
75
|
+
commands (list[ReplCommand]): List of commands in the session.
|
|
76
|
+
"""
|
|
77
|
+
config: ReplConfig
|
|
78
|
+
commands: list[ReplCommand]
|
|
79
|
+
# ~/~ end
|
|
80
|
+
|
|
81
|
+
# ~/~ begin <<docs/index.md#repl-contextmanager>>[init]
|
|
82
|
+
#| id: repl-contextmanager
|
|
83
|
+
|
|
84
|
+
@contextmanager
|
|
85
|
+
def repl(config: ReplConfig) -> Generator[Callable[[str], str]]:
|
|
86
|
+
key = str(uuid.uuid4())
|
|
87
|
+
|
|
88
|
+
with pexpect.spawn(config.command, timeout=config.timeout) as child:
|
|
89
|
+
child.expect(config.first_prompt)
|
|
90
|
+
change_prompt_cmd = config.change_prompt.format(key=key)
|
|
91
|
+
if config.append_newline:
|
|
92
|
+
change_prompt_cmd = change_prompt_cmd + "\n"
|
|
93
|
+
child.send(change_prompt_cmd)
|
|
94
|
+
if config.strip_command:
|
|
95
|
+
child.expect(key)
|
|
96
|
+
prompt = config.next_prompt.format(key=key)
|
|
97
|
+
child.expect(prompt)
|
|
98
|
+
|
|
99
|
+
def send(msg: str) -> str:
|
|
100
|
+
if config.append_newline:
|
|
101
|
+
msg = msg + "\n"
|
|
102
|
+
child.send(msg)
|
|
103
|
+
child.expect("(.*)" + prompt)
|
|
104
|
+
|
|
105
|
+
answer = child.match[1].decode()
|
|
106
|
+
if config.strip_ansi:
|
|
107
|
+
ansi_escape = re.compile(r'(\u001b\[|\x1B\[)[0-?]*[ -\/]*[@-~]')
|
|
108
|
+
answer = ansi_escape.sub("", answer)
|
|
109
|
+
if config.strip_command:
|
|
110
|
+
answer = answer.strip().replace("\r", "")
|
|
111
|
+
return answer.removeprefix(msg)
|
|
112
|
+
else:
|
|
113
|
+
return child.match[1].decode()
|
|
114
|
+
|
|
115
|
+
yield send
|
|
116
|
+
# ~/~ end
|
|
117
|
+
# ~/~ begin <<docs/index.md#run-session>>[init]
|
|
118
|
+
#| id: run-session
|
|
119
|
+
def run_session(session: ReplSession):
|
|
120
|
+
with repl(session.config) as run:
|
|
121
|
+
for cmd in session.commands:
|
|
122
|
+
expected = cmd.expected or cmd.output
|
|
123
|
+
output = run(cmd.command)
|
|
124
|
+
cmd.output = output
|
|
125
|
+
cmd.expected = expected
|
|
126
|
+
|
|
127
|
+
return session
|
|
128
|
+
# ~/~ end
|
|
129
|
+
# ~/~ begin <<docs/index.md#io>>[init]
|
|
130
|
+
#| id: io
|
|
131
|
+
|
|
132
|
+
def read_session(port: IO[str] = sys.stdin) -> ReplSession:
|
|
133
|
+
data: str = port.read()
|
|
134
|
+
return msgspec.yaml.decode(data, type=ReplSession)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def write_session(session: ReplSession, port: IO[str] = sys.stdout):
|
|
138
|
+
data = msgspec.json.encode(session)
|
|
139
|
+
port.write(data.decode())
|
|
140
|
+
# ~/~ end
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
@argh.arg("-v", "--version", help="show version and exit")
|
|
144
|
+
def repl_session(version: bool = False):
|
|
145
|
+
"""
|
|
146
|
+
repl-session runs a REPL session, reading JSON from standard input and
|
|
147
|
+
writing to standard output. Both the input and output follow the same
|
|
148
|
+
schema.
|
|
149
|
+
"""
|
|
150
|
+
if version:
|
|
151
|
+
print(f"repl-session {__version__}")
|
|
152
|
+
sys.exit(0)
|
|
153
|
+
|
|
154
|
+
write_session(run_session(read_session()))
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def main():
|
|
158
|
+
argh.dispatch_command(repl_session)
|
|
159
|
+
# ~/~ end
|
|
File without changes
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: repl-session
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Runs a session with a Read-Eval-Print-Loop (REPL). Takes YAML as input gives JSON as output.
|
|
5
|
+
Classifier: Development Status :: 3 - Alpha
|
|
6
|
+
Classifier: Environment :: Console
|
|
7
|
+
Classifier: Intended Audience :: Developers
|
|
8
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
9
|
+
Classifier: Topic :: Software Development
|
|
10
|
+
Classifier: Topic :: Utilities
|
|
11
|
+
Requires-Python: >=3.11
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: argh>=0.30.5
|
|
15
|
+
Requires-Dist: msgspec>=0.19.0
|
|
16
|
+
Requires-Dist: pexpect>=4.9.0
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# REPL Session
|
|
20
|
+
|
|
21
|
+
[](https://github.com/entangled/repl-session/actions/workflows/pages.yml)
|
|
22
|
+
[](https://entangled.github.io/repl-session)
|
|
23
|
+
|
|
24
|
+
This is script that runs a session on any REPL following a description in a JSON file. The output contains the commands entered and the results given. This can be useful to drive documentation tests or literate programming tasks. This way we decouple running the commands from rendering or presenting the corresponding results, leading to better reproducibility, caching output and modularity w.r.t. any other tools that you may use.
|
|
25
|
+
|
|
26
|
+
This is very similar to running a Jupyter notebook from the console, with the benefit that you don't need a Jupyter kernel available for the language you're using. The downside is that REPLs can be messy to interact with.
|
|
27
|
+
|
|
28
|
+
## Is this for you?
|
|
29
|
+
|
|
30
|
+
This is only really useful if you're hacking together a literate programming environment similar to [Entangled](https://entangled.github.io). Suppose you have your documentation written in Markdown, ready for rendering with MkDocs or Pandoc. You want to automatically evaluate some expressions in this document as if they're entered in a REPL and process the results for inclusion in the document generator. Here `repl-session` is a nicely confined command-line tool, so its easy to integrate into a build pipeline.
|
|
31
|
+
|
|
32
|
+
## How does it work?
|
|
33
|
+
|
|
34
|
+
The REPL of your choice is started and interacted with through the `pexpect` library. All I/O is dealt with through `msgspec`.
|
|
35
|
+
|
|
36
|
+
The preferred way to solve this is by using `jupyter-client` in combination with an existing Jupyter kernel. However, not all languages have a Jupyter kernel available, and developing one takes a bit more than configuring `pexpect` for an existing REPL. I may still include Jupyter support in this application at a later stage.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
Install with,
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install repl-session
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or equivalent Poetry, Astral Uv, Hatch or Conda commands.
|
|
47
|
+
|
|
48
|
+
## Documentation
|
|
49
|
+
|
|
50
|
+
The full documentation is available at [entangled.github.io/repl-session](https://entangled.github.io/repl-session).
|
|
51
|
+
|
|
52
|
+
## Examples
|
|
53
|
+
|
|
54
|
+
Here are some examples where we could interact with a REPL successfully. In general, the less intricate the REPL the better the results.
|
|
55
|
+
|
|
56
|
+
### Chez Scheme
|
|
57
|
+
|
|
58
|
+
I like to work with [Chez Scheme](https://cisco.github.io/ChezScheme/). Suppose I want to document an interactive session. This can be done:
|
|
59
|
+
|
|
60
|
+
```yaml
|
|
61
|
+
#| file: test/scheme.yml
|
|
62
|
+
config:
|
|
63
|
+
command: "scheme --eedisable"
|
|
64
|
+
first_prompt: "> "
|
|
65
|
+
change_prompt: "(waiter-prompt-string \"{key}>\")"
|
|
66
|
+
next_prompt: "{key}> "
|
|
67
|
+
strip_command: true
|
|
68
|
+
commands:
|
|
69
|
+
- command: (* 6 7)
|
|
70
|
+
- command: |
|
|
71
|
+
(define (fac n init)
|
|
72
|
+
(if (zero? n)
|
|
73
|
+
init
|
|
74
|
+
(fac (- n 1) (* init n)))))
|
|
75
|
+
- command: (fac 10 1)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Passing this to `repl-session`, it will start the Chez Scheme interpreter, waiting for the `>` prompt to appear. It then changes the prompt to a generated `uuid4` code, for instance `27e87a8a-742c-4501-b05d-b05814f5a010> `. This will make sure that we can't accidentally match something else for an interactive prompt (imagine we're generating some XML!). Since commands are also echoed to standard out, we need to strip them from the resulting output. Running this should give:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
repl-session < test/scheme.yml | jq '.commands.[].output'
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
"42"
|
|
86
|
+
"(define (fac n init)\n (if (zero? n)\n init\n (fac (- n 1) (* init n)))))"
|
|
87
|
+
"3628800"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Lua
|
|
91
|
+
|
|
92
|
+
This looks very similar to the previous example:
|
|
93
|
+
|
|
94
|
+
```yaml
|
|
95
|
+
#| file: test/lua.yml
|
|
96
|
+
config:
|
|
97
|
+
command: "lua"
|
|
98
|
+
first_prompt: "> "
|
|
99
|
+
change_prompt: "_PROMPT = \"{key}> \""
|
|
100
|
+
next_prompt: "{key}> "
|
|
101
|
+
strip_command: true
|
|
102
|
+
strip_ansi: true
|
|
103
|
+
commands:
|
|
104
|
+
- command: 6 * 7
|
|
105
|
+
- command: "\"Hello\" .. \", \" .. \"World!\""
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The Lua REPL is not so nice. It sends ANSI escape codes and those need to be filtered out.
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
repl-session < test/lua.yml | jq '.commands.[].output'
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
"42"
|
|
116
|
+
"Hello, World!"
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Input/Output structure
|
|
120
|
+
|
|
121
|
+
The user can configure how the REPL is called and interpreted.
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
#| id: input-data
|
|
125
|
+
class ReplConfig(msgspec.Struct):
|
|
126
|
+
"""Configuration
|
|
127
|
+
|
|
128
|
+
Attributes:
|
|
129
|
+
command (str): Command to start the REPL
|
|
130
|
+
first_prompt (str): Regex to match the first prompt
|
|
131
|
+
change_prompt (str): Command to change prompt; should contain '{key}' as an
|
|
132
|
+
argument.
|
|
133
|
+
next_prompt (str): Regex to match the changed prompts; should contain '{key}'
|
|
134
|
+
as an argument.
|
|
135
|
+
append_newline (bool): Whether to append a newline to given commands.
|
|
136
|
+
strip_command (bool): Whether to strip the original command from the gotten
|
|
137
|
+
output; useful if the REPL echoes your input before answering.
|
|
138
|
+
timeout (float): Command timeout for this session in seconds.
|
|
139
|
+
"""
|
|
140
|
+
command: str
|
|
141
|
+
first_prompt: str
|
|
142
|
+
change_prompt: str
|
|
143
|
+
next_prompt: str
|
|
144
|
+
append_newline: bool = True
|
|
145
|
+
strip_command: bool = False
|
|
146
|
+
strip_ansi: bool = False
|
|
147
|
+
timeout: float = 5.0
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Then, a session is a list of commands. Each command should be a UTF-8 string, and we allow to attach some meta-data like expected MIME type for the output. We can also pass an expected output in the case of a documentation test. If `output` was already given on the input, it is moved to `expected`. This way it becomes really easy to setup regression tests on your documentation. Just rerun on the generated output file.
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
#| id: input-data
|
|
154
|
+
class ReplCommand(msgspec.Struct):
|
|
155
|
+
"""A command to be sent to the REPL.
|
|
156
|
+
|
|
157
|
+
Attributes:
|
|
158
|
+
command (str): the command.
|
|
159
|
+
output_type (str): MIME type of expected output.
|
|
160
|
+
output (str | None): evaluated output.
|
|
161
|
+
expected (str | None): expected output.
|
|
162
|
+
"""
|
|
163
|
+
command: str
|
|
164
|
+
output_type: str = "text/plain"
|
|
165
|
+
output: str | None = None
|
|
166
|
+
expected: str | None = None
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
class ReplSession(msgspec.Struct):
|
|
170
|
+
"""A REPL session.
|
|
171
|
+
|
|
172
|
+
Attributes:
|
|
173
|
+
config (ReplConfig): Config for setting up a REPL session.
|
|
174
|
+
commands (list[ReplCommand]): List of commands in the session.
|
|
175
|
+
"""
|
|
176
|
+
config: ReplConfig
|
|
177
|
+
commands: list[ReplCommand]
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## License and contribution
|
|
181
|
+
|
|
182
|
+
Licensed under the Apache 2.0 license. Contributions are welcome: if you've succesfully applied `repl-session` to a REPL not listed in the documentation, consider contributing your configuration to the documentation. If your contribution fixes a bug, please first file an issue.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/repl_session/__init__.py
|
|
5
|
+
src/repl_session/py.typed
|
|
6
|
+
src/repl_session.egg-info/PKG-INFO
|
|
7
|
+
src/repl_session.egg-info/SOURCES.txt
|
|
8
|
+
src/repl_session.egg-info/dependency_links.txt
|
|
9
|
+
src/repl_session.egg-info/entry_points.txt
|
|
10
|
+
src/repl_session.egg-info/requires.txt
|
|
11
|
+
src/repl_session.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
repl_session
|