Switch carto to be synchronous.

This commit is contained in:
Young Hahn 2014-05-20 16:01:43 -04:00
parent cd18bea7ba
commit c5d8f4510e
6 changed files with 110 additions and 138 deletions

View File

@ -205,20 +205,19 @@ The `Renderer` interface is the main API for developers, and it takes an MML fil
// - data (a string containing the MML or an object of MML) // - data (a string containing the MML or an object of MML)
var carto = require('carto'); var carto = require('carto');
new carto.Renderer({ try {
var output = new carto.Renderer({
filename: input, filename: input,
local_data_dir: path.dirname(input), local_data_dir: path.dirname(input),
}).render(data, function(err, output) { }).render(data);
if (err) { } catch(err) {
if (Array.isArray(err)) { if (Array.isArray(err)) {
err.forEach(function(e) { err.forEach(function(e) {
carto.writeError(e, options); carto.writeError(e, options);
}); });
} else { throw err; } } else { throw err; }
} else {
sys.puts(output);
} }
}); console.log(output);
### Vim ### Vim

View File

@ -65,26 +65,13 @@ function compileMML(err, data) {
console.error(err); console.error(err);
process.exit(1); process.exit(1);
} }
try {
var renderer = new carto.Renderer({ var renderer = new carto.Renderer({
filename: input, filename: input,
benchmark: options.benchmark, benchmark: options.benchmark,
ppi: options.ppi ppi: options.ppi
}); });
renderer.render(data, function(err, output) { try {
if (err) { var output = renderer.render(data);
console.error(err);
throw err;
process.exit(1);
} else {
if (!options.benchmark) {
console.log(output);
} else {
var duration = (+new Date) - start;
console.log('TOTAL: ' + (duration) + 'ms');
}
}
});
} catch (e) { } catch (e) {
if (e.stack) { if (e.stack) {
console.error(e.stack); console.error(e.stack);
@ -93,6 +80,12 @@ function compileMML(err, data) {
} }
process.exit(1); process.exit(1);
} }
if (!options.benchmark) {
console.log(output);
} else {
var duration = (+new Date) - start;
console.log('TOTAL: ' + (duration) + 'ms');
}
}; };
function compileMSS(err, data) { function compileMSS(err, data) {
@ -100,26 +93,13 @@ function compileMSS(err, data) {
console.error(err); console.error(err);
process.exit(1); process.exit(1);
} }
try {
var renderer = new carto.Renderer({ var renderer = new carto.Renderer({
filename: path.basename(input), filename: path.basename(input),
benchmark: options.benchmark, benchmark: options.benchmark,
ppi: options.ppi ppi: options.ppi
}); });
renderer.renderMSS(data, function(err, output) { try {
if (err) { var output = renderer.renderMSS(data);
console.error(err);
throw err;
process.exit(1);
} else {
if (!options.benchmark) {
console.log(output);
} else {
var duration = (+new Date) - start;
console.log('TOTAL: ' + (duration) + 'ms');
}
}
});
} catch (e) { } catch (e) {
if (e.stack) { if (e.stack) {
console.error(e.stack); console.error(e.stack);
@ -128,6 +108,12 @@ function compileMSS(err, data) {
} }
process.exit(1); process.exit(1);
} }
if (!options.benchmark) {
console.log(output);
} else {
var duration = (+new Date) - start;
console.log('TOTAL: ' + (duration) + 'ms');
}
}; };
try { try {

View File

@ -223,7 +223,7 @@ carto.Parser = function Parser(env) {
// Start with the primary rule. // Start with the primary rule.
// The whole syntax tree is held under a Ruleset node, // The whole syntax tree is held under a Ruleset node,
// with the `root` property set to true, so no `{}` are // with the `root` property set to true, so no `{}` are
// output. The callback is called when the input is parsed. // output.
root = new tree.Ruleset([], $(this.parsers.primary)); root = new tree.Ruleset([], $(this.parsers.primary));
root.root = true; root.root = true;

View File

@ -12,9 +12,8 @@ carto.Renderer = function Renderer(env, options) {
* XML Style fragment (mostly useful for debugging) * XML Style fragment (mostly useful for debugging)
* *
* @param {String} data the mss contents as a string. * @param {String} data the mss contents as a string.
* @param {Object} callback renderer environment options.
*/ */
carto.Renderer.prototype.renderMSS = function render(data, callback) { carto.Renderer.prototype.renderMSS = function render(data) {
// effects is a container for side-effects, which currently // effects is a container for side-effects, which currently
// are limited to FontSets. // are limited to FontSets.
var env = _(this.env).defaults({ var env = _(this.env).defaults({
@ -24,7 +23,7 @@ carto.Renderer.prototype.renderMSS = function render(data, callback) {
}); });
if (!carto.tree.Reference.setVersion(this.options.mapnik_version)) { if (!carto.tree.Reference.setVersion(this.options.mapnik_version)) {
return callback(new Error("Could not set mapnik version to " + this.options.mapnik_version), null); throw new Error("Could not set mapnik version to " + this.options.mapnik_version);
} }
var output = []; var output = [];
@ -58,8 +57,8 @@ carto.Renderer.prototype.renderMSS = function render(data, callback) {
if (env.benchmark) console.timeEnd(bench_name); if (env.benchmark) console.timeEnd(bench_name);
} }
if (env.benchmark) console.timeEnd('Total Style generation'); if (env.benchmark) console.timeEnd('Total Style generation');
if (env.errors) return callback(env.errors); if (env.errors) throw env.errors;
return callback(null, output.join('\n')); return output.join('\n');
}; };
/** /**
@ -67,9 +66,8 @@ carto.Renderer.prototype.renderMSS = function render(data, callback) {
* fully-localized XML file ready for Mapnik2 consumption * fully-localized XML file ready for Mapnik2 consumption
* *
* @param {String} m - the JSON file as a string. * @param {String} m - the JSON file as a string.
* @param {Object} callback - renderer environment options.
*/ */
carto.Renderer.prototype.render = function render(m, callback) { carto.Renderer.prototype.render = function render(m) {
// effects is a container for side-effects, which currently // effects is a container for side-effects, which currently
// are limited to FontSets. // are limited to FontSets.
var env = _(this.env).defaults({ var env = _(this.env).defaults({
@ -80,7 +78,7 @@ carto.Renderer.prototype.render = function render(m, callback) {
}); });
if (!carto.tree.Reference.setVersion(this.options.mapnik_version)) { if (!carto.tree.Reference.setVersion(this.options.mapnik_version)) {
return callback(new Error("Could not set mapnik version to " + this.options.mapnik_version), null); throw new Error("Could not set mapnik version to " + this.options.mapnik_version);
} }
var output = []; var output = [];
@ -89,13 +87,12 @@ carto.Renderer.prototype.render = function render(m, callback) {
var definitions = _(m.Stylesheet).chain() var definitions = _(m.Stylesheet).chain()
.map(function(s) { .map(function(s) {
if (typeof s == 'string') { if (typeof s == 'string') {
callback(new Error("Stylesheet object is expected not a string: '" + s + "'")); throw new Error("Stylesheet object is expected not a string: '" + s + "'");
} }
// Passing the environment from stylesheet to stylesheet, // Passing the environment from stylesheet to stylesheet,
// allows frames and effects to be maintained. // allows frames and effects to be maintained.
env = _(env).extend({filename:s.id}); env = _(env).extend({filename:s.id});
// @TODO try/catch?
var time = +new Date(), var time = +new Date(),
root = (carto.Parser(env)).parse(s.data); root = (carto.Parser(env)).parse(s.data);
if (env.benchmark) if (env.benchmark)
@ -149,16 +146,10 @@ carto.Renderer.prototype.render = function render(m, callback) {
return e.toXML(env); return e.toXML(env);
}).join('\n')); }).join('\n'));
var map_properties; var map_properties = getMapProperties(m, definitions, env);
try {
map_properties = getMapProperties(m, definitions, env);
} catch (err) {
env.error(err);
return callback(err);
}
// Exit on errors. // Exit on errors.
if (env.errors) return callback(env.errors); if (env.errors) throw env.errors;
// Pass TileJSON and other custom parameters through to Mapnik XML. // Pass TileJSON and other custom parameters through to Mapnik XML.
var parameters = _(m).reduce(function(memo, v, k) { var parameters = _(m).reduce(function(memo, v, k) {
@ -222,7 +213,7 @@ carto.Renderer.prototype.render = function render(m, callback) {
'<!DOCTYPE Map[]>\n' + '<!DOCTYPE Map[]>\n' +
'<Map' + properties +'>\n'); '<Map' + properties +'>\n');
output.push('</Map>'); output.push('</Map>');
return callback(null, output.join('\n')); return output.join('\n');
}; };
/** /**

View File

@ -11,34 +11,30 @@ var helper = require('./support/helper');
describe('Rendering mss', function() { describe('Rendering mss', function() {
helper.files('rendering-mss', 'mss', function(file) { helper.files('rendering-mss', 'mss', function(file) {
it('should render mss ' + path.basename(file) + ' correctly', function(done) { it('should render mss ' + path.basename(file) + ' correctly', function() {
var completed = false; var completed = false;
var renderResult; var renderResult;
var mss = helper.mss(file); var mss = helper.mss(file);
new carto.Renderer({ try {
var output = new carto.Renderer({
paths: [ path.dirname(file) ], paths: [ path.dirname(file) ],
data_dir: path.join(__dirname, '../data'), data_dir: path.join(__dirname, '../data'),
local_data_dir: path.join(__dirname, 'rendering'), local_data_dir: path.join(__dirname, 'rendering'),
filename: file filename: file
}).renderMSS(mss, function (err, output) { }).renderMSS(mss);
if (err) { } catch(err) {
if (Array.isArray(err)){ if (Array.isArray(err)){
err.forEach(carto.writeError); err.forEach(carto.writeError);
done();
} else { } else {
throw err; throw err;
done();
} }
} else { }
var expected = file.replace(path.extname(file),'')+'.xml'; var expected = file.replace(path.extname(file),'')+'.xml';
if (!existsSync(expected)) { if (!existsSync(expected)) {
fs.writeFileSync(expected,output); fs.writeFileSync(expected,output);
} }
var expected_data = fs.readFileSync(expected).toString(); var expected_data = fs.readFileSync(expected).toString();
assert.equal(output,expected_data); assert.equal(output,expected_data);
done();
}
});
}); });
}); });
}); });

View File

@ -12,19 +12,21 @@ helper.files('rendering', 'mml', function(file) {
var completed = false; var completed = false;
var renderResult; var renderResult;
var mml = helper.mml(file); var mml = helper.mml(file);
new carto.Renderer({ try {
var output = new carto.Renderer({
paths: [ path.dirname(file) ], paths: [ path.dirname(file) ],
data_dir: path.join(__dirname, '../data'), data_dir: path.join(__dirname, '../data'),
local_data_dir: path.join(__dirname, 'rendering'), local_data_dir: path.join(__dirname, 'rendering'),
filename: file filename: file
}).render(mml, function (err, output) { }).render(mml);
if (err) { } catch(err) {
if (Array.isArray(err)){ if (Array.isArray(err)){
err.forEach(carto.writeError); err.forEach(carto.writeError);
return done();
} else { } else {
throw err; return done(err);
}
} }
} else {
var result = helper.resultFile(file); var result = helper.resultFile(file);
renderResult = output; renderResult = output;
helper.compareToXMLFile(result, output, function(err,expected_json,actual_json) { helper.compareToXMLFile(result, output, function(err,expected_json,actual_json) {
@ -56,8 +58,6 @@ helper.files('rendering', 'mml', function(file) {
helper.removeAbsoluteImages, helper.removeAbsoluteImages,
helper.removeAbsoluteDatasources helper.removeAbsoluteDatasources
]); ]);
}
});
// beforeExit(function() { // beforeExit(function() {
// if (!completed && renderResult) { // if (!completed && renderResult) {