1
+ from __future__ import annotations
2
+
1
3
import contextlib
2
4
import contextvars
3
5
import threading
4
6
5
7
import wrapt
6
8
9
+ from maidr .core .plot .boxplot import BoxPlotContainer
10
+
7
11
8
12
class ContextManager :
9
13
_instance = None
@@ -18,6 +22,10 @@ def __new__(cls):
18
22
cls ._instance = super (ContextManager , cls ).__new__ ()
19
23
return cls ._instance
20
24
25
+ @classmethod
26
+ def is_internal_context (cls ):
27
+ return cls ._internal_context .get ()
28
+
21
29
@classmethod
22
30
@contextlib .contextmanager
23
31
def set_internal_context (cls ):
@@ -27,10 +35,6 @@ def set_internal_context(cls):
27
35
finally :
28
36
cls ._internal_context .reset (token_internal_context )
29
37
30
- @classmethod
31
- def is_internal_context (cls ):
32
- return cls ._internal_context .get ()
33
-
34
38
35
39
@wrapt .decorator
36
40
def manage_context (wrapped = None , _ = None , args = None , kwargs = None ):
@@ -41,3 +45,74 @@ def manage_context(wrapped=None, _=None, args=None, kwargs=None):
41
45
# Set the internal context to avoid cyclic processing.
42
46
with ContextManager .set_internal_context ():
43
47
return wrapped (* args , ** kwargs )
48
+
49
+
50
+ class BoxplotContextManager (ContextManager ):
51
+ _bxp_context = contextvars .ContextVar ("bxp_context" , default = BoxPlotContainer ())
52
+
53
+ @classmethod
54
+ @contextlib .contextmanager
55
+ def set_internal_context (cls ):
56
+ with super (BoxplotContextManager , cls ).set_internal_context ():
57
+ token = cls ._bxp_context .set (BoxPlotContainer ())
58
+ try :
59
+ yield cls .get_bxp_context ()
60
+ finally :
61
+ cls ._bxp_context .reset (token )
62
+
63
+ @classmethod
64
+ def get_bxp_context (cls ) -> BoxPlotContainer :
65
+ return cls ._bxp_context .get ()
66
+
67
+ @classmethod
68
+ def add_bxp_context (cls , bxp_context : dict ) -> None :
69
+ cls .get_bxp_context ().add_artists (bxp_context )
70
+
71
+ @classmethod
72
+ def set_bxp_orientation (cls , orientation : str ) -> None :
73
+ cls .get_bxp_context ().set_orientation (orientation )
74
+
75
+
76
+ class HighlightContextManager :
77
+ _instance = None
78
+ _lock = threading .Lock ()
79
+
80
+ _maidr_element = contextvars .ContextVar ("_maidr_element" , default = False )
81
+ _elements = contextvars .ContextVar ("elements" , default = [])
82
+
83
+ def __new__ (cls ):
84
+ if not cls ._instance :
85
+ with cls ._lock :
86
+ if not cls ._instance :
87
+ cls ._instance = super (HighlightContextManager , cls ).__new__ ()
88
+ return cls ._instance
89
+
90
+ @classmethod
91
+ def is_maidr_element (cls ):
92
+ return cls ._maidr_element .get ()
93
+
94
+ @classmethod
95
+ @contextlib .contextmanager
96
+ def set_maidr_element (cls , element ):
97
+ if element not in cls ._elements .get ():
98
+ yield
99
+ return
100
+
101
+ token_maidr_element = cls ._maidr_element .set (True )
102
+ try :
103
+ yield
104
+ finally :
105
+ cls ._maidr_element .reset (token_maidr_element )
106
+ # Remove element from the context list after tagging
107
+ new_elements = cls ._elements .get ().copy ()
108
+ new_elements .remove (element )
109
+ cls ._elements .set (new_elements )
110
+
111
+ @classmethod
112
+ @contextlib .contextmanager
113
+ def set_maidr_elements (cls , elements : list ):
114
+ token_paths = cls ._elements .set (elements )
115
+ try :
116
+ yield
117
+ finally :
118
+ cls ._elements .reset (token_paths )
0 commit comments