Skip to content

Commit

Permalink
Comment on stages of the pipeline.
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Murphy-Skvorzov committed Dec 5, 2014
1 parent d77a7f5 commit be76a68
Show file tree
Hide file tree
Showing 22 changed files with 52 additions and 29 deletions.
3 changes: 1 addition & 2 deletions bin/antijoin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env node

var args = require('../lib/opt').parse({filename:__filename, usage:
var args = require('../lib/opt').fancy({filename:__filename, usage:
[ 'Usage: jline-antijoin (--help|--version)'
, ' jline-antijoin <join_key> <file1> <file2>'
].join("\n")});
process.stdout.on('error',process.exit);

var by = args['<join_key>']
, files = [args['<file1>'], args['<file1>']]
Expand Down
1 change: 1 addition & 0 deletions bin/by.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if ((process.argv[2] === undefined) || (process.argv[2] === '--help') || (divide
}

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var parseStream = require('./clean')
, parsePath = require('./parsePath')
Expand Down
1 change: 1 addition & 0 deletions bin/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if ((process.argv[2] === undefined) || (process.argv[2] === '--help')) {
}

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var parseStream = require('./clean');
var parsePath = require('./parsePath');
Expand Down
1 change: 1 addition & 0 deletions bin/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if ((process.argv[2] === undefined) || (process.argv[2] === '--help')) {
}

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var fs = require('fs');
var html = fs.readFileSync(__dirname+'/chart.html', {encoding:'utf8'});
Expand Down
26 changes: 14 additions & 12 deletions bin/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

function parseJlineStream(stream, options) {
options = options || {};
var logcode = {none:0, error:1, warn:2}[options.loglevel];
var diecode = {none:0, error:1, warn:2}[options.dielevel];
var logcode = {none:0, error:1, warn:2, undefined:0}[options.loglevel];
var diecode = {none:0, error:1, warn:2, undefined:0}[options.dielevel];
var log = function(msg,code){console.error(msg);};
log = options.logger || log;

Expand All @@ -18,28 +18,30 @@ function parseJlineStream(stream, options) {
record = JSON.parse(line);
recordNumber++;
this.emit('jline', record, lineNumber, line, recordNumber);
this.emit('line', record, lineNumber, line, recordNumber);
this.emit('line', line, lineNumber, line, recordNumber);
} catch(e){
// Does it look like an error?
var serious = !/^\w*(#|$)/.test(line);
var code = serious?1:2;
var name = serious?'parseError':'parseWarn';
this.emit(name, e, lineNumber, line);
this.emit('line', e, lineNumber, line);
// Does it look like an error? a comment? an empty line?
var match = line.match(/^[ \t]*(#.*|$)/);
var code=(match === null )?1 // malformed line
:(match[1].length)?2 // comment
: 3;// blank line
if (code <= logcode) log("Malformed JSON on line "+String(lineNumber)+" "+String(e));
if (code <= diecode) throw(e);
if (code === 1) this.emit('parseError' , e , lineNumber, line);
if (code === 2) this.emit('comment' , match[1], lineNumber, line);
this.emit('line' , line , lineNumber, line);
}
});
}

module.exports = parseJlineStream;

if(require.main === module) {
require('../lib/opt').parse({filename:__filename});
process.stdout.on('error',process.exit);
require('../lib/opt').fancy({filename:__filename});
var exit = 0;
parseJlineStream(process.stdin)
.on('jline', function(record, lineNumber, line, recordNumber){console.log(line);})
.on('jline', function(record, lineNumber, line, recordNumber){console.log(line);})
.on('comment', function(comment, lineNumber, line, recordNumber){console.log(line);})
.on('parseError', function(e,n,l){console.error("Malformed JSON on line", n, e); exit = 1;})
.on('end', function(){process.exit(exit);});
}
1 change: 1 addition & 0 deletions bin/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var by = process.argv[2]
, split = require('split');

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var lefts = {};
var ans = {};
Expand Down
1 change: 1 addition & 0 deletions bin/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ if(require.main === module) {
process.exit(2);
}
process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));
streamToCsv(process.stdin)
.on('csvHeader', console.log)
.on('csv' , console.log);
Expand Down
1 change: 1 addition & 0 deletions bin/csv2jl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if(require.main === module) {
}

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var csv = require('ya-csv');
var reader = csv.createCsvStreamReader(process.openStdin(), {
Expand Down
1 change: 1 addition & 0 deletions bin/csv2jla.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if(require.main === module) {
}

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var csv = require('ya-csv');
var reader = csv.createCsvStreamReader(process.openStdin(), {
Expand Down
1 change: 1 addition & 0 deletions bin/cumulative.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var code = function(record){
};

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

process.stdin
.pipe(require('split')())
Expand Down
1 change: 1 addition & 0 deletions bin/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ if(require.main === module) {
process.exit(2);
}
process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var code = (process.argv[2] === '-f')
? process.argv[3]
Expand Down
4 changes: 2 additions & 2 deletions bin/foreach.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

// Execute some code for each record.

require('../lib/opt').parse({filename:__filename, usage:
require('../lib/opt').fancy({filename:__filename, usage:
[ 'Usage: jline-foreach [--help|--version]'
, ' jline-foreach <cmd>...'
].join("\n")});
process.stdout.on('error',process.exit);

var eventHandlers = [];
process.argv.slice(2).forEach(function(arg){
Expand All @@ -32,6 +31,7 @@ function keyvals(dict, callback){
}

var source = require('./clean')(process.stdin);
source.on('comment', function(c){console.log(c);});
eventHandlers.forEach(function(tuple){
var event = tuple[0].toLowerCase();
var code = Function('require','emit','keyvals', 'record', 'lineNumber', 'line', 'recordNumber', tuple[1])
Expand Down
1 change: 1 addition & 0 deletions bin/intersect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var by = process.argv[2]
, split = require('split');

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var ans = {};

Expand Down
1 change: 1 addition & 0 deletions bin/join_left.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var by = process.argv[2]
, split = require('split');

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var ans = {};

Expand Down
1 change: 1 addition & 0 deletions bin/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function map(record, lineNumber, line, recordNumber){
}

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));
require('./clean')(process.stdin)
.on('jline', map);

1 change: 1 addition & 0 deletions bin/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if (process.argv[2] === '--help') {
}

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

require('./clean')(process.stdin)
.on('jline', function(record){
Expand Down
3 changes: 3 additions & 0 deletions bin/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if ((process.argv[2] === undefined) || (process.argv[2] === '--help')) {
}

process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));

var parseStream = require('./clean');
var parsePath = require('./parsePath');
Expand Down Expand Up @@ -33,8 +34,10 @@ function setPath(dict, path, val){
dict[last] = val;
}

console.log.apply(null,['#'].concat(process.argv));
var headers;
parseStream(process.stdin)
.on('comment', function(c){console.log(c);})
.on('jline', function(record){
var filtered = {};
fields.forEach(function(field){
Expand Down
13 changes: 6 additions & 7 deletions bin/sort.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#!/usr/bin/env node

if ((process.argv[2] === undefined) || (process.argv[2] === '--help')) {
console.error(require('fs').readFileSync(__filename.replace(/.js$/,'.md'),{encoding:'utf8'}));
process.exit(1);
}

process.stdout.on('error',process.exit);
var args = require('../lib/opt').fancy({filename:__filename, usage:
[ 'Usage: jline-sort [--help|--version]'
, ' jline-sort <key>'
].join("\n")});

var split = require('split')
, parsePath = require('./parsePath')
, parseData = require('./clean')
, getPath = require('tree-math').getPath;

var by = parsePath(process.argv[2]) // sort by this
var by = parsePath(args['<key>']) // sort by this
, lines = [];


parseData(process.stdin)
.on('comment', function(c){console.log(c);})
.on('jline', function(line){
lines.push(line);
})
Expand Down
7 changes: 7 additions & 0 deletions lib/opt.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ function handle(opts, vars, handlers){
if(opts['--'+handler.name]) handler(opts, vars);
});
}
function fancy(vars){
var ans = parse(vars);
process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv.map(JSON.stringify)));
return ans;
}

module.exports =
{ parse: parse
, handle: handle
, help: help
, fancy: fancy
, version:version
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jline",
"version": "0.0.33",
"version": "0.0.34",
"description": "Sort, grep and join files where every line is JSON.",
"main": "index.js",
"bin": {
Expand Down
8 changes: 4 additions & 4 deletions test/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

parseJlineStream(stream)
.on('record', function(parsed, lineNumber, line){console.log(parsed);})
.on('parseWarn', function(error, lineNumber, line){console.error("Comment or blank line on line", lineNumber);})
.on('comment', function(comment,lineNumber, line){console.error("Comment or blank line on line", lineNumber);})
.on('parseError', function(error, lineNumber, line){console.error("Malformed JSON on line", lineNumber, error);})
.on('end', function(){console.log("Finito!");});

stream = require('fs').createReadStream(__dirname+"/data.clean.jsonl");
parseJlineStream(stream, {loglevel:'warn', dielevel:'error'})
.on('jline', function(parsed, lineNumber, line){console.log(parsed);});
// stream = require('fs').createReadStream(__dirname+"/data.clean.jsonl");
// parseJlineStream(stream, {loglevel:'warn', dielevel:'error'})
// .on('jline', function(parsed, lineNumber, line){console.log(parsed);});
2 changes: 1 addition & 1 deletion test/clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ set -eux

./bin/clean.js --help || (($? == 2))
./bin/clean.js <test/data.jsonl || (($? == 1))
./bin/clean.js <test/data.clean.jsonl
./bin/clean.js <test/data.clean.jsonl|| (($? == 0))

echo OK

0 comments on commit be76a68

Please sign in to comment.