Skip to content

Commit

Permalink
fix travis build
Browse files Browse the repository at this point in the history
  • Loading branch information
wayhome committed Feb 12, 2015
1 parent 847183a commit 3c7134e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 52 deletions.
7 changes: 6 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
History
-------

0.0.2 (2015-02-12)
++++++++++++++++++

* add parse redis protocol stream

0.0.1 (2013-08-11)
++++++++++++++++++

* First release on PyPI.
* First release on PyPI.
18 changes: 6 additions & 12 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,11 @@ parse redis protocol stream
------
parse redis protocol stream to redis commands,such as redis pipeline requests or raw responses.

example :
-----------
::

data = '*3\r\n$3\r\nSET\r\n$15\r\nmemtier-8232902\r\n$2\r\nxx\r\n
*3\r\n$3\r\nSET\r\n$15\r\nmemtier-8232902\r\n$2\r\nxx\r\n
*3\r\n$3\r\nSET\r\n$15\r\nmemtier-7630684\r\n$3\r\nAAA\r\n'
print parse_stream(data)

output :
-----------
::

['SET memtier-8232902 xx', 'SET memtier-8232902 xx', 'SET memtier-7630684 AAA']
>>> from redis_protocol import parse_stream
>>> data = '*3\r\n$3\r\nSET\r\n$15\r\nmemtier-8232902\r\n$2\r\nxx\r\n' \
'*3\r\n$3\r\nSET\r\n$15\r\nmemtier-8232902\r\n$2\r\nxx\r\n' \
'*3\r\n$3\r\nSET\r\n$15\r\nmemtier-7630684\r\n$3\r\nAAA\r\n'
>>> print(parse_stream(data))
... ['SET memtier-8232902 xx', 'SET memtier-8232902 xx', 'SET memtier-7630684 AAA']
2 changes: 1 addition & 1 deletion redis_protocol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
__author__ = 'Young King'
__email__ = 'yanckin@gmail.com'
__version__ = '0.0.1'
from .protocol import encode, decode
from .protocol import encode, decode, parse_stream

__all__ = ["encode", "decode"]
71 changes: 36 additions & 35 deletions redis_protocol/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
DELIMITER = "\r\n"


def encode(*args):
"Pack a series of arguments into a value Redis command"
result = []
result.append("*")
result.append(str(len(args)))
result.append(DELIMITER)
for arg in args:
result.append("$")
result.append(str(len(arg)))
result.append(DELIMITER)
result.append(arg)
result.append(DELIMITER)
return "".join(result)


def decode(data):
processed, index = 0, data.find(DELIMITER)
if index == -1:
Expand All @@ -20,6 +35,26 @@ def decode(data):
return parse_integer(data)


def parse_stream(data):
cursor = 0
data_len = len(data)
result = []
while cursor < data_len:
pdata = data[cursor:]
index = pdata.find(DELIMITER)
count = int(pdata[1:index])

cmd = ''
start = index + len(DELIMITER)
for i in range(count):
chunk, length = parse_chunked(pdata, start)
start = length + len(DELIMITER)
cmd += " " + chunk
cursor += start
result.append(cmd.strip())
return result


def parse_multi_chunked(data):
index = data.find(DELIMITER)
count = int(data[1:index])
Expand Down Expand Up @@ -47,26 +82,6 @@ def parse_chunked(data, start=0):
return result if start == 0 else [result, index + len(DELIMITER) + length]


def parse_stream(data):
cursor = 0
data_len = len(data)
result = []
while cursor < data_len :
pdata = data[cursor:]
index = pdata.find(DELIMITER)
count = int(pdata[1:index])

cmd = ''
start = index + len(DELIMITER)
for i in range(count):
chunk, length = parse_chunked(pdata, start)
start = length + len(DELIMITER)
cmd += " " + chunk
cursor += start
result.append(cmd.strip())
return result


def parse_status(data):
return [True, data[1:]]

Expand All @@ -79,23 +94,9 @@ def parse_integer(data):
return [int(data[1:])]


def encode(*args):
"Pack a series of arguments into a value Redis command"
result = []
result.append("*")
result.append(str(len(args)))
result.append(DELIMITER)
for arg in args:
result.append("$")
result.append(str(len(arg)))
result.append(DELIMITER)
result.append(arg)
result.append(DELIMITER)
return "".join(result)

if __name__ == '__main__':
print(decode(encode("ping")))
print((encode("set some value")))
print(encode("foobar"))
data = '*3\r\n$3\r\nSET\r\n$15\r\nmemtier-8232902\r\n$2\r\nxx\r\n*3\r\n$3\r\nSET\r\n$15\r\nmemtier-8232902\r\n$2\r\nxx\r\n*3\r\n$3\r\nSET\r\n$15\r\nmemtier-7630684\r\n$3\r\nAAA\r\n'
print parse_stream(data)
print(parse_stream(data))
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build_sphinx]
source-dir = docs/
build-dir = docs/build
all_files = 1

[upload_sphinx]
upload-dir = docs/build/html
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='redis_protocol',
version='0.0.1',
version='0.0.2',
description='Redis Protocol implemented by python',
long_description=readme + '\n\n' + history,
author='Young King',
Expand Down
8 changes: 7 additions & 1 deletion tests/test_redis_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import unittest
from nose.tools import eq_

from redis_protocol import decode, encode
from redis_protocol import decode, encode, parse_stream


class TestRedis_protocol(unittest.TestCase):
Expand All @@ -35,6 +35,12 @@ def test_none(self):
def test_none_response(self):
eq_(decode("*3\r\n$3\r\nfoo\r\n$-1\r\n$3\r\nbar\r\n"), ["foo", None, "bar"])

def test_parse_tream(self):
data = '*3\r\n$3\r\nSET\r\n$15\r\nmemtier-8232902\r\n$2\r\nxx\r\n' \
'*3\r\n$3\r\nSET\r\n$15\r\nmemtier-8232902\r\n$2\r\nxx\r\n' \
'*3\r\n$3\r\nSET\r\n$15\r\nmemtier-7630684\r\n$3\r\nAAA\r\n'
eq_(parse_stream(data), ['SET memtier-8232902 xx', 'SET memtier-8232902 xx', 'SET memtier-7630684 AAA'])

def tearDown(self):
pass

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py27, py33
envlist = py27, py34

[testenv]
setenv =
Expand Down

0 comments on commit 3c7134e

Please sign in to comment.