Ejemplo n.º 1
0
 def __init__(self, encoding=None, errors=None):
     self._encoding = {}
     if encoding is not None:
         self._encoding["encoding"] = encoding
     if errors is not None:
         self._encoding["errors"] = errors
     if hiredis is None:
         self._reader = None
         self._unparsed = b""
         self._parser = self._parse()
         next(self._parser)
         self._ready = collections.deque()
         self.feed = self._feed
         self.gets = self._gets
         self.has_data = self._has_data
     else:
         self._reader = hiredis.Reader(**self._encoding)
         self.feed = self._reader.feed
         self.gets = self._reader.gets
         self.has_data = self._reader.has_data
Ejemplo n.º 2
0
 def __init__(self,
              host=DEFAULT_HOST,
              port=DEFAULT_PORT,
              db=DEFAULT_DB,
              on_close=None):
     self._buffer = bytes()
     self._busy = locks.Lock()
     self._client = tcpclient.TCPClient()
     self._connecting = None
     self._default_db = int(db or DEFAULT_DB)
     self._host = host
     self._port = port
     self._ioloop = ioloop.IOLoop.current()
     self._on_close = on_close
     self._pipeline = False
     self._pipeline_commands = []
     self._pool = []
     self._reader = hiredis.Reader()
     self._stream = None
     super(RedisClient, self).__init__()
Ejemplo n.º 3
0
 def __init__(self):
     self.response = collections.deque()
     self.parser = hiredis.Reader()
     self.transport = None  # type: Optional[asyncio.transports.Transport]
     self.commands = {
         b"COMMAND": self.com_command,
         b"SET": self.com_set,
         b"GET": self.com_get,
         b"PING": self.com_ping,
         b"INCR": self.com_incr,
         b"LPUSH": self.com_lpush,
         b"RPUSH": self.com_rpush,
         b"LPOP": self.com_lpop,
         b"RPOP": self.com_rpop,
         b"SADD": self.com_sadd,
         b"HSET": self.com_hset,
         b"SPOP": self.com_spop,
         b"LRANGE": self.com_lrange,
         b"MSET": self.com_mset,
     }
Ejemplo n.º 4
0
 def __init__(self, reader, writer, *, encoding=None, loop=None):
     if loop is None:
         loop = asyncio.get_event_loop()
     self._reader = reader
     self._writer = writer
     self._loop = loop
     self._waiters = deque()
     self._parser = hiredis.Reader(protocolError=ProtocolError,
                                   replyError=ReplyError)
     self._reader_task = asyncio.Task(self._read_data(), loop=self._loop)
     self._db = 0
     self._closing = False
     self._closed = False
     self._close_waiter = asyncio.Future(loop=self._loop)
     self._reader_task.add_done_callback(self._close_waiter.set_result)
     self._in_transaction = False
     self._transaction_error = None
     self._in_pubsub = 0
     self._pubsub_channels = coerced_keys_dict()
     self._pubsub_patterns = coerced_keys_dict()
     self._encoding = encoding
Ejemplo n.º 5
0
 def __init__(self):
     self.dictionary = dictionary
     self.response = collections.deque()
     self.parser = hiredis.Reader()
     self.transport = None  # type: asyncio.transports.Transport
     self.commands = {
         b"COMMAND": self.command,
         b"SET": self.set,
         b"GET": self.get,
         b"PING": self.ping,
         b"INCR": self.incr,
         b"LPUSH": self.lpush,
         b"RPUSH": self.rpush,
         b"LPOP": self.lpop,
         b"RPOP": self.rpop,
         b"SADD": self.sadd,
         b"HSET": self.hset,
         b"SPOP": self.spop,
         b"LRANGE": self.lrange,
         b"MSET": self.mset,
     }
Ejemplo n.º 6
0
Archivo: redis.py Proyecto: versee/vlcp
 def reconnect_init(self, connection):
     connection.xid = 1
     connection.redis_replyxid = 1
     connection.redis_ping = -1
     connection.redis_pingreply = -1
     connection.redis_bufferedxid = 0
     connection.redis_sendbuffer = []
     connection.redis_sender = False
     connection.redis_locker = object()
     write_buffer = []
     if self.usehiredis:
         connection.redis_reader = hiredis.Reader(
             protocolError=RedisProtocolException,
             replyError=RedisReplyException)
     else:
         connection.redis_reader = RedisParser()
     if connection.redis_select:
         write_buffer.append(
             self.format_request(b'SELECT', connection.redis_select))
         connection.xid += 1
     if connection.redis_subscribe:
         if connection.redis_subscribe_keys:
             write_buffer.append(
                 self.format_request(
                     b'SUBSCRIBE', *tuple(connection.redis_subscribe_keys)))
         if connection.redis_subscribe_pkeys:
             write_buffer.append(
                 self.format_request(
                     b'PSUBSCRIBE',
                     *tuple(connection.redis_subscribe_pkeys)))
     connection.scheduler.emergesend(
         ConnectionWriteEvent(connection,
                              connection.connmark,
                              data=b''.join(write_buffer)))
     for m in connection.waitForSend(
             RedisConnectionStateEvent(
                 RedisConnectionStateEvent.CONNECTION_UP, connection,
                 connection.connmark, self)):
         yield m
Ejemplo n.º 7
0
    def __init__(self, charset="utf-8", errors="strict"):
        self._reader = hiredis.Reader(protocolError=InvalidData,
                                      replyError=ResponseError)
        self.charset = charset
        self.errors = errors

        self.bulk_length = 0
        self.bulk_buffer = []

        self.post_proc = []

        self.replyQueue = DeferredQueue()

        self.transactions = 0
        self.inTransaction = False
        self.unwatch_cc = lambda: ()
        self.commit_cc = lambda: ()

        self.script_hashes = set()

        self.pipelining = False
        self.pipelined_commands = []
        self.pipelined_replies = []
Ejemplo n.º 8
0
    def __init__(self,
                 hosts,
                 on_close=None,
                 io_loop=None,
                 clustering=False,
                 auto_connect=True):
        """Create a new instance of the ``Client`` class.

        :param hosts: A list of host connection values.
        :type hosts: list(dict)
        :param io_loop: Override the current Tornado IOLoop instance
        :type io_loop: tornado.ioloop.IOLoop
        :param method on_close: The method to call if the connection is closed
        :param bool clustering: Toggle the cluster support in the client
        :param bool auto_connect: Toggle the auto-connect on creation feature

        """
        self._buffer = bytes()
        self._busy = locks.Lock()
        self._closing = False
        self._cluster = {}
        self._clustering = clustering
        self._connected = locks.Event()
        self._connect_future = concurrent.Future()
        self._connection = None
        self._discovery = False
        self._hosts = hosts
        self._on_close_callback = on_close
        self._reader = hiredis.Reader()
        self.io_loop = io_loop or ioloop.IOLoop.current()
        if not self._clustering:
            if len(hosts) > 1:
                raise ValueError('Too many hosts for non-clustering mode')
        if auto_connect:
            LOGGER.debug('Auto-connecting')
            self.connect()
Ejemplo n.º 9
0
 def on_connect(self, connection):
     self._sock = connection._sock
     self._reader = hiredis.Reader(protocolError=InvalidResponse,
                                   replyError=ResponseError)
Ejemplo n.º 10
0
def test_redis():
    reader = hiredis.Reader()
    reader.feed("$5\r\nHello\r\n")
    return reader.gets()
Ejemplo n.º 11
0
 def on_connect(self):
     self.stream.set_close_callback(self.on_close)
     self.stream.read_until_close(self.on_data, self.on_streaming_data)
     self.reader = hiredis.Reader()
     self.connected()
Ejemplo n.º 12
0
 def setUp(self):
     self.reader = hiredis.Reader()
Ejemplo n.º 13
0
 def test_set_encoding_invalid_error_handler(self):
     self.reader = hiredis.Reader(encoding="utf-8")
     self.assertRaises(LookupError,
                       self.reader.set_encoding,
                       encoding="utf-8",
                       errors="unknown")
Ejemplo n.º 14
0
 def test_should_decode_true_flag_decodes_as_normal(self):
     snowman = b"\xe2\x98\x83"
     self.reader = hiredis.Reader(encoding="utf-8")
     self.reader.feed(b"$3\r\n" + snowman + b"\r\n")
     self.assertEquals(snowman.decode("utf-8"), self.reader.gets(True))
Ejemplo n.º 15
0
 def test_decode_error_with_ignore_errors(self):
     self.reader = hiredis.Reader(encoding="utf-8", errors="ignore")
     self.reader.feed(b"+\x80value\r\n")
     self.assertEquals("value", self.reader.gets())
Ejemplo n.º 16
0
 def __init__(self):
     self.reader = hiredis.Reader()
     self.reader.setmaxbuf(0)
Ejemplo n.º 17
0
#!/usr/bin/env python
""" A redis appendonly file parser
"""

import logging
import hiredis
import sys

if len(sys.argv) != 2:
    print sys.argv[0], 'aof_file'
    sys.exit()
file = open(sys.argv[1])
line = file.readline()
cur_request = line
while line:
    req_reader = hiredis.Reader()
    req_reader.setmaxbuf(0)
    req_reader.feed(cur_request)
    command = req_reader.gets()
    try:
        if command is not False:
            print command
            cur_request = ''
    except hiredis.ProtocolError:
        print 'protocol error'
    line = file.readline()
    cur_request += line
file.close
Ejemplo n.º 18
0
 def __init__(self, redis):
     self.transport = None
     self.parser = hiredis.Reader()
     self.queue = collections.deque()
     self.redis = redis
Ejemplo n.º 19
0
 def test_bulk_string_with_encoding(self):
     snowman = b"\xe2\x98\x83"
     self.reader = hiredis.Reader(encoding="utf-8")
     self.reader.feed(b"$3\r\n" + snowman + b"\r\n")
     self.assertEquals(snowman.decode("utf-8"), self.reply())
Ejemplo n.º 20
0
 def _reset(self):
     self.reader = hiredis.Reader()
     self._sub_callback = None
Ejemplo n.º 21
0
 def test_decode_errors_defaults_to_strict(self):
     self.reader = hiredis.Reader(encoding="utf-8")
     self.reader.feed(b"+\x80\r\n")
     self.assertRaises(UnicodeDecodeError, self.reader.gets)
Ejemplo n.º 22
0
 def __init__(self, server: FakeRedisPubSubServer):
     self._server = server
     self._reader = hiredis.Reader()
Ejemplo n.º 23
0
 def test_decode_error_with_surrogateescape_errors(self):
     self.reader = hiredis.Reader(encoding="utf-8",
                                  errors="surrogateescape")
     self.reader.feed(b"+\x80value\r\n")
     self.assertEquals("\udc80value", self.reader.gets())
Ejemplo n.º 24
0
 def __init__(self, host='localhost', port=6379):
     self.host = host
     self.port = port
     self._sock = None
     self._reader = hiredis.Reader(protocolError=exceptions.ProtocolError,
                                   replyError=exceptions.ReplyError)
Ejemplo n.º 25
0
 def test_protocol_error_with_custom_class(self):
     self.reader = hiredis.Reader(protocolError=RuntimeError)
     self.reader.feed(b"x")
     self.assertRaises(RuntimeError, self.reply)
Ejemplo n.º 26
0
 def test_multi_bulk_with_invalid_encoding_and_partial_reply(self):
   self.reader = hiredis.Reader(encoding="unknown")
   self.reader.feed(b"*2\r\n$5\r\nhello\r\n")
   self.assertEquals(False, self.reply())
   self.reader.feed(b":1\r\n")
   self.assertRaises(LookupError, self.reply)
Ejemplo n.º 27
0
 def test_custom_not_enough_data(self):
     self.reader = hiredis.Reader(notEnoughData=Ellipsis)
     assert self.reader.gets() is Ellipsis
Ejemplo n.º 28
0
 def test_faulty_protocol_error_class(self):
   def make_error(errstr):
     1 / 0
   self.reader = hiredis.Reader(protocolError=make_error)
   self.reader.feed(b"x")
   self.assertRaises(ZeroDivisionError, self.reply)
Ejemplo n.º 29
0
 def test_bulk_string_with_invalid_encoding(self):
   self.reader = hiredis.Reader(encoding="unknown")
   self.reader.feed(b"$5\r\nhello\r\n")
   self.assertRaises(LookupError, self.reply)
Ejemplo n.º 30
0
 def __init__(self, db: ProtoRedis):
     self._db = db
     self.response = deque()
     self.parser = hiredis.Reader()
     self.timer = time.monotonic()