Skip to content

Commit 1f854f3

Browse files
authored
Merge pull request #233 from extrabacon/version2
Version3
2 parents 2e44ea4 + 0ab4624 commit 1f854f3

File tree

8 files changed

+921
-314
lines changed

8 files changed

+921
-314
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@
4545
"protocol": "inspector"
4646
}
4747
]
48-
}
48+
}

CHANGELOG.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
## 1.0.8
2-
* @joaoe fixed a bug with pythonshell not working with unset std streams
3-
* https://github.com/extrabacon/python-shell/milestone/9
1+
## [3.0.0] - 2021-03-07
2+
### Changed
3+
- **BREAKING** Default python path changed back to `python` on Windows. [#237](https://github.com/extrabacon/python-shell/issues/237)
4+
- **BREAKING** `error` event renamed to `pythonError` event. [#118](https://github.com/extrabacon/python-shell/issues/118)
5+
- **BREAKING** `receive` methods removed in favor of `splitter` arguments in the constructor. This lets the default splitting logic reside in a reuseable stream transformer. Now if you have extra pipes you can reuse `newlineTransformer` to split incoming data into newline-seperated lines.
46

5-
## 1.0.7
6-
* default python path updated to py on windows
7+
### Added
8+
- `error` event that is fired upon failure to launch process, among other things. [#118](https://github.com/extrabacon/python-shell/issues/118)
79

8-
## 1.0.4
9-
* added getVersionSync
10+
## [1.0.8]
11+
### Fixed
12+
- @joaoe fixed a bug with pythonshell not working with unset std streams
13+
- https://github.com/extrabacon/python-shell/milestone/9
1014

11-
## 0.0.3
12-
* fixed buffering in `PythonShell.receive`, fixing [#1](https://github.com/extrabacon/python-shell/issues/1)
15+
## [1.0.7]
16+
### Changed
17+
- default python path updated to py on windows
1318

14-
## 0.0.2
15-
* improved documentation
19+
## [1.0.4]
20+
### Added
21+
- added getVersionSync
1622

17-
## 0.0.1
18-
* initial version
19-
* independent module moved from [extrabacon/pyspreadsheet](https://github.com/extrabacon/pyspreadsheet)
23+
## [0.0.3]
24+
### Fixed
25+
- fixed buffering in `PythonShell.receive`, fixing [#1](https://github.com/extrabacon/python-shell/issues/1)
26+
27+
## [0.0.2]
28+
### Changed
29+
- improved documentation
30+
31+
## [0.0.1]
32+
### Added
33+
- initial version
34+
- independent module moved from [extrabacon/pyspreadsheet](https://github.com/extrabacon/pyspreadsheet)
2035

README.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [python-shell](https://www.npmjs.com/package/python-shell) [![Build status](https://ci.appveyor.com/api/projects/status/m8e3h53vvxg5wb2q/branch/master?svg=true)](https://ci.appveyor.com/project/Almenon/python-shell/branch/master) [![codecov](https://codecov.io/gh/extrabacon/python-shell/branch/master/graph/badge.svg)](https://codecov.io/gh/extrabacon/python-shell)
22

3-
<!-- chage above url accroding to repo -->
3+
<!-- change above url according to repo -->
44
A simple way to run Python scripts from Node.js with basic but efficient inter-process communication and better error handling.
55

66
## Features
@@ -11,17 +11,15 @@ A simple way to run Python scripts from Node.js with basic but efficient inter-p
1111
+ Simple and efficient data transfers through stdin and stdout streams
1212
+ Extended stack traces when an error is thrown
1313

14+
## Requirements
15+
First make sure you are able to run `python3` (Mac/Linux) or `python` (Windows) from the terminal. If you are not then you might need to add it to the PATH. If you want to use a version of python not in the PATH you should specify `options.pythonPath`.
16+
1417
## Installation
1518

1619
```bash
1720
npm install python-shell
1821
```
1922

20-
To run the tests:
21-
```bash
22-
npm test
23-
```
24-
2523
## Documentation
2624

2725
### Running python code:
@@ -163,17 +161,19 @@ Creates an instance of `PythonShell` and starts the Python process
163161
* `script`: the path of the script to execute
164162
* `options`: the execution options, consisting of:
165163
* `mode`: Configures how data is exchanged when data flows through stdin and stdout. The possible values are:
166-
* `text`: each line of data (ending with "\n") is emitted as a message (default)
167-
* `json`: each line of data (ending with "\n") is parsed as JSON and emitted as a message
164+
* `text`: each line of data is emitted as a message (default)
165+
* `json`: each line of data is parsed as JSON and emitted as a message
168166
* `binary`: data is streamed as-is through `stdout` and `stdin`
169-
* `formatter`: each message to send is transformed using this method, then appended with "\n"
170-
* `parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message
171-
* `stderrParser`: each line of logs (ending with "\n") is parsed with this function and its result is emitted as a message
167+
* `formatter`: each message to send is transformed using this method, then appended with a newline
168+
* `parser`: each line of data is parsed with this function and its result is emitted as a message
169+
* `stderrParser`: each line of logs is parsed with this function and its result is emitted as a message
172170
* `encoding`: the text encoding to apply on the child process streams (default: "utf8")
173-
* `pythonPath`: The path where to locate the "python" executable. Default: "python3" ("py" for Windows)
171+
* `pythonPath`: The path where to locate the "python" executable. Default: "python3" ("python" for Windows)
174172
* `pythonOptions`: Array of option switches to pass to "python"
175173
* `scriptPath`: The default path where to look for scripts. Default is the current working directory.
176174
* `args`: Array of arguments to pass to the script
175+
* `stdoutSplitter`: splits stdout into chunks, defaulting to splitting into newline-seperated lines
176+
* `stderrSplitter`: splits stderr into chunks, defaulting to splitting into newline-seperated lines
177177

178178
Other options are forwarded to `child_process.spawn`.
179179

@@ -271,14 +271,6 @@ let shell = new PythonShell('script.py', { mode: 'json'});
271271
shell.send({ command: "do_stuff", args: [1, 2, 3] });
272272
```
273273

274-
#### `.receive(data)`
275-
276-
Parses incoming data from the Python script written via stdout and emits `message` events. This method is called automatically as data is being received from stdout.
277-
278-
#### `.receiveStderr(data)`
279-
280-
Parses incoming logs from the Python script written via stderr and emits `stderr` events. This method is called automatically as data is being received from stderr.
281-
282274
#### `.end(callback)`
283275

284276
Closes the stdin stream, allowing the Python script to finish and exit. The optional callback is invoked when the process is terminated.
@@ -289,7 +281,7 @@ Terminates the python script. A kill signal may be provided by `signal`, if `sig
289281

290282
#### event: `message`
291283

292-
Fires when a chunk of data is parsed from the stdout stream via the `receive` method. If a `parser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
284+
After the stdout stream is split into chunks by stdoutSplitter the chunks are parsed by the parser and a message event is emitted for each parsed chunk. This event is not emitted in binary mode.
293285

294286
Example:
295287

@@ -309,7 +301,7 @@ shell.on('message', function (message) {
309301

310302
#### event: `stderr`
311303

312-
Fires when a chunk of logs is parsed from the stderr stream via the `receiveStderr` method. If a `stderrParser` method is specified, the result of this function will be the message value. This event is not emitted in binary mode.
304+
After the stderr stream is split into chunks by stderrSplitter the chunks are parsed by the parser and a message event is emitted for each parsed chunk. This event is not emitted in binary mode.
313305

314306
Example:
315307

@@ -325,10 +317,23 @@ shell.on('stderr', function (stderr) {
325317

326318
Fires when the process has been terminated, with an error or not.
327319

328-
#### event: `error`
320+
#### event: `pythonError`
329321

330322
Fires when the process terminates with a non-zero exit code.
331323

324+
#### event: `error`
325+
326+
Fires when:
327+
* The process could not be spawned, or
328+
* The process could not be killed, or
329+
* Sending a message to the child process failed.
330+
331+
If the process could not be spawned please double-check that python can be launched from the terminal.
332+
333+
### NewlineTransformer
334+
335+
A utility class for splitting stream data into newlines. Used as the default for stdoutSplitter and stderrSplitter if they are unspecified. You can use this class for any extra python streams if you'd like.
336+
332337
## Used By:
333338

334339
Python-Shell is used by [arepl-vscode](https://github.com/almenon/arepl-vscode), [gitinspector](https://github.com/ejwa/gitinspector), [pyspreadsheet](https://github.com/extrabacon/pyspreadsheet), [AtlantOS Ocean Data QC](https://github.com/ocean-data-qc/ocean-data-qc) and more!

0 commit comments

Comments
 (0)