# -*- coding: utf-8 -*-

"""
Interface to Redis-storage.

@author: Seva Zhidkov
@contact: zhidkovseva@gmail.com
@license: The MIT license

Copyright (C) 2015
"""
from .utils import logger

# We will catch all import exceptions in bot.py
from redis import StrictRedis


class Storage:
    def __init__(self, bot):
        """
        Create new storage for bot

        :param bot: Bot object
        :return:
        """
        self.bot = bot

        # Connect to Redis.
        # If we had problems with Redis - just set self.redis to None.
        # Not redis-required modules must work without Redis.
        try:
            # We get parameters for redis connection from bot's config.
            self.redis = StrictRedis(
                host=bot.config.get('SHELDON_REDIS_HOST', 'localhost'),
                port=bot.config.get('SHELDON_REDIS_PORT', '6379'),
                db=bot.config.get('SHELDON_REDIS_DB', '0')
            )
        except Exception as error:
            logger.error_log_message('Error while connecting Redis:')
            logger.error_log_message(str(error.__traceback__))
            self.redis = None

    def get(self, key, default_value=None):
        """
        Get value from redis

        :param key: string, redis key for needed value
        :param default_value: string, value that returns if
                              key not found or redis isn't
                              connected to this bot
        :return: value with that key or default value
        """
        # If we had problems with redis connection,
        # return None or other default value
        if not self.redis:
            return default_value

        value = self.redis.get(key)
        if value is not None:
            return value
        else:
            return default_value



# -*- coding: utf-8 -*-

"""
Interface to Redis-storage.

@author: Seva Zhidkov
@contact: zhidkovseva@gmail.com
@license: The MIT license

Copyright (C) 2015
"""
from .utils import logger

# We will catch all import exceptions in bot.py
from redis import StrictRedis


class Storage:
    def __init__(self, bot):
        """
        Create new storage for bot

        :param bot: Bot object
        :return:
        """
        self.bot = bot

        # Connect to Redis.
        # If we had problems with Redis - just set self.redis to None.
        # Not redis-required modules must work without Redis.
        try:
            # We get parameters for redis connection from bot's config.
            self.redis = StrictRedis(
                host=bot.config.get('SHELDON_REDIS_HOST', 'localhost'),
                port=bot.config.get('SHELDON_REDIS_PORT', '6379'),
                db=bot.config.get('SHELDON_REDIS_DB', '0')
            )
        except Exception as error:
            logger.error_log_message('Error while connecting Redis:')
            logger.error_log_message(str(error.__traceback__))
            self.redis = None

    def get(self, key, default_value=None):
        """
        Get value from redis storage

        :param key: string, redis key for needed value
        :param default_value: string, value that returns if
                              key not found or redis isn't
                              connected to this bot
        :return: value with that key or default value
        """
        # If we had problems with redis connection,
        # return None or other default value
        if not self.redis:
            return default_value

        value = self.redis.get(key)
        if value is not None:
            return value
        else:
            return default_value

    def set(self, key, value):
        """
        Set key to value in redis storage

        :param key: string
        :param value: string
        :return:
        """
        if not self.redis:
            return None

        return self.redis.set(key, value)




Add set function to bot's storage