index.d.ts
7.58 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/// <reference types="node" />
import { EventEmitter } from 'events';
import { ChildProcess, SpawnOptions } from 'child_process';
import { Readable, Writable } from 'stream';
export interface Options extends SpawnOptions {
/**
* if binary is enabled message and stderr events will not be emitted
*/
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
* @returns {Promise} rejects w/ stderr if syntax failure
*/
static checkSyntax(code: string): Promise<unknown>;
static getPythonPath(): string;
/**
* checks syntax without executing code
* @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
* @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. Unless python is listening for stdin in a loop
* this 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;
/**
* Sends a kill signal to the process
* @returns {PythonShell} The same instance for chaining calls
*/
kill(signal?: NodeJS.Signals): this;
/**
* Alias for kill.
* @deprecated
*/
terminate(signal?: NodeJS.Signals): this;
}
export interface PythonShell {
addListener(event: string, listener: (...args: any[]) => void): this;
emit(event: string | symbol, ...args: any[]): boolean;
on(event: string, listener: (...args: any[]) => void): this;
once(event: string, listener: (...args: any[]) => void): this;
prependListener(event: string, listener: (...args: any[]) => void): this;
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
addListener(event: "message", listener: (parsedChunk: any) => void): this;
emit(event: "message", parsedChunk: any): boolean;
on(event: "message", listener: (parsedChunk: any) => void): this;
once(event: "message", listener: (parsedChunk: any) => void): this;
prependListener(event: "message", listener: (parsedChunk: any) => void): this;
prependOnceListener(event: "message", listener: (parsedChunk: any) => void): this;
addListener(event: "stderr", listener: (parsedChunk: any) => void): this;
emit(event: "stderr", parsedChunk: any): boolean;
on(event: "stderr", listener: (parsedChunk: any) => void): this;
once(event: "stderr", listener: (parsedChunk: any) => void): this;
prependListener(event: "stderr", listener: (parsedChunk: any) => void): this;
prependOnceListener(event: "stderr", listener: (parsedChunk: any) => void): this;
addListener(event: "close", listener: () => void): this;
emit(event: "close"): boolean;
on(event: "close", listener: () => void): this;
once(event: "close", listener: () => void): this;
prependListener(event: "close", listener: () => void): this;
prependOnceListener(event: "close", listener: () => void): this;
addListener(event: "error", listener: (error: PythonShellError) => void): this;
emit(event: "error", error: PythonShellError): boolean;
on(event: "error", listener: (error: PythonShellError) => void): this;
once(event: "error", listener: (error: PythonShellError) => void): this;
prependListener(event: "error", listener: (error: PythonShellError) => void): this;
prependOnceListener(event: "error", listener: (error: PythonShellError) => void): this;
}