-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnested_context_mixin.py
66 lines (47 loc) · 1.49 KB
/
nested_context_mixin.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
#!/usr/bin/env python
# -*- coding: utf-8 vi:noet
# Mixin for having objects supporting nested context managers
# SPDX-FileCopyrightText: 2023 Jérôme Carretero <[email protected]>
# SPDX-License-Identifier: MIT
import logging
logger = logging.getLogger(__name__)
class NestedContextMixin:
def __init__(self):
self.__context_stack = []
def init(self):
raise NotImplementedError
return self
def exit(self, exc_type, exc_value, exc_traceback):
raise NotImplementedError
pass
def add_context_info(self, init, exit):
self.__context_stack.append((init, exit))
return self
def __enter__(self):
logger.debug("Enter %s", self.__context_stack)
if not self.__context_stack:
self.__context_stack.append((self.init, self.exit))
init, exit = self.__context_stack[-1]
return init()
def __exit__(self, exc_type, exc_value, exc_traceback):
logger.debug("Exit %s", self.__context_stack)
init, exit = self.__context_stack[-1]
self.__context_stack = self.__context_stack[:-1]
return exit(exc_type, exc_value, exc_traceback)
class Example(NestedContextMixin):
def __init__(self):
NestedContextMixin.__init__(self)
def init(self):
logger.info("Enter")
return self
def exit(self, exc_type, exc_value, exc_traceback):
logger.info("Exit")
pass
def like_this(self):
def init():
logger.info("Enter current measurement")
return self
def exit(exc_type, exc_value, exc_traceback):
logger.info("Exit current measurement")
pass
return self.add_context_info(init, exit)