Options range checking, filtering, minor name changes

pull/1/head
Piero Toffanin 2016-07-28 11:28:18 -05:00
rodzic 185467b0cd
commit 6e56c31ab1
3 zmienionych plików z 125 dodań i 23 usunięć

Wyświetl plik

@ -17,20 +17,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
"use strict"; "use strict";
let odmRunner = require('./odmRunner'); let odmRunner = require('./odmRunner');
let assert = require('assert');
let options = null; let odmOptions = null;
module.exports = { module.exports = {
getOptions: function(done){ getOptions: function(done){
if (options){ if (odmOptions){
done(null, options); done(null, odmOptions);
return; return;
} }
odmRunner.getJsonOptions((err, json) => { odmRunner.getJsonOptions((err, json) => {
if (err) done(err); if (err) done(err);
else{ else{
options = {}; odmOptions = [];
for (let option in json){ for (let option in json){
// Not all options are useful to the end user // Not all options are useful to the end user
// (num cores can be set programmatically, so can gcpFile, etc.) // (num cores can be set programmatically, so can gcpFile, etc.)
@ -39,8 +40,8 @@ module.exports = {
"--start-with", "--odm_georeferencing-gcpFile", "--end-with"].indexOf(option) !== -1) continue; "--start-with", "--odm_georeferencing-gcpFile", "--end-with"].indexOf(option) !== -1) continue;
let values = json[option]; let values = json[option];
option = option.replace(/^--/, "");
let name = option.replace(/^--/, "");
let type = ""; let type = "";
let value = ""; let value = "";
let help = values.help || ""; let help = values.help || "";
@ -80,11 +81,11 @@ module.exports = {
help = help.replace(/\%\(default\)s/g, value); help = help.replace(/\%\(default\)s/g, value);
options[option] = { odmOptions.push({
type, value, domain, help name, type, value, domain, help
}; });
} }
done(null, options); done(null, odmOptions);
} }
}); });
}, },
@ -94,14 +95,116 @@ module.exports = {
// The result of filtering is passed back via callback // The result of filtering is passed back via callback
// @param options[] // @param options[]
filterOptions: function(options, done){ filterOptions: function(options, done){
assert(odmOptions !== null, "odmOptions is not set. Have you initialized odmOptions properly?");
try{ try{
if (typeof options === "string") options = JSON.parse(options); if (typeof options === "string") options = JSON.parse(options);
let result = [];
let errors = [];
function addError(opt, descr){
errors.push({
name: opt.name,
error: descr
});
}
// TODO: range checks, filtering let typeConversion = {
'float': Number.parseFloat,
'int': Number.parseInt,
'bool': function(value){
if (value === 'true') return true;
else if (value === 'false') return false;
else if (typeof value === 'boolean') return value;
else throw new Error(`Cannot convert ${value} to boolean`);
}
};
let domainChecks = [
{
regex: /^(positive |negative )?(integer|float)$/,
validate: function(matches, value){
if (matches[1] === 'positive ') return value >= 0;
else if (matches[1] === 'negative ') return value <= 0;
else if (matches[2] === 'integer') return Number.isInteger(value);
else if (matches[2] === 'float') return Number.isFinite(value);
}
},
{
regex: /^percent$/,
validate: function(matches, value){
return value >= 0 && value <= 100;
}
},
{
regex: /^(float): ([\-\+\.\d]+) <= x <= ([\-\+\.\d]+)$/,
validate: function(matches, value){
let [str, type, lower, upper] = matches;
lower = parseFloat(lower);
upper = parseFloat(upper);
return value >= lower && value <= upper;
}
},
{
regex: /^(float) (>=|>|<|<=) ([\-\+\.\d]+)$/,
validate: function(matches, value){
let [str, type, oper, bound] = matches;
bound = parseFloat(bound);
switch(oper){
case '>=':
return value >= bound;
case '>':
return value > bound;
case '<=':
return value <= bound;
case '<':
return value < bound;
default:
return false;
}
}
}
];
done(null, options); function checkDomain(domain, value){
let dc, matches;
if (dc = domainChecks.find(dc => { return matches = domain.match(dc.regex); })){
if (!dc.validate(matches, value)) throw new Error(`Invalid value ${value} (out of range)`);
}else{
throw new Error(`Domain value cannot be handled: '${domain}' : '${value}'`);
}
}
// Scan through all possible options
for (let odmOption of odmOptions){
// Was this option selected by the user?
let opt;
if (opt = options.find(o => { return o.name === odmOption.name; })){
try{
// Convert to proper data type
let value = typeConversion[odmOption.type](opt.value);
// Domain check
if (odmOption.domain){
checkDomain(odmOption.domain, value);
}
result.push({
name: odmOption.name,
value: value
});
}catch(e){
addError(opt, e.message);
}
}
}
if (errors.length > 0) done(new Error(JSON.stringify(errors)));
else done(null, result);
}catch(e){ }catch(e){
done(e); done(e);
} }
} }
}; };

Wyświetl plik

@ -54,18 +54,18 @@
<div data-bind="visible: showOptions()"> <div data-bind="visible: showOptions()">
<div data-bind="foreach: options"> <div data-bind="foreach: options">
<label data-bind="text: name + (params.domain ? ' (' + params.domain + ')' : '')"></label><br/> <label data-bind="text: properties.name + (properties.domain ? ' (' + properties.domain + ')' : '')"></label><br/>
<!-- ko if: params.type !== 'bool' --> <!-- ko if: properties.type !== 'bool' -->
<input type="text" class="form-control" data-bind="attr: {placeholder: params.value}, value: value"> <input type="text" class="form-control" data-bind="attr: {placeholder: properties.value}, value: value">
<!-- /ko --> <!-- /ko -->
<!-- ko if: params.type === 'bool' --> <!-- ko if: properties.type === 'bool' -->
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" data-bind="checked: value"> Enable <input type="checkbox" data-bind="checked: value"> Enable
</label> </label>
</div> </div>
<!-- /ko --> <!-- /ko -->
<button type="submit" class="btn glyphicon glyphicon-info-sign btn-info" data-toggle="tooltip" data-placement="top" data-bind="attr: {title: params.help}"></button> <button type="submit" class="btn glyphicon glyphicon-info-sign btn-info" data-toggle="tooltip" data-placement="top" data-bind="attr: {title: properties.help}"></button>
<button type="submit" class="btn glyphicon glyphicon glyphicon-repeat btn-default" data-toggle="tooltip" data-placement="top" title="Reset to default" data-bind="click: resetToDefault"></button> <button type="submit" class="btn glyphicon glyphicon glyphicon-repeat btn-default" data-toggle="tooltip" data-placement="top" title="Reset to default" data-bind="click: resetToDefault"></button>
<br/><br/> <br/><br/>

Wyświetl plik

@ -297,9 +297,8 @@ $(function(){
}); });
// Load options // Load options
function Option(name, params){ function Option(properties){
this.name = name; this.properties = properties;
this.params = params;
this.value = ko.observable(); this.value = ko.observable();
} }
Option.prototype.resetToDefault = function(){ Option.prototype.resetToDefault = function(){
@ -322,8 +321,8 @@ $(function(){
.done(function(json){ .done(function(json){
if (json.error) self.error(json.error); if (json.error) self.error(json.error);
else{ else{
for (var optionName in json){ for (var i in json){
self.options.push(new Option(optionName, json[optionName])); self.options.push(new Option(json[i]));
} }
} }
}) })
@ -337,7 +336,7 @@ $(function(){
var opt = this.options()[i]; var opt = this.options()[i];
if (opt.value() !== undefined){ if (opt.value() !== undefined){
result.push({ result.push({
name: opt.name, name: opt.properties.name,
value: opt.value() value: opt.value()
}); });
} }