# Coverage for utils/viral.py : 61%

## 29 statements 19 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

3# -- stdlib --

4 import sys

5

6# -- third party --

7# -- own --

8

9

10# -- code --

11 class ViralContext(object):

12 VIRAL_SEARCH = ['self']

13 CONTINUE = object()

14

15 _viral_mro_cache = None

16

17 def __new__(cls, *a, **k):

18 self = object.__new__(cls)

19 cache = cls._viral_mro_cache if '_viral_mro_cache' in cls.__dict__ else None

20 if not cache:

21 cache = [c for c in cls.mro() if ViralContext in c.__bases__]

22 cls._viral_mro_cache = cache

23

24 for c in cache:

25 that = c.viral_search(start=2) if cls.VIRAL_SEARCH else None

26 c.viral_import(self, that.viral_export() if that else None)

27

28 return self

29

30 @classmethod

31 def viral_search(cls, start=1):

32 f = sys._getframe(start)

33 while f:

34 for name in cls.VIRAL_SEARCH:

35 that = f.f_locals.get(name)

36 if that is not None and isinstance(that, cls):

37 return that

38

39 f = f.f_back

40

41 return None

42

43 def viral_import(self, env):

44 raise Exception('Override this!')

45

46 def viral_export(self):

47 raise Exception('Override this!')