diff --git a/python-shell/index.d.ts b/python-shell/index.d.ts new file mode 100644 index 0000000000..f28cf45693 --- /dev/null +++ b/python-shell/index.d.ts @@ -0,0 +1,39 @@ +// Type definitions for python-shell 0.4 +// Project: https://github.com/extrabacon/python-shell +// Definitions by: Dolan Miu +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export class PythonShell { + on(message: string, callback: (message: string) => void): void; + end(callback: (message: string) => void): void; + send(message: any | string): void; + + constructor(scriptName: string, options?: InstanceOptions); + defaultOptions: RunOptions; +} + +export interface RunOptions { + mode?: string; + formatter?: string; + parser?: string; + encoding?: string; + pythonPath?: string; + pythonOptions?: string[]; + scriptPath?: string; + args?: string[]; +} + +export interface InstanceOptions { + script?: string; + command?: string; + stdin?: any; + stdout?: any; + stderr?: any; + childProcess?: string; + terminated?: any; + exitCode?: any; + args?: any[]; +} + +export function run(scriptName: string, RunOptions: RunOptions, callback: (err: Error, results?: any) => void): void; +export function run(scriptName: string, callback: (err: Error, results?: any) => void): void; diff --git a/python-shell/python-shell-tests.ts b/python-shell/python-shell-tests.ts new file mode 100644 index 0000000000..83bca24b6e --- /dev/null +++ b/python-shell/python-shell-tests.ts @@ -0,0 +1,38 @@ +import * as ps from 'python-shell'; + +let PythonShell = ps.PythonShell; + +ps.run('my_script.py', function (err) { + if (err) throw err; + console.log('finished'); +}); + +var options = { + mode: 'text', + pythonPath: 'path/to/python', + pythonOptions: ['-u'], + scriptPath: 'path/to/my/scripts', + args: ['value1', 'value2', 'value3'] +}; + +ps.run('my_script.py', options, function (err, results) { + if (err) throw err; + // results is an array consisting of messages collected during execution + console.log('results: %j', results); +}); + +var pyshell = new PythonShell('my_script.py'); + +// sends a message to the Python script via stdin +pyshell.send('hello'); + +pyshell.on('message', function (message) { + // received a message sent from the Python script (a simple "print" statement) + console.log(message); +}); + +// end the input stream and allow the process to exit +pyshell.end(function (err) { + if (err) throw err; + console.log('finished'); +}); \ No newline at end of file diff --git a/python-shell/tsconfig.json b/python-shell/tsconfig.json new file mode 100644 index 0000000000..27748977a6 --- /dev/null +++ b/python-shell/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "python-shell-tests.ts" + ] +} \ No newline at end of file diff --git a/python-shell/tslint.json b/python-shell/tslint.json new file mode 100644 index 0000000000..2221e40e4a --- /dev/null +++ b/python-shell/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } \ No newline at end of file