Skip to content

Commit

Permalink
python 3 4ever
Browse files Browse the repository at this point in the history
  • Loading branch information
jrabbit committed Aug 12, 2016
1 parent 1644c04 commit 58bf64b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
classifiers=["License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Programming Language :: Python :: 2 :: Only",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.5"
])
2 changes: 1 addition & 1 deletion taskc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.3b1"
__version__ = "0.0.3b2"
15 changes: 11 additions & 4 deletions taskc/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import struct

import email
import six
import ssl
from taskc import transaction

Expand Down Expand Up @@ -97,19 +98,22 @@ def recv(self):
bytes_recd = 0
while bytes_recd < bytes-4:
chunk = self.conn.recv(min(bytes - bytes_recd, 2048))
if chunk == '':
if chunk == b'':
logger.error("socket connection broken")
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
msg = ''.join(chunks)
msg = b''.join(chunks)

# logging data
logger.info("%s Byte Response", bytes)
logger.debug(msg)

# parse the response
resp = email.message_from_string(
msg, _class=transaction.TaskdResponse)
if six.PY3:
resp = email.message_from_bytes(msg, _class=transaction.TaskdResponse)
else:
resp = email.message_from_string(
msg, _class=transaction.TaskdResponse)

if 'code' in resp:
if int(resp['code']) >= 400:
Expand Down Expand Up @@ -164,8 +168,11 @@ def put(self, tasks):
"""
msg = transaction.mk_message(self.group, self.username, self.uuid)
# returns a email.message.Message
print(msg.as_string())
msg.set_payload(tasks)
msg['type'] = 'sync'
print(msg.as_string())
# logging.info(type(msg))
tx_msg = transaction.prep_message(msg)
self.conn.sendall(tx_msg)

Expand Down
29 changes: 17 additions & 12 deletions taskc/test_simple.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import unittest
import logging
import os
import time
import unittest
import uuid
import logging

import six
from docker import Client
from docker.errors import APIError
from taskc.simple import TaskdConnection

try:
import unittest.mock as mock
except ImportError:
import mock

from docker import Client
from docker.errors import APIError

from taskc.simple import TaskdConnection


class TestRCParse(unittest.TestCase):

Expand Down Expand Up @@ -51,7 +51,7 @@ def setUp(self):
def test_pull(self, tdc_connect, mk_msg, conn, recv):
output = "timbo?"
recv.return_value = output
self.assertEquals(self.tc.pull(), output)
self.assertEqual(self.tc.pull(), output)
tdc_connect.assert_called_with()
mk_msg.assert_called_with('sync')

Expand All @@ -70,13 +70,13 @@ def test_put(self, tdc_connect, conn, recv):
class TestConnection(unittest.TestCase):

def setUp(self):
logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig(level=logging.DEBUG)
self.docker = Client(base_url='unix://var/run/docker.sock')
# self.volume_name = "taskc_fixture_pki"
try:
self.docker.remove_container("taskc_test", force=True)
except APIError as e:
logging.exception(e)
logging.error(e)
# volume = self.docker.create_volume(self.volume_name)
# logging.debug(volume)
pki_abs_path = os.path.abspath("taskc/fixture/pki")
Expand All @@ -88,8 +88,13 @@ def setUp(self):
self.tc = TaskdConnection()
o = self.docker.exec_start(our_exec['Id'])
logging.debug(o)
self.tc.uuid = o.split(b'\n')[0].split()[-1]
# print self.tc.uuid
#bytes
our_uuid = o.split(b'\n')[0].split()[-1]
if six.PY3:
our_uuid = our_uuid.decode("utf8")
self.tc.uuid = our_uuid
logging.debug("Type of uuid: %s", type(self.tc.uuid))

self.tc.server = "localhost"
c = self.docker.inspect_container("taskc_test")

Expand Down
11 changes: 9 additions & 2 deletions taskc/transaction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import struct
import logging

from email.message import Message

import six

from taskc.errors import TaskdError
from taskc import __version__

Expand All @@ -25,12 +28,16 @@ def prep_message(msg):
"""
Add the size header
"""
if six.PY3:
msg_out = msg.as_string().encode("utf-8")
else:
msg_out = msg.as_string()

our_len = len(msg.as_string()) + 4
our_len = len(msg_out) + 4
size = struct.pack('>L', our_len)
# why the hell is this "bytes" on python3?
return str(size) + msg.as_string()

return size + msg_out

class TaskdResponse(Message):

Expand Down

0 comments on commit 58bf64b

Please sign in to comment.