Merge pull request #15 from elbaschid/add_python3_support

Make docker-py compatible with python 3
This commit is contained in:
Joffrey F 2013-07-05 10:31:24 -07:00
commit d03d4fb5ca
5 changed files with 23 additions and 9 deletions

3
.gitignore vendored
View File

@ -1,5 +1,8 @@
build build
dist dist
*.egg-info *.egg-info
*.egg/
*.pyc *.pyc
*.swp
.tox

View File

@ -1,8 +1,13 @@
import json import json
import logging import logging
import re import re
import six
import shlex import shlex
from StringIO import StringIO
if six.PY3:
from io import StringIO
else:
from StringIO import StringIO
import requests import requests
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
@ -43,7 +48,7 @@ class Client(requests.Session):
def _container_config(self, image, command, hostname=None, user=None, def _container_config(self, image, command, hostname=None, user=None,
detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None, detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None,
environment=None, dns=None, volumes=None, volumes_from=None): environment=None, dns=None, volumes=None, volumes_from=None):
if isinstance(command, basestring): if isinstance(command, six.string_types):
command = shlex.split(command) command = shlex.split(command)
return { return {
'Hostname': hostname, 'Hostname': hostname,
@ -68,7 +73,7 @@ class Client(requests.Session):
# so we do this disgusting thing here. # so we do this disgusting thing here.
data2 = {} data2 = {}
if data is not None: if data is not None:
for k, v in data.iteritems(): for k, v in six.iteritems(data):
if v is not None: if v is not None:
data2[k] = v data2[k] = v
@ -180,7 +185,7 @@ class Client(requests.Session):
'repo': repository, 'repo': repository,
'tag': tag 'tag': tag
} }
if type(src) == str or type(src) == unicode: if isinstance(src, six.string_types):
params['fromSrc'] = src params['fromSrc'] = src
return self._result(self.post(u, None, params=params)) return self._result(self.post(u, None, params=params))

View File

@ -11,7 +11,7 @@ setup(
version='0.0.6', version='0.0.6',
description="Python client for Docker.", description="Python client for Docker.",
packages=['docker'], packages=['docker'],
install_requires=['requests'] + test_requirements, install_requires=['requests', 'six'] + test_requirements,
zip_safe=False, zip_safe=False,
classifiers=['Development Status :: 3 - Alpha', classifiers=['Development Status :: 3 - Alpha',
'Environment :: Other Environment', 'Environment :: Other Environment',

View File

@ -1,5 +1,5 @@
import six
import unittest import unittest
import time
import docker import docker
@ -66,7 +66,7 @@ class TestImages(BaseTestCase):
class TestImageIds(BaseTestCase): class TestImageIds(BaseTestCase):
def runTest(self): def runTest(self):
res1 = self.client.images(quiet=True) res1 = self.client.images(quiet=True)
self.assertEqual(type(res1[0]), unicode) self.assertEqual(type(res1[0]), six.text_type)
class TestListContainers(BaseTestCase): class TestListContainers(BaseTestCase):
def runTest(self): def runTest(self):
@ -230,7 +230,7 @@ class TestPull(BaseTestCase):
self.assertIn('Images', info) self.assertIn('Images', info)
img_count = info['Images'] img_count = info['Images']
res = self.client.pull('joffrey/test001') res = self.client.pull('joffrey/test001')
self.assertEqual(type(res), unicode) self.assertEqual(type(res), six.text_type)
self.assertEqual(img_count + 2, self.client.info()['Images']) self.assertEqual(img_count + 2, self.client.info()['Images'])
img_info = self.client.inspect_image('joffrey/test001') img_info = self.client.inspect_image('joffrey/test001')
self.assertIn('id', img_info) self.assertIn('id', img_info)
@ -328,4 +328,4 @@ class TestRunShlex(BaseTestCase):
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

6
tox.ini Normal file
View File

@ -0,0 +1,6 @@
[tox]
envlist = py26, py27, py32, py33
[testenv]
commands =
{envpython} setup.py install
{envpython} tests/test.py