1
# -*- coding: utf-8 -*-
3
# vim: tabstop=4 shiftwidth=4 softtabstop=4
5
# Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
24
# These arguments are ones that we will skip when parsing for requirements
25
# for a function to operate (when used as a task).
26
AUTO_ARGS = ('self', 'context', 'cls')
29
def is_decorated(functor):
30
if not isinstance(functor, (types.MethodType, types.FunctionType)):
32
return getattr(extract(functor), '__task__', False)
36
# Extract the underlying functor if its a method since we can not set
37
# attributes on instance methods, this is supposedly fixed in python 3
40
# TODO(harlowja): add link to this fix.
41
assert isinstance(functor, (types.MethodType, types.FunctionType))
42
if isinstance(functor, types.MethodType):
43
return functor.__func__
48
def _mark_as_task(functor):
49
setattr(functor, '__task__', True)
52
def _get_wrapped(function):
53
"""Get the method at the bottom of a stack of decorators."""
55
if hasattr(function, '__wrapped__'):
56
return getattr(function, '__wrapped__')
58
if not hasattr(function, 'func_closure') or not function.func_closure:
61
def _get_wrapped_function(function):
62
if not hasattr(function, 'func_closure') or not function.func_closure:
65
for closure in function.func_closure:
66
func = closure.cell_contents
68
deeper_func = _get_wrapped_function(func)
71
elif hasattr(closure.cell_contents, '__call__'):
72
return closure.cell_contents
74
return _get_wrapped_function(function)
80
# In certain decorator cases it seems like we get the function to be
81
# decorated as an argument, we don't want to take that as a real argument.
82
if isinstance(a, collections.Callable):
88
"""This will not be needed in python 3.2 or greater which already has this
89
built-in to its functools.wraps method.
93
f = functools.wraps(fn)(f)
94
f.__wrapped__ = getattr(fn, '__wrapped__', fn)
103
def wrapper(self, *args, **kwargs):
105
return f(self, *args, **kwargs)
110
def task(*args, **kwargs):
111
"""Decorates a given function and ensures that all needed attributes of
112
that function are set so that the function can be used as a task.
118
def noop(*args, **kwargs):
121
# Mark as being a task
124
# By default don't revert this.
125
w_f.revert = kwargs.pop('revert_with', noop)
127
# Associate a name of this task that is the module + function name.
128
w_f.name = "%s.%s" % (f.__module__, f.__name__)
130
# Sets the version of the task.
131
version = kwargs.pop('version', (1, 0))
132
f = _versionize(*version)(f)
134
# Attach any requirements this function needs for running.
135
requires_what = kwargs.pop('requires', [])
136
f = _requires(*requires_what, **kwargs)(f)
138
# Attach any optional requirements this function needs for running.
139
optional_what = kwargs.pop('optional', [])
140
f = _optional(*optional_what, **kwargs)(f)
142
# Attach any items this function provides as output
143
provides_what = kwargs.pop('provides', [])
144
f = _provides(*provides_what, **kwargs)(f)
147
def wrapper(*args, **kwargs):
148
return f(*args, **kwargs)
152
# This is needed to handle when the decorator has args or the decorator
153
# doesn't have args, python is rather weird here...
154
if kwargs or not args:
157
if isinstance(args[0], collections.Callable):
158
return decorator(args[0])
163
def _versionize(major, minor=None):
164
"""A decorator that marks the wrapped function with a major & minor version
173
w_f.version = (major, minor)
176
def wrapper(*args, **kwargs):
177
return f(*args, **kwargs)
184
def _optional(*args, **kwargs):
185
"""Attaches a set of items that the decorated function would like as input
186
to the functions underlying dictionary.
192
if not hasattr(w_f, 'optional'):
195
w_f.optional.update([a for a in args if _take_arg(a)])
198
def wrapper(*args, **kwargs):
199
return f(*args, **kwargs)
203
# This is needed to handle when the decorator has args or the decorator
204
# doesn't have args, python is rather weird here...
205
if kwargs or not args:
208
if isinstance(args[0], collections.Callable):
209
return decorator(args[0])
214
def _requires(*args, **kwargs):
215
"""Attaches a set of items that the decorated function requires as input
216
to the functions underlying dictionary.
222
if not hasattr(w_f, 'requires'):
225
if kwargs.pop('auto_extract', True):
226
inspect_what = _get_wrapped(f)
227
f_args = inspect.getargspec(inspect_what).args
228
w_f.requires.update([a for a in f_args if _take_arg(a)])
230
w_f.requires.update([a for a in args if _take_arg(a)])
233
def wrapper(*args, **kwargs):
234
return f(*args, **kwargs)
238
# This is needed to handle when the decorator has args or the decorator
239
# doesn't have args, python is rather weird here...
240
if kwargs or not args:
243
if isinstance(args[0], collections.Callable):
244
return decorator(args[0])
249
def _provides(*args, **kwargs):
250
"""Attaches a set of items that the decorated function provides as output
251
to the functions underlying dictionary.
257
if not hasattr(f, 'provides'):
260
w_f.provides.update([a for a in args if _take_arg(a)])
263
def wrapper(*args, **kwargs):
264
return f(*args, **kwargs)
268
# This is needed to handle when the decorator has args or the decorator
269
# doesn't have args, python is rather weird here...
270
if kwargs or not args:
273
if isinstance(args[0], collections.Callable):
274
return decorator(args[0])