<commit_before># coding=utf-8
import time
import logging
import collections

from rtmbot.core import Plugin

import tasks
from .jobs import WorldTick
from .storage import Storage


class StandupPonyPlugin(Plugin):
    """Standup Pony plugin."""
    def __init__(self, name=None, slack_client=None, plugin_config=None):
        super(StandupPonyPlugin, self).__init__(
            name, slack_client, plugin_config)
        self.slow_queue = collections.deque()
        self.fast_queue = collections.deque()

        self.storage = Storage(plugin_config.get('db_file'))

        # world updates
        self.slow_queue.append(tasks.UpdateUserList())
        self.slow_queue.append(tasks.UpdateIMList())
        self.slow_queue.append(tasks.CheckReports())
        self.slow_queue.append(tasks.SyncDB())

    def get_channel(self, channel_id):
        channels = self.storage.get('channels', dict())

        if channel_id not in channels:
            channels[channel_id] = self.slack_client.api_call()
            self.storage.set('channels', channels)

        return channels[channel_id]

    def get_user_by_id(self, user_id):
        users = self.storage.get('users')
        for user in users:
            if user['id'] == user_id:
                return user

    def get_user_by_name(self, user_name):
        user_name = user_name.strip('@')
        users = self.storage.get('users')
        for user in users:
            if user['name'] == user_name:
                return user

    def user_is_online(self, user_id):
        data = self.slack_client.api_call('users.getPresence', user=user_id)
        if data['ok']:
            return data['presence'] == 'active'

        return False

    def send_typing(self, to, over_time=2):
        time.sleep(over_time * 0.25)
        self.slack_client.server.send_to_websocket(
            dict(type='typing', channel=to))
        time.sleep(over_time * 0.75)

    def lock_user(self, user_id, teams, expire_in):
        lock_key = '{}_lock'.format(user_id)
        self.storage.set(lock_key, teams, expire_in=expire_in)
        logging.info('Locked user {} for {} sec'.format(user_id, expire_in))

    def get_user_lock(self, user_id):
        lock_key = '{}_lock'.format(user_id)
        return self.storage.get(lock_key)

    def process_message(self, data):
        self.fast_queue.append(tasks.ReadMessage(data=data))

    def process_im_created(self, data):
        logging.info('IM created for user {}'.format(data.get('user')))
        self.fast_queue.append(tasks.UpdateIMList())

    def process_user_typing(self, data):
        logging.info('User {} is typing to channel {}'.format(
            data.get('user'), data.get('channel')
        ))

    def process_presence_change(self, data):
        logging.info('User {} is now {}'.format(
            data.get('user'), data.get('presence')
        ))

    def register_jobs(self):
        # slow queue, some minutes between runs (slow world queue)
        self.jobs.append(
            WorldTick(
                bot=self,
                queue=self.slow_queue,
                interval=5 * 60
            )<commit_msg>Allow slow queue to be more granular.<commit_after># coding=utf-8
import time
import logging
import collections

from rtmbot.core import Plugin

import tasks
from .jobs import WorldTick
from .storage import Storage


class StandupPonyPlugin(Plugin):
    """Standup Pony plugin."""
    def __init__(self, name=None, slack_client=None, plugin_config=None):
        super(StandupPonyPlugin, self).__init__(
            name, slack_client, plugin_config)
        self.slow_queue = collections.deque()
        self.fast_queue = collections.deque()

        self.storage = Storage(plugin_config.get('db_file'))

        # world updates
        self.slow_queue.append(tasks.UpdateUserList())
        self.slow_queue.append(tasks.UpdateIMList())
        self.slow_queue.append(tasks.CheckReports())
        self.slow_queue.append(tasks.SyncDB())

    def get_channel(self, channel_id):
        channels = self.storage.get('channels', dict())

        if channel_id not in channels:
            channels[channel_id] = self.slack_client.api_call()
            self.storage.set('channels', channels)

        return channels[channel_id]

    def get_user_by_id(self, user_id):
        users = self.storage.get('users')
        for user in users:
            if user['id'] == user_id:
                return user

    def get_user_by_name(self, user_name):
        user_name = user_name.strip('@')
        users = self.storage.get('users')
        for user in users:
            if user['name'] == user_name:
                return user

    def user_is_online(self, user_id):
        data = self.slack_client.api_call('users.getPresence', user=user_id)
        if data['ok']:
            return data['presence'] == 'active'

        return False

    def send_typing(self, to, over_time=2):
        time.sleep(over_time * 0.25)
        self.slack_client.server.send_to_websocket(
            dict(type='typing', channel=to))
        time.sleep(over_time * 0.75)

    def lock_user(self, user_id, teams, expire_in):
        lock_key = '{}_lock'.format(user_id)
        self.storage.set(lock_key, teams, expire_in=expire_in)
        logging.info('Locked user {} for {} sec'.format(user_id, expire_in))

    def get_user_lock(self, user_id):
        lock_key = '{}_lock'.format(user_id)
        return self.storage.get(lock_key)

    def process_message(self, data):
        self.fast_queue.append(tasks.ReadMessage(data=data))

    def process_im_created(self, data):
        logging.info('IM created for user {}'.format(data.get('user')))
        self.fast_queue.append(tasks.UpdateIMList())

    def process_user_typing(self, data):
        logging.info('User {} is typing to channel {}'.format(
            data.get('user'), data.get('channel')
        ))

    def process_presence_change(self, data):
        logging.info('User {} is now {}'.format(
            data.get('user'), data.get('presence')
        ))

    def register_jobs(self):
        # slow queue, some minutes between runs (slow world queue)
        self.jobs.append(
            WorldTick(
                bot=self,
                queue=self.slow_queue,
                interval=2 * 60
            )
{'fraction_non_alphanumeric': 0.11963775023832221, 'fraction_numerical': 0.0034953924372418178, 'mean_word_length': 2.580773606370876, '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': '3568582', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1873, 'n_words': 390}