Permalink
Browse files

Merge pull request #27 from featalion/test_python_3_support

Python v3 support
  • Loading branch information...
2 parents 173631e + 560cf0a commit db69b8448741e659aab04b9225692d10a9ff1892 @featalion featalion committed May 23, 2013
Showing with 63 additions and 36 deletions.
  1. +50 −31 iron_worker.py
  2. +9 −3 setup.py
  3. +4 −2 test.py
View
81 iron_worker.py
@@ -1,5 +1,9 @@
import os
-import httplib
+import sys
+if sys.version_info >= (3,):
+ import http.client
+else:
+ import httplib
import mimetypes
import zipfile
import time
@@ -16,7 +20,7 @@ def file_exists(file):
if not os.path.exists(file):
return False
try:
- open(file)
+ open(file).close()
except IOError:
return False
return True
@@ -78,18 +82,26 @@ def __repr__(self):
def __set(self, attr, value):
if attr in self.__rfc3339_attrs:
- if isinstance(value, basestring):
- value = iron_core.IronClient.fromRfc3339(value)
+ if sys.version_info >= (3,):
+ if isinstance(value, str):
+ value = iron_core.IronClient.fromRfc3339(value)
+ else:
+ if isinstance(value, basestring):
+ value = iron_core.IronClient.fromRfc3339(value)
if attr in self.__schedule_attrs:
self.scheduled = True
if attr in self.__repeating_attrs:
self.repeating = True
if attr in self.__json_attrs:
- if isinstance(value, basestring):
- try:
- value = json.loads(value)
- except:
- pass
+ try:
+ if sys.version_info >= (3,):
+ if isinstance(value, str):
+ value = json.loads(value)
+ else:
+ if isinstance(value, basestring):
+ value = json.loads(value)
+ except:
+ pass
setattr(self, attr, value)
def __init__(self, values=None, **kwargs):
@@ -179,24 +191,26 @@ def merge(self, target, ignoreRootDir=False):
else:
raise ValueError("'%s' is not a file or directory." % target)
if len(self.files) == 1:
- for dest, loc in self.files.iteritems():
+ for dest, loc in self.files.items():
self.executable = dest
def merge_dependency(self, dep):
dependency = __import__(dep)
- location = os.path.dirname(dependency.__file__)
- print location
- parent = location.rstrip(os.path.basename(location))
- print parent
- for dirname, dirnames, filenames in os.walk(location):
- for filename in filenames:
- path = os.path.join(dirname, filename)
- if path.startswith(parent):
- newpath = path[len(parent):]
- else:
- newpath = path
- ziploc = newpath.lstrip("/")
- self.files[ziploc] = path
+ location = os.path.dirname(dependency.__file__)
+ sys.stdout.write(str(location))
+
+ parent = location.rstrip(os.path.basename(location))
+ sys.stdout.write(str(parent))
+
+ for dirname, dirnames, filenames in os.walk(location):
+ for filename in filenames:
+ path = os.path.join(dirname, filename)
+ if path.startswith(parent):
+ newpath = path[len(parent):]
+ else:
+ newpath = path
+ ziploc = newpath.lstrip("/")
+ self.files[ziploc] = path
def zip(self, destination=None, overwrite=True):
if destination is None:
@@ -372,8 +386,12 @@ def queue(self, task=None, tasks=None, retry=None, **kwargs):
tasks = [task]
for task in tasks:
payload = task.payload
- if not isinstance(payload, basestring):
- payload = json.dumps(payload)
+ if sys.version_info >= (3,):
+ if not isinstance(payload, str):
+ payload = json.dumps(payload)
+ else:
+ if not isinstance(payload, basestring):
+ payload = json.dumps(payload)
if task.code_name is None:
raise ValueError("task.code_name is required.")
task_data = {
@@ -391,15 +409,15 @@ def queue(self, task=None, tasks=None, retry=None, **kwargs):
if task.run_every is not None:
task_data["run_every"] = task.run_every
if task.end_at is not None:
- if task.end_at.tzinfo is None:
- task.end_at = task.end_at.replace(tzinfo=tzlocal())
+ if task.end_at.tzinfo is None:
+ task.end_at = task.end_at.replace(tzinfo=tzlocal())
task_data["end_at"] = iron_core.IronClient.toRfc3339(
task.end_at)
if task.run_times is not None:
task_data["run_times"] = task.run_times
if task.start_at is not None:
- if task.start_at.tzinfo is None:
- task.start_at = task.start_at.replace(tzinfo=tzlocal())
+ if task.start_at.tzinfo is None:
+ task.start_at = task.start_at.replace(tzinfo=tzlocal())
task_data["start_at"] = iron_core.IronClient.toRfc3339(
task.start_at)
tasks_data.append(task_data)
@@ -469,17 +487,18 @@ def encode_multipart_formdata(fields, files):
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
- L.append(value)
+ L.append(str(value))
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"; filename="%s"'
% (key, filename))
L.append('Content-Type: %s'
% IronWorker.get_content_type(filename))
L.append('')
- L.append(value)
+ L.append(str(value))
L.append('--' + BOUNDARY + '--')
L.append('')
+
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, str(body)
View
12 setup.py
@@ -1,11 +1,16 @@
from setuptools import setup
+import sys
+
+python3_setup = {}
+if sys.version_info >= (3,):
+ python3_setup['use_2to3'] = True
setup(
name='iron-worker',
py_modules=["iron_worker"],
packages=["testDir"],
- version='1.2.0',
- install_requires=["iron_core", "python-dateutil"],
+ version='1.3.0',
+ install_requires=["iron_core >= 1.1.0", "python-dateutil"],
description='The Python client for IronWorker, a cloud service for background processing.',
author='Iron.io',
author_email="support@iron.io",
@@ -34,5 +39,6 @@
IronWorker is a background processing and task queuing system that lets your
applications use the cloud to do their heavy lifting. Find out more at
-http://www.iron.io/products/worker."""
+http://www.iron.io/products/worker.""",
+ **python3_setup
)
View
6 test.py
@@ -61,16 +61,18 @@ def test_getTaskDetails(self):
self.assertEqual(task_id, task.id)
def test_zcancelTask(self):
+ time.sleep(2)
tasks = self.worker.tasks()
-
for task in tasks:
self.worker.cancel(task)
+ time.sleep(3)
new_tasks = self.worker.tasks()
real_tasks = []
for task in new_tasks:
- if task.status not in ['cancelled', 'error']:
+ if task.status not in ['cancelled', 'error', 'killed']:
real_tasks.append(task)
+
self.assertEqual(len(real_tasks), 0)
def test_postSchedule(self):

0 comments on commit db69b84

Please sign in to comment.