Skip to content

Commit

Permalink
Merge pull request #6 from lobis/2-javascript-nodejs-bindings
Browse files Browse the repository at this point in the history
nodejs bindings - not complete
  • Loading branch information
lobis authored Jun 19, 2023
2 parents 2484a71 + fb02ba6 commit 741bc33
Show file tree
Hide file tree
Showing 17 changed files with 392 additions and 29 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ jobs:

- name: "Run pytest"
run: python -m pytest

- name: "Run from cli"
run: python -m hvps --version

- uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
cache-dependency-path: bindings/nodejs/package-lock.json

- name: "Install nodejs bindings"
run: |
cd bindings/nodejs
npm --version
node --version
npm install
node cli.js
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ dmypy.json

.idea/
.vscode/

node_modules/
16 changes: 15 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
repos:

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
- id: check-xml
- id: requirements-txt-fixer
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 22.10.0
rev: 23.3.0
hooks:
- id: black
41 changes: 24 additions & 17 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
MIT License
BSD 3-Clause License

Copyright (c) 2022 Luis Antonio Obis Aparicio
Copyright (c) 2023, Luis Antonio Obis Aparicio

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:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

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.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 changes: 25 additions & 0 deletions bindings/nodejs/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { ExecutionContext } = require('./index');
const yargs = require('yargs');

const cli = yargs
.scriptName('hvps')
.usage('$0 <cmd> [args]')
.option('python', {
description: 'Specify the Python path to run HVPS.',
alias: 'p',
type: 'string',
default: 'python', // Set a default string value
})
.help()
.alias('h', 'help')
.argv;

// check python is available
const context = new ExecutionContext(cli.python);
context.print();

// Get the remaining arguments as a string
const remainingArgs = cli._.join(' ');
// these should be passed to the python code. TODO: not working!

context.run(remainingArgs);
62 changes: 62 additions & 0 deletions bindings/nodejs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { execSync } = require('child_process');


class ExecutionContext {
constructor(pythonPath) {
this.python = pythonPath;
// check if python path is correct and use absolute path
this.python = this.getPythonPath();

// check if the hvps package is available
this.getPackageVersion();
}

getPythonVersion() {
const command = `${this.python} -c "import sys; print(sys.version)"`;
try {
return execSync(command, { encoding: 'utf-8' }).trim();
} catch (err) {
console.error(`Error executing command (${command}): ${err}`);
throw err;
}
}

getPythonPath() {
const command = `${this.python} -c "import sys; print(sys.executable)"`;
try {
return execSync(command, { encoding: 'utf-8' }).trim();
} catch (err) {
console.error(`Error executing command (${command}): ${err}`);
throw err;
}
}

getPackageVersion() {
const command = `${this.python} -m hvps --version`;
try {
return execSync(command, { encoding: 'utf-8' }).trim();
} catch (err) {
throw new Error(`Error executing command (${command}). Please make sure the package is installed.`);
}
}

print() {
console.log(`Python version: ${this.getPythonVersion()}`);
console.log(`Python path: ${this.getPythonPath()}`);
console.log(`Package version: ${this.getPackageVersion()}`);
}

// run method should call the python script with the arguments
run(args) {
const command = `${this.python} -m hvps ${args}`;
try {
return execSync(command, { encoding: 'utf-8' }).trim();
} catch (err) {
throw new Error(`Error executing command (${command}): ${err}`);
}
}
}

module.exports = {
ExecutionContext,
};
180 changes: 180 additions & 0 deletions bindings/nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions bindings/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "hvps",
"version": "0.0.1",
"description": "Bindings for the HVPS python package",
"main": "index.js",
"bin": {
"hvps": "cli.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lobis/hvps.git"
},
"author": "Luis Antonio Obis Aparicio (@lobis)",
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/lobis/hvps/issues"
},
"homepage": "https://github.com/lobis/hvps#readme",
"dependencies": {
"yargs": "^17.7.2"
}
}
Loading

0 comments on commit 741bc33

Please sign in to comment.