<commit_before>var fs      = require("fs");
var cheerio = require("cheerio");
var _       = require('underscore');
var moment  = require('moment');
var argv    = require('argv');
var net     = require('net');
var path    = require('path');
var repl    = require('repl');
var csv     = require('csv');
var wkhtmltopdf = require('wkhtmltopdf');

// Args
argv.option({
  name: 'input',
  short: 'i',
  type: 'string',
  description: 'Defines schema file to open',
  example: "'beardo.js --input=value' or 'beardo.js -i data/hours.json"
});

argv.option({
  name: 'format',
  short: 'f',
  type: 'csv,string',
  description: 'Defines output formats with html,cli',
  example: "'beardo.js --format=value' or 'beardo.js -f value1,value2'"
});

argv.option({
  name: 'output',
  short: 'o',
  type: 'string',
  description: 'Defines name to save output files as',
  example: "'beardo.js --save=value' or 'beardo.js -s January Invoice'"
});

argv.option({
  name: 'date',
  short: 'd',
  type: 'string',
  description: 'Returns only by current date (currently month only)',
  example: "'beardo.js --date=value' or 'beardo.js -d January or Jan or 01'"
});


argv.option({
  name: 'trim',
  short: 't',
  type: 'list,string',
  description: 'Trims output by a given string value declared in schema',
  example: "'beardo.js --trim=value' or 'beardo.js -t Client'"
});

var args = argv.run();


// File Manipulation
var SaveFile = require('./lib/save_file');

// Beardo
var Beardo = {};

Beardo.Date = {};

Beardo.Date.full = function(date) {

  var this_date = date.replace(/-/g,'');
  var filter_date = args.options.date.replace(/-/g, '');

  if (this_date >= filter_date) {
    //console.log('this date: 'this_date + ' is greater than filter date: ' + filter_date);
    return true;
  } else {
    return false;
  }
};

Beardo.Date.year_month = function(date) {

  if (date.indexOf(args.options.date) > -1) {
    //console.log('date filter by year_month ' + date);
    return true;
  } else {
    return false;
  }
};

Beardo.Date.month_day = function(date) {

  if (date.indexOf(args.options.date) > -1) {
    //console.log('date filter by month_day ' + date);
    return true;
  } else {
    return false;
  }
};

Beardo.Date.year = function(date) {

  if (date.indexOf(args.options.date) > -1) {
    //console.log('date filter by year ' + date);
    return true;
  } else {
    return false;
  }
};

Beardo.Date.month = function(date) {

  var date_trim = args.options.date.toLowerCase();
  var date_full = moment(date).format('MMMM').toLowerCase();
  var date_abbr = moment(date).format('MMM').toLowerCase();
  var date_numb = moment(date).format('MM').toLowerCase();

  if (_.indexOf([date_full, date_abbr, date_numb], date_trim) > -1) {
    //console.log('date matches filter: ' + date_trim)
    return true;
  } else {
    return false;
  }
};


Beardo.Date.none = function(parts) {
  console.log('no date filtering performed');
};


Beardo.Trim = function(parts) {
  console.log(parts);
  if (args.options.trim !== undefined) {
    var part = parts[3].trim();

    if (_.indexOf(args.options.trim, part) > -1) {
      //console.log('client: ' + part + ' matches: ' + args.options.trim);
      return true;
    } else {
      return false;
    }
  } else {
    return true;
  }
};


Beardo.getIngredients = function(config_file, callback){
  // Finds the config file if it exists.
  fs.exists(config_file, function(exists) {
    if (exists) {
      console.log("Found a config file");
      var contents = fs.readFileSync(config_file);
      var config = JSON.parse(contents);
      return callback(config);
    } else {
      return callback({'exists': exists})
    }
  });
}

Beardo.summonUser = function(callback){
  // TODO: We should probably remove the depency on args here, and pass it in
  // as a variable.
  if (args.config !== undefined && args.config.user !== undefined){
    return callback(args.config.user);
  } else {
    return callback({'exists': false});
  }
};

Beardo.magickData = function(data, resource, outputs) {

  // Filter by Date / Type
  var date_filter = 'none';

  if (args.options.date !== undefined) {

    var is_date_full        = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
    var is_date_year_month  = /[0-9]{4}-[0-9]{2}/;
    var is_date_month_day   = /[0-9]{2}-[0-9]{2}/;
    var is_date_year        = /[0-9]{4}/;

    if (is_date_full.exec(args.options.date)) {
      date_filter = 'full';
    } else if (is_date_year_month.exec(args.options.date)) {
      date_filter = 'year_month';
    } else if (is_date_month_day.exec(args.options.date)) {
      date_filter = 'month_day';
    } else if (is_date_year.exec(args.options.date)) {
      date_filter = 'year';
    } else {
      date_filter = 'month';
    }

    console.log('filter date by: ' + date_filter);
  }

  // Make Totals
  var increment_output = function(item) {

    outputs.totals.hours += item.time;
    outputs.totals.money += (item.time * item.rate);

    // CLI format
    outputs.cli += item.date + '\t ' + item.time + ' \t' + item.client + ' \t' + item.description + '\n';

    // HTML format
    if (_.indexOf(args.options.format, 'html') > -1 || _.indexOf(args.options.format, 'pdf') > -1) {
      outputs.html += '<tr>\n';
      outputs.html += '   <td>' + item.date + '</td>\n';
      outputs.html += '   <td class="text-right">' + item.time + '</td>\n';
      outputs.html += '   <td class="text-left"> hrs</td>\n';
      outputs.html += '   <td>' + item.description + '</td>\n';
      outputs.html += '</tr>\n';
    }
  };

  _.each(data, function(line, index){
    if (index !== 0){ // skip the first line
      var parts = line;
      // Filter Date & Trim
      var check_date = Beardo.Date[date_filter](parts[0]);
      var check_trim = Beardo.Trim(parts);

      if (_.indexOf([check_date, check_trim], false) === -1) {
        var item_output = Beardo.murmurLineToSchema(line, resource.schema);

        increment_output(item_output);
      }
    }
  });

  return outputs;
}

Beardo.murmurLineToSchema = function(parts, schema, fields) {
  // line is the line being processed,
  // schema is the schema provided in the resource,
  // fields is the fields required in the output, if blank it
  // defaults to everything in the schema.
  // var parts = line.split(',');
  var lineItem = {};

  _.each(schema.fields, function(field, index){
    if (fields === undefined || fields.indexOf(field.name) !== -1){
      var parsed;

      if (field.type === 'date') {
        parsed = moment(parts[index]).format('D MMM');
      }
      if (field.type === 'number'){
        parsed = parseFloat(parts[index]);
      }
      if (field.type === 'string'){
        parsed = parts[index].trim();
      }
      if (field.type === 'boolean'){
        parsed = parts[index];
        if (parsed === 'yes') parsed = true;
        else if (parsed === 'no') parse = false;
      }

      // For Output
      lineItem[field.name] = parsed;

    }
  });
  return lineItem;
};

Beardo.castToHTML = function(outputs, user){
  // Output HTML

  if (_.indexOf(args.options.format, 'html') > -1 || _.indexOf(args.options.format, 'pdf') > -1) {

    // Get HTML Template
    var template = args.config.invoice_template || 'invoice';
    console.log("Using template:", template);
    var template_path = './templates/' + template + '.html';

    // Open
    fs.exists(template_path, function(exists) {
      if (exists) {

        console.log('Beardo loaded template');
        fs.stat(template_path, function(error, stats) {
          fs.open(template_path, "r", function(error, fd) {
            var buffer = new Buffer(stats.size);
            fs.read(fd, buffer, 0, buffer.length, null, function(error, bytesRead, buffer) {
              fs.close(fd);

              // Invoice Name
              var output_name = 'Beardo - ' + moment().format('D MMMM YYYY');
              if (args.options.output) {
                output_name = args.options.output;
              }

              var template_file = buffer.toString("utf8", 0, buffer.length);
              var template_html = _.template(template_file);

              var template_data = {
                generated_name: output_name,
                generated_date: moment().format('Do MMMM, YYYY'),
                hours_rows: outputs.html,
                hours_total: outputs.totals.hours,
                money_total: outputs.totals.money
              };

              if (user !== undefined){
                template_data.user = user;
              }<commit_msg>Remove console statement<commit_after>var fs      = require("fs");
var cheerio = require("cheerio");
var _       = require('underscore');
var moment  = require('moment');
var argv    = require('argv');
var net     = require('net');
var path    = require('path');
var repl    = require('repl');
var csv     = require('csv');
var wkhtmltopdf = require('wkhtmltopdf');

// Args
argv.option({
  name: 'input',
  short: 'i',
  type: 'string',
  description: 'Defines schema file to open',
  example: "'beardo.js --input=value' or 'beardo.js -i data/hours.json"
});

argv.option({
  name: 'format',
  short: 'f',
  type: 'csv,string',
  description: 'Defines output formats with html,cli',
  example: "'beardo.js --format=value' or 'beardo.js -f value1,value2'"
});

argv.option({
  name: 'output',
  short: 'o',
  type: 'string',
  description: 'Defines name to save output files as',
  example: "'beardo.js --save=value' or 'beardo.js -s January Invoice'"
});

argv.option({
  name: 'date',
  short: 'd',
  type: 'string',
  description: 'Returns only by current date (currently month only)',
  example: "'beardo.js --date=value' or 'beardo.js -d January or Jan or 01'"
});


argv.option({
  name: 'trim',
  short: 't',
  type: 'list,string',
  description: 'Trims output by a given string value declared in schema',
  example: "'beardo.js --trim=value' or 'beardo.js -t Client'"
});

var args = argv.run();


// File Manipulation
var SaveFile = require('./lib/save_file');

// Beardo
var Beardo = {};

Beardo.Date = {};

Beardo.Date.full = function(date) {

  var this_date = date.replace(/-/g,'');
  var filter_date = args.options.date.replace(/-/g, '');

  if (this_date >= filter_date) {
    //console.log('this date: 'this_date + ' is greater than filter date: ' + filter_date);
    return true;
  } else {
    return false;
  }
};

Beardo.Date.year_month = function(date) {

  if (date.indexOf(args.options.date) > -1) {
    //console.log('date filter by year_month ' + date);
    return true;
  } else {
    return false;
  }
};

Beardo.Date.month_day = function(date) {

  if (date.indexOf(args.options.date) > -1) {
    //console.log('date filter by month_day ' + date);
    return true;
  } else {
    return false;
  }
};

Beardo.Date.year = function(date) {

  if (date.indexOf(args.options.date) > -1) {
    //console.log('date filter by year ' + date);
    return true;
  } else {
    return false;
  }
};

Beardo.Date.month = function(date) {

  var date_trim = args.options.date.toLowerCase();
  var date_full = moment(date).format('MMMM').toLowerCase();
  var date_abbr = moment(date).format('MMM').toLowerCase();
  var date_numb = moment(date).format('MM').toLowerCase();

  if (_.indexOf([date_full, date_abbr, date_numb], date_trim) > -1) {
    //console.log('date matches filter: ' + date_trim)
    return true;
  } else {
    return false;
  }
};


Beardo.Date.none = function(parts) {
  console.log('no date filtering performed');
};


Beardo.Trim = function(parts) {
  if (args.options.trim !== undefined) {
    var part = parts[3].trim();

    if (_.indexOf(args.options.trim, part) > -1) {
      //console.log('client: ' + part + ' matches: ' + args.options.trim);
      return true;
    } else {
      return false;
    }
  } else {
    return true;
  }
};


Beardo.getIngredients = function(config_file, callback){
  // Finds the config file if it exists.
  fs.exists(config_file, function(exists) {
    if (exists) {
      console.log("Found a config file");
      var contents = fs.readFileSync(config_file);
      var config = JSON.parse(contents);
      return callback(config);
    } else {
      return callback({'exists': exists})
    }
  });
}

Beardo.summonUser = function(callback){
  // TODO: We should probably remove the depency on args here, and pass it in
  // as a variable.
  if (args.config !== undefined && args.config.user !== undefined){
    return callback(args.config.user);
  } else {
    return callback({'exists': false});
  }
};

Beardo.magickData = function(data, resource, outputs) {

  // Filter by Date / Type
  var date_filter = 'none';

  if (args.options.date !== undefined) {

    var is_date_full        = /[0-9]{4}-[0-9]{2}-[0-9]{2}/;
    var is_date_year_month  = /[0-9]{4}-[0-9]{2}/;
    var is_date_month_day   = /[0-9]{2}-[0-9]{2}/;
    var is_date_year        = /[0-9]{4}/;

    if (is_date_full.exec(args.options.date)) {
      date_filter = 'full';
    } else if (is_date_year_month.exec(args.options.date)) {
      date_filter = 'year_month';
    } else if (is_date_month_day.exec(args.options.date)) {
      date_filter = 'month_day';
    } else if (is_date_year.exec(args.options.date)) {
      date_filter = 'year';
    } else {
      date_filter = 'month';
    }

    console.log('filter date by: ' + date_filter);
  }

  // Make Totals
  var increment_output = function(item) {

    outputs.totals.hours += item.time;
    outputs.totals.money += (item.time * item.rate);

    // CLI format
    outputs.cli += item.date + '\t ' + item.time + ' \t' + item.client + ' \t' + item.description + '\n';

    // HTML format
    if (_.indexOf(args.options.format, 'html') > -1 || _.indexOf(args.options.format, 'pdf') > -1) {
      outputs.html += '<tr>\n';
      outputs.html += '   <td>' + item.date + '</td>\n';
      outputs.html += '   <td class="text-right">' + item.time + '</td>\n';
      outputs.html += '   <td class="text-left"> hrs</td>\n';
      outputs.html += '   <td>' + item.description + '</td>\n';
      outputs.html += '</tr>\n';
    }
  };

  _.each(data, function(line, index){
    if (index !== 0){ // skip the first line
      var parts = line;
      // Filter Date & Trim
      var check_date = Beardo.Date[date_filter](parts[0]);
      var check_trim = Beardo.Trim(parts);

      if (_.indexOf([check_date, check_trim], false) === -1) {
        var item_output = Beardo.murmurLineToSchema(line, resource.schema);

        increment_output(item_output);
      }
    }
  });

  return outputs;
}

Beardo.murmurLineToSchema = function(parts, schema, fields) {
  // line is the line being processed,
  // schema is the schema provided in the resource,
  // fields is the fields required in the output, if blank it
  // defaults to everything in the schema.
  // var parts = line.split(',');
  var lineItem = {};

  _.each(schema.fields, function(field, index){
    if (fields === undefined || fields.indexOf(field.name) !== -1){
      var parsed;

      if (field.type === 'date') {
        parsed = moment(parts[index]).format('D MMM');
      }
      if (field.type === 'number'){
        parsed = parseFloat(parts[index]);
      }
      if (field.type === 'string'){
        parsed = parts[index].trim();
      }
      if (field.type === 'boolean'){
        parsed = parts[index];
        if (parsed === 'yes') parsed = true;
        else if (parsed === 'no') parse = false;
      }

      // For Output
      lineItem[field.name] = parsed;

    }
  });
  return lineItem;
};

Beardo.castToHTML = function(outputs, user){
  // Output HTML

  if (_.indexOf(args.options.format, 'html') > -1 || _.indexOf(args.options.format, 'pdf') > -1) {

    // Get HTML Template
    var template = args.config.invoice_template || 'invoice';
    console.log("Using template:", template);
    var template_path = './templates/' + template + '.html';

    // Open
    fs.exists(template_path, function(exists) {
      if (exists) {

        console.log('Beardo loaded template');
        fs.stat(template_path, function(error, stats) {
          fs.open(template_path, "r", function(error, fd) {
            var buffer = new Buffer(stats.size);
            fs.read(fd, buffer, 0, buffer.length, null, function(error, bytesRead, buffer) {
              fs.close(fd);

              // Invoice Name
              var output_name = 'Invoice - ' + moment().format('D MMMM YYYY');
              if (args.options.output) {
                output_name = args.options.output;
              }

              var template_file = buffer.toString("utf8", 0, buffer.length);
              var template_html = _.template(template_file);

              var template_data = {
                generated_name: output_name,
                generated_date: moment().format('Do MMMM, YYYY'),
                hours_rows: outputs.html,
                hours_total: outputs.totals.hours,
                money_total: outputs.totals.money
              };

              if (user !== undefined){
                template_data.user = user;
              }
{'fraction_non_alphanumeric': 0.16600047025628967, 'fraction_numerical': 0.005290383258876087, 'mean_word_length': 2.6236421725239616, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3970065', 'n_tokens_mistral': 5663, 'n_tokens_neox': 5551, 'n_words': 1333}