added python-shell typings

This commit is contained in:
Dolan Miu
2016-12-23 20:39:15 +00:00
parent 7c90a158de
commit e990e9e11e
4 changed files with 100 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
// Type definitions for python-shell 0.4
// Project: https://github.com/extrabacon/python-shell
// Definitions by: Dolan Miu <https://github.com/dolanmiu>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module "python-shell" {
export class PythonShell {
on(message: string, callback: (message: string) => void): void;
end(callback: (message: string) => void): void;
send(message: string): void;
send(message: any): void;
constructor(scriptName: string, options?: InstanceOptions);
defaultOptions: RunOptions;
}
export interface RunOptions {
mode?: string;
formatter?: string;
parser?: string;
encoding?: string;
pythonPath?: string;
pythonOptions?: Array<string>;
scriptPath?: string;
args?: Array<string>;
}
export interface InstanceOptions {
script?: string;
command?: string;
stdin?: any;
stdout?: any;
stderr?: any;
childProcess?: string;
terminated?: any;
exitCode?: any;
args?: Array<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;
}
+38
View File
@@ -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');
});
+19
View File
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"python-shell-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "../tslint.json" }