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