Skip to content

Commit

Permalink
node tool template finished
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaelicke committed Nov 15, 2022
1 parent 9792feb commit 6bf4cd0
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 14 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Docker Image CI

on:
release:
types: [published]

jobs:

build:

runs-on: ubuntu-latest

steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: build and push
uses: docker/build-push-action@v1
with:
registry: ghcr.io
username: "vforwater"
password: ${{ secrets.PAT }}
repository: vforwater/tbr_template_node
tags: latest,${{ github.ref_name }}
10 changes: 7 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
FROM node:18.12.1-buster

# Do anything you need to install tool dependencies here
RUN echo "Replace this line with a tool"

# create the tool input structure
RUN mkdir /in
COPY ./in /in
Expand All @@ -11,4 +8,11 @@ RUN mkdir /src
COPY ./src /src

WORKDIR /src

# install dependencies: js-yaml and papaparse
# in node, the dependencies are directly install into /src/
RUN npm install js-yaml
RUN npm install papaparse

# run command
CMD ["node", "run.js"]
11 changes: 11 additions & 0 deletions in/foo_csv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
A,B,C,D
3,14,10,17
2,0,2,24
2,17,19,24
3,13,1,18
13,13,7,10
10,7,13,14
12,12,9,5
16,24,3,16
11,3,2,14
19,4,5,20
8 changes: 8 additions & 0 deletions in/foo_mat.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Created by Octave 6.4.0, Thu Oct 27 08:57:13 2022 UTC <root@b3d554c31921>
# name: foo_matrix
# type: matrix
# rows: 3
# columns: 2
1 2
3 4
5 6.0999999999999996
9 changes: 7 additions & 2 deletions in/parameters.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"foobar": {
"foo_int": "42",
"foo_str": "Foobar"
"foo_int": 42,
"foo_float": 13.37,
"foo_string": "Never eat yellow snow",
"foo_enum": "bar",
"foo_array": [34, 55, 23, 43, 23],
"foo_matrix": "/in/foo_matrix.dat",
"foo_csv": "/in/foo_csv.csv"
}
}
74 changes: 72 additions & 2 deletions src/getParameter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,80 @@
const { PARAM_FILE, CONF_FILE } = process.env;
const { PARAM_FILE, CONF_FILE, TOOL_RUN } = process.env;
const fs = require('fs');
const yaml = require('js-yaml');
const papa = require('papaparse');

const getParameter = () => {
// load parameters file
const params = require(PARAM_FILE || '/in/parameters.json');

return params;
// if no tool name was passed, return first configured tool
const toolName = TOOL_RUN ? TOOL_RUN : Object.keys(params)[0]

// load the yaml file
const config = yaml.load(fs.readFileSync(CONF_FILE || '/src/tool.yml'))
const toolConf = config['tools'][toolName]

// create the kwargs
const kwargs = {}

// foreach param passed
Object.entries(params[toolName]).forEach(([key, val]) => {
// get the param conf
const parConf = toolConf[key]

// if it is not specified, add anyway
if (!parConf) {
kwargs[key] = val
} else {
// parse dates
if (['date', 'time', 'datetime'].includes(parConf['type'].toLowerCase())) {
kwargs[key] = new Date(val)
}

if (parConf['type'] === 'file') {
ext = val.split('.').pop().toLowerCase()

// switch the file extension
// json
if (ext === 'json') {
kwargs[key] = require(val)
}

// dat
else if (ext === 'dat') {
raw = fs.readFileSync(val)
const dat = []
raw.split('\n').forEach(line => {
if (!line.startsWith('#')) {
dat.push(line.split(' '))
}
})

// add data
kwargs[key] = dat

}

// read csv data
else if (ext === 'csv') {
//
raw = fs.readFileSync(val).split('\n')
const recs = papa.parse(raw, {header: true})

// add
kwargs[key] = recs
}

// else the tool should handle it
else {
// pass the file path anyway, because we don't know this file
kwargs[key] = val
}
}
}
})

return kwargs;
}

module.exports = getParameter
2 changes: 1 addition & 1 deletion src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ params = getParameter();
// switch the toolName
if (toolName === 'foobar') {
console.log('You are running the template directly. Please change the foobar function.')
console.log(params[toolName])
console.log(params)
} else {
console.error(`The toolname ${toolName} is not recognized. Did you forget to implement it?`)
}
27 changes: 21 additions & 6 deletions src/tool.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
tools:
foobar:
title: Foobar function
description: Dummy default function of a toolbox template
version: 1.0
title: Foo Bar
description: A dummy tool to exemplify the YAML file
version: 0.1
parameters:
foo_int:
foo_int:
type: integer
foo_str:
type: string
foo_float:
type: float
foo_string:
type: string
foo_enum:
type: enum
values:
- foo
- bar
- baz
foo_array:
type: integer
array: true
foo_matrix:
type: file
foo_csv:
type: file

0 comments on commit 6bf4cd0

Please sign in to comment.