<commit_before>/*jslint node: true, nomen: true, white: true*/
/*!
 * Sitegear3
 * Copyright(c) 2014 <NAME>, Sitegear.org
 * MIT Licensed
 */

(function (_, path, fs, mkdirp, defaults) {
	"use strict";

	module.exports = function (options) {
		var convertKey = function (key) {
				return key.replace(/^\//, '').replace(/\//g, '_') + options.extension;
			};

		options = _.merge(defaults, options);
		return {
			set: function (type, key, value, callback) {
				var parentPath = path.join(options.root, type),
					filePath = path.join(parentPath, convertKey(key));
				mkdirp(parentPath, function (mkdirError) {
					if (mkdirError) {
						callback(mkdirError);
					} else {
						if (!_.isString(value)) {
							value = JSON.stringify(value, null, '\t');
						}
						fs.writeFile(filePath, value, { encoding: options.encoding }, function (writeFileError) {
							callback(writeFileError);
						});
					}
				});
			},

			get: function (type, key, callback) {
				var filePath = path.join(options.root, type, convertKey(key));
				fs.readFile(filePath, { encoding: options.encoding }, function (error, value) {
					if (_.isString(value)) {
						try {
							value = JSON.parse(value);
						} catch (parseError) {
							callback(parseError);
						}
					}
					callback(error, value);
				});
			},

			remove: function (type, key, callback) {
				var filePath = path.join(options.root, type, convertKey(key));
				fs.unlink(filePath, function (error) {
					callback(error);
				});
			}
		};
	};
}(require('lodash'), require('path'), require('fs'), require('mkdirp'), require('./defaults.json')));<commit_msg>Correct handling of errors in filesystem storage driver.<commit_after>/*jslint node: true, nomen: true, white: true*/
/*!
 * Sitegear3
 * Copyright(c) 2014 <NAME>, Sitegear.org
 * MIT Licensed
 */

(function (_, path, fs, mkdirp, defaults) {
	"use strict";

	module.exports = function (options) {
		var convertKey = function (key) {
				return key.replace(/^\//, '').replace(/\//g, '_') + options.extension;
			};

		options = _.merge(defaults, options);
		return {
			set: function (type, key, value, callback) {
				var parentPath = path.join(options.root, type),
					filePath = path.join(parentPath, convertKey(key));
				mkdirp(parentPath, function (mkdirError) {
					if (mkdirError) {
						callback(mkdirError);
					} else {
						if (!_.isString(value)) {
							value = JSON.stringify(value, null, '\t');
						}
						fs.writeFile(filePath, value, { encoding: options.encoding }, function (writeFileError) {
							callback(writeFileError);
						});
					}
				});
			},

			get: function (type, key, callback) {
				var filePath = path.join(options.root, type, convertKey(key));
				fs.readFile(filePath, { encoding: options.encoding }, function (error, value) {
					if (_.isString(value)) {
						try {
							value = JSON.parse(value);
						} catch (parseError) {
							callback(parseError);
						}
					}
					if (error && error.code === 'ENOENT') {
						error = null;
					}
					callback(error, value);
				});
			},

			remove: function (type, key, callback) {
				var filePath = path.join(options.root, type, convertKey(key));
				fs.unlink(filePath, function (error) {
					if (error && error.code === 'ENOENT') {
						error = null;
					}
					callback(error);
				});
			}
		};
	};
}(require('lodash'), require('path'), require('fs'), require('mkdirp'), require('./defaults.json')));
{'fraction_non_alphanumeric': 0.170689153823786, 'fraction_numerical': 0.002907822041291073, 'mean_word_length': 2.9585730724971233, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1445632', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1079, 'n_words': 246}