<commit_before>/*
 * Copyright (c) 2013, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE.txt file for terms.
 */


/*jslint nomen:true, anon:true, node:true */
"use strict";


var libfs       = require('fs'),
    liblang     = require('yui').YUI().Lang,
    liboop      = require('yui/oop'),
    libpath     = require('path'),
    libpromise  = require('yui/promise'),
    libycb      = require('ycb'),
    MESSAGES = {
        'unknown bundle': 'Unknown bundle "{bundle}"',
        'unknown config': 'Unknown config "{config}" in bundle "{bundle}"',
        'missing dimensions': 'Failed to find a dimensions.json file',
        'parse error': 'Failed to parse "{path}"\n{message}'
    };


/**
 * A component for reading configuration files.
 * @module ModownConfig
 */


/**
 * Determines if a config is YCB or not.
 * @private
 * @static
 * @method contentsIsYCB
 * @param {mixed} contents The config contents.
 * @return {boolean} Whether the config is YCB or not.
 */
function contentsIsYCB(contents) {
    var s,
        section;
    // not sure what this is
    if ('object' !== typeof contents) {
        return false;
    }
    if (Array.isArray(contents) && contents.length) {
        for (s = 0; s < contents.length; s += 1) {
            section = contents[s];
            if (!section.settings) {
                return false;
            }
            if (!Array.isArray(section.settings)) {
                return false;
            }
        }
        return true;
    }
    return false;
}


/**
 * Create the YCB object.
 * @private
 * @static
 * @method makeYCB
 * @param {Config} config The config object.
 * @param {object} dimensions The dimensions definitions.
 * @param {array} contents The contents of the YCB file.
 * @return {YCB} The YCB object.
 */
function makeYCB(config, dimensions, contents) {
    var ycbBundle,
        ycb,
        originalRead,
        originalReadNoMerge;

    ycbBundle = [{dimensions: dimensions}];
    // need to copy contents, since YCB messes with it
    ycbBundle = ycbBundle.concat(liboop.clone(contents, true));
    ycb = new libycb.Ycb(ycbBundle);

    // monkey-patch to apply baseContext
    originalRead = ycb.read;
    ycb.read = function (context, options) {
        return originalRead.call(ycb, config._mergeBaseContext(context), options);
    };
    originalReadNoMerge = ycb.readNoMerge;
    ycb.readNoMerge = function (context, options) {
        return originalReadNoMerge.call(ycb, config._mergeBaseContext(context), options);
    };
    return ycb;
}


/**
 * Create an object that presents the same interface as a YCB object.
 * @private
 * @static
 * @method makeFakeYCB
 * @param {object} dimensions The dimensions definitions.
 * @param {array} contents The contents of the YCB file.
 * @return {YCB} The fake YCB object.
 */
 function makeFakeYCB(dimensions, contents) {
    return {
        getDimensions: function () {
            return dimensions;
        },
        walkSettings: function (callback) {
            callback({}, contents);
        },
        read: function () {
            return contents;
        },
        readNoMerge: function () {
            return [contents];
        }
    };
}


/**
 * @class ModownConfig
 * @constructor
 * @param {object} [options] Options for how the configuration files are handled.
 * @param {object} [options.baseContext] Context to apply to all reads.
 * @param {string} [options.dimensionsBundle] Bundle in which to find the dimensions file.
 * See `readDimensions()` for details.
 * @param {string} [options.dimensionsPath] Full path to the dimensions file.<commit_msg>delinted<commit_after>/*
 * Copyright (c) 2013, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE.txt file for terms.
 */


/*jslint nomen:true, anon:true, node:true */
"use strict";


var libfs       = require('fs'),
    liblang     = require('yui').YUI().Lang,
    liboop      = require('yui/oop'),
    libpath     = require('path'),
    libpromise  = require('yui/promise'),
    libycb      = require('ycb'),
    MESSAGES = {
        'unknown bundle': 'Unknown bundle "{bundle}"',
        'unknown config': 'Unknown config "{config}" in bundle "{bundle}"',
        'missing dimensions': 'Failed to find a dimensions.json file',
        'parse error': 'Failed to parse "{path}"\n{message}'
    };


/**
 * A component for reading configuration files.
 * @module ModownConfig
 */


/**
 * Determines if a config is YCB or not.
 * @private
 * @static
 * @method contentsIsYCB
 * @param {mixed} contents The config contents.
 * @return {boolean} Whether the config is YCB or not.
 */
function contentsIsYCB(contents) {
    var s,
        section;
    // not sure what this is
    if ('object' !== typeof contents) {
        return false;
    }
    if (Array.isArray(contents) && contents.length) {
        for (s = 0; s < contents.length; s += 1) {
            section = contents[s];
            if (!section.settings) {
                return false;
            }
            if (!Array.isArray(section.settings)) {
                return false;
            }
        }
        return true;
    }
    return false;
}


/**
 * Create the YCB object.
 * @private
 * @static
 * @method makeYCB
 * @param {Config} config The config object.
 * @param {object} dimensions The dimensions definitions.
 * @param {array} contents The contents of the YCB file.
 * @return {YCB} The YCB object.
 */
function makeYCB(config, dimensions, contents) {
    var ycbBundle,
        ycb,
        originalRead,
        originalReadNoMerge;

    ycbBundle = [{dimensions: dimensions}];
    // need to copy contents, since YCB messes with it
    ycbBundle = ycbBundle.concat(liboop.clone(contents, true));
    ycb = new libycb.Ycb(ycbBundle);

    // monkey-patch to apply baseContext
    originalRead = ycb.read;
    ycb.read = function (context, options) {
        return originalRead.call(ycb, config._mergeBaseContext(context), options);
    };
    originalReadNoMerge = ycb.readNoMerge;
    ycb.readNoMerge = function (context, options) {
        return originalReadNoMerge.call(ycb, config._mergeBaseContext(context), options);
    };
    return ycb;
}


/**
 * Create an object that presents the same interface as a YCB object.
 * @private
 * @static
 * @method makeFakeYCB
 * @param {object} dimensions The dimensions definitions.
 * @param {array} contents The contents of the YCB file.
 * @return {YCB} The fake YCB object.
 */
function makeFakeYCB(dimensions, contents) {
    return {
        getDimensions: function () {
            return dimensions;
        },
        walkSettings: function (callback) {
            callback({}, contents);
        },
        read: function () {
            return contents;
        },
        readNoMerge: function () {
            return [contents];
        }
    };
}


/**
 * @class ModownConfig
 * @constructor
 * @param {object} [options] Options for how the configuration files are handled.
 * @param {object} [options.baseContext] Context to apply to all reads.
 * @param {string} [options.dimensionsBundle] Bundle in which to find the dimensions file.
 * See `readDimensions()` for details.
 * @param {string} [options.dimensionsPath] Full path to the dimensions file.
{'fraction_non_alphanumeric': 0.11955337690631808, 'fraction_numerical': 0.0016339869281045752, 'mean_word_length': 2.6004901960784315, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3248947', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2086, 'n_words': 673}