pyconf.js/README.md

63 lines
1.3 KiB
Markdown
Raw Normal View History

2018-09-03 19:29:19 +00:00
# pyconf.js
<small>formerly `node-config-python`</small>
2015-12-19 18:15:22 +00:00
2015-12-19 18:19:09 +00:00
Read and write python config files non-destructively (preserves comments and line-order)
2015-12-19 18:15:22 +00:00
Turns this kind of thing:
```python
foo = True
bar = None
baz = whatever
qux = apples,bananas
```
Into this kind of thing:
```javascript
{ foo: true
, bar: null
, baz: "whatever"
, qux: ["apples", "bananas"]
}
```
(comments are stored in meta-data keys `__lines` and `__keys`)
2015-12-19 18:18:54 +00:00
## Install
2015-12-19 18:15:22 +00:00
```bash
npm install --save pyconf
```
2015-12-19 18:18:54 +00:00
## Usage
2015-12-19 18:15:22 +00:00
```javascript
var pyconf = require('pyconf');
2015-12-19 18:19:50 +00:00
// alias for fs.readFile() then pyconf.parse()
2015-12-19 18:15:22 +00:00
pyconf.readFile("/path/to/foo.conf", function (err, obj) {
console.log(obj);
});
2015-12-19 18:19:50 +00:00
// alias for pyconf.stringify() then safeReplace.writeFile()
2015-12-19 18:15:22 +00:00
pyconf.writeFile("/path/to/foo.conf", obj, function (err, obj) {
console.log("wrote file");
});
```
Note: the `writeFile` function uses `safe-replace` so that it will work even in environments where race conditions are possible and will also create a backup file `whatever.conf.bak` of the config being overwritten.
## API
2015-12-19 18:16:23 +00:00
```javascript
2015-12-19 18:15:56 +00:00
pyconf
2015-12-19 18:16:23 +00:00
.parse(str, cb) // => err, object
2018-09-03 19:29:19 +00:00
2015-12-19 18:16:23 +00:00
.stringify(obj, cb) // => err, text
2018-09-03 19:29:19 +00:00
2015-12-19 18:16:23 +00:00
.readFile(filename, cb) // => err, object
2018-09-03 19:29:19 +00:00
.writeFile(filename, obj, cb) // => err
2015-12-19 18:15:22 +00:00
```