Skip to content

Commit

Permalink
global: support for Python 3
Browse files Browse the repository at this point in the history
* Fixes problems with exceptions handling and str/unicode.  (closes #7)
  • Loading branch information
jirikuncar committed Aug 26, 2014
1 parent 576d318 commit 5fb2148
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 43 deletions.
3 changes: 2 additions & 1 deletion bin/run_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# more details.

import glob
import six
import sys
import os
import imp
Expand Down Expand Up @@ -139,7 +140,7 @@ def run_workflow(file_or_module,
@return: workflow engine instance (after its workflow was executed)
"""

if isinstance(file_or_module, basestring):
if isinstance(file_or_module, six.string_types):
log.info("Loading: %s" % file_or_module)
workflow = get_workflow(file_or_module)
elif isinstance(file_or_module, list):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def run_tests(self):
'coverage'
],
cmdclass={'test': PyTest},
install_requires=['configobj>4.7.0'],
install_requires=['configobj>4.7.0', 'six'],
long_description="""\
Simple workflows for Python
-------------------------------------
Expand Down
9 changes: 5 additions & 4 deletions tests/test_engine_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# more details.

import unittest
import six
import sys
import os

Expand Down Expand Up @@ -78,14 +79,14 @@ def test_init(self):
callback_chooser='x',
before_processing='x',
after_processing='x')
except Exception, msg:
assert 'must be a callable' not in msg
except Exception as msg:
assert 'must be a callable' in str(msg)

try:
we3 = GenericWorkflowEngine(callback_chooser=asterisk_chooser,
after_processing='x')
except Exception, msg:
assert 'must be a callable' not in msg
except Exception as msg:
assert 'must be a callable' in str(msg)

we1.addManyCallbacks('*', [
m('mouse'),
Expand Down
15 changes: 4 additions & 11 deletions tests/test_engine_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# more details.

import unittest
import six
import sys
import os

Expand Down Expand Up @@ -89,10 +90,7 @@ def x(token, inst):
class FakeToken(object):

def __init__(self, data, **attributes):
if isinstance(data, basestring):
self.data = unicode(data)
else:
self.data = data
self.data = data
self.pos = None # set TokenCollection on obj return
# here link to TokenCollection (when returning)
self.backreference = None
Expand All @@ -103,10 +101,7 @@ def __init__(self, data, **attributes):
self.setFeature(attr_name, attr_value)

def __str__(self):
if isinstance(self.data, unicode):
return self.data
else:
return str(self.data)
return str(self.data)

def __repr__(self):
return 'Token(%s, **%s)' % (repr(self.data), repr(self.__attributes))
Expand Down Expand Up @@ -159,8 +154,6 @@ def getFeature(self, key):
return None

def setFeature(self, key, value):
if isinstance(value, basestring):
value = unicode(value)
self.__attributes[key] = value

def setFeatureKw(self, **kwargs):
Expand All @@ -178,7 +171,7 @@ class TestWorkflowEngine(unittest.TestCase):
def setUp(self):
self.key = '*'
self.we = wfe_impl()
self.data = u"one\ntwo\nthree\nfour\nfive"
self.data = "one\ntwo\nthree\nfour\nfive"
self.doc = [FakeToken(x, type='*') for x in self.data.splitlines()]

def tearDown(self):
Expand Down
11 changes: 7 additions & 4 deletions tests/test_exgine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
# under the terms of the Revised BSD License; see COPYING.txt file for
# more details.

from __future__ import print_function

import unittest
import six
import sys
import os

Expand Down Expand Up @@ -52,7 +55,7 @@ def after_processing(objects, self):

# create a new wfe (but make sure we don't call serialization again)
wfe2 = pickle.dumps(self)
assert isinstance(wfe2, basestring)
assert isinstance(wfe2, six.string_types)
wfe2 = pickle.loads(wfe2)
wfe2.after_processing = lambda objs, eng: []

Expand All @@ -65,9 +68,9 @@ def after_processing(objects, self):
assert s1 == s2
assert orig_objs == objects
else:
print 'WFE executed threads, results may be different'
print 'original result:', objects
print 're-executed res:', orig_objs
print('WFE executed threads, results may be different')
print('original result:', objects)
print('re-executed res:', orig_objs)


def suite():
Expand Down
2 changes: 1 addition & 1 deletion workflow/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def load(self, cfgfile, force_reload=False, failonerror=True,
for k, v in replace_keys.items():
if k in config:
config[k] = v
except ConfigObjError, msg:
except ConfigObjError as msg:
if failonerror:
raise ConfigObjError(msg)
else:
Expand Down
13 changes: 6 additions & 7 deletions workflow/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

# we are not using the newseman logging to make this library independent
import logging
import new
import copy
import pickle
import sys
Expand Down Expand Up @@ -347,7 +346,7 @@ def processing_factory(objects, self):
"Processing was stopped: '%s' (object: %s)" % (
str(callbacks), repr(obj)))
break
except JumpTokenBack, step:
except JumpTokenBack as step:
if step.args[0] > 0:
raise WorkflowError(
"JumpTokenBack cannot be positive number")
Expand All @@ -356,7 +355,7 @@ def processing_factory(objects, self):
'Warning, we go back [%s] objects' % step.args[0])
i[0] = max(-1, i[0] - 1 + step.args[0])
i[1] = [0] # reset the callbacks pointer
except JumpTokenForward, step:
except JumpTokenForward as step:
if step.args[0] < 0:
raise WorkflowError(
"JumpTokenForward cannot be negative number")
Expand Down Expand Up @@ -428,15 +427,15 @@ def run_callbacks(self, callbacks, objects, obj, indent=0):
if DEBUG:
self.log.debug('Break from this loop')
return
except JumpCallBack, step:
except JumpCallBack as step:
if DEBUG:
self.log.debug(
'Warning, we go [%s] calls back' % step.args[0])
if step.args[0] > 0:
raise WorkflowError(
"JumpCallBack cannot be positive number")
y[indent] = max(-1, y[indent] + step.args[0] - 1)
except JumpCallForward, step:
except JumpCallForward as step:
if DEBUG:
self.log.debug('We skip [%s] calls' % step.args[0])
if step.args[0] < 0:
Expand Down Expand Up @@ -476,7 +475,7 @@ def getCallbacks(self, key='*'):
if key:
try:
return self._callbacks[key]
except KeyError, e:
except KeyError as e:
raise WorkflowMissingKey(
'No workflow is registered for the key: %s. Perhaps you '
'forgot to load workflows or the workflow definition for '
Expand All @@ -493,7 +492,7 @@ def addCallback(self, key, func, before=None, after=None,
except WorkflowMissingKey:
self._callbacks[key] = []
return self._callbacks[key].append(func)
except Exception, e:
except Exception as e:
self.log.debug(
'Impossible to add callback %s for key: %s' % (str(func), key))
self.log.debug(e)
Expand Down
9 changes: 2 additions & 7 deletions workflow/patterns/controlflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
# more details.

import threading
import thread
import Queue
import time
import copy

from six.moves import _thread as thread, queue

MAX_TIMEOUT = 30000

Expand Down Expand Up @@ -210,7 +209,6 @@ def PARALLEL_SPLIT(*args):
@postcondition: eng object will contain lock (to be used
by threads)
"""

def _parallel_split(obj, eng, calls):
lock = thread.allocate_lock()
i = 0
Expand Down Expand Up @@ -332,10 +330,7 @@ def SIMPLE_MERGE(*args):
# ------------------------------------------------------------- #


class MyTimeoutQueue(Queue.Queue):

def __init__(self, *args):
Queue.Queue.__init__(self, *args)
class MyTimeoutQueue(queue.Queue):

def join_with_timeout(self, timeout):
self.all_tasks_done.acquire()
Expand Down
17 changes: 10 additions & 7 deletions workflow/patterns/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
# under the terms of the Revised BSD License; see COPYING.txt file for
# more details.

from __future__ import print_function

import inspect
import traceback
import six
import sys
import pstats
import timeit
Expand Down Expand Up @@ -172,7 +175,7 @@ def OBJ_GET(something, cond='all'):
"""
def x(obj, eng):
if isinstance(something, basestring):
if isinstance(something, six.string_types):
return something in obj and obj[something]
else:
if cond.lower() == 'any':
Expand Down Expand Up @@ -246,7 +249,7 @@ def x(obj, eng):
try:
onecall(obj, eng)
break # success
except WorkflowTransition, msg:
except WorkflowTransition:
raise # just let it propagate
except:
if verbose:
Expand Down Expand Up @@ -343,8 +346,8 @@ def DEBUG_CYCLE(stmt, setup=None,
...
>>> def engtask(config, something):
... def x(obj, eng):
... print config
... print something
... print(config)
... print(something)
... return x
...
>>> config = {'some': 'value'}
Expand Down Expand Up @@ -423,8 +426,8 @@ def timeit(self):
t.obj = obj
t.eng = eng

# print t.src
print 'Execution time: %.3s' % (t.timeit())
# print(t.src)
print('Execution time: %.3s' % (t.timeit()))
except:
traceback.print_exc()
lasterr = traceback.format_exc().splitlines()
Expand Down Expand Up @@ -487,7 +490,7 @@ def x(obj, eng):
raise Exception(
"%s is not inside obj nor eng, try specifying "
"Okey or Ekey" % key)
except Exception, msg:
except Exception as msg:
eng.log.error(traceback.format_exc())
eng.log.error(
'Check your "oeargs" configuration. '
Expand Down

0 comments on commit 5fb2148

Please sign in to comment.