<commit_before>import EventEmitter from 'parser/core/modules/EventEmitter';
import decompress from 'decompress';
import ConfigLoader from 'interface/report/ConfigLoader';

const _CACHE = {};

// asynchronously load and parse a log. returns a promise that resolves
// to the log object
export function loadLog(filename, searchPath='test-logs/') {
  if(_CACHE[filename] !== undefined) {
    return Promise.resolve(_CACHE[filename]);
  }
  return decompress(`${searchPath}${filename}.zip`).then(files => {
    const result = files.reduce((res, file) => {
      res[file.path.split('.')[0]] = JSON.parse(file.data.toString());
      return res;
    }, {});

    _CACHE[filename] = result;
    return result;
  });
}

/**
 * Suppress logging within the test.
 *
 * @param {boolean} log - Suppress console.log?
 * @param {boolean} warn - Suppress console.warn?
 * @param {boolean} error - Suppress console.error?
 */
export function suppressLogging(log, warn, error, cb) {
  const _console = {};
  if(warn) {
    _console.warn = console.warn;
    console.warn = () => undefined;
  }
  if(log) {
    _console.log = console.log;
    console.log = () => undefined;
  }
  if(error) {
    _console.error = console.error;
    console.error = () => undefined;
  }

  const res = cb();

  Object.keys(_console)
    .forEach(key => { console[key] = _console[key]; });
  return res;
}

export function parseLog(parserClass, log) {
  const friendlies = log.report.friendlies.find(({id}) => id === log.meta.player.id);
  const fight = {...log.report.fights.find(({id}) => id === log.meta.fight.id), offset_time: 0};
  const builds = ConfigLoader.getConfig(log.meta.player.specID).builds;
  const build = log.meta.player.build;
  const buildKey = builds && Object.keys(builds).find(b => builds[b].url === build);
  builds && Object.keys(builds).forEach(key => {
    builds[key].active = key === buildKey;
  });
  const parser = new parserClass(
    log.report,
    friendlies,
    fight,
    log.combatants,
    build,
    builds,
  );
  return suppressLogging(true, true, false, () => {
    parser.normalize(JSON.parse(JSON.stringify(log.events))).forEach(event => parser.getModule(EventEmitter).triggerEvent(event));
    parser.finish();
    return parser;
  });
}
<commit_msg>Update log-tools.js<commit_after>import EventEmitter from 'parser/core/modules/EventEmitter';
import decompress from 'decompress';
import ConfigLoader from 'interface/report/ConfigLoader';

const _CACHE = {};

// asynchronously load and parse a log. returns a promise that resolves
// to the log object
export function loadLog(filename, searchPath='test-logs/') {
  if(_CACHE[filename] !== undefined) {
    return Promise.resolve(_CACHE[filename]);
  }
  return decompress(`${searchPath}${filename}.zip`).then(files => {
    const result = files.reduce((res, file) => {
      res[file.path.split('.')[0]] = JSON.parse(file.data.toString());
      return res;
    }, {});

    _CACHE[filename] = result;
    return result;
  });
}

/**
 * Suppress logging within the test.
 *
 * @param {boolean} log - Suppress console.log?
 * @param {boolean} warn - Suppress console.warn?
 * @param {boolean} error - Suppress console.error?
 */
export function suppressLogging(log, warn, error, cb) {
  const _console = {};
  if(warn) {
    _console.warn = console.warn;
    console.warn = () => undefined;
  }
  if(log) {
    _console.log = console.log;
    console.log = () => undefined;
  }
  if(error) {
    _console.error = console.error;
    console.error = () => undefined;
  }

  const res = cb();

  Object.keys(_console)
    .forEach(key => { console[key] = _console[key]; });
  return res;
}

export function parseLog(parserClass, log, build = undefined) {
  const friendlies = log.report.friendlies.find(({id}) => id === log.meta.player.id);
  const fight = {...log.report.fights.find(({id}) => id === log.meta.fight.id), offset_time: 0};
  const builds = ConfigLoader.getConfig(log.meta.player.specID).builds;
  const buildKey = builds && Object.keys(builds).find(b => builds[b].url === build);
  builds && Object.keys(builds).forEach(key => {
    builds[key].active = key === buildKey;
  });
  const parser = new parserClass(
    log.report,
    friendlies,
    fight,
    log.combatants,
    null,
    build,
    builds
  );
  return suppressLogging(true, true, false, () => {
    parser.normalize(JSON.parse(JSON.stringify(log.events))).forEach(event => parser.getModule(EventEmitter).triggerEvent(event));
    parser.finish();
    return parser;
  });
}

{'fraction_non_alphanumeric': 0.164562569213732, 'fraction_numerical': 0.0008859357696566999, 'mean_word_length': 3.670113753877973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '3610272', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1433, 'n_words': 311}