├── .travis.yml ├── LICENSE.txt ├── README.md ├── example └── app.js ├── lib └── python.js ├── package.json └── test └── python.test.js /.travis.yml: -------------------------------------------------------------------------------- 1 | os: linux 2 | dist: 3 | - xenial # python --version --> Python 2.7.12 so the tests pass 4 | #- focal # python --version --> Python 3.8.5 and the tests will hang until Travis times out 5 | language: node_js 6 | before_script: python --version 7 | node_js: 8 | - "14" 9 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2011 Darren DeRidder 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-python 2 | =========== 3 | 4 | A super-simple wrapper for NodeJS to interact programatically with the Python shell. Enables the use of Python-based tools from Node. 5 | 6 | Installation 7 | ------------ 8 | `npm install python` 9 | 10 | [![NPM Stats](https://nodei.co/npm/python.png?downloads=true&stars=true)](https://npmjs.org/package/python) 11 | 12 | ![NPM Downloads](https://nodei.co/npm-dl/python.png?months=9) 13 | 14 | 15 | 16 | Example 17 | ------- 18 | 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. 19 | 20 | ```javascript 21 | // ------ 22 | // app.js 23 | // ------ 24 | var python=require('python').shell; 25 | 26 | // a callback to handle the response 27 | var mycallback = function(err, data) { 28 | if (err) { 29 | console.error(err); 30 | } else { 31 | console.log("Callback function got : " + data); 32 | } 33 | }; 34 | 35 | // to test, read and execute commands from stdin 36 | process.stdin.resume(); 37 | process.stdin.setEncoding('utf8'); 38 | process.stdin.on('data', function(chunk) { 39 | python(chunk, mycallback); 40 | }); 41 | ``` 42 | 43 | License 44 | ------- 45 | MIT 46 | -------------------------------------------------------------------------------- /example/app.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var python = require('../lib/python').shell; 3 | var mycallback = function(err, data) { 4 | if (err) { 5 | console.error(err); 6 | } else { 7 | process.stdout.write(data + '\n>>> '); 8 | } 9 | }; 10 | process.stdout.write('Using Python from NodeJS\n>>> '); 11 | process.stdin.resume(); 12 | process.stdin.setEncoding('utf8'); 13 | process.stdin.on('data', function (chunk) { 14 | python(chunk, mycallback); 15 | }); 16 | 17 | process.stdin.on('end', function() { 18 | python('quit()'); 19 | }); 20 | -------------------------------------------------------------------------------- /lib/python.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | var spawn = require('child_process').spawn; 3 | var child = spawn('python',['-u','-i']); 4 | var cmdQueue = new Array(); 5 | 6 | 7 | child.stdout.on('data', handleStdout); 8 | child.stderr.on('data', handleStderr); 9 | child.on('exit', handleExit); 10 | 11 | 12 | function handleStdout(data) { 13 | var datastr = data.toString('utf8'); 14 | var finished = false; 15 | if (datastr.match(/Command Start\n/)) { 16 | datastr = datastr.replace(/Command Start\n/,''); 17 | } 18 | if (datastr.match(/Command End\n/)) { 19 | datastr = datastr.replace(/Command End\n/,''); 20 | finished = true; 21 | } 22 | if (cmdQueue.length > 0) { 23 | cmdQueue[0].data+=datastr; 24 | } 25 | if (finished) { 26 | cmd = cmdQueue.shift(); 27 | if (cmd && cmd.command) { 28 | if (undefined != typeof cmd.callback) { 29 | cmd.callback(null, cmd.data); 30 | processQueue(); 31 | } 32 | } 33 | } 34 | }; 35 | 36 | 37 | function handleStderr(data) { 38 | processQueue(); 39 | }; 40 | 41 | function processQueue() { 42 | if (cmdQueue.length > 0 && cmdQueue[0].state === 'pending') { 43 | cmdQueue[0].state = 'processing'; 44 | child.stdin.write(cmdQueue[0].command, encoding='utf8'); 45 | } 46 | }; 47 | 48 | 49 | function handleExit(code) { 50 | console.log('child process exited with code ' + code); 51 | process.exit(); 52 | }; 53 | 54 | 55 | this.shell = function (command, callback) { 56 | command = 'print "Command Start"; ' + command + '\nprint "Command End"'; 57 | if (command.charAt[command.length-1]!='\n') command += '\n'; 58 | cmdQueue.push({'command':command, 'callback':callback, 'data': '', state: 'pending'}); 59 | processQueue(); 60 | }; 61 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Darren DeRidder (https://github.com/darrenderidder)", 3 | "license": "MIT", 4 | "name": "python", 5 | "main": "./lib/python.js", 6 | "description": "Interact with a long-running python child process", 7 | "version": "0.0.3", 8 | "homepage": "https://github.com/darrenderidder/node-python", 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/darrenderidder/node-python.git" 12 | }, 13 | "engines": { "node": ">= 0.4.1" }, 14 | "scripts": { 15 | "test": "node ./test/python.test.js" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /test/python.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var python = require('../lib/python').shell; 3 | var testCounter = 0; 4 | 5 | var runTests = function() { 6 | 7 | // Run a couple commands in series 8 | python('print("Hello World!")', function(err, data) { 9 | assert.equal('Hello World!\n', data); 10 | console.log('test 1 ok!'); 11 | completeTest(); 12 | python('print("Goodbye, Cruel World!")', function (err, data) { 13 | assert.equal('Goodbye, Cruel World!\n', data); 14 | console.log('test 2 ok!'); 15 | python('quit()'); 16 | completeTest(); 17 | }); 18 | }); 19 | 20 | // Run one in parallel with the first two 21 | python('print("Asynch")', function (err, data) { 22 | assert.equal('Asynch\n', data); 23 | console.log('test 3 ok!'); 24 | completeTest(); 25 | }); 26 | }; 27 | 28 | function completeTest() { 29 | testCounter++; 30 | if (testCounter >= 3) { 31 | console.log("All tests completed ok."); 32 | process.exit(0); 33 | } 34 | } 35 | 36 | runTests(); 37 | --------------------------------------------------------------------------------