python-shell
Advanced tools
Comparing version 0.5.0 to 1.0.0
{ | ||
"name": "python-shell", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"description": "Run Python scripts from Node.js with simple (but efficient) inter-process communication through stdio", | ||
@@ -9,8 +9,14 @@ "keywords": [ | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "tsc -p ./ && mocha -R spec", | ||
"appveyorTest": "tsc -p ./ && mocha --ui tdd --reporter mocha-appveyor-reporter test/*.js", | ||
"compile":"tsc -watch -p ./" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"should": "^6.0.0", | ||
"mocha": "^2.2.5" | ||
"@types/mocha": "^2.2.48", | ||
"@types/node": "^9.3.0", | ||
"mocha": "^5.0.0", | ||
"mocha-appveyor-reporter": "^0.4.0", | ||
"should": "^13.2.1", | ||
"typescript": "^3.0.1" | ||
}, | ||
@@ -17,0 +23,0 @@ "repository": { |
115
README.md
@@ -1,2 +0,2 @@ | ||
# python-shell | ||
# python-shell [![Build status](https://ci.appveyor.com/api/projects/status/m8e3h53vvxg5wb2q?svg=true)](https://ci.appveyor.com/project/Almenon/python-shell) | ||
@@ -26,6 +26,19 @@ A simple way to run Python scripts from Node.js with basic but efficient inter-process communication and better error handling. | ||
### Running python code: | ||
```typescript | ||
import {PythonShell} from 'python-shell'; | ||
PythonShell.runString('x=1+1;print(x)', function (err) { | ||
if (err) throw err; | ||
console.log('finished'); | ||
}); | ||
``` | ||
If the script exits with a non-zero code, an error will be thrown. | ||
### Running a Python script: | ||
```js | ||
var PythonShell = require('python-shell'); | ||
```typescript | ||
import {PythonShell} from 'python-shell'; | ||
@@ -38,13 +51,13 @@ PythonShell.run('my_script.py', function (err) { | ||
If the script writes to stderr or exits with a non-zero code, an error will be thrown. | ||
If the script exits with a non-zero code, an error will be thrown. | ||
### Running a Python script with arguments and options: | ||
```js | ||
var PythonShell = require('python-shell'); | ||
```typescript | ||
import {PythonShell} from 'python-shell'; | ||
var options = { | ||
let options = { | ||
mode: 'text', | ||
pythonPath: 'path/to/python', | ||
pythonOptions: ['-u'], | ||
pythonOptions: ['-u'], // get print results in real-time | ||
scriptPath: 'path/to/my/scripts', | ||
@@ -63,5 +76,5 @@ args: ['value1', 'value2', 'value3'] | ||
```js | ||
var PythonShell = require('python-shell'); | ||
var pyshell = new PythonShell('my_script.py'); | ||
```typescript | ||
import {PythonShell} from 'python-shell'; | ||
let pyshell = new PythonShell('my_script.py'); | ||
@@ -98,5 +111,6 @@ // sends a message to the Python script via stdin | ||
An error will be thrown if the process exits with a non-zero exit code or if data has been written to stderr. Additionally, if "stderr" contains a formatted Python traceback, the error is augmented with Python exception details including a concatenated stack trace. | ||
An error will be thrown if the process exits with a non-zero exit code. Additionally, if "stderr" contains a formatted Python traceback, the error is augmented with Python exception details including a concatenated stack trace. | ||
Sample error with traceback (from test/python/error.py): | ||
``` | ||
@@ -110,4 +124,6 @@ Traceback (most recent call last): | ||
``` | ||
would result into the following error: | ||
```js | ||
```typescript | ||
{ [Error: ZeroDivisionError: integer division or modulo by zero] | ||
@@ -121,3 +137,5 @@ traceback: 'Traceback (most recent call last):\n File "test/python/error.py", line 6, in <module>\n divide_by_zero()\n File "test/python/error.py", line 4, in divide_by_zero\n print 1/0\nZeroDivisionError: integer division or modulo by zero\n', | ||
``` | ||
and `err.stack` would look like this: | ||
``` | ||
@@ -150,2 +168,3 @@ Error: ZeroDivisionError: integer division or modulo by zero | ||
* `parser`: each line of data (ending with "\n") is parsed with this function and its result is emitted as a message | ||
* `stderrParser`: each line of logs (ending with "\n") is parsed with this function and its result is emitted as a message | ||
* `encoding`: the text encoding to apply on the child process streams (default: "utf8") | ||
@@ -164,3 +183,3 @@ * `pythonPath`: The path where to locate the "python" executable. Default: "python" | ||
* `stdout`: the Python stdout stream, used for receiving data from the child process | ||
* `stderr`: the Python stderr stream, used for communicating errors | ||
* `stderr`: the Python stderr stream, used for communicating logs & errors | ||
* `childProcess`: the process instance created via `child_process.spawn` | ||
@@ -171,5 +190,6 @@ * `terminated`: boolean indicating whether the process has exited | ||
Example: | ||
```js | ||
```typescript | ||
// create a new instance | ||
var shell = new PythonShell('script.py', options); | ||
let shell = new PythonShell('script.py', options); | ||
``` | ||
@@ -182,3 +202,4 @@ | ||
Example: | ||
```js | ||
```typescript | ||
// setup a default "scriptPath" | ||
@@ -195,3 +216,4 @@ PythonShell.defaultOptions = { scriptPath: '../scripts' }; | ||
Example: | ||
```js | ||
```typescript | ||
// run a simple script | ||
@@ -203,2 +225,17 @@ PythonShell.run('script.py', function (err, results) { | ||
#### `#runString(code, options, callback)` | ||
Runs the Python code and invokes `callback` with the results. The callback contains the execution error (if any) as well as an array of messages emitted from the Python script. | ||
This method is also returning the `PythonShell` instance. | ||
Example: | ||
```typescript | ||
// run a simple script | ||
PythonShell.run('x=1;print(x)', function (err, results) { | ||
// script finished | ||
}); | ||
``` | ||
#### `.send(message)` | ||
@@ -209,9 +246,10 @@ | ||
Example: | ||
```js | ||
```typescript | ||
// send a message in text mode | ||
var shell = new PythonShell('script.py', { mode: 'text '}); | ||
let shell = new PythonShell('script.py', { mode: 'text '}); | ||
shell.send('hello world!'); | ||
// send a message in JSON mode | ||
var shell = new PythonShell('script.py', { mode: 'json '}); | ||
let shell = new PythonShell('script.py', { mode: 'json '}); | ||
shell.send({ command: "do_stuff", args: [1, 2, 3] }); | ||
@@ -224,2 +262,6 @@ ``` | ||
#### `.receiveStderr(data)` | ||
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. | ||
#### `.end(callback)` | ||
@@ -233,2 +275,12 @@ | ||
#### `checkSyntax(code:string)` | ||
Checks the syntax of the code and returns a promise. | ||
Promise is rejected if there is a syntax error. | ||
#### `checkSyntaxFile(filePath:string)` | ||
Checks the syntax of the file and returns a promise. | ||
Promise is rejected if there is a syntax error. | ||
#### event: `message` | ||
@@ -239,5 +291,6 @@ | ||
Example: | ||
```js | ||
```typescript | ||
// receive a message in text mode | ||
var shell = new PythonShell('script.py', { mode: 'text '}); | ||
let shell = new PythonShell('script.py', { mode: 'text '}); | ||
shell.on('message', function (message) { | ||
@@ -248,3 +301,3 @@ // handle message (a line of text from stdout) | ||
// receive a message in JSON mode | ||
var shell = new PythonShell('script.py', { mode: 'json '}); | ||
let shell = new PythonShell('script.py', { mode: 'json '}); | ||
shell.on('message', function (message) { | ||
@@ -255,2 +308,16 @@ // handle message (a line of text from stdout, parsed as JSON) | ||
#### event: `stderr` | ||
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. | ||
Example: | ||
```typescript | ||
// receive a message in text mode | ||
let shell = new PythonShell('script.py', { mode: 'text '}); | ||
shell.on('stderr', function (stderr) { | ||
// handle stderr (a line of text from stderr) | ||
}); | ||
``` | ||
#### event: `close` | ||
@@ -257,0 +324,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
46169
17
740
0
336
6