Skip to content

Commit

Permalink
global: pep8 and pep257 improvements
Browse files Browse the repository at this point in the history
* Improves code style. (closes #6)

Signed-off-by: Jiri Kuncar <[email protected]>
  • Loading branch information
jirikuncar committed Aug 25, 2014
1 parent d0f1758 commit 6768530
Show file tree
Hide file tree
Showing 13 changed files with 1,101 additions and 885 deletions.
85 changes: 52 additions & 33 deletions bin/run_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@

log = main_engine.get_logger('workflow.run-worklfow')


def run(selection,
listwf=None,
places=None,
verbose=False,
profile=None,
**kwargs):
'''
"""
Example usage: %prog -l
%prog 1 [to select first workflow to run]
usage: %prog glob_pattern(s) [options]
-l, --listwf: list available workflows
-i, --places = places: list of glob patterns to search for workflows (separate with commas!)
-i, --places = places: list of glob patterns to search for workflows
(separate with commas!)
-p, --profile=profile: profile the workflow and save output as x
-v, --verbose: makes for a lot of output
'''
"""

workflows = set()

Expand All @@ -59,7 +61,9 @@ def run(selection,
for i in range(len(workflows)):
print "%d - %s" % (i, short_names[i])
if not len(workflows):
log.warning('No workflows found using default search path: \n%s' % '\n'.join(places))
log.warning(
'No workflows found using default search path: \n%s' % (
'\n'.join(places)))

if workflows:
for s in selection:
Expand All @@ -76,7 +80,9 @@ def run(selection,
if len(ids) == 0:
raise Exception("I found no wf for this id: %s" % (s, ))
elif len(ids) > 1:
raise Exception("There is more than one wf for this id: %s (%s)" % (s, ids))
raise Exception(
"There is more than one wf for this id: %s (%s)" % (
s, ids))
else:
if verbose:
run_workflow(workflows[ids[0]],
Expand All @@ -95,15 +101,16 @@ def find_workflow(workflows, name):
i += 1
return candidates


def run_workflow(file_or_module,
data=None,
engine=None,
processing_factory = None,
callback_chooser = None,
before_processing = None,
after_processing = None,
profile = None):
"""Runs the workflow
processing_factory=None,
callback_chooser=None,
before_processing=None,
after_processing=None,
profile=None):
"""Run the workflow
@var file_or_module: you can pass string (filepath) to the
workflow module, the module will be loaded as an anonymous
module (from the file) and <module>.workflow will be
Expand Down Expand Up @@ -140,7 +147,6 @@ def run_workflow(file_or_module,
else:
workflow = file_or_module


if workflow:
if profile:
workflow_def = PROFILE(workflow.workflow, profile)
Expand All @@ -164,7 +170,7 @@ def run_workflow(file_or_module,
before_processing,
after_processing)
datae.process(data)
if data[0]: # get prepared data
if data[0]: # get prepared data
data = data[0]

log.info('Running the workflow')
Expand All @@ -173,12 +179,13 @@ def run_workflow(file_or_module,
else:
raise Exception('No workfow found in: %s' % file_or_module)


def create_workflow_engine(workflow,
engine=None,
processing_factory = None,
callback_chooser = None,
before_processing = None,
after_processing = None):
processing_factory=None,
callback_chooser=None,
before_processing=None,
after_processing=None):
"""Instantiate engine and set the workflow and callbacks
directly
@var workflow: normal workflow tasks definition
Expand All @@ -192,10 +199,12 @@ def create_workflow_engine(workflow,
"""
if engine is None:
engine = main_engine.GenericWorkflowEngine
wf = engine(processing_factory, callback_chooser, before_processing, after_processing)
wf = engine(processing_factory, callback_chooser,
before_processing, after_processing)
wf.setWorkflow(workflow)
return wf


def get_workflow(file):
""" Initializes module into a separate object (not included in sys) """
name = 'XXX'
Expand All @@ -217,8 +226,8 @@ def get_workflow(file):
# old_cwd = os.getcwd()

try:
#filedir, filename = os.path.split(file)
#os.chdir(filedir)
# filedir, filename = os.path.split(file)
# os.chdir(filedir)
execfile(file, x.__dict__)
except Exception, excp:
sys.stderr.write(traceback.format_exc())
Expand All @@ -228,6 +237,7 @@ def get_workflow(file):

return x


def import_workflow(workflow):
"""Import workflow module
@var workflow: string as python import, eg: merkur.workflow.load_x"""
Expand All @@ -238,23 +248,27 @@ def import_workflow(workflow):
return mod





class TalkativeWorkflowEngine(main_engine.GenericWorkflowEngine):
counter = 0

def __init__(self, *args, **kwargs):
main_engine.GenericWorkflowEngine.__init__(self, *args, **kwargs)
self.log = main_engine.get_logger('TalkativeWFE<%d>' % TalkativeWorkflowEngine.counter)
self.log = main_engine.get_logger(
'TalkativeWFE<%d>' % TalkativeWorkflowEngine.counter)
TalkativeWorkflowEngine.counter += 1

def execute_callback(self, callback, obj):
obj_rep = []
max_len = 60

def val_format(v):
return '<%s ...>' % repr(v)[:max_len]

def func_format(c):
return '<%s ...%s:%s>' % (c.func_name, c.func_code.co_filename[-max_len:], c.func_code.co_firstlineno)
return '<%s ...%s:%s>' % (
c.func_name,
c.func_code.co_filename[-max_len:],
c.func_code.co_firstlineno)
if isinstance(obj, dict):
for k, v in obj.items():
obj_rep.append('%s:%s' % (k, val_format(v)))
Expand All @@ -268,12 +282,15 @@ def func_format(c):
self.log.debug('%s ( %s )' % (func_format(callback), obj_rep))
callback(obj, self)


class WorkflowModule(object):
"""This is used just as a replacement for when module is needed but workflow
was supplied directly"""

"""Workflow wrapper."""

def __init__(self, workflow):
self.workflow = workflow


def usage():
print """
usage: %(prog)s [options] <workflow name or pattern>
Expand All @@ -296,15 +313,17 @@ def usage():
the less messages are printed
-h, --help: this help message
""" % {'prog' : os.path.basename(__file__) }
""" % {'prog': os.path.basename(__file__)}


def main():

try:
opts, args = getopt.getopt(sys.argv[1:], "lp:o:ve:h", ['list', 'places=', 'profile=', 'verbose', 'Vlevel=', 'help'])
opts, args = getopt.getopt(sys.argv[1:], "lp:o:ve:h", [
'list', 'places=', 'profile=', 'verbose', 'Vlevel=', 'help'])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)

Expand Down Expand Up @@ -334,15 +353,15 @@ def main():
else:
assert False, "unhandled option %s" % o



if (not len(args) or not len(opts)) and 'listwf' not in kw_args:
usage()
sys.exit()

if 'places' not in kw_args:
d = os.path.dirname(os.path.abspath(__file__))
kw_args['places'] = ['%s/workflows/*.py' % d, '%s/workflows/*.pyw' % d, '%s/workflows/*.cfg' % d]
kw_args['places'] = ['%s/workflows/*.py' % d,
'%s/workflows/*.pyw' % d,
'%s/workflows/*.cfg' % d]

run(args, **kw_args)

Expand Down
32 changes: 16 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
).group('version')

setup(
name = 'workflow',
packages = ['workflow', 'workflow.patterns'],
name='workflow',
packages=['workflow', 'workflow.patterns'],
scripts=['bin/run_workflow.py'],
version=version,
description = 'Simple workflows for Python',
author = 'Roman Chyla',
url = 'https://github.com/romanchyla/workflow',
keywords = ['workflows', 'finite state machine', 'task execution'],
classifiers = [
description='Simple workflows for Python',
author='Roman Chyla',
url='https://github.com/romanchyla/workflow',
keywords=['workflows', 'finite state machine', 'task execution'],
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Development Status :: 5 - Production/Stable',
Expand All @@ -43,11 +43,11 @@
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Software Development :: Libraries :: Application Frameworks',
'Topic :: Utilities',
],
],
test_suite='nose.collector',
tests_require=['nose', 'cloud', 'coverage'],
install_requires = ['configobj>4.7.0'],
long_description = """\
install_requires=['configobj>4.7.0'],
long_description="""\
Simple workflows for Python
-------------------------------------
Expand All @@ -73,8 +73,8 @@
]
You can probably guess what the processing pipeline does with tokens - the
whole task is made of four steps and the whole configuration is just stored
as a Python list. Every task is implemeted as a function that takes two objects:
whole task is made of four steps and the whole configuration is just stored as
a Python list. Every task is implemeted as a function that takes two objects:
* currently processed object
* workflow engine instance
Expand All @@ -84,10 +84,10 @@
def next_token(obj, eng):
eng.ContinueNextToken()
There are NO explicit states, conditions, transitions - the job of the engine is
simply to run the tasks one after another. It is the responsibility of the task
to tell the engine what is going to happen next; whether to continue, stop,
jump back, jump forward and few other options.
There are NO explicit states, conditions, transitions - the job of the engine
is simply to run the tasks one after another. It is the responsibility of the
task to tell the engine what is going to happen next; whether to continue,
stop, jump back, jump forward and few other options.
This is actually a *feature*, I knew that there will be a lot of possible
exceptions and transition states to implement for NLP processing and I also
Expand Down
4 changes: 3 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

from workflow.config import config_reader


class TestConfig(unittest.TestCase):

"""Tests of the WE interface"""

def setUp(self):
Expand Down Expand Up @@ -45,4 +47,4 @@ def suite():

if __name__ == '__main__':
unittest.main()
#unittest.TextTestRunner(verbosity=2).run(suite())
# unittest.TextTestRunner(verbosity=2).run(suite())
Loading

0 comments on commit 6768530

Please sign in to comment.