Skip to content

Instantly share code, notes, and snippets.

@thisismedium
Created September 30, 2010 18:22
Show Gist options
  • Save thisismedium/605051 to your computer and use it in GitHub Desktop.
Save thisismedium/605051 to your computer and use it in GitHub Desktop.
Evaluate a YAML document in Node.JS using Python.
// Evaluate a YAML document.
//
// + text - String YAML document
// + next - Function callback
//
// For example:
//
// loadYAML("this: is\nyaml:", function(err, obj) {
// if (err) throw err;
// console.dir(obj);
// });
//
// Returns nothing.
function loadYAML(text, next) {
var exec = require('child_process').exec,
script = 'import json, yaml, sys; json.dump(yaml.load(sys.stdin), sys.stdout)',
py = exec("python -c '" + script + "'", function(err, stdout, stderr) {
err ? next(err) : next(null, JSON.parse(stdout));
});
py.stdin.end(text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment