<commit_before>﻿var fs = require('fs')
  , ini = require('ini')
  , path = require('path')
  , extend = require('node.extend');

module.exports.parseFile = function(filePath, inheritanceLevel, cb) {
    parseFileIntern(filePath, inheritanceLevel, cb);
};

function parseFileIntern(filePath, inheritanceLevel, cb, mergedConfig) {
    if (typeof inheritanceLevel === 'function') {
        cb = inheritanceLevel;
        inheritanceLevel = undefined;
    }
    inheritanceLevel = getRealInheritanceLevel(inheritanceLevel);

    fs.exists(filePath, function(exists) {
        
        if (!exists) {
            handleInheritance(inheritanceLevel, filePath, cb, mergedConfig);
        } else {
            fs.readFile(filePath, 'ascii', function(err, fileData) {
                if (err) {
                    cb(err);
                    return;
                }

                var config = ini.parse(fileData);
                mergedConfig = mergeConfig(config, mergedConfig);

                handleInheritance(inheritanceLevel, filePath, cb, mergedConfig);
            });
        }
    });
}

module.exports.parseFileSync = function(filePath, inheritanceLevel) {
    
};

function handleInheritance(inheritanceLevel, filePath, cb, mergedConfig) {
    if (inheritanceLevel > 1) {
        var parentFile = getParentFile(filePath).toLowerCase(); //Work with absolute paths.
        filePath = path.resolve(filePath).toLowerCase(); //Work with absolute paths.
        if (parentFile != filePath) {            
            parseFileIntern(parentFile, inheritanceLevel-1, cb, mergedConfig);
            return;
        }
    }

    cb(null, mergedConfig);
}
function getParentFile(filePath) {
    var curentFileDir = path.dirname(filePath);
    currentFileDir = path.join(curentFileDir, '..');
    filePath = path.join(currentFileDir, path.basename(filePath));
    return path.resolve(path.normalize(filePath));
}

function mergeConfig(config1, config2) {
    return extend(true, config1, config2);
}

function getRealInheritanceLevel(level) {
    if (typeof level === 'number') {
        return Math.max(level, 1);
    }
    
    return 1;
}<commit_msg>Ran jslint to fix javascript formatting issues.<commit_after>﻿"use strict";

var fs = require('fs');
var ini = require('ini');
var path = require('path');
var extend = require('node.extend');

function getRealInheritanceLevel(level) {
    if (typeof level === 'number') {
        return Math.max(level, 1);
    }
    return 1;
}

function mergeConfig(config1, config2) {
    return extend(true, config1, config2);
}

function getParentFile(filePath) {
    var currentFileDir = path.dirname(filePath);
    currentFileDir = path.join(currentFileDir, '..');
    filePath = path.join(currentFileDir, path.basename(filePath));
    return path.resolve(path.normalize(filePath));
}

function handleInheritance(inheritanceLevel, filePath, cb, mergedConfig) {
    if (inheritanceLevel > 1) {
        var parentFile = getParentFile(filePath).toLowerCase(); //Work with absolute paths.
        filePath = path.resolve(filePath).toLowerCase(); //Work with absolute paths.
        if (parentFile !== filePath) {
            parseFileIntern(parentFile, inheritanceLevel - 1, cb, mergedConfig);
            return;
        }
    }

    cb(null, mergedConfig);
}

function parseFileIntern(filePath, inheritanceLevel, cb, mergedConfig) {
    if (typeof inheritanceLevel === 'function') {
        cb = inheritanceLevel;
        inheritanceLevel = undefined;
    }
    inheritanceLevel = getRealInheritanceLevel(inheritanceLevel);

    fs.exists(filePath, function (exists) {
        if (!exists) {
            handleInheritance(inheritanceLevel, filePath, cb, mergedConfig);
        } else {
            fs.readFile(filePath, 'ascii', function (err, fileData) {
                if (err) {
                    cb(err);
                    return;
                }

                var config = ini.parse(fileData);
                mergedConfig = mergeConfig(config, mergedConfig);

                handleInheritance(inheritanceLevel, filePath, cb, mergedConfig);
            });
        }
    });
}

module.exports.parseFile = function (filePath, inheritanceLevel, cb) {
    parseFileIntern(filePath, inheritanceLevel, cb);
};
{'fraction_non_alphanumeric': 0.11690140845070422, 'fraction_numerical': 0.003755868544600939, 'mean_word_length': 2.7741364038972542, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4509107', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1178, 'n_words': 256}