<filename>mass/input_handler.py<gh_stars>10-100
# -*- coding: utf-8 -*-
from functools import wraps
import inspect


class InputHandler(object):

    HANDLERS = {}

    def __init__(self, protocol=None):
        self.protocol = protocol
        self.HANDLERS.setdefault('load', {})
        self.HANDLERS.setdefault('save', {})

    def save(self, data, genealogy):
        """Save data by registered function.
        """
        if not self.protocol:
            return data
        kwargs = locals()
        kwargs.pop('self')
        func = self.HANDLERS['save'][self.protocol]
        args = inspect.formatargspec(*inspect.getargspec(func))
        return func(**{k: v for k, v in kwargs.items() if k in args})

    def load(self, from_save):
        """Load data by registered function.
        """
        if not self.protocol:
            return from_save
        return self.HANDLERS['load'][self.protocol](from_save)

    def saver(self, protocol=None):
        """Return a decorator to register function to save data for specific
        protocol.
        """
        protocol = protocol or self.protocol
        assert protocol is not None

        def decorator(func):
            self.HANDLERS['save'][protocol] = func

            @wraps(func)
            def wrapper(*args, **kwargs):
                func(*args, **kwargs)
            return wrapper
        return decorator

    def loader(self, protocol=None):
        """Return a decorator to register function to load data for specific
        protocol.
        """
        protocol = protocol or self.protocol
        assert protocol is not None

        def decorator(func):
            self.HANDLERS['load'][protocol] = func

            @wraps(func)
            def wrapper(*args, **kwargs):
                func(*args, **kwargs)
            return wrapper
        return decorator

<filename>mass/input_handler.py<gh_stars>10-100
# -*- coding: utf-8 -*-
from functools import wraps
import inspect


class InputHandler(object):

    HANDLERS = {}

    def __init__(self, protocol=None):
        self.protocol = protocol
        self.HANDLERS.setdefault('load', {})
        self.HANDLERS.setdefault('save', {})

    def save(self, data, genealogy):
        """Save data by registered function.
        """
        if not self.protocol:
            return data
        kwargs = locals()
        kwargs.pop('self')
        func = self.HANDLERS['save'][self.protocol]
        args = inspect.formatargspec(*inspect.getargspec(func))
        return func(**{k: v for k, v in kwargs.items() if k in args})

    def load(self, from_save):
        """Load data by registered function.
        """
        if not self.protocol:
            return from_save
        return self.HANDLERS['load'][self.protocol](from_save)

    def saver(self, protocol=None):
        """Return a decorator to register function to save data for specific
        protocol.
        """
        protocol = protocol or self.protocol
        assert protocol is not None

        def decorator(func):
            self.HANDLERS['save'][protocol] = func

            @wraps(func)
            def wrapper(*args, **kwargs):
                func(*args, **kwargs)
            return wrapper
        return decorator

    def loader(self, protocol=None):
        """Return a decorator to register function to load data for specific
        protocol.
        """
        protocol = protocol or self.protocol
        assert protocol is not None

        def decorator(func):
            self.HANDLERS['load'][protocol] = func

            @wraps(func)
            def wrapper(*args, **kwargs):
                func(*args, **kwargs)
            return wrapper
        return decorator

# -*- coding: utf-8 -*- Save data by registered function. Load data by registered function. Return a decorator to register function to save data for specific         protocol. Return a decorator to register function to load data for specific         protocol.