<commit_before>"""mist.io.socket

Here we define the socketio namespace and handlers.

When a user loads mist.io or comes back online, their browser will request a
new socket and the initialize function will be triggered on the server within a
greenlet.

"""

import random
from time import time

import gevent
from gevent.socket import wait_read, wait_write

import requests

from socketio.namespace import BaseNamespace

try:
    from mist.core.helpers import user_from_request
    from mist.core import config
    from mist.core.methods import get_stats
except ImportError:
    from mist.io.helpers import user_from_request
    from mist.io import config
    from mist.io.methods import get_stats

from mist.io.helpers import amqp_subscribe_user
from mist.io.methods import notify_user

from mist.io import methods
from mist.io import tasks
from mist.io.shell import Shell


class ShellNamespace(BaseNamespace):
    def initialize(self):
        self.user = user_from_request(self.request)
        self.channel = None
        print "opening shell socket"

    def on_shell_open(self, data):
        print "opened shell"
        self.shell = Shell(data['host'])
        key_id, ssh_user = self.shell.autoconfigure(self.user, data['backend_id'], data['machine_id'])
        self.channel = self.shell.ssh.invoke_shell('xterm')
        self.spawn(self.get_ssh_data)

    def on_shell_close(self):
        print "closing shell"
        if self.channel:
            self.channel.close()

    def on_shell_data(self, data):
        self.channel.send(data)

    def get_ssh_data(self):
        try:
            while True:
                wait_read(self.channel.fileno())
                data = self.channel.recv(1024).decode('utf-8','ignore')
                if not len(data):
                    return
                self.emit('shell_data', data)
        finally:
            self.channel.close()


class MistNamespace(BaseNamespace):
    def initialize(self):
        print "init"
        self.user = user_from_request(self.request)
        self.probes = {}
        self.channel = None
        self._old_machines = set()

    def spawn_later(self, delay, fn, *args, **kwargs):
        """Spawn a new process, attached to this Namespace after no less than
        delay seconds.

        It will be monitored by the "watcher" process in the Socket. If the
        socket disconnects, all these greenlets are going to be killed, after
        calling BaseNamespace.disconnect()

        This method uses the ``exception_handler_decorator``.  See
        Namespace documentation for more information.

        """
        # self.log.debug("Spawning sub-Namespace Greenlet: %s" % fn.__name__)
        if hasattr(self, 'exception_handler_decorator'):
            fn = self.exception_handler_decorator(fn)
        import gevent
        new = gevent.spawn_later(delay, fn, *args, **kwargs)
        self.jobs.append(new)
        return new

    def on_ready(self):
        print "Ready to go!"
        self.update_greenlet = self.spawn(update_subscriber, self)

        self.monitoring_greenlet = self.spawn_later(2, check_monitoring_from_socket, self)
        self.backends_greenlet = self.spawn_later(2, list_backends_from_socket, self)
        self.keys_greenlet = self.spawn_later(2, list_keys_from_socket, self)
        #self.probe_greenlet = self.spawn(probe_subscriber, self)

    def on_stats(self, backend_id, machine_id, start, stop, step, requestID):

        try:
            data = get_stats(user, backend_id, machine_id,
                             start - 50, stop + 50, step / 1000)
        except Exception as exc:
            self.emit('notify', {'title': 'Error getting stats: %s' % exc})
            return<commit_msg>fix typo<commit_after>"""mist.io.socket

Here we define the socketio namespace and handlers.

When a user loads mist.io or comes back online, their browser will request a
new socket and the initialize function will be triggered on the server within a
greenlet.

"""

import random
from time import time

import gevent
from gevent.socket import wait_read, wait_write

import requests

from socketio.namespace import BaseNamespace

try:
    from mist.core.helpers import user_from_request
    from mist.core import config
    from mist.core.methods import get_stats
except ImportError:
    from mist.io.helpers import user_from_request
    from mist.io import config
    from mist.io.methods import get_stats

from mist.io.helpers import amqp_subscribe_user
from mist.io.helpers import amqp_log
from mist.io.methods import notify_user

from mist.io import methods
from mist.io import tasks
from mist.io.shell import Shell


class ShellNamespace(BaseNamespace):
    def initialize(self):
        self.user = user_from_request(self.request)
        self.channel = None
        print "opening shell socket"

    def on_shell_open(self, data):
        print "opened shell"
        self.shell = Shell(data['host'])
        key_id, ssh_user = self.shell.autoconfigure(self.user, data['backend_id'], data['machine_id'])
        self.channel = self.shell.ssh.invoke_shell('xterm')
        self.spawn(self.get_ssh_data)

    def on_shell_close(self):
        print "closing shell"
        if self.channel:
            self.channel.close()

    def on_shell_data(self, data):
        self.channel.send(data)

    def get_ssh_data(self):
        try:
            while True:
                wait_read(self.channel.fileno())
                data = self.channel.recv(1024).decode('utf-8','ignore')
                if not len(data):
                    return
                self.emit('shell_data', data)
        finally:
            self.channel.close()


class MistNamespace(BaseNamespace):
    def initialize(self):
        print "init"
        self.user = user_from_request(self.request)
        self.probes = {}
        self.channel = None
        self._old_machines = set()

    def spawn_later(self, delay, fn, *args, **kwargs):
        """Spawn a new process, attached to this Namespace after no less than
        delay seconds.

        It will be monitored by the "watcher" process in the Socket. If the
        socket disconnects, all these greenlets are going to be killed, after
        calling BaseNamespace.disconnect()

        This method uses the ``exception_handler_decorator``.  See
        Namespace documentation for more information.

        """
        # self.log.debug("Spawning sub-Namespace Greenlet: %s" % fn.__name__)
        if hasattr(self, 'exception_handler_decorator'):
            fn = self.exception_handler_decorator(fn)
        import gevent
        new = gevent.spawn_later(delay, fn, *args, **kwargs)
        self.jobs.append(new)
        return new

    def on_ready(self):
        print "Ready to go!"
        self.update_greenlet = self.spawn(update_subscriber, self)

        self.monitoring_greenlet = self.spawn_later(2, check_monitoring_from_socket, self)
        self.backends_greenlet = self.spawn_later(2, list_backends_from_socket, self)
        self.keys_greenlet = self.spawn_later(2, list_keys_from_socket, self)
        #self.probe_greenlet = self.spawn(probe_subscriber, self)

    def on_stats(self, backend_id, machine_id, start, stop, step, requestID):
        try:
            data = get_stats(self.user, backend_id, machine_id,
                             start - 50, stop + 50, step / 1000)
        except Exception as exc:
            amqp_log("Error getting stats: %r" % exc)
            self.emit("notify", "Error getting stats: %s" % exc)
            return
{'fraction_non_alphanumeric': 0.08659329001458692, 'fraction_numerical': 0.004243469035936878, 'mean_word_length': 2.6593886462882095, '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': '315840', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2137, 'n_words': 618}