Compare commits
	
		
			No commits in common. "254eb3beda4ec883738b322c6d1cdedd2a75c644" and "6e7e24be49a25b7752594176625823dc7a3ba136" have entirely different histories.
		
	
	
		
			254eb3beda
			...
			6e7e24be49
		
	
		
							
								
								
									
										36
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								index.js
									
									
									
									
									
								
							| @ -4,7 +4,6 @@ var fs = require('fs'); | |||||||
| var sfs = require('safe-replace').create(); | var sfs = require('safe-replace').create(); | ||||||
| 
 | 
 | ||||||
| function snakeCase(key) { | function snakeCase(key) { | ||||||
|   // TODO let user supply list of exceptions
 |  | ||||||
|   if ('tlsSni01Port' === key) { |   if ('tlsSni01Port' === key) { | ||||||
|     return 'tls_sni_01_port'; |     return 'tls_sni_01_port'; | ||||||
|   } |   } | ||||||
| @ -36,7 +35,7 @@ function parsePythonConf(str, cb) { | |||||||
| 
 | 
 | ||||||
|     if (!line) { return; } |     if (!line) { return; } | ||||||
| 
 | 
 | ||||||
|     var parts = line.split('='); |     var parts = line.trim().split('='); | ||||||
|     var pykey = parts.shift().trim(); |     var pykey = parts.shift().trim(); | ||||||
|     var key = camelCase(pykey); |     var key = camelCase(pykey); | ||||||
|     var val = parts.join('=').trim(); |     var val = parts.join('=').trim(); | ||||||
| @ -47,13 +46,13 @@ function parsePythonConf(str, cb) { | |||||||
|     else if ('False' === val) { |     else if ('False' === val) { | ||||||
|       val = false; |       val = false; | ||||||
|     } |     } | ||||||
|     else if ('None' === val || '' === val) { |     else if ('None' === val) { | ||||||
|       val = null; |       val = null; | ||||||
|     } |     } | ||||||
|     else if (/,/.test(val) && !/^"[^"]*"$/.test(val)) { |     else if (/,/.test(val) && !/^"[^"]*"$/.test(val)) { | ||||||
|       val = val.split(',').map(function(x) { return x.trim(); }); |       val = val.split(','); | ||||||
|     } |     } | ||||||
|     else if (/^-?[0-9]+$/.test(val)) { |     else if (/^[0-9]+$/.test(val)) { | ||||||
|       val = parseInt(val, 10); |       val = parseInt(val, 10); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -73,7 +72,7 @@ function parsePythonConf(str, cb) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function toPyVal(val) { | function toPyVal(val) { | ||||||
|   if (null === val || '' === val) { |   if (null === val) { | ||||||
|     return 'None'; |     return 'None'; | ||||||
|   } |   } | ||||||
|   else if (true === val) { |   else if (true === val) { | ||||||
| @ -91,7 +90,7 @@ function toPyVal(val) { | |||||||
|   else if (Array.isArray(val)) { |   else if (Array.isArray(val)) { | ||||||
|     val = val.join(','); |     val = val.join(','); | ||||||
|     if (-1 === val.indexOf(',')) { |     if (-1 === val.indexOf(',')) { | ||||||
|       val += ','; // disambiguates value from array with one element
 |       val += ','; // disambguates value from array with one element
 | ||||||
|     } |     } | ||||||
|     return val;  |     return val;  | ||||||
|   } |   } | ||||||
| @ -117,13 +116,6 @@ function stringifyPythonConf(obj, cb) { | |||||||
|     var num = obj.__keys[key]; |     var num = obj.__keys[key]; | ||||||
|     var comment = ''; |     var comment = ''; | ||||||
| 
 | 
 | ||||||
|     if ('undefined' === typeof pyval) { |  | ||||||
|       if ('number' === typeof num) { |  | ||||||
|         pyval = 'None'; |  | ||||||
|       } else { |  | ||||||
|         return; |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     if ('number' !== typeof num) { |     if ('number' !== typeof num) { | ||||||
|       obj.__lines.push(pykey + ' = ' + pyval); |       obj.__lines.push(pykey + ' = ' + pyval); | ||||||
| @ -135,18 +127,22 @@ function stringifyPythonConf(obj, cb) { | |||||||
|       return; |       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
 |     // restore comments
 | ||||||
|     if (-1 !== obj.__lines[num].indexOf('#')) { |     if (-1 !== obj.__lines[num].indexOf('#')) { | ||||||
|       comment = obj.__lines[num].replace(/.*?(\s*#.*)/, '$1'); |       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[num] = pykey + ' = ' + pyval + comment; | ||||||
|  |     } | ||||||
|  |   }); | ||||||
|  | 
 | ||||||
|  |   obj.__lines = obj.__lines.filter(function (line) { | ||||||
|  |     if ("___DELETE_ME___" !== line) { | ||||||
|  |       return true; | ||||||
|  |     } | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   if ('string' === typeof endline) { |   if ('string' === typeof endline) { | ||||||
|  | |||||||
| @ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "pyconf", |   "name": "pyconf", | ||||||
|   "version": "1.1.2", |   "version": "1.1.0", | ||||||
|   "description": "Read and write python config files non-destructively (preserves comments)", |   "description": "Read and write python config files non-destructively (preserves comments)", | ||||||
|   "main": "index.js", |   "main": "index.js", | ||||||
|   "scripts": { |   "scripts": { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user