# Path: invenio_stats/proxies.py

# 
# Path: invenio_stats/utils.py
# def obj_or_import_string(value, default=None):
#     """Import string or return object.
# 
#     :params value: Import path or class object to instantiate.
#     :params default: Default object to return if the import fails.
#     :returns: The imported object.
#     """
#     if isinstance(value, six.string_types):
#         return import_string(value)
#     elif value:
#         return value
#     return default


from flask import current_app
from .proxies import current_stats
from .utils import obj_or_import_string



class EventEmmiter(object):
    """Receive a signal and send an event."""

    def __init__(self, name, builders):
        """Contructor."""
        self.name = name
        self.builders = builders

    def __call__(self, *args, **kwargs):
        """Receive a signal and send an event."""
        # Send the event only if it is registered
        try:
            if self.name in current_stats.events:
                event = {}
                for builder in self.builders:
                    event = builder(event, *args, **kwargs)
                    if event is None:
                        return
                current_stats.publish(self.name, [event])
        except Exception:
            current_app.logger.exception(u'Error building event')


def register_receivers(app, config):
    """Register signal receivers which send events."""
    for event_name, event_config in config.items():
        event_builders = [

# Path: invenio_stats/proxies.py

# 
# Path: invenio_stats/utils.py
# def obj_or_import_string(value, default=None):
#     """Import string or return object.
# 
#     :params value: Import path or class object to instantiate.
#     :params default: Default object to return if the import fails.
#     :returns: The imported object.
#     """
#     if isinstance(value, six.string_types):
#         return import_string(value)
#     elif value:
#         return value
#     return default


# Path: invenio_stats/receivers.py
from flask import current_app
from .proxies import current_stats
from .utils import obj_or_import_string




class EventEmmiter(object):
    """Receive a signal and send an event."""

    def __init__(self, name, builders):
        """Contructor."""
        self.name = name
        self.builders = builders

    def __call__(self, *args, **kwargs):
        """Receive a signal and send an event."""
        # Send the event only if it is registered
        try:
            if self.name in current_stats.events:
                event = {}
                for builder in self.builders:
                    event = builder(event, *args, **kwargs)
                    if event is None:
                        return
                current_stats.publish(self.name, [event])
        except Exception:
            current_app.logger.exception(u'Error building event')


def register_receivers(app, config):
    """Register signal receivers which send events."""
    for event_name, event_config in config.items():
        event_builders = [

            obj_or_import_string(func)