You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
7.8 KiB

  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. /**
  7. * if binary is enabled message and stderr events will not be emitted
  8. */
  9. mode?: 'text' | 'json' | 'binary';
  10. formatter?: (param: string) => any;
  11. parser?: (param: string) => any;
  12. stderrParser?: (param: string) => any;
  13. encoding?: string;
  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. /**
  33. * An interactive Python shell exchanging data through stdio
  34. * @param {string} script The python script to execute
  35. * @param {object} [options] The launch options (also passed to child_process.spawn)
  36. * @constructor
  37. */
  38. export declare class PythonShell extends EventEmitter {
  39. scriptPath: string;
  40. command: string[];
  41. mode: string;
  42. formatter: (param: string | Object) => any;
  43. parser: (param: string) => any;
  44. stderrParser: (param: string) => any;
  45. terminated: boolean;
  46. childProcess: ChildProcess;
  47. stdin: Writable;
  48. stdout: Readable;
  49. stderr: Readable;
  50. exitSignal: string;
  51. exitCode: number;
  52. private stderrHasEnded;
  53. private stdoutHasEnded;
  54. private _remaining;
  55. private _endCallback;
  56. static defaultPythonPath: string;
  57. static defaultOptions: Options;
  58. /**
  59. * spawns a python process
  60. * @param scriptPath path to script. Relative to current directory or options.scriptFolder if specified
  61. * @param options
  62. */
  63. constructor(scriptPath: string, options?: Options);
  64. static format: {
  65. text: (data: any) => string;
  66. json: (data: any) => string;
  67. };
  68. static parse: {
  69. text: (data: any) => string;
  70. json: (data: string) => any;
  71. };
  72. /**
  73. * checks syntax without executing code
  74. * @returns {Promise} rejects w/ stderr if syntax failure
  75. */
  76. static checkSyntax(code: string): Promise<unknown>;
  77. static getPythonPath(): string;
  78. /**
  79. * checks syntax without executing code
  80. * @returns {Promise} rejects w/ stderr if syntax failure
  81. */
  82. static checkSyntaxFile(filePath: string): Promise<string>;
  83. /**
  84. * Runs a Python script and returns collected messages
  85. * @param {string} scriptPath The path to the script to execute
  86. * @param {Options} options The execution options
  87. * @param {Function} callback The callback function to invoke with the script results
  88. * @return {PythonShell} The PythonShell instance
  89. */
  90. static run(scriptPath: string, options?: Options, callback?: (err?: PythonShellError, output?: any[]) => any): PythonShell;
  91. /**
  92. * Runs the inputted string of python code and returns collected messages. DO NOT ALLOW UNTRUSTED USER INPUT HERE!
  93. * @param {string} code The python code to execute
  94. * @param {Options} options The execution options
  95. * @param {Function} callback The callback function to invoke with the script results
  96. * @return {PythonShell} The PythonShell instance
  97. */
  98. static runString(code: string, options?: Options, callback?: (err: PythonShellError, output?: any[]) => any): PythonShell;
  99. static getVersion(pythonPath?: string): Promise<{
  100. stdout: string;
  101. stderr: string;
  102. }>;
  103. static getVersionSync(pythonPath?: string): string;
  104. /**
  105. * Parses an error thrown from the Python process through stderr
  106. * @param {string|Buffer} data The stderr contents to parse
  107. * @return {Error} The parsed error with extended stack trace when traceback is available
  108. */
  109. private parseError;
  110. /**
  111. * Sends a message to the Python shell through stdin
  112. * Override this method to format data to be sent to the Python process
  113. * @returns {PythonShell} The same instance for chaining calls
  114. */
  115. send(message: string | Object): this;
  116. /**
  117. * Parses data received from the Python shell stdout stream and emits "message" events
  118. * This method is not used in binary mode
  119. * Override this method to parse incoming data from the Python process into messages
  120. * @param {string|Buffer} data The data to parse into messages
  121. */
  122. receive(data: string | Buffer): this;
  123. /**
  124. * Parses data received from the Python shell stderr stream and emits "stderr" events
  125. * This method is not used in binary mode
  126. * Override this method to parse incoming logs from the Python process into messages
  127. * @param {string|Buffer} data The data to parse into messages
  128. */
  129. receiveStderr(data: string | Buffer): this;
  130. private receiveInternal;
  131. /**
  132. * Closes the stdin stream. Unless python is listening for stdin in a loop
  133. * this should cause the process to finish its work and close.
  134. * @returns {PythonShell} The same instance for chaining calls
  135. */
  136. end(callback: (err: PythonShellError, exitCode: number, exitSignal: string) => any): this;
  137. /**
  138. * Sends a kill signal to the process
  139. * @returns {PythonShell} The same instance for chaining calls
  140. */
  141. kill(signal?: NodeJS.Signals): this;
  142. /**
  143. * Alias for kill.
  144. * @deprecated
  145. */
  146. terminate(signal?: NodeJS.Signals): this;
  147. }
  148. export interface PythonShell {
  149. addListener(event: string, listener: (...args: any[]) => void): this;
  150. emit(event: string | symbol, ...args: any[]): boolean;
  151. on(event: string, listener: (...args: any[]) => void): this;
  152. once(event: string, listener: (...args: any[]) => void): this;
  153. prependListener(event: string, listener: (...args: any[]) => void): this;
  154. prependOnceListener(event: string, listener: (...args: any[]) => void): this;
  155. addListener(event: "message", listener: (parsedChunk: any) => void): this;
  156. emit(event: "message", parsedChunk: any): boolean;
  157. on(event: "message", listener: (parsedChunk: any) => void): this;
  158. once(event: "message", listener: (parsedChunk: any) => void): this;
  159. prependListener(event: "message", listener: (parsedChunk: any) => void): this;
  160. prependOnceListener(event: "message", listener: (parsedChunk: any) => void): this;
  161. addListener(event: "stderr", listener: (parsedChunk: any) => void): this;
  162. emit(event: "stderr", parsedChunk: any): boolean;
  163. on(event: "stderr", listener: (parsedChunk: any) => void): this;
  164. once(event: "stderr", listener: (parsedChunk: any) => void): this;
  165. prependListener(event: "stderr", listener: (parsedChunk: any) => void): this;
  166. prependOnceListener(event: "stderr", listener: (parsedChunk: any) => void): this;
  167. addListener(event: "close", listener: () => void): this;
  168. emit(event: "close"): boolean;
  169. on(event: "close", listener: () => void): this;
  170. once(event: "close", listener: () => void): this;
  171. prependListener(event: "close", listener: () => void): this;
  172. prependOnceListener(event: "close", listener: () => void): this;
  173. addListener(event: "error", listener: (error: PythonShellError) => void): this;
  174. emit(event: "error", error: PythonShellError): boolean;
  175. on(event: "error", listener: (error: PythonShellError) => void): this;
  176. once(event: "error", listener: (error: PythonShellError) => void): this;
  177. prependListener(event: "error", listener: (error: PythonShellError) => void): this;
  178. prependOnceListener(event: "error", listener: (error: PythonShellError) => void): this;
  179. }