<commit_before>define([
    "hr/hr",
    "hr/utils",
    "hr/promise",
    "core/rpc",
    "core/commands",
    "core/events"
], function(hr, _, Q, rpc, commands, events) {
    var File = hr.Model.extend({
        defaults: {
            path: null,
            name: null,
            directory: false,
            size: 0,
            mtime: 0,
            atime: 0,
            buffer: null
        },
        idAttribute: "name",

        // Initialize
        initialize: function() {
            File.__super__.initialize.apply(this, arguments);

            this.listenTo(events, "e:fs", function(e) {
                if (e != this.get("path")) return;
            });
        },

        // Open this file
        open: function() {
            return commands.run("file.open", {
                path: this.get("path")
            });
        },

        // Check if is a directory
        isDirectory: function() {
            return this.get("directory");
        },

        // Check if a file is a buffer or exists
        isBuffer: function() {
            return this.get("buffer") != null;
        },

        // List files in this directory
        list: function() {
            return rpc.execute("fs/list", {
                'path': this.get("path")
            })
            .then(function(files) {
                return _.map(files, function(file) {
                    return new File({}, file);
                });
            });
        },

        // Get a specific file
        stat: function(path) {
            var that = this;

            return rpc.execute("fs/stat", {
                'path': path
            })
            .then(function(file) {
                return that.set(file);
            })
            .thenResolve(that);
        },

        // Read file content
        read: function() {
            if (this.isBuffer()) return Q(this.get("buffer"));

            return rpc.execute("fs/read", {
                'path': this.get("path")
            })
            .get("content");
        },

        // Write file content
        write: function(content) {
            if (this.isBuffer()) return Q(this.set("buffer", content));

            return rpc.execute("fs/write", {
                'path': this.get("path"),
                'content': content
            });
        }
    }, {
        // Get a specific file
        get: function(path) {
            var f = new File();

            return f.stat(path);
        }
    });

    return File;
});<commit_msg>Make it easy to set buffer file<commit_after>define([
    "hr/hr",
    "hr/utils",
    "hr/promise",
    "core/rpc",
    "core/commands",
    "core/events"
], function(hr, _, Q, rpc, commands, events) {
    var File = hr.Model.extend({
        defaults: {
            path: null,
            name: null,
            directory: false,
            size: 0,
            mtime: 0,
            atime: 0,
            buffer: null
        },
        idAttribute: "name",

        // Initialize
        initialize: function() {
            File.__super__.initialize.apply(this, arguments);

            this.listenTo(events, "e:fs", function(e) {
                if (this.isBuffer() || e != this.get("path")) return;
            });
        },

        // Open this file
        open: function() {
            return commands.run("file.open", {
                path: this.get("path")
            });
        },

        // Check if is a directory
        isDirectory: function() {
            return this.get("directory");
        },

        // Check if a file is a buffer or exists
        isBuffer: function() {
            return this.get("buffer") != null;
        },

        // List files in this directory
        list: function() {
            return rpc.execute("fs/list", {
                'path': this.get("path")
            })
            .then(function(files) {
                return _.map(files, function(file) {
                    return new File({}, file);
                });
            });
        },

        // Get a specific file
        stat: function(path) {
            var that = this;

            return rpc.execute("fs/stat", {
                'path': path
            })
            .then(function(file) {
                return that.set(file);
            })
            .thenResolve(that);
        },

        // Read file content
        read: function() {
            if (this.isBuffer()) return Q(this.get("buffer"));

            return rpc.execute("fs/read", {
                'path': this.get("path")
            })
            .get("content");
        },

        // Write file content
        write: function(content) {
            if (this.isBuffer()) return Q(this.set("buffer", content));

            return rpc.execute("fs/write", {
                'path': this.get("path"),
                'content': content
            });
        }
    }, {
        // Get a specific file
        get: function(path) {
            var f = new File();

            return f.stat(path);
        },

        // Create a file buffer
        buffer: function(name, content) {
            var f = new File({
                name: name,
                buffer: content,
                directory: false
            });

            return f;
        }
    });

    return File;
});
{'fraction_non_alphanumeric': 0.13767161933421102, 'fraction_numerical': 0.0011284558961820576, 'mean_word_length': 1.188477366255144, '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': '2186203', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1384, 'n_words': 338}