Compare commits

...

6 Commits

Author SHA1 Message Date
Mike Stegeman 254eb3beda Update Python value of null/undefined/empty. 2017-10-30 10:36:16 -07:00
Mike Stegeman e0a28b434e Fix parsing and writing of empty values.
Also, fix other parsing issues.
2017-10-30 10:36:16 -07:00
AJ ONeal 9d334161bc v1.1.2 2016-02-11 13:34:54 -05:00
AJ ONeal 38709c789e don't add 'undefined' values 2016-02-11 13:34:40 -05:00
AJ ONeal 942dc7b38a v1.1.1 2016-02-11 12:48:49 -05:00
AJ ONeal 10093659b4 warn and ignore on line mismatch 2016-02-11 12:46:08 -05:00
2 changed files with 23 additions and 19 deletions

View File

@ -4,6 +4,7 @@ var fs = require('fs');
var sfs = require('safe-replace').create();
function snakeCase(key) {
// TODO let user supply list of exceptions
if ('tlsSni01Port' === key) {
return 'tls_sni_01_port';
}
@ -35,7 +36,7 @@ function parsePythonConf(str, cb) {
if (!line) { return; }
var parts = line.trim().split('=');
var parts = line.split('=');
var pykey = parts.shift().trim();
var key = camelCase(pykey);
var val = parts.join('=').trim();
@ -46,13 +47,13 @@ function parsePythonConf(str, cb) {
else if ('False' === val) {
val = false;
}
else if ('None' === val) {
else if ('None' === val || '' === val) {
val = null;
}
else if (/,/.test(val) && !/^"[^"]*"$/.test(val)) {
val = val.split(',');
val = val.split(',').map(function(x) { return x.trim(); });
}
else if (/^[0-9]+$/.test(val)) {
else if (/^-?[0-9]+$/.test(val)) {
val = parseInt(val, 10);
}
@ -72,7 +73,7 @@ function parsePythonConf(str, cb) {
}
function toPyVal(val) {
if (null === val) {
if (null === val || '' === val) {
return 'None';
}
else if (true === val) {
@ -90,9 +91,9 @@ function toPyVal(val) {
else if (Array.isArray(val)) {
val = val.join(',');
if (-1 === val.indexOf(',')) {
val += ','; // disambguates value from array with one element
val += ','; // disambiguates value from array with one element
}
return val;
return val;
}
return val && JSON.stringify(val);
@ -116,6 +117,13 @@ function stringifyPythonConf(obj, cb) {
var num = obj.__keys[key];
var comment = '';
if ('undefined' === typeof pyval) {
if ('number' === typeof num) {
pyval = 'None';
} else {
return;
}
}
if ('number' !== typeof num) {
obj.__lines.push(pykey + ' = ' + pyval);
@ -127,22 +135,18 @@ function stringifyPythonConf(obj, cb) {
return;
}
if (!obj.__lines[num] || !obj.__lines[num].indexOf) {
console.warn('[pyconf] WARN index past array length:');
console.log(obj.__lines.length, num, obj.__lines[num]);
return;
}
// restore comments
if (-1 !== obj.__lines[num].indexOf('#')) {
comment = obj.__lines[num].replace(/.*?(\s*#.*)/, '$1');
}
if ('undefined' === typeof pyval) {
obj.__lines[num] = "___DELETE_ME___";
} else {
obj.__lines[num] = pykey + ' = ' + pyval + comment;
}
});
obj.__lines = obj.__lines.filter(function (line) {
if ("___DELETE_ME___" !== line) {
return true;
}
obj.__lines[num] = pykey + ' = ' + pyval + comment;
});
if ('string' === typeof endline) {

View File

@ -1,6 +1,6 @@
{
"name": "pyconf",
"version": "1.1.0",
"version": "1.1.2",
"description": "Read and write python config files non-destructively (preserves comments)",
"main": "index.js",
"scripts": {