-
Notifications
You must be signed in to change notification settings - Fork 0
/
metawrap.py
563 lines (408 loc) · 17.7 KB
/
metawrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# -*- coding: utf-8 -*-
"""
The module ``metawrap`` provides support decorating functions and classes.
===============================================================================
Overview
===============================================================================
The module ``metawrap`` extends wrapping abilities found in |functools|_. In
particular, it is ensured all wrapped functions contain an attribute
``__wrapped__``, which points back to the original function before the wrapper
was applied. Also, the ability to wrap classes with a decorator to apply a
``metaclass`` or series of ``metaclass``es is provided. Making it much easier
to transform classes without mucking in their internals.
.. |functools| replace:: ``functools``
.. _functools: http://docs.python.org/2/library/functools.html
===============================================================================
API
===============================================================================
"""
__author__ = "John Kirkham <kirkhamj@janelia.hhmi.org>"
__date__ = "$Jul 23, 2014 16:24:36 EDT$"
import collections
import inspect
import functools
import types
def update_wrapper(wrapper,
wrapped,
assigned=functools.WRAPPER_ASSIGNMENTS,
updated=functools.WRAPPER_UPDATES):
"""
Extends functools.update_wrapper to ensure that it stores the wrapped
function in the attribute __wrapped__.
Args:
wrapper(callable): the replacement callable.
wrapped(callable): the callable that is being wrapped.
assigned(tuple): is a tuple naming the attributes assigned
directly from the wrapped function to the
wrapper function (defaults to
functools.WRAPPER_ASSIGNMENTS)
updated(tuple): is a tuple naming the attributes of the
wrapper that are updated with the
corresponding attribute from the wrapped
function (defaults to
functools.WRAPPER_UPDATES)
Returns:
(callable): the wrapped callable.
"""
wrapper = functools.update_wrapper(
wrapper, wrapped, assigned=assigned, updated=updated
)
# Store the underlying callable. Automatic in Python 3.
setattr(wrapper, "__wrapped__", getattr(wrapper, "__wrapped__", wrapped))
return(wrapper)
def wraps(wrapped,
assigned=functools.WRAPPER_ASSIGNMENTS,
updated=functools.WRAPPER_UPDATES):
"""
Builds on functools.wraps to ensure that it stores the wrapped function
in the attribute __wrapped__.
Args:
wrapped(callable): the callable that is being wrapped.
assigned(tuple): is a tuple naming the attributes assigned
directly from the wrapped function to the
wrapper function (defaults to
functools.WRAPPER_ASSIGNMENTS)
updated(tuple): is a tuple naming the attributes of the
wrapper that are updated with the
corresponding attribute from the wrapped
function (defaults to
functools.WRAPPER_UPDATES)
Returns:
(callable): a decorator for callable, which will
contain wrapped.
"""
return(functools.partial(
update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated
))
def identity_wrapper(a_callable):
"""
Trivially wraps a given callable without doing anything else to it.
Args:
a_callable(callable): the callable that is being wrapped.
Returns:
(callable): a wrapped callable.
"""
@wraps(a_callable)
def wrapped_callable(*args, **kwargs):
"""
Trivially wraps a given callable without doing anything else to it.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
Same as what `a_callable` returns.
"""
return(a_callable(*args, **kwargs))
return(wrapped_callable)
def static_variables(**kwargs):
"""
Returns a decorator that decorates a callable such that it has the
given static variables set.
Args:
*kwargs(tuple): keyword args will be set to the value provided.
Returns:
(decorator): a decorator for the callable.
"""
def static_variables_tie(a_callable):
"""
Decorates a function such that it has the given static variables
set.
Args:
a_callable(callable): the callable to decorate.
Returns:
(callable): the callable returned.
"""
callable_wrapped = identity_wrapper(a_callable)
for each_kwd, each_val in kwargs.items():
setattr(callable_wrapped, each_kwd, each_val)
return(callable_wrapped)
return(static_variables_tie)
def metaclass(meta):
"""
Returns a decorator that decorates a class such that the given
metaclass is applied.
Note:
Decorator will add the __metaclass__ attribute so the last
metaclass applied is known. Also, decorator will add the
__wrapped__ attribute so that the unwrapped class can be retrieved.
Args:
meta(metaclass): metaclass to apply to a given class.
Returns:
(decorator): a decorator for the class.
"""
def metaclass_wrapper(cls):
"""
Returns a decorated class such that the given metaclass is applied.
Note:
Adds the __metaclass__ attribute so the last metaclass used is
known. Also, adds the __wrapped__ attribute so that the
unwrapped class can be retrieved.
Args:
cls(class): class to decorate.
Returns:
(class): the decorated class.
"""
__name = str(cls.__name__)
__bases = tuple(cls.__bases__)
__dict = dict(cls.__dict__)
__dict.pop("__dict__", None)
__dict.pop("__weakref__", None)
for each_slot in __dict.get("__slots__", tuple()):
__dict.pop(each_slot, None)
__dict["__metaclass__"] = meta
__dict["__wrapped__"] = cls
return(meta(__name, __bases, __dict))
return(metaclass_wrapper)
def metaclasses(*metas):
"""
Returns a decorator that decorates a class such that the given
metaclasses are applied.
Note:
Shorthand for repeated application of metaclass.
Args:
*metas(metaclasses): metaclasses to apply to a given class.
Returns:
(decorator): a decorator for the class.
"""
def metaclasses_wrapper(cls):
"""
Returns a decorated class such that the given metaclasses are
applied.
Args:
cls(class): class to decorate.
Returns:
(class): the decorated class.
"""
new_cls = cls
for each_meta in metas:
new_cls = metaclass(each_meta)(new_cls)
return(new_cls)
return(metaclasses_wrapper)
def class_static_variables(**kwargs):
"""
Returns a decorator that decorates a class such that it has the given
static variables set.
Args:
**kwargs(tuple): keyword args will be set to the value
provided.
Returns:
(decorator): a decorator for the class.
"""
class MetaStaticVariables(type):
"""
Metaclass, which adds static variable with the given value to a
class.
"""
def __new__(meta, name, bases, dct):
dct.update(kwargs)
return(super(MetaStaticVariables, meta).__new__(
meta, name, bases, dct
))
return(metaclass(MetaStaticVariables))
def class_decorate_all_methods(*decorators):
"""
Returns a decorator that decorates a class such that all its methods
are decorated by the decorators provided.
Args:
*decorators(tuple): decorators to decorate all methods with.
Returns:
(decorator): a decorator for the class.
"""
class MetaAllMethodsDecorator(type):
"""
Metaclass, which decorates all methods with the list of decorators
in order.
"""
def __new__(meta, name, bases, dct):
for _k, _v in dct.items():
# Are all of FunctionType at this point.
# Will be of MethodType at a later step.
if isinstance(_v, types.FunctionType):
for each_decorator in decorators:
_v = each_decorator(_v)
dct[_k] = _v
return(super(MetaAllMethodsDecorator, meta).__new__(
meta, name, bases, dct
))
return(metaclass(MetaAllMethodsDecorator))
def class_decorate_methods(**method_decorators):
"""
Returns a decorator that decorates a class such that specified methods
are decorated by the decorators provided.
Args:
**method_decorators(tuple): method names with a single
decorator or a list of decorators.
Returns:
(decorator): a decorator for the class.
"""
class MetaMethodsDecorator(type):
"""
Metaclass, which decorates some methods based on the keys given.
Uses the decorator(s) provided for each method to decorator in
order.
"""
def __new__(meta, name, bases, dct):
for _k, _v in dct.items():
if isinstance(_v, types.FunctionType):
_dl = method_decorators.get(_k)
if (_dl is not None):
try:
iter(_dl)
except TypeError:
_dl = [_dl]
for _d in _dl:
_v = _d(_v)
dct[_k] = _v
return(super(MetaMethodsDecorator, meta).__new__(
meta, name, bases, dct
))
return(metaclass(MetaMethodsDecorator))
def unwrap(a_callable):
"""
Returns the underlying function that was wrapped.
Args:
a_callable(callable): some wrapped (or not) callable.
Returns:
(callable): the callable that is no longer wrapped.
"""
unwrapped_callable = a_callable
while hasattr(unwrapped_callable, "__wrapped__"):
unwrapped_callable = unwrapped_callable.__wrapped__
return(unwrapped_callable)
def tied_call_args(a_callable, *args, **kwargs):
"""
Ties all the args to their respective variable names.
Args:
a_callable(callable): some callable.
*args(callable): positional arguments for the callable.
**kwargs(callable): keyword arguments for the callable.
Returns:
args (tuple): ordered dictionary of arguments name and
their values, all variadic position
arguments, all variadic keyword
arguments.
"""
sig = inspect.getargspec(a_callable)
unsorted_callargs = inspect.getcallargs(a_callable, *args, **kwargs)
new_args = tuple()
if (sig.varargs is not None):
new_args = unsorted_callargs[sig.varargs]
new_kwargs = dict()
if (sig.keywords is not None):
new_kwargs = unsorted_callargs[sig.keywords]
callargs = collections.OrderedDict()
for each_arg in sig.args:
callargs[each_arg] = unsorted_callargs[each_arg]
return(callargs, new_args, new_kwargs)
def repack_call_args(a_callable, *args, **kwargs):
"""
Reorganizes args and kwargs to match the given callables signature.
Args:
a_callable(callable): some callable.
*args(callable): positional arguments for the callable.
**kwargs(callable): keyword arguments for the callable.
Returns:
args (tuple): all arguments as passed as position
arguments, all default arguments and
all arguments passed as keyword
arguments.
"""
callargs, new_args, new_kwargs = tied_call_args(
a_callable, *args, **kwargs
)
new_args = tuple(list(callargs.values())[:len(args)]) + new_args
new_kwargs.update(dict(list(callargs.items())[len(args):]))
return(new_args, new_kwargs)
def with_setup_state(setup=None, teardown=None):
"""
Adds setup and teardown callable to a function s.t. they can mutate it.
Based on ``with_setup`` from ``nose``. This goes a bit further than
``nose`` does and provides a mechanism for the setup and teardown
functions to change the callable in question. In other words, variables
generated in setup can be stored in the functions globals and then
cleaned up and removed in teardown. The final result of using this
function should be a function equivalent to one generated by
``with_setup``.
Args:
setup(callable): A callable that takes the decorated
function as an argument. This sets up the
function before execution.
teardown(callable): A callable that takes the decorated
function as an argument. This cleans up the
function after execution.
Returns:
callable: Does the actual decoration.
"""
def with_setup_state_wrapper(a_callable, setup=setup, teardown=teardown):
"""
Mutates the callable s.t. it has globals for setup and teardown.
Args:
a_callable(callable): A callable to run ``setup`` and
``teardown`` on.
setup(callable): A callable that takes the decorated
function as an argument. This sets up
the function before execution. Simply
forwarded from before.
teardown(callable): A callable that takes the decorated
function as an argument. This cleans up
the function after execution. Simply
forwarded from before.
Returns:
callable: The original callable with setup and
teardown globals.
"""
stage_dict = collections.OrderedDict([
("setup", setup),
("teardown", teardown)
])
stage_orderer = [(lambda a, b: (a, b)), (lambda a, b: (b, a))]
stage_itr = zip(reversed(list(stage_dict.items())), stage_orderer)
for (each_stage_name, each_new_stage), each_stage_orderer in stage_itr:
each_old_stage = getattr(a_callable, each_stage_name, None)
if each_new_stage:
each_new_stage = functools.partial(each_new_stage, a_callable)
chained_stages = None
if each_old_stage and each_new_stage:
first_stage, second_stage = each_stage_orderer(
each_old_stage, each_new_stage
)
def stages(first_stage=first_stage,
second_stage=second_stage):
first_stage()
second_stage()
chained_stages = stages
elif each_old_stage:
chained_stages = each_old_stage
elif each_new_stage:
def a_stage(each_new_stage=each_new_stage):
each_new_stage()
chained_stages = a_stage
else:
continue
setattr(a_callable, each_stage_name, chained_stages)
return(a_callable)
return(with_setup_state_wrapper)
def with_setup_state_handler(a_callable):
"""
A final wrapper for ``with_setup_state``.
This calls ``setup`` and ``teardown`` before and after if
defined. When used as a decorator, this should come after
all ``setup`` and ``teardown`` calls.
Args:
a_callable(callable): A callable to run ``setup`` and
``teardown`` on.
Returns:
callable: The wrapped function.
"""
@wraps(a_callable)
def wrapped(*args, **kwargs):
a_callable_setup = getattr(a_callable, "setup", None)
a_callable_teardown = getattr(a_callable, "teardown", None)
if a_callable_setup is not None:
a_callable_setup()
try:
return(a_callable())
finally:
if a_callable_teardown is not None:
a_callable_teardown()
return(wrapped)