<commit_before>/*
Parsing options and setting the defaults
*/
var url = require('url'),
	fs = require('fs'),
	glob = require('glob').sync,
	FirefoxProfile = require('firefox-profile'),
	assert = require('assert');

var noop = function() {};

var defaults = function(opts) {
	opts = opts || {};
	opts.log = opts.logger || opts.log || {
		'fatal': noop,
		'error': noop,
		'warn': noop,
		'info': noop,
		'debug': noop,
		'trace': noop
	};;
	log = opts.log;

	opts = parseConfigFile(opts);
	opts = parseSelenium(opts);
	opts = parseBrowsers(opts);
	opts = parseCredentials(opts);
	opts = parsePreScript(opts);

	validateOptions(opts);
	return opts;
};

var parsePreScript = function(opts) {
	if (opts.preScriptFile) {
		var path = require('path');
		opts.preScript = require(path.resolve(opts.preScriptFile));
	}

	opts.preScript = opts.preScript || function(browser, cb) {
		cb(null, true);
	};
	return opts;
}

// Trying to fail quickly before starting up selenium
var validateOptions = function(opts) {
	assert(typeof opts.seleniumConfig.hostname !== 'undefined', 'No hostname specified in selenium configuration');
	assert(typeof opts.browserConfig !== 'undefined', 'No browsers sepcified');
}

var parseConfigFile = function(opts) {
	if (opts.configFile) {
		try {
			config = JSON.parse(fs.readFileSync(opts.configFile, 'utf-8'));
			log.debug('Reading configuration from file %s', opts.configFile);
			if (config) {
				for (var key in opts) {
					if (typeof opts[key] !== 'undefined') {
						config[key] = opts[key];
					}
				}
			}
			opts = config;
		} catch (e) {
			log.error('Could not read or parse configuration file %s', opts.configFile, e);
		}
	}
	return opts;
}

var parseSelenium = function(opts) {
	var selenium = opts.selenium || 'localhost:4444';
	if (typeof selenium === 'string') {
		if (!selenium.match(/^http:\/\//)) {
			selenium = 'http://' + selenium
		}
		var seleniumUrl = url.parse(selenium);
		opts.seleniumConfig = {
			hostname: seleniumUrl.hostname,
			port: seleniumUrl.port,
			auth: seleniumUrl.auth
		}
	} else if (typeof selenium === 'object') {
		opts.seleniumConfig = opts.selenium;
	}

	if (typeof opts.username !== 'undefined') {
		opts.seleniumConfig.user = opts.username;
	}

	if (typeof opts.accesskey !== 'undefined') {
		opts.seleniumConfig.pwd = opts.accesskey;
	}

	opts.seleniumConfig.port = parseInt(opts.seleniumConfig.port, 10);

	return opts;
}

var browserConfig = {
	'chrome': {
		browserName: 'chrome',
		chromeOptions: {
			args: ['--disable-popup-blocking', '--enable-gpu-benchmarking', '--enable-thread-composting'],
			extensions: glob(__dirname + '/../*.crx').map(function(file) {
				return fs.readFileSync(file).toString('base64');
			})
		},
		'disable-popup-handler': true
	},
	'firefox': {
		browserName: 'firefox',
		firefox_profile: (function() {
			var fp = new FirefoxProfile();
			fp.setPreference('dom.send_after_paint_to_content', true);
			fp.setPreference('dom.disable_open_during_load', false);
			fp.updatePreferences();
			return fp.encodedSync();
		}()),
		'disable-popup-handler': true
	}
};

var parseBrowsers = function(opts) {
	var passedBrowsers = opts.browsers || opts.browser;
	if (typeof passedBrowsers === 'undefined') {
		passedBrowsers = ['chrome'];
	} else if (typeof passedBrowsers === 'string') {
		passedBrowsers = passedBrowsers.split(/[,;]/);
	}

	var result = [];

	passedBrowsers.forEach(function(passedBrowser) {
		passedBrowser = (typeof passedBrowser === 'string' ? {
			'browserName': passedBrowser
		} : passedBrowser);

		if (typeof browserConfig[passedBrowser.browserName] !== 'undefined') {
			var matchingBrowserCfg = browserConfig[passedBrowser.browserName]
			for (var key in matchingBrowserCfg) {
				if (typeof passedBrowser[key] === 'undefined') {
					passedBrowser[key] = matchingBrowserCfg[key];
				}
			}
		}

		// Extension loading for Chrome
		if (passedBrowser.browserName === 'chrome' && opts.debug === true) {
			passedBrowser.debug = true;
			passedBrowser.chromeOptions.args.push('--load-extension=' + __dirname);
			delete passedBrowser.chromeOptions.extensions;
		}
		result.push(passedBrowser);
	});
	opts.browserConfig = result;
	log.trace(opts.browserConfig);
	return opts;
};

var parseCredentials = function(opts) {
	if (opts.seleniumConfig.hostname.match(/ondemand.sauelabs.com/)) {
		opts.SAUCE_USERNAME = opts.username;
		opts.SAUCE_ACCESSKEY = opts.accesskey;
	} else if (opts.seleniumConfig.hostname.match(/ondemand.sauelabs.com/)) {
		opts.BROWSERSTACK_USERNAME = opts.username;
		opts.BROWSERSTACK_KEY = opts.key;
	}

	if (typeof opts.BROWSERSTACK_USERNAME !== 'undefined') {
		opts.browserConfig.forEach(function(browser) {
			browser['browserstack.user'] = opts.BROWSERSTACK_USERNAME;
			browser['browserstack.key'] = opts.BROWSERSTACK_KEY;
		});
	} else if (typeof opts.SAUCE_USERNAME !== 'undefined') {
		opts.seleniumConfig.username = opts.SAUCE_USERNAME;
		opts.seleniumConfig.pwd = opts.SAUCE_ACCESSKEY;
	}
	return opts;
}

module.exports = {
	defaults: defaults,
	validateOptions: validateOptions,
	parseConfigFile: parseConfigFile,
	parseBrowsers: parseBrowsers,
	parseCredentials: parseCredentials,
	parseSelenium: parseSelenium,
	browserConfig: browserConfig
}<commit_msg>Fix misspelling saucelabs<commit_after>/*
Parsing options and setting the defaults
*/
var url = require('url'),
	fs = require('fs'),
	glob = require('glob').sync,
	FirefoxProfile = require('firefox-profile'),
	assert = require('assert');

var noop = function() {};

var defaults = function(opts) {
	opts = opts || {};
	opts.log = opts.logger || opts.log || {
		'fatal': noop,
		'error': noop,
		'warn': noop,
		'info': noop,
		'debug': noop,
		'trace': noop
	};;
	log = opts.log;

	opts = parseConfigFile(opts);
	opts = parseSelenium(opts);
	opts = parseBrowsers(opts);
	opts = parseCredentials(opts);
	opts = parsePreScript(opts);

	validateOptions(opts);
	return opts;
};

var parsePreScript = function(opts) {
	if (opts.preScriptFile) {
		var path = require('path');
		opts.preScript = require(path.resolve(opts.preScriptFile));
	}

	opts.preScript = opts.preScript || function(browser, cb) {
		cb(null, true);
	};
	return opts;
}

// Trying to fail quickly before starting up selenium
var validateOptions = function(opts) {
	assert(typeof opts.seleniumConfig.hostname !== 'undefined', 'No hostname specified in selenium configuration');
	assert(typeof opts.browserConfig !== 'undefined', 'No browsers sepcified');
}

var parseConfigFile = function(opts) {
	if (opts.configFile) {
		try {
			config = JSON.parse(fs.readFileSync(opts.configFile, 'utf-8'));
			log.debug('Reading configuration from file %s', opts.configFile);
			if (config) {
				for (var key in opts) {
					if (typeof opts[key] !== 'undefined') {
						config[key] = opts[key];
					}
				}
			}
			opts = config;
		} catch (e) {
			log.error('Could not read or parse configuration file %s', opts.configFile, e);
		}
	}
	return opts;
}

var parseSelenium = function(opts) {
	var selenium = opts.selenium || 'localhost:4444';
	if (typeof selenium === 'string') {
		if (!selenium.match(/^http:\/\//)) {
			selenium = 'http://' + selenium
		}
		var seleniumUrl = url.parse(selenium);
		opts.seleniumConfig = {
			hostname: seleniumUrl.hostname,
			port: seleniumUrl.port,
			auth: seleniumUrl.auth
		}
	} else if (typeof selenium === 'object') {
		opts.seleniumConfig = opts.selenium;
	}

	if (typeof opts.username !== 'undefined') {
		opts.seleniumConfig.user = opts.username;
	}

	if (typeof opts.accesskey !== 'undefined') {
		opts.seleniumConfig.pwd = opts.accesskey;
	}

	opts.seleniumConfig.port = parseInt(opts.seleniumConfig.port, 10);

	return opts;
}

var browserConfig = {
	'chrome': {
		browserName: 'chrome',
		chromeOptions: {
			args: ['--disable-popup-blocking', '--enable-gpu-benchmarking', '--enable-thread-composting'],
			extensions: glob(__dirname + '/../*.crx').map(function(file) {
				return fs.readFileSync(file).toString('base64');
			})
		},
		'disable-popup-handler': true
	},
	'firefox': {
		browserName: 'firefox',
		firefox_profile: (function() {
			var fp = new FirefoxProfile();
			fp.setPreference('dom.send_after_paint_to_content', true);
			fp.setPreference('dom.disable_open_during_load', false);
			fp.updatePreferences();
			return fp.encodedSync();
		}()),
		'disable-popup-handler': true
	}
};

var parseBrowsers = function(opts) {
	var passedBrowsers = opts.browsers || opts.browser;
	if (typeof passedBrowsers === 'undefined') {
		passedBrowsers = ['chrome'];
	} else if (typeof passedBrowsers === 'string') {
		passedBrowsers = passedBrowsers.split(/[,;]/);
	}

	var result = [];

	passedBrowsers.forEach(function(passedBrowser) {
		passedBrowser = (typeof passedBrowser === 'string' ? {
			'browserName': passedBrowser
		} : passedBrowser);

		if (typeof browserConfig[passedBrowser.browserName] !== 'undefined') {
			var matchingBrowserCfg = browserConfig[passedBrowser.browserName]
			for (var key in matchingBrowserCfg) {
				if (typeof passedBrowser[key] === 'undefined') {
					passedBrowser[key] = matchingBrowserCfg[key];
				}
			}
		}

		// Extension loading for Chrome
		if (passedBrowser.browserName === 'chrome' && opts.debug === true) {
			passedBrowser.debug = true;
			passedBrowser.chromeOptions.args.push('--load-extension=' + __dirname);
			delete passedBrowser.chromeOptions.extensions;
		}
		result.push(passedBrowser);
	});
	opts.browserConfig = result;
	log.trace(opts.browserConfig);
	return opts;
};

var parseCredentials = function(opts) {
	if (opts.seleniumConfig.hostname.match(/ondemand.saucelabs.com/)) {
		opts.SAUCE_USERNAME = opts.username;
		opts.SAUCE_ACCESSKEY = opts.accesskey;
	} else if (opts.seleniumConfig.hostname.match(/ondemand.saucelabs.com/)) {
		opts.BROWSERSTACK_USERNAME = opts.username;
		opts.BROWSERSTACK_KEY = opts.key;
	}

	if (typeof opts.BROWSERSTACK_USERNAME !== 'undefined') {
		opts.browserConfig.forEach(function(browser) {
			browser['browserstack.user'] = opts.BROWSERSTACK_USERNAME;
			browser['browserstack.key'] = opts.BROWSERSTACK_KEY;
		});
	} else if (typeof opts.SAUCE_USERNAME !== 'undefined') {
		opts.seleniumConfig.username = opts.SAUCE_USERNAME;
		opts.seleniumConfig.pwd = opts.SAUCE_ACCESSKEY;
	}
	return opts;
}

module.exports = {
	defaults: defaults,
	validateOptions: validateOptions,
	parseConfigFile: parseConfigFile,
	parseBrowsers: parseBrowsers,
	parseCredentials: parseCredentials,
	parseSelenium: parseSelenium,
	browserConfig: browserConfig
}
{'fraction_non_alphanumeric': 0.1512893982808023, 'fraction_numerical': 0.0017191977077363897, 'mean_word_length': 4.394641937145801, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2303741', 'n_tokens_mistral': 3871, 'n_tokens_neox': 3474, 'n_words': 700}