<commit_before>"""
SublimeNodeServer.py
"""

import getpass
import os
import platform
import socket
import subprocess
import sys
import time
import threading

import sublime

SERVER_ADDRESS = "/tmp/sublime-node-server.sock"
SERVER_PATH = os.path.join(
    sublime.packages_path(),
    os.path.dirname(os.path.realpath(__file__)),
    "SublimeNodeServer.js"
)

def plugin_loaded():
    """Called when the Sublime Text API is ready for use."""
    if os.path.exists(SERVER_ADDRESS):
        os.unlink(SERVER_ADDRESS)

    SublimeNodeServer.thread = SublimeNodeServer(SERVER_ADDRESS, SERVER_PATH)
    SublimeNodeServer.thread.start()

    client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    connected = False
    while not connected:
        try:
            client.connect(SERVER_ADDRESS)
            connected = True
        except (ConnectionRefusedError, FileNotFoundError):
            time.sleep(0.1)

    client.send(bytes("Hello world!", "utf-8"))

def plugin_unloaded():
    """Called just before the plugin is unloaded."""
    if SublimeNodeServer.thread:
        SublimeNodeServer.thread.exit()

def get_node_paths():
    """Finds platform-specific node paths."""
    node_paths = []

    if platform.system() == 'Darwin':
        nvm_path = "/Users/{0}/.nvm".format(getpass.getuser())
        try:
            with open("{0}/alias/default".format(nvm_path), "r") as file:
                content = file.read()
            version = content.strip()
            node_path = "{0}/versions/node/v{1}/bin".format(nvm_path, version)
            node_paths.append(node_path)
        except FileNotFoundError:
            pass

    return node_paths

class SublimeNodeServer(threading.Thread):
    """Manages the node server."""

    thread = None

    def __init__(self, server_address, server_path):
        threading.Thread.__init__(self)
        self.server_address = server_address
        self.server_path = server_path
        self.child = None

    def run(self):
        env = os.environ.copy()
        env["PATH"] += ''.join([':' + path for path in get_node_paths()])
        try:
            child = subprocess.Popen(
                ["node", self.server_path, self.server_address],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                env=env
            )
            self.child = child
        except OSError:
            raise Exception(
                "Couldn't find `node` in `{0}`.".format(env["PATH"])
            )

        while child.poll() is None:
            stdout = child.stdout.readline().decode("utf-8")
            sys.stdout.write(stdout)

        stdout, stderr = child.communicate()
        stdout = stdout.decode("utf-8")
        stderr = stderr.decode("utf-8")
        if stdout:
            sys.stdout.write(stdout)
        if stderr:
            message = "Node server encountered an error.\n{0}".format(
                "\n".join(["> " + line for line in stderr.split("\n")])
            )
            raise Exception(message)

    def exit(self):
        """Sends SIGINT to the node child process."""<commit_msg>Move client connection off-thread with message queue<commit_after>"""
SublimeNodeServer.py
"""

import getpass
import os
import platform
import socket
import subprocess
import sys
import time
import threading

import sublime

SERVER_ADDRESS = "/tmp/sublime-node-server.sock"
SERVER_PATH = os.path.join(
    sublime.packages_path(),
    os.path.dirname(os.path.realpath(__file__)),
    "SublimeNodeServer.js"
)

def plugin_loaded():
    """Called when the Sublime Text API is ready for use."""
    SublimeNodeServer.thread = SublimeNodeServer(SERVER_ADDRESS, SERVER_PATH)
    SublimeNodeServer.thread.start()

    SublimeNodeServer.thread.send("Hello world!")

def plugin_unloaded():
    """Called just before the plugin is unloaded."""
    if SublimeNodeServer.thread:
        SublimeNodeServer.thread.exit()

def get_node_paths():
    """Finds platform-specific node paths."""
    node_paths = []

    if platform.system() == 'Darwin':
        nvm_path = "/Users/{0}/.nvm".format(getpass.getuser())
        try:
            with open("{0}/alias/default".format(nvm_path), "r") as file:
                content = file.read()
            version = content.strip()
            node_path = "{0}/versions/node/v{1}/bin".format(nvm_path, version)
            node_paths.append(node_path)
        except FileNotFoundError:
            pass

    return node_paths

class SublimeNodeServer(threading.Thread):
    """Manages the node server."""

    thread = None

    def __init__(self, server_address, server_path):
        threading.Thread.__init__(self)
        self.server_address = server_address
        self.server_path = server_path
        self.child = None
        self.client = None
        self.queue = []

    def run(self):
        if os.path.exists(self.server_address):
            os.unlink(self.server_address)

        env = os.environ.copy()
        env["PATH"] += ''.join([':' + path for path in get_node_paths()])
        try:
            child = subprocess.Popen(
                ["node", self.server_path, self.server_address],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                env=env
            )
            self.child = child
        except OSError:
            raise Exception(
                "Couldn't find `node` in `{0}`.".format(env["PATH"])
            )

        self.client = self.connect()
        for (message, callback) in self.queue:
            self.send(message, callback)
        del self.queue[:]

        while child.poll() is None:
            stdout = child.stdout.readline().decode("utf-8")
            sys.stdout.write(stdout)

        stdout, stderr = child.communicate()
        stdout = stdout.decode("utf-8")
        stderr = stderr.decode("utf-8")
        if stdout:
            sys.stdout.write(stdout)
        if stderr:
            message = "Node server encountered an error.\n{0}".format(
                "\n".join(["> " + line for line in stderr.split("\n")])
            )
            raise Exception(message)

    def connect(self):
        """Connects to the node server."""
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        end_time = time.time() + 10
        while True:
            remaining_time = end_time - time.time()
            if remaining_time < 0:
                raise Exception(
                    "Unable to connect to `{0}`".format(self.server_address)
                )
            try:
                client.connect(self.server_address)
                break
            except (ConnectionRefusedError, FileNotFoundError):
                time.sleep(0.1)

        return client

    def send(self, message, callback=None):
        """Sends a message to the node server."""
        if self.client:
            self.client.send(bytes(message, "utf-8"))
            if callback:
                callback()
        else:
            self.queue.append((message, callback))

    def exit(self):
        """Sends SIGINT to the node child process."""
{'fraction_non_alphanumeric': 0.11906439340566437, 'fraction_numerical': 0.003945329012258701, 'mean_word_length': 2.1393188854489162, 'pattern_counts': {'":': 0, '<': 4, '<?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': '7016710', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2030, 'n_words': 460}