UNPKG

8.6 kBTypeScriptView Raw
1/// <reference types="node" />
2import { EventEmitter } from 'events';
3import { ChildProcess, SpawnOptions } from 'child_process';
4import { Readable, Transform, TransformCallback, Writable } from 'stream';
5export interface Options extends SpawnOptions {
6 /**
7 * if binary is enabled message and stderr events will not be emitted
8 */
9 mode?: 'text' | 'json' | 'binary';
10 formatter?: string | ((param: string) => any);
11 parser?: string | ((param: string) => any);
12 stderrParser?: string | ((param: string) => any);
13 encoding?: BufferEncoding;
14 pythonPath?: string;
15 /**
16 * see https://docs.python.org/3.7/using/cmdline.html
17 */
18 pythonOptions?: string[];
19 /**
20 * overrides scriptPath passed into PythonShell constructor
21 */
22 scriptPath?: string;
23 /**
24 * arguments to your program
25 */
26 args?: string[];
27}
28export declare class PythonShellError extends Error {
29 traceback: string | Buffer;
30 exitCode?: number;
31}
32export declare class PythonShellErrorWithLogs extends PythonShellError {
33 logs: any[];
34}
35/**
36 * Takes in a string stream and emits batches seperated by newlines
37 */
38export declare class NewlineTransformer extends Transform {
39 private _lastLineData;
40 _transform(chunk: any, encoding: string, callback: TransformCallback): void;
41 _flush(done: TransformCallback): void;
42}
43/**
44 * An interactive Python shell exchanging data through stdio
45 * @param {string} script The python script to execute
46 * @param {object} [options] The launch options (also passed to child_process.spawn)
47 * @param [stdoutSplitter] Optional. Splits stdout into chunks, defaulting to splitting into newline-seperated lines
48 * @param [stderrSplitter] Optional. splits stderr into chunks, defaulting to splitting into newline-seperated lines
49 * @constructor
50 */
51export declare class PythonShell extends EventEmitter {
52 scriptPath: string;
53 command: string[];
54 mode: string;
55 formatter: (param: string | Object) => any;
56 parser: (param: string) => any;
57 stderrParser: (param: string) => any;
58 terminated: boolean;
59 childProcess: ChildProcess;
60 stdin: Writable;
61 stdout: Readable;
62 stderr: Readable;
63 exitSignal: string;
64 exitCode: number;
65 private stderrHasEnded;
66 private stdoutHasEnded;
67 private _remaining;
68 private _endCallback;
69 static defaultPythonPath: string;
70 static defaultOptions: Options;
71 /**
72 * spawns a python process
73 * @param scriptPath path to script. Relative to current directory or options.scriptFolder if specified
74 * @param options
75 * @param stdoutSplitter Optional. Splits stdout into chunks, defaulting to splitting into newline-seperated lines
76 * @param stderrSplitter Optional. splits stderr into chunks, defaulting to splitting into newline-seperated lines
77 */
78 constructor(scriptPath: string, options?: Options, stdoutSplitter?: Transform, stderrSplitter?: Transform);
79 static format: {
80 text: (data: any) => string;
81 json: (data: any) => string;
82 };
83 static parse: {
84 text: (data: any) => string;
85 json: (data: string) => any;
86 };
87 /**
88 * checks syntax without executing code
89 * @returns rejects promise w/ string error output if syntax failure
90 */
91 static checkSyntax(code: string): Promise<{
92 stdout: string;
93 stderr: string;
94 }>;
95 static getPythonPath(): string;
96 /**
97 * checks syntax without executing code
98 * @returns {Promise} rejects w/ stderr if syntax failure
99 */
100 static checkSyntaxFile(filePath: string): Promise<{
101 stdout: string;
102 stderr: string;
103 }>;
104 /**
105 * Runs a Python script and returns collected messages as a promise.
106 * If the promise is rejected, the err will probably be of type PythonShellErrorWithLogs
107 * @param scriptPath The path to the script to execute
108 * @param options The execution options
109 */
110 static run(scriptPath: string, options?: Options): Promise<any[]>;
111 /**
112 * Runs the inputted string of python code and returns collected messages as a promise. DO NOT ALLOW UNTRUSTED USER INPUT HERE!
113 * @param code The python code to execute
114 * @param options The execution options
115 * @return a promise with the output from the python script
116 */
117 static runString(code: string, options?: Options): Promise<any[]>;
118 static getVersion(pythonPath?: string): import("child_process").PromiseWithChild<{
119 stdout: string;
120 stderr: string;
121 }>;
122 static getVersionSync(pythonPath?: string): string;
123 /**
124 * Parses an error thrown from the Python process through stderr
125 * @param {string|Buffer} data The stderr contents to parse
126 * @return {Error} The parsed error with extended stack trace when traceback is available
127 */
128 private parseError;
129 /**
130 * Sends a message to the Python shell through stdin
131 * Override this method to format data to be sent to the Python process
132 * @returns {PythonShell} The same instance for chaining calls
133 */
134 send(message: string | Object): this;
135 /**
136 * Closes the stdin stream. Unless python is listening for stdin in a loop
137 * this should cause the process to finish its work and close.
138 * @returns {PythonShell} The same instance for chaining calls
139 */
140 end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any): this;
141 /**
142 * Sends a kill signal to the process
143 * @returns {PythonShell} The same instance for chaining calls
144 */
145 kill(signal?: NodeJS.Signals): this;
146 /**
147 * Alias for kill.
148 * @deprecated
149 */
150 terminate(signal?: NodeJS.Signals): this;
151}
152export interface PythonShell {
153 addListener(event: string, listener: (...args: any[]) => void): this;
154 emit(event: string | symbol, ...args: any[]): boolean;
155 on(event: string, listener: (...args: any[]) => void): this;
156 once(event: string, listener: (...args: any[]) => void): this;
157 prependListener(event: string, listener: (...args: any[]) => void): this;
158 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
159 addListener(event: "message", listener: (parsedChunk: any) => void): this;
160 emit(event: "message", parsedChunk: any): boolean;
161 on(event: "message", listener: (parsedChunk: any) => void): this;
162 once(event: "message", listener: (parsedChunk: any) => void): this;
163 prependListener(event: "message", listener: (parsedChunk: any) => void): this;
164 prependOnceListener(event: "message", listener: (parsedChunk: any) => void): this;
165 addListener(event: "stderr", listener: (parsedChunk: any) => void): this;
166 emit(event: "stderr", parsedChunk: any): boolean;
167 on(event: "stderr", listener: (parsedChunk: any) => void): this;
168 once(event: "stderr", listener: (parsedChunk: any) => void): this;
169 prependListener(event: "stderr", listener: (parsedChunk: any) => void): this;
170 prependOnceListener(event: "stderr", listener: (parsedChunk: any) => void): this;
171 addListener(event: "close", listener: () => void): this;
172 emit(event: "close"): boolean;
173 on(event: "close", listener: () => void): this;
174 once(event: "close", listener: () => void): this;
175 prependListener(event: "close", listener: () => void): this;
176 prependOnceListener(event: "close", listener: () => void): this;
177 addListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this;
178 emit(event: "error", error: NodeJS.ErrnoException): boolean;
179 on(event: "error", listener: (error: NodeJS.ErrnoException) => void): this;
180 once(event: "error", listener: (error: NodeJS.ErrnoException) => void): this;
181 prependListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this;
182 prependOnceListener(event: "error", listener: (error: NodeJS.ErrnoException) => void): this;
183 addListener(event: "pythonError", listener: (error: PythonShellError) => void): this;
184 emit(event: "pythonError", error: PythonShellError): boolean;
185 on(event: "pythonError", listener: (error: PythonShellError) => void): this;
186 once(event: "pythonError", listener: (error: PythonShellError) => void): this;
187 prependListener(event: "pythonError", listener: (error: PythonShellError) => void): this;
188 prependOnceListener(event: "pythonError", listener: (error: PythonShellError) => void): this;
189}