from pubnub import Pubnub
from functools import wraps

from .base_request import BaseRequest
from .models.config import Config
from .models.device import Device


# TODO: https://github.com/resin-io/resin-sdk/pull/277/files
class Logs(object):
    """
    This class implements functions that allow processing logs from device.

    This class is implemented using pubnub python sdk.

    For more details about pubnub, please visit: https://www.pubnub.com/docs/python/pubnub-python-sdk

    """

    def __init__(self):
        self.config = Config()
        self.device = Device()

    def _init_pubnub(func):
        @wraps(func)
        def wrapper(self, *args, **kwargs):
            if not hasattr(self, 'pubnub'):
                pubnub_key = self.config.get_all()['pubnub']
                self.pubnub = Pubnub(
                    publish_key=pubnub_key['publish_key'],
                    subscribe_key=pubnub_key['subscribe_key']
                )
            return func(self, *args, **kwargs)
        return wrapper

    @_init_pubnub
    def subscribe(self, uuid, callback, error):
        """
        This function allows subscribing to device logs.
        There are fields (`m`, `t`, `s`, `c`) in the output which can be unclear. They stand for:
            m - The log message itself.
            t - The log timestamp.
            s - Is this a system message?
            c - The id of the service which produced this log (or null if the device does not support multiple containers).

        Args:
            uuid (str): device uuid.
            callback (function): this callback is called on receiving a message from the channel.
            error (function): this callback is called on an error event.
            For more details about callbacks in pubnub subscribe, visit here: https://www.pubnub.com/docs/python/api-reference#subscribe

        Examples:
            # Define callback and error.
            >>> def callback(message, channel):
            ...     print(message)
            >>> def error(message):
            ...     print('Error:'+ str(message))
            >>> Logs.subscribe(uuid=uuid, callback=callback, error=error)

        """

        channel = self.get_channel(uuid)
        self.pubnub.subscribe(channels=channel, callback=callback, error=error)

    @_init_pubnub
    def history(self, uuid, callback, error):
        """
        This function allows fetching historical device logs.

        Args:
            uuid (str): device uuid.
            callback (function): this callback is called on receiving a message from the channel.
            error (function): this callback is called on an error event.
            For more details about callbacks in pubnub subscribe, visit here: https://www.pubnub.com/docs/python/api-reference#history

        Examples:
            # Define callback and error.
            >>> def callback(message):
            ...     print(message)
            >>> def error(message):
            ...     print('Error:'+ str(message))
            Logs.history(uuid=uuid, callback=callback, error=error)

        """

        channel = self.get_channel(uuid)
        self.pubnub.history(channel=channel, callback=callback, error=error)

    def unsubscribe(self, uuid):
        """
        This function allows unsubscribing to device logs.

        Args:
            uuid (str): device uuid.

        """

        if hasattr(self, 'pubnub'):
            channel = self.get_channel(uuid)
            self.pubnub.unsubscribe(channel=channel)

    def get_channel(self, uuid):
        """
        This function returns pubnub channel for a specific device.

        Args:
            uuid (str): device uuid.

        Returns:
            str: device channel.

        """

        if not hasattr(self, 'logs_channel'):
            device_info = self.device.get(uuid)
            if 'logs_channel' in device_info:
                self.logs_channel = device_info['logs_channel']
            else:
                self.logs_channel = uuid

        return 'device-{logs_channel}-logs'.format(logs_channel=self.logs_channel)

@@ -46,16 +46,31 @@
 rt wraps
+%0Aimport logging
 %0A%0Afrom .
@@ -171,16 +171,262 @@
 evice%0A%0A%0A
+logging.basicConfig(format='%25(levelname)s:%25(message)s')%0A%0A%0Adef _print_deprecation_warning():%0A    logging.warning(%22 using legacy logging services, this will stop working shortly.%5CnPlease update to ensure logs are correctly retrieved in future.%22)%0A%0A%0A
 # TODO: 
@@ -2427,32 +2427,69 @@
 )%0A%0A        %22%22%22%0A%0A
+        _print_deprecation_warning()%0A
         channel 
@@ -3384,32 +3384,69 @@
 )%0A%0A        %22%22%22%0A%0A
+        _print_deprecation_warning()%0A
         channel 
@@ -3710,32 +3710,69 @@
 .%0A%0A        %22%22%22%0A%0A
+        _print_deprecation_warning()%0A
         if hasat
@@ -4112,32 +4112,69 @@
 .%0A%0A        %22%22%22%0A%0A
+        _print_deprecation_warning()%0A
         if not h

Print deprecation warning for all legacy logs methods