Spaces:
Sleeping
Sleeping
Upload __init__.py
Browse files- aworld/__init__.py +51 -0
aworld/__init__.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from uuid import uuid4
|
2 |
+
from contextvars import ContextVar
|
3 |
+
from types import MappingProxyType
|
4 |
+
|
5 |
+
_BAGGAGE_KEY = "aworld.baggage." + str(uuid4())
|
6 |
+
|
7 |
+
_BAGGAGE_CONTEXT = ContextVar(_BAGGAGE_KEY, default=None)
|
8 |
+
|
9 |
+
|
10 |
+
class BaggageContext:
|
11 |
+
"""
|
12 |
+
Baggage context.
|
13 |
+
"""
|
14 |
+
|
15 |
+
@staticmethod
|
16 |
+
def get_baggage() -> dict:
|
17 |
+
"""
|
18 |
+
Get the baggage. This is a read-only view of the baggage.
|
19 |
+
Returns:
|
20 |
+
The baggage.
|
21 |
+
"""
|
22 |
+
baggage = _BAGGAGE_CONTEXT.get()
|
23 |
+
if isinstance(baggage, dict):
|
24 |
+
return MappingProxyType(baggage)
|
25 |
+
return {}
|
26 |
+
|
27 |
+
@staticmethod
|
28 |
+
def get_baggage_value(key: str):
|
29 |
+
"""
|
30 |
+
Get the value for a key from baggage.
|
31 |
+
Args:
|
32 |
+
key: The key of the value to retrieve.
|
33 |
+
Returns:
|
34 |
+
The baggage value.
|
35 |
+
"""
|
36 |
+
baggage = BaggageContext.get_baggage()
|
37 |
+
if key:
|
38 |
+
return baggage.get(key)
|
39 |
+
return None
|
40 |
+
|
41 |
+
@staticmethod
|
42 |
+
def set_baggage(key: str, value: object):
|
43 |
+
"""
|
44 |
+
Set the value for a key in baggage.
|
45 |
+
Args:
|
46 |
+
key: The key of the value to set.
|
47 |
+
value: The value to set.
|
48 |
+
"""
|
49 |
+
baggage = BaggageContext.get_baggage().copy()
|
50 |
+
baggage[key] = value
|
51 |
+
_BAGGAGE_CONTEXT.set(baggage)
|