# Path: aredis/exceptions.py
# class ConnectionError(RedisError):
#     pass
# 
# class TimeoutError(RedisError):
#     pass
# 
# class RedisError(Exception):
#     pass
# 
# class ExecAbortError(ResponseError):
#     pass
# 
# class BusyLoadingError(ConnectionError):
#     pass
# 
# class NoScriptError(ResponseError):
#     pass
# 
# class ReadOnlyError(ResponseError):
#     pass
# 
# class ResponseError(RedisError):
#     pass
# 
# class InvalidResponse(RedisError):
#     pass
# 
# class AskError(ResponseError):
#     """
#     src node: MIGRATING to dst node
#         get > ASK error
#         ask dst node > ASKING command
#     dst node: IMPORTING from src node
#         asking command only affects next command
#         any op will be allowed after asking command
#     """
# 
#     def __init__(self, resp):
#         """should only redirect to master node"""
#         self.args = (resp,)
#         self.message = resp
#         slot_id, new_node = resp.split(' ')
#         host, port = new_node.rsplit(':', 1)
#         self.slot_id = int(slot_id)
#         self.node_addr = self.host, self.port = host, int(port)
# 
# class MovedError(AskError):
#     pass
# 
# class TryAgainError(ResponseError):
# 
#     def __init__(self, *args, **kwargs):
#         pass
# 
# class ClusterDownError(ClusterError, ResponseError):
# 
#     def __init__(self, resp):
#         self.args = (resp,)
#         self.message = resp
# 
# class ClusterCrossSlotError(ResponseError):
#     message = "Keys in request don't hash to the same slot"
# 
# Path: aredis/utils.py
# def b(x):
#     return x.encode('latin-1') if not isinstance(x, bytes) else x
# 
# def nativestr(x):
#     return x if isinstance(x, str) else x.decode('utf-8', 'replace')
# 
# LOOP_DEPRECATED = sys.version_info >= (3, 8)


import asyncio
import inspect
import os
import socket
import ssl
import sys
import time
import warnings
import aredis.compat
    import hiredis
from io import BytesIO
from aredis.exceptions import (ConnectionError, TimeoutError,
                               RedisError, ExecAbortError,
                               BusyLoadingError, NoScriptError,
                               ReadOnlyError, ResponseError,
                               InvalidResponse, AskError,
                               MovedError, TryAgainError,
                               ClusterDownError, ClusterCrossSlotError)
from aredis.utils import b, nativestr, LOOP_DEPRECATED

        # if a database is specified, switch to it
        if self.db:
            await self.send_command('SELECT', self.db)
            if nativestr(await self.read_response()) != 'OK':
                raise ConnectionError('Invalid Database')
        self.last_active_at = time.time()

    async def read_response(self):
        try:
            response = await exec_with_timeout(self._parser.read_response(), self._stream_timeout, loop=self.loop)
            self.last_active_at = time.time()
        except TimeoutError:
            self.disconnect()
            raise
        if isinstance(response, RedisError):
            raise response
        self.awaiting_response = False
        return response

    async def send_packed_command(self, command):
        """Sends an already packed command to the Redis server"""
        if not self._writer:
            await self.connect()
        try:
            if isinstance(command, str):
                command = [command]
            self._writer.writelines(command)
        except aredis.compat.TimeoutError:
            self.disconnect()

# Path: aredis/exceptions.py
# class ConnectionError(RedisError):
#     pass
# 
# class TimeoutError(RedisError):
#     pass
# 
# class RedisError(Exception):
#     pass
# 
# class ExecAbortError(ResponseError):
#     pass
# 
# class BusyLoadingError(ConnectionError):
#     pass
# 
# class NoScriptError(ResponseError):
#     pass
# 
# class ReadOnlyError(ResponseError):
#     pass
# 
# class ResponseError(RedisError):
#     pass
# 
# class InvalidResponse(RedisError):
#     pass
# 
# class AskError(ResponseError):
#     """
#     src node: MIGRATING to dst node
#         get > ASK error
#         ask dst node > ASKING command
#     dst node: IMPORTING from src node
#         asking command only affects next command
#         any op will be allowed after asking command
#     """
# 
#     def __init__(self, resp):
#         """should only redirect to master node"""
#         self.args = (resp,)
#         self.message = resp
#         slot_id, new_node = resp.split(' ')
#         host, port = new_node.rsplit(':', 1)
#         self.slot_id = int(slot_id)
#         self.node_addr = self.host, self.port = host, int(port)
# 
# class MovedError(AskError):
#     pass
# 
# class TryAgainError(ResponseError):
# 
#     def __init__(self, *args, **kwargs):
#         pass
# 
# class ClusterDownError(ClusterError, ResponseError):
# 
#     def __init__(self, resp):
#         self.args = (resp,)
#         self.message = resp
# 
# class ClusterCrossSlotError(ResponseError):
#     message = "Keys in request don't hash to the same slot"
# 
# Path: aredis/utils.py
# def b(x):
#     return x.encode('latin-1') if not isinstance(x, bytes) else x
# 
# def nativestr(x):
#     return x if isinstance(x, str) else x.decode('utf-8', 'replace')
# 
# LOOP_DEPRECATED = sys.version_info >= (3, 8)


# Path: aredis/connection.py
import asyncio
import inspect
import os
import socket
import ssl
import sys
import time
import warnings
import aredis.compat
    import hiredis
from io import BytesIO
from aredis.exceptions import (ConnectionError, TimeoutError,
                               RedisError, ExecAbortError,
                               BusyLoadingError, NoScriptError,
                               ReadOnlyError, ResponseError,
                               InvalidResponse, AskError,
                               MovedError, TryAgainError,
                               ClusterDownError, ClusterCrossSlotError)
from aredis.utils import b, nativestr, LOOP_DEPRECATED


        # if a database is specified, switch to it
        if self.db:
            await self.send_command('SELECT', self.db)
            if nativestr(await self.read_response()) != 'OK':
                raise ConnectionError('Invalid Database')
        self.last_active_at = time.time()

    async def read_response(self):
        try:
            response = await exec_with_timeout(self._parser.read_response(), self._stream_timeout, loop=self.loop)
            self.last_active_at = time.time()
        except TimeoutError:
            self.disconnect()
            raise
        if isinstance(response, RedisError):
            raise response
        self.awaiting_response = False
        return response

    async def send_packed_command(self, command):
        """Sends an already packed command to the Redis server"""
        if not self._writer:
            await self.connect()
        try:
            if isinstance(command, str):
                command = [command]
            self._writer.writelines(command)
        except aredis.compat.TimeoutError:
            self.disconnect()

            raise TimeoutError("Timeout writing to socket")