callchain / callchain / root /

# -*- coding: utf-8 -*-
'''root chain mixins'''

from stuf.utils import lazy_class

from callchain.base import ResetLocalMixin
from callchain.mixins import ChainMixin, PriorityMixin, EventMixin

class RootMixin(ResetLocalMixin):

    '''root chain mixin'''

    def __init__(self, **kw):
        super(RootMixin, self).__init__()
        if self.M is not None:
            # freeze external appspace global settings
            self.M = None

    def __call__(self, *args):
        '''new chain session'''
        # clear call chain and queues and extend incoming things
        return self.clear().extend(args)

    def port(self):
        '''python 2.x <-> python 3.x porting helper'''
        from import port
        return port

    def back(self, branch):
        handle switch from branch chain

        @param branch: branch chain
        # sync with branch callable
        self._call = branch._call
        # sync with branch postitional arguments
        self._args = branch._args
        # sync with branch keyword arguments
        self._kw = branch._kw
        # sync with branch incoming and outgoing things
        return self.extend(branch.incoming).outextend(branch.outgoing)

    def join(self, *things):
        join things to session

        @param things: some things
        # root internal global settings
        G = self.G
        # root external appspace manager
        M = self.M
        for thing in things:
            # add session
            thing.root = self
            # root internal global settings
            thing.G = G
            # root external appspace manager
            thing.M = M
        return self

class EventRootMixin(RootMixin):

    '''root event mixin'''

    def _eventq(self, event):
        linked chain bound to `event`

        @param event: event label
        key = self.E.event(event)
        # fetch linked chain bound to event
        queue = self.E.get(key)
        if queue is None:
            # create linked chain if nonexistent
            queue = self._linkedchain
            self.E.on(key, queue)
        return queue

    def _event(self, event):
        calls bound to `event`

        @param event: event label

class callchain(RootMixin, ChainMixin):

    '''generic call chain'''

class prioritychain(RootMixin, PriorityMixin):

    '''generic priority chain generic'''

class eventchain(EventRootMixin, EventMixin):

    '''generic event chain'''