# author: askfiy

import contextvars


class GlobalContext:
    def __init__(self):
        super().__setattr__('_contextvar', contextvars.ContextVar(f"{__class__.__name__}_g"))

    def __getattr__(self, k: str):
        ctx = self._contextvar.get()
        return ctx[k]

    def __setattr__(self, k: str, v):
        try:
            ctx = self._contextvar.get()
        except LookupError:
            ctx = {}

        ctx.update({k: v})
        self._contextvar.set(ctx)

    def get_context(self):
        return self._contextvar.get()

    def update_context(self, ctx):
        self._contextvar.set(ctx)


g = GlobalContext()


