Source

callchain / callchain / chainlet / registry.py

Full commit
# -*- coding: utf-8 -*-
'''callchain event registry'''

from stuf.six import u

from appspace.registry import Registry
from callchain.keys.event import EEvent


class EventRegistry(Registry):

    '''event registry'''

    __slots__ = ('_root', '_key')

    def __init__(self, label, key=EEvent):
        '''
        init

        @param label: label for event registry
        @param key: default key for event registry (default: EEvent)
        '''
        super(EventRegistry, self).__init__(label, key)

    def event(self, label):
        '''
        create or fetch event

        @param event: event label
        '''
        return self._unlazy(label, self._key, self.key(self._key, label))

    def events(self, key):
        '''
        fetch things bound to event

        @param label: event label
        '''
        return self.subscriptions(self._key, key)

    def get(self, key):
        '''
        fetch thing from events

        @param key: event key
        '''
        return self.lookup1(self._key, key)

    def set(self, key, call):
        '''
        bind thing to event

        @param label: event label
        @param key: event key
        @param call: some thing
        '''
        self.register([self._key], key, call)

    def on(self, key, thing):
        '''
        bind thing to event

        @param label: event label
        @param key: event key
        @param call: some thing
        '''
        self.subscribe(self._key, key, thing.commit)
        self.register([self._key], key, u(''), thing)

    def unevent(self, label):
        '''
        drop event

        @param event: event label
        '''
        self.E.unkey(self._key, self.event(label))

    def unset(self, label):
        '''
        clear all callables bound to event

        @param event: event label
        '''
        self.ez_unsubscribe(self._key, self.event(label))