Coverage for utils/viral.py : 61%
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 -*-
3# -- stdlib --
4import sys
6# -- third party --
7# -- own --
10# -- code --
11class ViralContext(object):
12 VIRAL_SEARCH = ['self']
13 CONTINUE = object()
15 _viral_mro_cache = None
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
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)
28 return self
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
39 f = f.f_back
41 return None
43 def viral_import(self, env):
44 raise Exception('Override this!')
46 def viral_export(self):
47 raise Exception('Override this!')