<commit_before>var
    async   = require("async"),
    _       = require("underscore")._,
    exec    = require("child_process").exec,
    fs      = require("fs"),
    path    = require("path"),
    request = require("request"),
    util    = require("util")

    helpers = require("./helpers.js");

module.exports = function(log, settings_file) {
    /******************************************************************************\
    Define module globals / constants. Some of these are / can be exposed to the
    person including the module.
    \******************************************************************************/
    var
        _settings_file = settings_file,
        _settings = null,

        _ERRORS = {
            UNKNOWN_ERROR:  { id: 0, name: "UNKNOWN_ERROR" },
        },

        _DEFAULT_SETTINGS = {
            general: {
                show_help:          true,
                enable_autosave:    false,
            },
            preview: {
                fullscreen:         false,
                nopreview:          false,
                opacity:            255,
            },
            camera: {
                sharpness:          0,
                contrast:           0,
                brightness:         50,
                saturation:         0,
                ISO:                100,
                vstab:              false,
                ev:                 0,
                hflip:              false,
                vflip:              false,
            }
        };

    /******************************************************************************\
    Helper functions
    \******************************************************************************/
    function flush_settings(callback) {
        fs.writeFile(_settings_file, JSON.stringify(_settings, null, 4), "utf-8", callback);
    }

    function load_settings(callback) {
        fs.readFile(_settings_file, "utf-8", function(error, data) {
            if(error) {
                log.info("Unable to load settings file for RPI Camera");
                log.info("Using default settings...");
                _settings = _DEFAULT_SETTINGS;
                flush_settings(callback);
            } else {
                _settings = JSON.parse(data);
                callback();
            }
        });
    }

    // Helper function to run a given command line
    function run_command_line(cmd, callback) {
        log.info("Running cmd: " + cmd);
        exec(cmd, function(error, stderr, stdout) {
            callback(error, stdout);
        });
    }

    /******************************************************************************\
    Define external interfaces
    \******************************************************************************/
    // Initialize the rpi_camera, called once explicitly
    // by whoever requires this
    var _init = function(callback) {
        load_settings(function(error) {
            if(!error) {
                log.info("RPI Camera Online...");
                log.info("Using " + _settings_file + " for settings");
            }
            callback(error);
        });
    },

    /******************************************************************************/
    /* istanbul ignore next */
    /******************************************************************************/
    _take_picture = function(image_path, callback) {
        var
            options_str =
                helpers.build_cmd_from_options(_settings.preview) + " " +
                helpers.build_cmd_from_options(_settings.camera),
            cmd = "raspistill -t 1 -n -rot 180 "+options_str+" -o \"" + image_path + "\"";

        run_command_line(cmd, function(error, stdout) {
            callback();
        });
    },

    _save_settings = function(settings, callback) {<commit_msg>istanbul ignore for take pic end point<commit_after>var
    async   = require("async"),
    _       = require("underscore")._,
    exec    = require("child_process").exec,
    fs      = require("fs"),
    path    = require("path"),
    request = require("request"),
    util    = require("util")

    helpers = require("./helpers.js");

module.exports = function(log, settings_file) {
    /******************************************************************************\
    Define module globals / constants. Some of these are / can be exposed to the
    person including the module.
    \******************************************************************************/
    var
        _settings_file = settings_file,
        _settings = null,

        _ERRORS = {
            UNKNOWN_ERROR:  { id: 0, name: "UNKNOWN_ERROR" },
        },

        _DEFAULT_SETTINGS = {
            general: {
                show_help:          true,
                enable_autosave:    false,
            },
            preview: {
                fullscreen:         false,
                nopreview:          false,
                opacity:            255,
            },
            camera: {
                sharpness:          0,
                contrast:           0,
                brightness:         50,
                saturation:         0,
                ISO:                100,
                vstab:              false,
                ev:                 0,
                hflip:              false,
                vflip:              false,
            }
        };

    /******************************************************************************\
    Helper functions
    \******************************************************************************/
    function flush_settings(callback) {
        fs.writeFile(_settings_file, JSON.stringify(_settings, null, 4), "utf-8", callback);
    }

    function load_settings(callback) {
        fs.readFile(_settings_file, "utf-8", function(error, data) {
            if(error) {
                log.info("Unable to load settings file for RPI Camera");
                log.info("Using default settings...");
                _settings = _DEFAULT_SETTINGS;
                flush_settings(callback);
            } else {
                _settings = JSON.parse(data);
                callback();
            }
        });
    }

    // Helper function to run a given command line
    function run_command_line(cmd, callback) {
        log.info("Running cmd: " + cmd);
        exec(cmd, function(error, stderr, stdout) {
            callback(error, stdout);
        });
    }

    /******************************************************************************\
    Define external interfaces
    \******************************************************************************/
    // Initialize the rpi_camera, called once explicitly
    // by whoever requires this
    var _init = function(callback) {
        load_settings(function(error) {
            if(!error) {
                log.info("RPI Camera Online...");
                log.info("Using " + _settings_file + " for settings");
            }
            callback(error);
        });
    },

    /******************************************************************************/
    /* istanbul ignore next */
    _take_picture = function(image_path, callback) {
        var
            options_str =
                helpers.build_cmd_from_options(_settings.preview) + " " +
                helpers.build_cmd_from_options(_settings.camera),
            cmd = "raspistill -t 1 -n -rot 180 "+options_str+" -o \"" + image_path + "\"";

        run_command_line(cmd, function(error, stdout) {
            callback();
        });
    },

    _save_settings = function(settings, callback) {
{'fraction_non_alphanumeric': 0.24391520544360115, 'fraction_numerical': 0.005234231876472127, 'mean_word_length': 1.7651953690303908, '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': '718335', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1721, 'n_words': 380}