GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
facade.py
Go to the documentation of this file.
1 
2 import sys
3 
4 
5 class Facade(object):
6 
7  def __init__(self, modulename):
8  """
9  Replaces a module with an instance of a types.ModuleType-based class
10  modulename is probably given by __name__ in that module
11 
12  Example:
13  @Facade(__name__)
14  class AnInteriorClass(ModuleType):
15  pass
16  """
17  self.modulename = modulename
18 
19  def __call__(self, Interior):
20  """
21  Replace current module with an instance of Interior in sys.modules
22  Update the globals of this instance from the globals of the module
23  Keep a reference to the overwritten "scaffold" module to avoid GC
24  cf. https://mail.python.org/pipermail/python-ideas/2012-May/014969.html
25  """
26  n = self.modulename
27  w = Interior(n)
28  w.__dict__.update(sys.modules[n].__dict__)
29  w._scaffold = sys.modules[n]
30  sys.modules[n] = w
31 
32 
33