In order to move away from the existing flows having their own implementation of running, start moving the existing flows to be patterns that only structure tasks (and impose constraints about how the group of tasks can run) in useful ways. Let the concept of running those patterns be handled by an engine instead of being handled by the flow itself. This will allow for varying engines to be able to run flows in whichever way the engine chooses (as long as the constraints set up by the flow are observed). Currently threaded flow and graph flow are broken by this commit, since they have not been converted to being a structure of tasks + constraints. The existing engine has not yet been modified to run those structures either, work is underway to remediate this. Part of: blueprint patterns-and-engines Followup bugs that must be addressed: Bug: 1221448 Bug: 1221505 Change-Id: I3a8b96179f336d1defe269728ebae0caa3d832d7
77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
# Copyright (C) 2012-2013 Yahoo! Inc. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import inspect
|
|
import types
|
|
|
|
|
|
def get_callable_name(function):
|
|
"""Generate a name from callable
|
|
|
|
Tries to do the best to guess fully qualified callable name.
|
|
"""
|
|
im_class = getattr(function, 'im_class', None)
|
|
if im_class is not None:
|
|
if im_class is type:
|
|
# this is bound class method
|
|
im_class = function.im_self
|
|
parts = (im_class.__module__, im_class.__name__,
|
|
function.__name__)
|
|
elif isinstance(function, types.FunctionType):
|
|
parts = (function.__module__, function.__name__)
|
|
else:
|
|
im_class = type(function)
|
|
if im_class is type:
|
|
im_class = function
|
|
parts = (im_class.__module__, im_class.__name__)
|
|
return '.'.join(parts)
|
|
|
|
|
|
def is_bound_method(method):
|
|
return getattr(method, 'im_self', None) is not None
|
|
|
|
|
|
def _get_arg_spec(function):
|
|
if isinstance(function, type):
|
|
bound = True
|
|
function = function.__init__
|
|
elif isinstance(function, (types.FunctionType, types.MethodType)):
|
|
bound = is_bound_method(function)
|
|
function = getattr(function, '__wrapped__', function)
|
|
else:
|
|
function = function.__call__
|
|
bound = is_bound_method(function)
|
|
return inspect.getargspec(function), bound
|
|
|
|
|
|
def get_required_callable_args(function):
|
|
"""Get names of argument required by callable"""
|
|
argspec, bound = _get_arg_spec(function)
|
|
f_args = argspec.args
|
|
if argspec.defaults:
|
|
f_args = f_args[:-len(argspec.defaults)]
|
|
if bound:
|
|
f_args = f_args[1:]
|
|
return f_args
|
|
|
|
|
|
def accepts_kwargs(function):
|
|
"""Returns True if function accepts kwargs"""
|
|
argspec, _bound = _get_arg_spec(function)
|
|
return bool(argspec.keywords)
|