<commit_before>"use strict";

var winston         = require('winston');
var fs              = require('fs');

module.exports.loadGeneralConfig = function loadGeneralConfig(configFilePath){
  return new Promise((resolve) => {
      var filePath = configFilePath;

  fs.stat(filePath, function(err, res){
    if(err){
      logger.log('error',`Load General conf file ${filePath} not exists.`, err);
      throw new Error(`Load General conf file ${filePath} not found.`);
      resolve();
    }else {
      try {
        fs.readFile(filePath, 'utf8', function (err, res) {
          if (err) {
            logger.log('error', 'Load General conf loadConfig readFile: ' + err);
            resolve();
          } else {
            var objConf = JSON.parse(res).config;

            //TODO: INCLUIR TODOS LOS PARAMTEROS OBLIGATORIOS DE CONFIG EN ESTA VALIDACIÓN:
            if (objConf.hasOwnProperty('general')) {
              resolve(objConf);
            } else {
              throw new Error('Invalid Config file, general not found.', objConf);
              resolve();
            }
          }
        });
      } catch (e) {
        throw new Error('Invalid Config file, incorrect JSON format: ' + e.message, e);
        resolve();
      }
    }
  });
});
};

module.exports.loadConfigSection = function loadConfigSection(config, section, id_config){
  return new Promise(function(resolve, reject) {

    if (config.hasOwnProperty(section)) {
      var sectionLength = config[section].length;
      var cnf = undefined;
      while (sectionLength--) {
        if (config[section][sectionLength].id === id_config) {
          cnf = config[section][sectionLength];
        }
      }

      if (cnf){
        resolve(cnf);
      }else{
        reject(`Config for ${id_config} not found in section ${section}`);
      }
    }else{
      reject(`Section ${section} not found in config file.`, config);
    }
  });
};

module.exports.loadSQLFile = function loadSQLFile(filePath){
  return new Promise((resolve) => {

  fs.stat(filePath, function(err, res){
    if(err){
      logger.log('error',`Load SQL file ${filePath} not exists.`, err);
      throw new Error(`Load SQL file ${filePath} not found.`);
      resolve();
    }else {
        fs.readFile(filePath, 'utf8', function (err, res) {
          if (err) {
            logger.log('error', 'Load SQL file readFile: ' + err);
            resolve();
          } else {
            resolve(res);
          }
        });
    }
  });
});
};

module.exports.replaceWith = function replaceWith(text, objParams){

  if(global.config.global_values){

    var gvl =  global.config.global_values.length;
    var gv = {};

    while(gvl--){
      var keymaster = Object.keys(global.config.global_values[gvl])[0];
      var valueObjects = global.config.global_values[gvl][keymaster];
      var keysValueObjects = Object.keys(valueObjects);
      var keysValueObjectsLength = keysValueObjects.length;

      while(keysValueObjectsLength--){
        var valueKey = keysValueObjects[keysValueObjectsLength];
        gv[keymaster.toUpperCase() + '_' + keysValueObjects[keysValueObjectsLength].toUpperCase()] = global.config.global_values[gvl][keymaster][valueKey];
      }
    }
    objParams = Object.assign(objParams, gv);
  }

  function pad(pad, str, padLeft) {
    if(!padLeft) padLeft = true;
    if (typeof str === 'undefined')
      return pad;
    if (padLeft) {
      return (pad + str).slice(-pad.length);
    } else {
      return (str + pad).substring(0, pad.length);
    }
  }

  var currentTime = new Date();
  objParams.DD   = pad('00',currentTime.getDate());
  objParams.MM   = pad('00',currentTime.getMonth() + 1);
  objParams.YY   = pad('00',currentTime.getFullYear().toString().substr(2,2));
  objParams.YYYY = pad('00',currentTime.getFullYear());
  objParams.HH   = pad('00',currentTime.getHours());
  objParams.mm   = pad('00',currentTime.getMinutes());
  objParams.ss   = pad('00',currentTime.getSeconds());

  var keys = Object.keys(objParams);

  function orderByLength(a, b) {
    if (a.length > b.length) {
      return 1;
    }
    if (a.length < b.length) {
      return -1;
    }
    return 0;
  }

  keys.sort(orderByLength);
  var keysLength = keys.length;

  while (keysLength--) {
    text = text.replace(new RegExp('\\:' + keys[keysLength], 'g'), objParams[keys[keysLength]] || '');
  }
  return text;<commit_msg>global param YYYY fixed<commit_after>"use strict";

var winston         = require('winston');
var fs              = require('fs');

module.exports.loadGeneralConfig = function loadGeneralConfig(configFilePath){
  return new Promise((resolve) => {
      var filePath = configFilePath;

  fs.stat(filePath, function(err, res){
    if(err){
      logger.log('error',`Load General conf file ${filePath} not exists.`, err);
      throw new Error(`Load General conf file ${filePath} not found.`);
      resolve();
    }else {
      try {
        fs.readFile(filePath, 'utf8', function (err, res) {
          if (err) {
            logger.log('error', 'Load General conf loadConfig readFile: ' + err);
            resolve();
          } else {
            var objConf = JSON.parse(res).config;

            //TODO: INCLUIR TODOS LOS PARAMTEROS OBLIGATORIOS DE CONFIG EN ESTA VALIDACIÓN:
            if (objConf.hasOwnProperty('general')) {
              resolve(objConf);
            } else {
              throw new Error('Invalid Config file, general not found.', objConf);
              resolve();
            }
          }
        });
      } catch (e) {
        throw new Error('Invalid Config file, incorrect JSON format: ' + e.message, e);
        resolve();
      }
    }
  });
});
};

module.exports.loadConfigSection = function loadConfigSection(config, section, id_config){
  return new Promise(function(resolve, reject) {

    if (config.hasOwnProperty(section)) {
      var sectionLength = config[section].length;
      var cnf = undefined;
      while (sectionLength--) {
        if (config[section][sectionLength].id === id_config) {
          cnf = config[section][sectionLength];
        }
      }

      if (cnf){
        resolve(cnf);
      }else{
        reject(`Config for ${id_config} not found in section ${section}`);
      }
    }else{
      reject(`Section ${section} not found in config file.`, config);
    }
  });
};

module.exports.loadSQLFile = function loadSQLFile(filePath){
  return new Promise((resolve) => {

  fs.stat(filePath, function(err, res){
    if(err){
      logger.log('error',`Load SQL file ${filePath} not exists.`, err);
      throw new Error(`Load SQL file ${filePath} not found.`);
      resolve();
    }else {
        fs.readFile(filePath, 'utf8', function (err, res) {
          if (err) {
            logger.log('error', 'Load SQL file readFile: ' + err);
            resolve();
          } else {
            resolve(res);
          }
        });
    }
  });
});
};

module.exports.replaceWith = function replaceWith(text, objParams){

  if(global.config.global_values){

    var gvl =  global.config.global_values.length;
    var gv = {};

    while(gvl--){
      var keymaster = Object.keys(global.config.global_values[gvl])[0];
      var valueObjects = global.config.global_values[gvl][keymaster];
      var keysValueObjects = Object.keys(valueObjects);
      var keysValueObjectsLength = keysValueObjects.length;

      while(keysValueObjectsLength--){
        var valueKey = keysValueObjects[keysValueObjectsLength];
        gv[keymaster.toUpperCase() + '_' + keysValueObjects[keysValueObjectsLength].toUpperCase()] = global.config.global_values[gvl][keymaster][valueKey];
      }
    }
    objParams = Object.assign(objParams, gv);
  }

  function pad(pad, str, padLeft) {
    if(!padLeft) padLeft = true;
    if (typeof str === 'undefined')
      return pad;
    if (padLeft) {
      return (pad + str).slice(-pad.length);
    } else {
      return (str + pad).substring(0, pad.length);
    }
  }

  var currentTime = new Date();
  objParams.DD   = pad('00',currentTime.getDate());
  objParams.MM   = pad('00',currentTime.getMonth() + 1);
  objParams.YY   = pad('00',currentTime.getFullYear().toString().substr(2,2));
  objParams.YYYY = currentTime.getFullYear().toString();
  objParams.HH   = pad('00',currentTime.getHours());
  objParams.mm   = pad('00',currentTime.getMinutes());
  objParams.ss   = pad('00',currentTime.getSeconds());

  var keys = Object.keys(objParams);

  function orderByLength(a, b) {
    if (a.length > b.length) {
      return 1;
    }
    if (a.length < b.length) {
      return -1;
    }
    return 0;
  }

  keys.sort(orderByLength);
  var keysLength = keys.length;

  while (keysLength--) {
    text = text.replace(new RegExp('\\:' + keys[keysLength], 'g'), objParams[keys[keysLength]] || '');
  }
  return text;
{'fraction_non_alphanumeric': 0.14661354581673305, 'fraction_numerical': 0.005236198064883324, 'mean_word_length': 2.638095238095238, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '565012', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2676, 'n_words': 588}