| 2139 | |
| 2140 | |
| 2141 | class use_scope_: |
| 2142 | def __init__(self, name, before_enter=None): |
| 2143 | self.before_enter = before_enter |
| 2144 | self.name = name |
| 2145 | |
| 2146 | def __enter__(self): |
| 2147 | if self.before_enter: |
| 2148 | self.before_enter() |
| 2149 | get_current_session().push_scope(self.name) |
| 2150 | return self.name |
| 2151 | |
| 2152 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 2153 | """ |
| 2154 | If this method returns True, it means that the context manager can handle the exception, |
| 2155 | so that the with statement terminates the propagation of the exception |
| 2156 | """ |
| 2157 | scope = get_current_session().pop_scope() |
| 2158 | send_msg('output_ctl', dict(loose=scope2dom(scope))) # revoke lock the height of the scope |
| 2159 | return False # Propagate Exception |
| 2160 | |
| 2161 | def __call__(self, func): |
| 2162 | """decorator implement""" |
| 2163 | |
| 2164 | @wraps(func) |
| 2165 | def wrapper(*args, **kwargs): |
| 2166 | self.__enter__() |
| 2167 | try: |
| 2168 | return func(*args, **kwargs) |
| 2169 | finally: |
| 2170 | self.__exit__(None, None, None) |
| 2171 | |
| 2172 | @wraps(func) |
| 2173 | async def coro_wrapper(*args, **kwargs): |
| 2174 | self.__enter__() |
| 2175 | try: |
| 2176 | return await func(*args, **kwargs) |
| 2177 | finally: |
| 2178 | self.__exit__(None, None, None) |
| 2179 | |
| 2180 | if iscoroutinefunction(func): |
| 2181 | return coro_wrapper |
| 2182 | else: |
| 2183 | return wrapper |