This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2011 Darren DeRidder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+39
View File
@@ -0,0 +1,39 @@
node-python
===========
A super-simple wrapper for NodeJS to interact programatically with the Python shell. Enables the use of Python-based tools from Node.
[![NPM Stats](https://nodei.co/npm/python.png?downloads=true&stars=true)](https://npmjs.org/package/python)
![NPM Downloads](https://nodei.co/npm-dl/python.png?months=9)
Example
-------
This example starts a python child process, reads stdin for python commands, pipes them through to the python shell and runs the callback method with the resulting output. State is preserved in the shell between calls.
```javascript
// ------
// app.js
// ------
var python=require('python').shell;
// a callback to handle the response
var mycallback = function(err, data) {
if (err) {
console.error(err);
} else {
console.log("Callback function got : " + data);
}
};
// to test, read and execute commands from stdin
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
python(chunk, mycallback);
});
```
License
-------
MIT
+19
View File
@@ -0,0 +1,19 @@
#!/usr/bin/env node
var python = require('../lib/python').shell;
var mycallback = function(err, data) {
if (err) {
console.error(err);
} else {
process.stdout.write(data + '\n>>> ');
}
};
process.stdout.write('Using Python from NodeJS\n>>> ');
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
python(chunk, mycallback);
});
process.stdin.on('end', function() {
python('quit()');
});
+60
View File
@@ -0,0 +1,60 @@
var util = require('util');
var spawn = require('child_process').spawn;
var child = spawn('python',['-u','-i']);
var cmdQueue = new Array();
child.stdout.on('data', handleStdout);
child.stderr.on('data', handleStderr);
child.on('exit', handleExit);
function handleStdout(data) {
var datastr = data.toString('utf8');
var finished = false;
if (datastr.match(/Command Start\n/)) {
datastr = datastr.replace(/Command Start\n/,'');
}
if (datastr.match(/Command End\n/)) {
datastr = datastr.replace(/Command End\n/,'');
finished = true;
}
if (cmdQueue.length > 0) {
cmdQueue[0].data+=datastr;
}
if (finished) {
cmd = cmdQueue.shift();
if (cmd && cmd.command) {
if (undefined != typeof cmd.callback) {
cmd.callback(null, cmd.data);
processQueue();
}
}
}
};
function handleStderr(data) {
processQueue();
};
function processQueue() {
if (cmdQueue.length > 0 && cmdQueue[0].state === 'pending') {
cmdQueue[0].state = 'processing';
child.stdin.write(cmdQueue[0].command, encoding='utf8');
}
};
function handleExit(code) {
console.log('child process exited with code ' + code);
process.exit();
};
this.shell = function (command, callback) {
command = 'print "Command Start"; ' + command + '\nprint "Command End"';
if (command.charAt[command.length-1]!='\n') command += '\n';
cmdQueue.push({'command':command, 'callback':callback, 'data': '', state: 'pending'});
processQueue();
};
+46
View File
@@ -0,0 +1,46 @@
{
"_from": "python",
"_id": "python@0.0.4",
"_inBundle": false,
"_integrity": "sha1-MJTomO8Xozqpw+lzs4SKOOR9GBg=",
"_location": "/python",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "python",
"name": "python",
"escapedName": "python",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/python/-/python-0.0.4.tgz",
"_shasum": "3094e898ef17a33aa9c3e973b3848a38e47d1818",
"_spec": "python",
"_where": "c:\\xampp\\htdocs\\laravel",
"author": {
"name": "Darren DeRidder"
},
"bugs": {
"url": "https://github.com/73rhodes/node-python/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Interact with a long-running python child process",
"engines": {
"node": ">= 0.4.1"
},
"homepage": "https://github.com/73rhodes/node-python",
"main": "./lib/python.js",
"name": "python",
"repository": {
"type": "git",
"url": "git://github.com/73rhodes/node-python.git"
},
"version": "0.0.4"
}
+22
View File
@@ -0,0 +1,22 @@
var assert = require('assert');
var python = require('../lib/python').shell;
var runTests = function() {
// Run a couple commands in series
python('print "Hello World!"', function(err, data) {
assert.equal('Hello World!\n', data);
console.log('test 1 ok!');
python('print "Goodbye, Cruel World!"', function (err, data) {
assert.equal('Goodbye, Cruel World!\n', data);
console.log('test 2 ok!');
python('quit()');
});
});
// Run one in parallel with the first two
python('print "Asynch"', function (err, data) {
assert.equal('Asynch\n', data);
console.log('test 3 ok!');
});
};
runTests();