<commit_before># -*- encoding: utf-8


import hashlib

from suplemon.suplemon_module import Module


class ApplicationState(Module):
    def init(self):
        self.init_logging(__name__)
        self.bind_event_after("app_loaded", self.on_load)
        self.bind_event_before("app_exit", self.on_exit)

    def on_load(self, event):
        """Runs when suplemon is fully loaded."""
        self.restore_states()

    def on_exit(self, event):
        """Runs before suplemon is exits."""
        self.store_states()

    def get_file_states(self):
        """Get the state of currently opened files. Returns a dict with the file path as key and file state as value."""
        states = {}
        for file in self.app.get_files():
            states[file.get_path()] = self.get_file_state(file)
        return states

    def get_file_state(self, file):
        """Get the state of a single file."""
        editor = file.get_editor()
        state = {
            "cursors": [cursor.tuple() for cursor in editor.get_cursors()],
            "scroll_pos": editor.get_scroll_pos(),
            "hash": self.get_hash(editor),
        }
        return state

    def get_hash(self, editor):
        # We don't need cryptographic security so we just use md5
        return hashlib.md5(editor.data.encode("utf-8")).hexdigest()

    def set_file_state(self, file, state):
        """Set the state of a file."""
        file.editor.set_cursors(state["cursors"])
        file.editor.set_scroll_pos(state["scroll_pos"])

    def store_states(self):
        """Store the states of opened files."""
        states = self.get_file_states()
        for path in states.keys():
            self.storage[path] = states[path]
        self.storage.store()

    def restore_states(self):
        """Restore the states of files that are currently open."""
        for file in self.app.get_files():
            path = file.get_path()
            if path in self.storage.get_data().keys():
                state = self.storage[path]
                if "hash" not in state:
                    self.set_file_state(file, state)
                elif state["hash"] == self.get_hash(file.get_editor()):
                    self.set_file_state(file, state)


module = {
    "class": ApplicationState,
    "name": "application_state",
}
<commit_msg>Get the most recent data from editor, not the cached data.<commit_after># -*- encoding: utf-8


import hashlib

from suplemon.suplemon_module import Module


class ApplicationState(Module):
    def init(self):
        self.init_logging(__name__)
        self.bind_event_after("app_loaded", self.on_load)
        self.bind_event_before("app_exit", self.on_exit)

    def on_load(self, event):
        """Runs when suplemon is fully loaded."""
        self.restore_states()

    def on_exit(self, event):
        """Runs before suplemon is exits."""
        self.store_states()

    def get_file_states(self):
        """Get the state of currently opened files. Returns a dict with the file path as key and file state as value."""
        states = {}
        for file in self.app.get_files():
            states[file.get_path()] = self.get_file_state(file)
        return states

    def get_file_state(self, file):
        """Get the state of a single file."""
        editor = file.get_editor()
        state = {
            "cursors": [cursor.tuple() for cursor in editor.get_cursors()],
            "scroll_pos": editor.get_scroll_pos(),
            "hash": self.get_hash(editor),
        }
        return state

    def get_hash(self, editor):
        # We don't need cryptographic security so we just use md5
        return hashlib.md5(editor.get_data().encode("utf-8")).hexdigest()

    def set_file_state(self, file, state):
        """Set the state of a file."""
        file.editor.set_cursors(state["cursors"])
        file.editor.set_scroll_pos(state["scroll_pos"])

    def store_states(self):
        """Store the states of opened files."""
        states = self.get_file_states()
        for path in states.keys():
            self.storage[path] = states[path]
        self.storage.store()

    def restore_states(self):
        """Restore the states of files that are currently open."""
        for file in self.app.get_files():
            path = file.get_path()
            if path in self.storage.get_data().keys():
                state = self.storage[path]
                if "hash" not in state:
                    self.set_file_state(file, state)
                elif state["hash"] == self.get_hash(file.get_editor()):
                    self.set_file_state(file, state)


module = {
    "class": ApplicationState,
    "name": "application_state",
}

{'fraction_non_alphanumeric': 0.11842385516506922, 'fraction_numerical': 0.0017039403620873268, 'mean_word_length': 2.3495007132667616, 'pattern_counts': {'":': 10, '<': 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': '3937517', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1335, 'n_words': 366}