<commit_before>import warnings

from time import time, sleep
from contextlib import contextmanager

from kombu import Consumer, Exchange, Queue
from kombu.utils import cached_property

from .agents import Agent
from .consumers import ConsumerMixin
from .pools import producers


class State(object):
    heartbeat_expire = 20

    def __init__(self, presence):
        self.presence = presence
        self._agents = {}
        self.handlers = {"online": self.when_online,
                         "offline": self.when_offline,
                         "heartbeat": self.when_heartbeat,
                         "wakeup": self.when_wakeup}

    def can(self, actor):
        able = set()
        for id, state in self.agents.iteritems():
            if actor in state["actors"]:
                able.add(id)
        return able

    def on_message(self, body, message):
        print("RECEIVED EVENT: %r" % (body, ))
        event = body["event"]
        self.handlers[event](**body)
        print("AGENTS NOW: %r" % (self.agents, ))

    def when_online(self, agent=None, actors=None, ts=None, **kw):
        self._update_agent(agent, actors, ts)

    def when_wakeup(self, **kw):
        self.presence.send_heartbeat()

    def when_heartbeat(self, agent=None, actors=None, ts=None, **kw):
        self._update_agent(agent, actors, ts)

    def when_offline(self, agent=None, actors=None, ts=None, **kw):
        self._remove_agent(agent)

    def expire_agents(self):
        expired = set()
        for id, state in self._agents.iteritems():
            if time() > state["ts"] + self.heartbeat_expire:
                expired.add(id)

        for id in expired:
            self._remove_agent(id)
        return self._agents

    def _update_agent(self, agent, actors, ts):
        self._agents[agent] = {"actors": actors, "ts": ts}

    def _remove_agent(self, agent):
        print("REMOVE AGENT: %r" % (agent, ))
        self._agents.pop(agent, None)

    @property
    def agents(self):
        return self.expire_agents()


class Presence(ConsumerMixin):
    State = State

    exchange = Exchange("cl.agents", type="topic", auto_delete=True)
    interval = 5<commit_msg>Removes debugging statements<commit_after>"""scs.presence"""

from __future__ import absolute_import, with_statement

import warnings

from time import time, sleep
from contextlib import contextmanager

from kombu import Consumer, Exchange, Queue
from kombu.utils import cached_property

from .agents import Agent
from .consumers import ConsumerMixin
from .pools import producers


class State(object):
    heartbeat_expire = 20

    def __init__(self, presence):
        self.presence = presence
        self._agents = {}
        self.handlers = {"online": self.when_online,
                         "offline": self.when_offline,
                         "heartbeat": self.when_heartbeat,
                         "wakeup": self.when_wakeup}

    def can(self, actor):
        able = set()
        for id, state in self.agents.iteritems():
            if actor in state["actors"]:
                able.add(id)
        return able

    def on_message(self, body, message):
        event = body["event"]
        self.handlers[event](**body)

    def when_online(self, agent=None, actors=None, ts=None, **kw):
        self._update_agent(agent, actors, ts)

    def when_wakeup(self, **kw):
        self.presence.send_heartbeat()

    def when_heartbeat(self, agent=None, actors=None, ts=None, **kw):
        self._update_agent(agent, actors, ts)

    def when_offline(self, agent=None, actors=None, ts=None, **kw):
        self._remove_agent(agent)

    def expire_agents(self):
        expired = set()
        for id, state in self._agents.iteritems():
            if time() > state["ts"] + self.heartbeat_expire:
                expired.add(id)

        for id in expired:
            self._remove_agent(id)
        return self._agents

    def _update_agent(self, agent, actors, ts):
        self._agents[agent] = {"actors": actors, "ts": ts}

    def _remove_agent(self, agent):
        self._agents.pop(agent, None)

    @property
    def agents(self):
        return self.expire_agents()


class Presence(ConsumerMixin):
    State = State

    exchange = Exchange("cl.agents", type="topic", auto_delete=True)
    interval = 5
{'fraction_non_alphanumeric': 0.11392111368909513, 'fraction_numerical': 0.0013921113689095127, 'mean_word_length': 2.5539983511953834, 'pattern_counts': {'":': 12, '<': 3, '<?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': '5765903', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1265, 'n_words': 331}