Skip to content

Commit d535423

Browse files
committed
Merge pull request #1 from 18F/py3
Python3 support + django 1.7 in tests
2 parents 26f3cbe + 6f13633 commit d535423

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Requirements
1212
------------
1313

1414
* pylibmc
15-
* Django 1.3+.
15+
* Django 1.5+.
1616

17-
It was written and tested on Python 2.7.
17+
It was written and tested on Python 2.7 and 3.4.
1818

1919
Installation
2020
------------

django_elasticache/cluster_utils.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
utils for discovery cluster
33
"""
44
from distutils.version import StrictVersion
5+
from django.utils.encoding import smart_text
56
import re
67
from telnetlib import Telnet
78

@@ -28,20 +29,20 @@ def get_cluster_info(host, port):
2829
}
2930
"""
3031
client = Telnet(host, int(port))
31-
client.write('version\n')
32-
res = client.read_until('\r\n').strip()
33-
version_list = res.split(' ')
34-
if len(version_list) != 2 or version_list[0] != 'VERSION':
32+
client.write(b'version\n')
33+
res = client.read_until(b'\r\n').strip()
34+
version_list = res.split(b' ')
35+
if len(version_list) != 2 or version_list[0] != b'VERSION':
3536
raise WrongProtocolData('version', res)
3637
version = version_list[1]
37-
if StrictVersion(version) >= StrictVersion('1.4.14'):
38-
cmd = 'config get cluster\n'
38+
if StrictVersion(smart_text(version)) >= StrictVersion('1.4.14'):
39+
cmd = b'config get cluster\n'
3940
else:
40-
cmd = 'get AmazonElastiCache:cluster\n'
41+
cmd = b'get AmazonElastiCache:cluster\n'
4142
client.write(cmd)
42-
res = client.read_until('\n\r\nEND\r\n')
43+
res = client.read_until(b'\n\r\nEND\r\n')
4344
client.close()
44-
ls = filter(None, re.compile(r'\r?\n').split(res))
45+
ls = list(filter(None, re.compile(br'\r?\n').split(res)))
4546
if len(ls) != 4:
4647
raise WrongProtocolData(cmd, res)
4748

@@ -51,9 +52,10 @@ def get_cluster_info(host, port):
5152
raise WrongProtocolData(cmd, res)
5253
nodes = []
5354
try:
54-
for node in ls[2].split(' '):
55-
host, ip, port = node.split('|')
56-
nodes.append('{}:{}'.format(ip or host, port))
55+
for node in ls[2].split(b' '):
56+
host, ip, port = node.split(b'|')
57+
nodes.append('{}:{}'.format(smart_text(ip or host),
58+
smart_text(port)))
5759
except ValueError:
5860
raise WrongProtocolData(cmd, res)
5961
return {

tests/test_backend.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
from django.conf import global_settings
2-
from mock import patch, Mock
1+
from django.conf import global_settings, settings
32
from nose.tools import eq_, raises
3+
import sys
4+
if sys.version < '3':
5+
from mock import patch, Mock
6+
else:
7+
from unittest.mock import patch, Mock
8+
9+
10+
# Initialize django 1.7
11+
settings.configure()
12+
global_settings.configured = True
413

514

615
@patch('django.conf.settings', global_settings)

tests/test_protocol.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
from mock import patch, call, MagicMock
21
from django_elasticache.cluster_utils import (
32
get_cluster_info, WrongProtocolData)
43
from nose.tools import eq_, raises
4+
import sys
5+
if sys.version < '3':
6+
from mock import patch, call, MagicMock
7+
else:
8+
from unittest.mock import patch, call, MagicMock
9+
510

611
TEST_PROTOCOL_1 = [
7-
'VERSION 1.4.14',
8-
'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
12+
b'VERSION 1.4.14',
13+
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
914
]
1015

1116
TEST_PROTOCOL_2 = [
12-
'VERSION 1.4.13',
13-
'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
17+
b'VERSION 1.4.13',
18+
b'CONFIG cluster 0 138\r\n1\nhost|ip|port host||port\n\r\nEND\r\n',
1419
]
1520

1621

@@ -35,8 +40,8 @@ def test_last_versions(Telnet):
3540
client.read_until.side_effect = TEST_PROTOCOL_1
3641
get_cluster_info('', 0)
3742
client.write.assert_has_calls([
38-
call('version\n'),
39-
call('config get cluster\n'),
43+
call(b'version\n'),
44+
call(b'config get cluster\n'),
4045
])
4146

4247

@@ -46,6 +51,6 @@ def test_prev_versions(Telnet):
4651
client.read_until.side_effect = TEST_PROTOCOL_2
4752
get_cluster_info('', 0)
4853
client.write.assert_has_calls([
49-
call('version\n'),
50-
call('get AmazonElastiCache:cluster\n'),
54+
call(b'version\n'),
55+
call(b'get AmazonElastiCache:cluster\n'),
5156
])

0 commit comments

Comments
 (0)