1 |
|
2 | import { EventEmitter } from 'events';
|
3 | import { ChildProcess, SpawnOptions } from 'child_process';
|
4 | import { Readable, Transform, TransformCallback, Writable } from 'stream';
|
5 | export interface Options extends SpawnOptions {
|
6 | |
7 |
|
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 | }
|
28 | export declare class PythonShellError extends Error {
|
29 | traceback: string | Buffer;
|
30 | exitCode?: number;
|
31 | }
|
32 | export declare class PythonShellErrorWithLogs extends PythonShellError {
|
33 | logs: any[];
|
34 | }
|
35 | /**
|
36 | * Takes in a string stream and emits batches seperated by newlines
|
37 | */
|
38 | export 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 | */
|
51 | export 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 |
|
73 |
|
74 |
|
75 |
|
76 |
|
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 |
|
89 |
|
90 |
|
91 | static checkSyntax(code: string): Promise<{
|
92 | stdout: string;
|
93 | stderr: string;
|
94 | }>;
|
95 | static getPythonPath(): string;
|
96 | |
97 |
|
98 |
|
99 |
|
100 | static checkSyntaxFile(filePath: string): Promise<{
|
101 | stdout: string;
|
102 | stderr: string;
|
103 | }>;
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | static run(scriptPath: string, options?: Options): Promise<any[]>;
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
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 |
|
125 |
|
126 |
|
127 |
|
128 | private parseError;
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 | send(message: string | Object): this;
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 | end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any): this;
|
141 | |
142 |
|
143 |
|
144 |
|
145 | kill(signal?: NodeJS.Signals): this;
|
146 | |
147 |
|
148 |
|
149 |
|
150 | terminate(signal?: NodeJS.Signals): this;
|
151 | }
|
152 | export 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 | }
|