# Coverage for utils/viral.py : 63%

## 31 statements 21 run 10 missing 0 excluded 0 partial

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 -*-

2 from __future__ import annotations

3

4# -- stdlib --

5 from typing import Sequence, Type

6 import sys

7

8# -- third party --

9# -- own --

10

11

12# -- code --

13 class ViralContext(object):

14 VIRAL_SEARCH = ['self']

15 CONTINUE = object()

16

17 _viral_mro_cache: Sequence[Type[ViralContext]] = []

18

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

25

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)

29

30 return self

31

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

40

41 f = f.f_back

42

43 return None

44

45 def viral_import(self, env):

46 raise Exception('Override this!')

47

48 def viral_export(self):

49 raise Exception('Override this!')