<commit_before>var komodoWakatime = {

    VERSION: '2.0.3',

    action_frequency: 2,
    time: 0,
    api_key: '',
    view: null,
    fileName: '',
    keyPressListener: null,

    onLoad: function (thisObject) {
        var prefs = Components.classes['@activestate.com/koPrefService;1']
          .getService(Components.interfaces.koIPrefService)
          .prefs;
        var pref_name = 'wakatime_api_key';
        if (!prefs.hasStringPref(pref_name) || prefs.getStringPref(pref_name) === '') {
            thisObject.api_key = thisObject.promptForAPIKey();
            prefs.setStringPref('wakatime_api_key', thisObject.api_key);
        } else {
            thisObject.api_key = prefs.getStringPref(pref_name);
        }
        komodoWakatime.initViewListener(komodoWakatime);
    },
    log: function(msg) {
        ko.logging.getLogger('').warn(msg);
    },
    apiClientLocation: function () {
        var currProfPath = Components.classes["@mozilla.org/file/directory_service;1"]
          .getService(Components.interfaces.nsIProperties)
          .get("PrefD", Components.interfaces.nsILocalFile)
          .path;
        var plugin_dir = currProfPath + '/extensions/wakatime@wakatime.com';
        return plugin_dir + '/components/wakatime/cli.py';
    },
    promptForAPIKey: function () {
        return ko.dialogs.prompt("[WakaTime] Enter your wakatime.com api key:");
    },
    getFileName: function (thisObject) {
        return thisObject.fileName;
    },
    enoughTimePassed: function (thisObject) {
        var d = new Date();
        if ((d.getTime() - thisObject.time) > thisObject.action_frequency * 60000) {
            thisObject.time = d.getTime();
            return true;
        }
        return false;
    },
    sendDataToAPI: function (thisObject, writeFlag) {
        writeFlag = typeof writeFlag !== 'undefined' ? writeFlag : false;
        var cmdWriteFlag = writeFlag ? '--write' : '';
        var fileName = thisObject.getFileName(thisObject);
        var cmd = 'python ' +
          thisObject.apiClientLocation().replace(/(@)/g, "\\@").replace(/(\s)/g, "\\ ") +
          ' ' + cmdWriteFlag + ' --file ' + fileName + ' --plugin komodo-wakatime/'+ thisObject.VERSION +
          ' --key ' + thisObject.api_key;
        var runSvc = Components.classes["@activestate.com/koRunService;1"]
          .createInstance(Components.interfaces.koIRunService);
        var process = runSvc.RunAndNotify(cmd, '', '', '');
    },
    keyPressEvent: function (thisObject) {
        if (thisObject.enoughTimePassed(thisObject)) {
            thisObject.sendDataToAPI(thisObject);
        }
    },
    fileSaveEvent: function (thisObject) {
        thisObject.sendDataToAPI(thisObject, true);
    },
    initViewListener: function (thisObject) {
        thisObject.keyPressListener = function (event) {
            thisObject.keyPressEvent(thisObject, event);
        };
        if (thisObject.view !== null) {
            thisObject.view.removeEventListener('keypress', thisObject.keyPressListener, true);
        }
        if (ko.views.manager.currentView !== null) {
            thisObject.view = ko.views.manager.currentView;
            thisObject.view.addEventListener('keypress', thisObject.keyPressListener, true);
        }
    },
    fileChanged: function (thisObject, event) {
        thisObject.fileName = event.originalTarget.koDoc.file.displayPath;
        thisObject.keyPressListener = function (event) {
            thisObject.keyPressEvent(thisObject, event);
        };
        ko.views.manager.currentView.removeEventListener('keypress', thisObject.keyPressListener, true);
        thisObject.view = event.originalTarget;
        thisObject.view.addEventListener('keypress', thisObject.keyPressListener, true);<commit_msg>only remove keypress event listener when view is not null<commit_after>var komodoWakatime = {

    VERSION: '2.0.3',

    action_frequency: 2,
    time: 0,
    api_key: '',
    view: null,
    fileName: '',
    keyPressListener: null,

    onLoad: function (thisObject) {
        var prefs = Components.classes['@activestate.com/koPrefService;1']
          .getService(Components.interfaces.koIPrefService)
          .prefs;
        var pref_name = 'wakatime_api_key';
        if (!prefs.hasStringPref(pref_name) || prefs.getStringPref(pref_name) === '') {
            thisObject.api_key = thisObject.promptForAPIKey();
            prefs.setStringPref('wakatime_api_key', thisObject.api_key);
        } else {
            thisObject.api_key = prefs.getStringPref(pref_name);
        }
        komodoWakatime.initViewListener(komodoWakatime);
    },
    log: function(msg) {
        ko.logging.getLogger('').warn(msg);
    },
    apiClientLocation: function () {
        var currProfPath = Components.classes["@mozilla.org/file/directory_service;1"]
          .getService(Components.interfaces.nsIProperties)
          .get("PrefD", Components.interfaces.nsILocalFile)
          .path;
        var plugin_dir = currProfPath + '/extensions/wakatime@wakatime.com';
        return plugin_dir + '/components/wakatime/cli.py';
    },
    promptForAPIKey: function () {
        return ko.dialogs.prompt("[WakaTime] Enter your wakatime.com api key:");
    },
    getFileName: function (thisObject) {
        return thisObject.fileName;
    },
    enoughTimePassed: function (thisObject) {
        var d = new Date();
        if ((d.getTime() - thisObject.time) > thisObject.action_frequency * 60000) {
            thisObject.time = d.getTime();
            return true;
        }
        return false;
    },
    sendDataToAPI: function (thisObject, writeFlag) {
        writeFlag = typeof writeFlag !== 'undefined' ? writeFlag : false;
        var cmdWriteFlag = writeFlag ? '--write' : '';
        var fileName = thisObject.getFileName(thisObject);
        var cmd = 'python ' +
          thisObject.apiClientLocation().replace(/(@)/g, "\\@").replace(/(\s)/g, "\\ ") +
          ' ' + cmdWriteFlag + ' --file ' + fileName + ' --plugin komodo-wakatime/'+ thisObject.VERSION +
          ' --key ' + thisObject.api_key;
        var runSvc = Components.classes["@activestate.com/koRunService;1"]
          .createInstance(Components.interfaces.koIRunService);
        var process = runSvc.RunAndNotify(cmd, '', '', '');
    },
    keyPressEvent: function (thisObject) {
        if (thisObject.enoughTimePassed(thisObject)) {
            thisObject.sendDataToAPI(thisObject);
        }
    },
    fileSaveEvent: function (thisObject) {
        thisObject.sendDataToAPI(thisObject, true);
    },
    initViewListener: function (thisObject) {
        thisObject.keyPressListener = function (event) {
            thisObject.keyPressEvent(thisObject, event);
        };
        if (thisObject.view !== null) {
            thisObject.view.removeEventListener('keypress', thisObject.keyPressListener, true);
        }
        if (ko.views.manager.currentView !== null) {
            ko.views.manager.currentView.removeEventListener('keypress', thisObject.keyPressListener, true);
        }
        thisObject.view = ko.views.manager.currentView;
        if (thisObject.view !== null) {
            thisObject.view.removeEventListener('keypress', thisObject.keyPressListener, true);
            thisObject.view.addEventListener('keypress', thisObject.keyPressListener, true);
        }
    },
    fileChanged: function (thisObject, event) {
        thisObject.fileName = event.originalTarget.koDoc.file.displayPath;
        thisObject.keyPressListener = function (event) {
            thisObject.keyPressEvent(thisObject, event);
        };
        if (thisObject.view !== null) {
            thisObject.view.removeEventListener('keypress', thisObject.keyPressListener, true);
        }
        if (ko.views.manager.currentView !== null) {
            ko.views.manager.currentView.removeEventListener('keypress', thisObject.keyPressListener, true);
        }
        thisObject.view = event.originalTarget;
        if (thisObject.view !== null) {
            thisObject.view.removeEventListener('keypress', thisObject.keyPressListener, true);
            thisObject.view.addEventListener('keypress', thisObject.keyPressListener, true);
        }
{'fraction_non_alphanumeric': 0.1268911664226452, 'fraction_numerical': 0.00317227916056613, 'mean_word_length': 2.675784753363229, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1327426', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2380, 'n_words': 384}