# -*- coding: utf-8 -*-
# wasp_general/network/messenger/envelope.py
#
# Copyright (C) 2017 the wasp-general authors and contributors
# <see AUTHORS file>
#
# This file is part of wasp-general.
#
# wasp-general is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wasp-general is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with wasp-general. If not, see <http://www.gnu.org/licenses/>.
# noinspection PyUnresolvedReferences
from wasp_general.version import __author__, __version__, __credits__, __license__, __copyright__, __email__
# noinspection PyUnresolvedReferences
from wasp_general.version import __status__
from wasp_general.verify import verify_type
from wasp_general.network.messenger.proto import WMessengerEnvelopeProto
[docs]class WMessengerEnvelope(WMessengerEnvelopeProto):
""" Simple :class:`.WMessengerEnvelopeProto` implementation. "Meta-dictionary" is used as is without any
restrictions or limitations, so any further envelopes (envelopes that uses this envelope as meta) can rewrite
or remove saved information
"""
@verify_type(meta=(WMessengerEnvelopeProto, dict, None))
def __init__(self, data, meta=None):
""" Construct new envelope
:param data: original message
:param meta: envelope meta-data to copy
"""
self.__data = data
self.__meta = {}
if meta is not None:
if isinstance(meta, WMessengerEnvelopeProto) is True:
if isinstance(meta, WMessengerEnvelope) is False:
raise TypeError('meta must be WMessengerEnvelope (or derived classes) object')
self.__meta = meta.meta()
elif isinstance(meta, dict):
self.__meta = meta.copy()
[docs] def message(self):
""" Return original message
:return: any type
"""
return self.__data
[docs]class WMessengerTextEnvelope(WMessengerEnvelope):
""" Envelope for str-objects
"""
@verify_type(data=str, meta=(WMessengerEnvelopeProto, dict, None))
def __init__(self, data, meta=None):
WMessengerEnvelope.__init__(self, data, meta=meta)
[docs]class WMessengerBytesEnvelope(WMessengerEnvelope):
""" Envelope for bytes-objects
"""
@verify_type(data=bytes, meta=(WMessengerEnvelopeProto, dict, None))
def __init__(self, data, meta=None):
WMessengerEnvelope.__init__(self, data, meta=meta)
[docs]class WMessengerDictEnvelope(WMessengerEnvelope):
""" Envelope for dict-objects (dictionary objects)
"""
@verify_type(data=dict, meta=(WMessengerEnvelopeProto, dict, None))
def __init__(self, data, meta=None):
WMessengerEnvelope.__init__(self, data, meta=meta)