index.d.ts
5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/// <reference types="node" />
import { EventEmitter } from 'events';
import { ChildProcess, SpawnOptions } from 'child_process';
import { Readable, Writable } from 'stream';
export interface Options extends SpawnOptions {
mode?: 'text' | 'json' | 'binary';
formatter?: (param: string) => any;
parser?: (param: string) => any;
stderrParser?: (param: string) => any;
encoding?: string;
pythonPath?: string;
/**
* see https://docs.python.org/3.7/using/cmdline.html
*/
pythonOptions?: string[];
/**
* overrides scriptPath passed into PythonShell constructor
*/
scriptPath?: string;
/**
* arguments to your program
*/
args?: string[];
}
export declare class PythonShellError extends Error {
traceback: string | Buffer;
exitCode?: number;
}
/**
* An interactive Python shell exchanging data through stdio
* @param {string} script The python script to execute
* @param {object} [options] The launch options (also passed to child_process.spawn)
* @constructor
*/
export declare class PythonShell extends EventEmitter {
scriptPath: string;
command: string[];
mode: string;
formatter: (param: string | Object) => any;
parser: (param: string) => any;
stderrParser: (param: string) => any;
terminated: boolean;
childProcess: ChildProcess;
stdin: Writable;
stdout: Readable;
stderr: Readable;
exitSignal: string;
exitCode: number;
private stderrHasEnded;
private stdoutHasEnded;
private _remaining;
private _endCallback;
static defaultPythonPath: string;
static defaultOptions: Options;
/**
* spawns a python process
* @param scriptPath path to script. Relative to current directory or options.scriptFolder if specified
* @param options
*/
constructor(scriptPath: string, options?: Options);
static format: {
text: (data: any) => string;
json: (data: any) => string;
};
static parse: {
text: (data: any) => string;
json: (data: string) => any;
};
/**
* checks syntax without executing code
* @param {string} code
* @returns {Promise} rejects w/ stderr if syntax failure
*/
static checkSyntax(code: string): Promise<{}>;
static getPythonPath(): string;
/**
* checks syntax without executing code
* @param {string} filePath
* @returns {Promise} rejects w/ stderr if syntax failure
*/
static checkSyntaxFile(filePath: string): Promise<string>;
/**
* Runs a Python script and returns collected messages
* @param {string} scriptPath The path to the script to execute
* @param {Options} options The execution options
* @param {Function} callback The callback function to invoke with the script results
* @return {PythonShell} The PythonShell instance
*/
static run(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any): PythonShell;
/**
* Runs the inputted string of python code and returns collected messages. DO NOT ALLOW UNTRUSTED USER INPUT HERE!
* @param {string} code The python code to execute
* @param {Options} options The execution options
* @param {Function} callback The callback function to invoke with the script results
* @return {PythonShell} The PythonShell instance
*/
static runString(code: string, options?: Options, callback?: (err: PythonShellError, output?: any[]) => any): PythonShell;
static getVersion(pythonPath?: string): Promise<{
stdout: string;
stderr: string;
}>;
static getVersionSync(pythonPath?: string): string;
/**
* Parses an error thrown from the Python process through stderr
* @param {string|Buffer} data The stderr contents to parse
* @return {Error} The parsed error with extended stack trace when traceback is available
*/
private parseError;
/**
* Sends a message to the Python shell through stdin
* Override this method to format data to be sent to the Python process
* @param {string|Object} data The message to send
* @returns {PythonShell} The same instance for chaining calls
*/
send(message: string | Object): this;
/**
* Parses data received from the Python shell stdout stream and emits "message" events
* This method is not used in binary mode
* Override this method to parse incoming data from the Python process into messages
* @param {string|Buffer} data The data to parse into messages
*/
receive(data: string | Buffer): this;
/**
* Parses data received from the Python shell stderr stream and emits "stderr" events
* This method is not used in binary mode
* Override this method to parse incoming logs from the Python process into messages
* @param {string|Buffer} data The data to parse into messages
*/
receiveStderr(data: string | Buffer): this;
private receiveInternal;
/**
* Closes the stdin stream, which should cause the process to finish its work and close
* @returns {PythonShell} The same instance for chaining calls
*/
end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any): this;
/**
* Closes the stdin stream, which should cause the process to finish its work and close
* @returns {PythonShell} The same instance for chaining calls
*/
terminate(signal?: string): this;
}