Coverage for utils/viral.py : 63%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2from __future__ import annotations
4# -- stdlib --
5from typing import Sequence, Type
6import sys
8# -- third party --
9# -- own --
12# -- code --
13class ViralContext(object):
14 VIRAL_SEARCH = ['self']
15 CONTINUE = object()
17 _viral_mro_cache: Sequence[Type[ViralContext]] = []
19 def __new__(cls: Type[ViralContext], *a, **k):
20 self = object.__new__(cls)
21 cache = cls._viral_mro_cache if '_viral_mro_cache' in cls.__dict__ else None
22 if not cache:
23 cache = [c for c in cls.mro() if ViralContext in c.__bases__]
24 cls._viral_mro_cache = cache
26 for c in cache:
27 that = c.viral_search(start=2) if cls.VIRAL_SEARCH else None
28 c.viral_import(self, that.viral_export() if that else None)
30 return self
32 @classmethod
33 def viral_search(cls, start=1):
34 f = sys._getframe(start) or None
35 while f:
36 for name in cls.VIRAL_SEARCH:
37 that = f.f_locals.get(name)
38 if that is not None and isinstance(that, cls):
39 return that
41 f = f.f_back
43 return None
45 def viral_import(self, env):
46 raise Exception('Override this!')
48 def viral_export(self):
49 raise Exception('Override this!')