<commit_before># -*- coding: utf-8 -*-
from collections import defaultdict
import weakref
import collections
import json
import logging
import time

from rocketws.helpers import Singleton


logger = logging.getLogger('registry')


class SocketRegistry(object):

    """Reserved client socket registry"""

    __metaclass__ = Singleton

    def __init__(self):
        self.registry = dict()
        logger.debug('Init SocketRegistry')

    def register(self, *clients):
        for client in clients:
            self.registry[client.address] = weakref.ref(client)

    def unregister(self, client):
        if client.address in self.registry:
            del self.registry[client.address]
        return True

    def get_client(self, address):
        if isinstance(address, (list, tuple)) and len(address) == 2:
            address = tuple(address)

            # NOTE: client_ref is a proxy ref here
            client_ref = self.registry.get(address)
            if client_ref is None:
                del self.registry[address]
            else:
                return client_ref()

    @property
    def clients(self):
        return self.registry.values()

    def flush(self):
        self.registry.clear()
        logger.debug('Flush for SocketRegistry')

    def __unicode__(self):
        return unicode(
            "{}(sockets={})".format(
                self.__class__.__name__, len(self.registry)))

    def __repr__(self):
        return unicode(
            "{}(sockets={})".format(
                self.__class__.__name__, len(self.registry)))


class ChannelRegistry(object):

    """Registry for websockets channels for clients"""

    __metaclass__ = Singleton

    MESSAGE_TYPES = {
        'broadcast': 'broadcast',
        'message': 'message'
    }

    def __init__(self, **kwargs):
        self.registry = defaultdict(list)
        logger.debug('Init ChannelRegistry')

    def subscribe(self, channel, *clients):
        """Subscribe client for a channel.
        Method supports multiple subscriptions.

        :param channel: string channel name
        :param clients: geventwebsocket.handler.Client list
        """

        # Remove NoneType references
        active_subscribers = self._get_active_subscribers_idx(channel)

        for client in clients:
            # Store each client as a proxy weak reference
            active_subscribers.update({client.address: weakref.proxy(client)})
        self.registry[channel] = active_subscribers.values()
        return 'subscribed'

    def unsubscribe(self, channel, *clients):
        active_subscribers = self._get_active_subscribers_idx(channel)
        for client in clients:
            if client.address in active_subscribers:
                del active_subscribers[client.address]

        # Remove channel from registry if there are no subscribers
        if not active_subscribers:
            del self.registry[channel]
        else:
            self.registry[channel] = active_subscribers.values()

        return 'unsubscribed'

    def _get_active_subscribers_idx(self, channel):
        """Get active clients idx for particular channel,
        remove NoneType references

        :param channel:
        :return: dict
        """
        active_clients_idx = {}
        # Remove NoneType references
        for c in self.registry[channel]:
            try:
                if bool(c):
                    active_clients_idx[c.address] = c
            except ReferenceError:
                continue
        return active_clients_idx

    @classmethod
    def _add_message_meta(cls, data, _type):
        """Helper method to add meta info for message

        :param data:
        :param _type:

        Meta information:
            __type  - optional message type, default is 'message'
            __ts    - message server timestamp
        """
        data.update(
            __type=cls.MESSAGE_TYPES.get(_type, 'message'),
            __ts=time.time()
        )

    @property
    def channels(self):
        return self.registry.keys()

    @property
    def subscribers(self):
        """Return total subscribers for all channels

        :return: list
        """
        subscribers = []
        for channel in self.registry.keys():
            active = self._get_active_subscribers_idx(channel)
            subscribers.extend(active.values())
        return subscribers

    def get_channel_subscribers(self, channel):
        return self._get_active_subscribers_idx(channel).values()

    def is_client_in_channel(self, client, channel):<commit_msg>cast int for __ts meta parameter<commit_after># -*- coding: utf-8 -*-
from collections import defaultdict
import weakref
import collections
import json
import logging
import time

from rocketws.helpers import Singleton


logger = logging.getLogger('registry')


class SocketRegistry(object):

    """Reserved client socket registry"""

    __metaclass__ = Singleton

    def __init__(self):
        self.registry = dict()
        logger.debug('Init SocketRegistry')

    def register(self, *clients):
        for client in clients:
            self.registry[client.address] = weakref.ref(client)

    def unregister(self, client):
        if client.address in self.registry:
            del self.registry[client.address]
        return True

    def get_client(self, address):
        if isinstance(address, (list, tuple)) and len(address) == 2:
            address = tuple(address)

            # NOTE: client_ref is a proxy ref here
            client_ref = self.registry.get(address)
            if client_ref is None:
                del self.registry[address]
            else:
                return client_ref()

    @property
    def clients(self):
        return self.registry.values()

    def flush(self):
        self.registry.clear()
        logger.debug('Flush for SocketRegistry')

    def __unicode__(self):
        return unicode(
            "{}(sockets={})".format(
                self.__class__.__name__, len(self.registry)))

    def __repr__(self):
        return unicode(
            "{}(sockets={})".format(
                self.__class__.__name__, len(self.registry)))


class ChannelRegistry(object):

    """Registry for websockets channels for clients"""

    __metaclass__ = Singleton

    MESSAGE_TYPES = {
        'broadcast': 'broadcast',
        'message': 'message'
    }

    def __init__(self, **kwargs):
        self.registry = defaultdict(list)
        logger.debug('Init ChannelRegistry')

    def subscribe(self, channel, *clients):
        """Subscribe client for a channel.
        Method supports multiple subscriptions.

        :param channel: string channel name
        :param clients: geventwebsocket.handler.Client list
        """

        # Remove NoneType references
        active_subscribers = self._get_active_subscribers_idx(channel)

        for client in clients:
            # Store each client as a proxy weak reference
            active_subscribers.update({client.address: weakref.proxy(client)})
        self.registry[channel] = active_subscribers.values()
        return 'subscribed'

    def unsubscribe(self, channel, *clients):
        active_subscribers = self._get_active_subscribers_idx(channel)
        for client in clients:
            if client.address in active_subscribers:
                del active_subscribers[client.address]

        # Remove channel from registry if there are no subscribers
        if not active_subscribers:
            del self.registry[channel]
        else:
            self.registry[channel] = active_subscribers.values()

        return 'unsubscribed'

    def _get_active_subscribers_idx(self, channel):
        """Get active clients idx for particular channel,
        remove NoneType references

        :param channel:
        :return: dict
        """
        active_clients_idx = {}
        # Remove NoneType references
        for c in self.registry[channel]:
            try:
                if bool(c):
                    active_clients_idx[c.address] = c
            except ReferenceError:
                continue
        return active_clients_idx

    @classmethod
    def _add_message_meta(cls, data, _type):
        """Helper method to add meta info for message

        :param data:
        :param _type:

        Meta information:
            __type  - optional message type, default is 'message'
            __ts    - message server timestamp
        """
        data.update(
            __type=cls.MESSAGE_TYPES.get(_type, 'message'),
            __ts=int(time.time())
        )

    @property
    def channels(self):
        return self.registry.keys()

    @property
    def subscribers(self):
        """Return total subscribers for all channels

        :return: list
        """
        subscribers = []
        for channel in self.registry.keys():
            active = self._get_active_subscribers_idx(channel)
            subscribers.extend(active.values())
        return subscribers

    def get_channel_subscribers(self, channel):
        return self._get_active_subscribers_idx(channel).values()

    def is_client_in_channel(self, client, channel):
{'fraction_non_alphanumeric': 0.08834463153291056, 'fraction_numerical': 0.0004373496610540127, 'mean_word_length': 2.2436170212765956, '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': '4852285', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2348, 'n_words': 640}