mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-27 18:04:12 +08:00
88a53fbdd1
Moves _nop_context to context_tricks under the name nop_context. Uses an explicit object for the actual context manager in callback manager.
74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
@object.__new__
|
|
class nop_context(object):
|
|
"""A nop context manager.
|
|
"""
|
|
def __enter__(self):
|
|
pass
|
|
|
|
def __exit__(self, *excinfo):
|
|
pass
|
|
|
|
|
|
def _nop(*args, **kwargs):
|
|
pass
|
|
|
|
|
|
class CallbackManager(object):
|
|
"""Create a context manager from a pre-execution callback and a
|
|
post-execution callback.
|
|
|
|
Parameters
|
|
----------
|
|
pre : (...) -> any, optional
|
|
A pre-execution callback. This will be passed ``*args`` and
|
|
``**kwargs``.
|
|
post : (...) -> any, optional
|
|
A post-execution callback. This will be passed ``*args`` and
|
|
``**kwargs``.
|
|
|
|
Notes
|
|
-----
|
|
The enter value of this context manager will be the result of calling
|
|
``pre(*args, **kwargs)``
|
|
|
|
Examples
|
|
--------
|
|
>>> def pre(where):
|
|
... print('entering %s block' % where)
|
|
>>> def post(where):
|
|
... print('exiting %s block' % where)
|
|
>>> manager = CallbackManager(pre, post)
|
|
>>> with manager('example'):
|
|
... print('inside example block')
|
|
entering example block
|
|
inside example
|
|
exiting example block
|
|
|
|
These are reusable with different args:
|
|
>>> with manager('another'):
|
|
... print('inside another block')
|
|
entering another block
|
|
inside another block
|
|
exiting another block
|
|
"""
|
|
def __init__(self, pre=None, post=None):
|
|
self.pre = pre if pre is not None else _nop
|
|
self.post = post if post is not None else _nop
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
return _ManagedCallbackContext(self.pre, self.post, args, kwargs)
|
|
|
|
|
|
class _ManagedCallbackContext(object):
|
|
def __init__(self, pre, post, args, kwargs):
|
|
self._pre = pre
|
|
self._post = post
|
|
self._args = args
|
|
self._kwargs = kwargs
|
|
|
|
def __enter__(self):
|
|
return self._pre(*self._args, **self._kwargs)
|
|
|
|
def __exit__(self, *excinfo):
|
|
self._post(*self._args, **self._kwargs)
|