Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
# Copyright (c) 2015 Docker, Inc. All rights reserved.

import abc
import logging

from pykafka.exceptions import InvalidMessageError

log = logging.getLogger(__name__)


class BaseEvent(object):
    """
    Base class that defines the kafka event's schema
    """
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def _make_body(cls, raw_data):
        """This method should be implemented by any class that inherits from it"""

    @classmethod
    def create(cls, raw_data):
        """
        Generates the event payload that will used to send the kafka message

        :param raw_data: raw data event
        :return: protobuf event message or python dictionary
                 Docs: https://github.com/docker/saas-mega/tree/master/schema
        """

        if not raw_data:
            log.exception('Event data is None.')
            raise InvalidMessageError

        try:
            event = cls._make_body(raw_data)
        except (TypeError, ValueError, NameError) as exc:
            log.exception('Unable to parse the event data.')
            raise exc

        return event