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
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__()
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, }
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
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, }
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
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 = []
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()
def on_connect(self, connection): self._sock = connection._sock self._reader = hiredis.Reader(protocolError=InvalidResponse, replyError=ResponseError)
def test_redis(): reader = hiredis.Reader() reader.feed("$5\r\nHello\r\n") return reader.gets()
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()
def setUp(self): self.reader = hiredis.Reader()
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")
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))
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())
def __init__(self): self.reader = hiredis.Reader() self.reader.setmaxbuf(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
def __init__(self, redis): self.transport = None self.parser = hiredis.Reader() self.queue = collections.deque() self.redis = redis
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())
def _reset(self): self.reader = hiredis.Reader() self._sub_callback = None
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)
def __init__(self, server: FakeRedisPubSubServer): self._server = server self._reader = hiredis.Reader()
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())
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)
def test_protocol_error_with_custom_class(self): self.reader = hiredis.Reader(protocolError=RuntimeError) self.reader.feed(b"x") self.assertRaises(RuntimeError, self.reply)
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)
def test_custom_not_enough_data(self): self.reader = hiredis.Reader(notEnoughData=Ellipsis) assert self.reader.gets() is Ellipsis
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)
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)
def __init__(self, db: ProtoRedis): self._db = db self.response = deque() self.parser = hiredis.Reader() self.timer = time.monotonic()