Back to index

salome-kernel  6.5.0
Functions | Variables
KERNEL_PY.import_hook Namespace Reference

Functions

def register_name
def register_pattern
def is_shared
def get_shared_imported
def get_real_module
def set_shared_imported
def import_module
def ensure_fromlist
def import_hook
def reload_hook
def init_shared_modules

Variables

dictionary shared_imported = {}
list patterns = []
 original_import = __builtin__.__import__

Function Documentation

def KERNEL_PY.import_hook.ensure_fromlist (   m,
  fromlist,
  recursive = 0 
)
Return the real modules list to be imported

Definition at line 130 of file import_hook.py.

00130 
00131 def ensure_fromlist(m, fromlist, recursive=0):
00132     """ Return the real modules list to be imported
00133     """
00134     #print "ensure_fromlist",m, fromlist, recursive
00135     l=[]
00136     for sub in fromlist:
00137         if sub == "*":
00138             if not recursive:
00139                 try:
00140                     all = m.__all__
00141                 except AttributeError:
00142                     pass
00143                 else:
00144                     l.extend(ensure_fromlist(m, all, 1))
00145         else:
00146           #try to find if sub is an attribute (eventually dotted) of m
00147           components=sub.split('.')
00148           has_submod=True
00149           submod=m
00150           for comp in components:
00151             if hasattr(submod,comp):
00152               submod=getattr(submod, comp)
00153             else:
00154               has_submod=False
00155               break
00156 
00157           if has_submod:
00158             #the attribute has been found
00159             if type(submod) == type(sys):
00160                l.append(("%s.%s" % (m.__name__, sub),submod))
00161           else:
00162             subname="%s.%s" % (m.__name__, sub)
00163             submod = import_module(sub, subname, m)
00164             #if not found ignore it
00165             if submod:
00166               l.append((subname,submod))
00167     return l

Here is the call graph for this function:

Here is the caller graph for this function:

def KERNEL_PY.import_hook.get_real_module (   mod,
  name 
)
Return effective module on import
   Standard import returns module A on import A.B
   To get module A.B use get_real_module with name "A.B"

Definition at line 101 of file import_hook.py.

00101 
00102 def get_real_module(mod,name):
00103     """Return effective module on import
00104        Standard import returns module A on import A.B
00105        To get module A.B use get_real_module with name "A.B"
00106     """
00107     components = name.split('.')
00108     for comp in components[1:]:
00109         mod = getattr(mod, comp)
00110     return mod

Here is the caller graph for this function:

def KERNEL_PY.import_hook.get_shared_imported (   name,
  fromlist 
)
If the module is registered in shared_imported
    update the sys.modules dict
    Let the real import be done by original_import

Definition at line 86 of file import_hook.py.

00086 
00087 def get_shared_imported(name,fromlist):
00088     """ If the module is registered in shared_imported
00089         update the sys.modules dict
00090         Let the real import be done by original_import
00091     """
00092     module= shared_imported.get(name)
00093     if module is None :
00094        #module name is not shared or not already imported
00095        #let original_import do the job
00096        return None
00097 
00098     # module is already imported and shared. Put it in sys.modules and 
00099     # let original_import finish the job
00100     sys.modules[name]=module

Here is the caller graph for this function:

def KERNEL_PY.import_hook.import_hook (   name,
  globals = None,
  locals = None,
  fromlist = None,
  args,
  kwds 
)
Import replacement for sharing modules among multiple interpreters
    Mostly update sys.modules before doing real import

Definition at line 168 of file import_hook.py.

00168 
00169 def import_hook(name, globals=None, locals=None, fromlist=None, *args, **kwds):
00170     """ Import replacement for sharing modules among multiple interpreters
00171         Mostly update sys.modules before doing real import
00172     """
00173     #print "import_hook",name,fromlist
00174     m=get_shared_imported(name,fromlist)
00175 
00176     module= original_import(name, globals, locals, fromlist, *args, **kwds)
00177 
00178     if fromlist:
00179        #when fromlist is specified, module is the real module
00180        #fromlist is a list of possibly dotted name
00181        m=module
00182        for nam,mod in ensure_fromlist(m, fromlist):
00183            if is_shared(nam):
00184               set_shared_imported(nam,mod)
00185     else: 
00186        #when fromlist is not specified and name is a dotted name,
00187        # module is the root package not the real module
00188        #so we need to retrieve it
00189        # note: some modules like xml.dom do not play the rule
00190        # (import xml: no attribute dom, but import xml.dom OK)
00191        try:
00192            m=get_real_module(module,name)
00193        except AttributeError:
00194            m=None
00195 
00196     if type(m) == type(sys) and is_shared(m.__name__):
00197        set_shared_imported(m.__name__,m)
00198 
00199     return module
00200 
00201 original_reload=__builtin__.reload

Here is the call graph for this function:

def KERNEL_PY.import_hook.import_module (   partname,
  fqname,
  parent 
)
Try to import module fqname
    It's parent is module parent and has name partname

Definition at line 118 of file import_hook.py.

00118 
00119 def import_module(partname, fqname, parent):
00120     """ Try to import module fqname
00121         It's parent is module parent and has name partname
00122     """
00123     #print "import_module",partname, fqname, parent
00124     try:
00125        m = sys.modules[fqname]
00126     except KeyError:
00127        pass
00128     else:
00129        return m

Here is the caller graph for this function:

Definition at line 211 of file import_hook.py.

00211 
00212 def init_shared_modules(shared_module):
00213     global shared_imported, patterns
00214     shared_imported=shared_module.shared_imported
00215     patterns=       shared_module.patterns
00216     for k,v in shared_imported.items():
00217        if v is not None:sys.modules[k]=v
00218     shared_imported["salome_shared_modules"]=shared_module
00219     import salome_shared_modules
00220     for m in salome_shared_modules.list_modules:
00221         m.init_shared_modules()
Indicate if module name is a shared module
    among multiple interpreters (return value=1)

Definition at line 77 of file import_hook.py.

00077 
00078 def is_shared(name):
00079     """ Indicate if module name is a shared module
00080         among multiple interpreters (return value=1)
00081     """
00082     if shared_imported.has_key(name):return 1
00083     for pattern in patterns:
00084         if pattern(name) : return 1
00085     return 0

Here is the caller graph for this function:

Definition at line 70 of file import_hook.py.

00070 
00071 def register_name(name):
00072     if shared_imported.has_key(name):return
00073     shared_imported[name]=None

Definition at line 74 of file import_hook.py.

00074 
00075 def register_pattern(pattern):
00076     patterns.append(pattern)

Definition at line 202 of file import_hook.py.

00202 
00203 def reload_hook(module):
00204     if is_shared(module.__name__):
00205        return module
00206     return original_reload(module)
00207 
00208 __builtin__.__import__=import_hook
00209 # Reload is not replaced 
00210 #__builtin__.reload=reload_hook

Here is the call graph for this function:

def KERNEL_PY.import_hook.set_shared_imported (   name,
  module 
)
Register a shared module
    Name can be a dotted name : package

Definition at line 111 of file import_hook.py.

00111 
00112 def set_shared_imported(name,module):
00113     """ Register a shared module
00114         Name can be a dotted name : package
00115     """
00116     shared_imported[name]=module
00117     #print "Module %s shared registered" % name,module

Here is the caller graph for this function:


Variable Documentation

KERNEL_PY.import_hook.original_import = __builtin__.__import__

Definition at line 68 of file import_hook.py.

Definition at line 66 of file import_hook.py.

Definition at line 61 of file import_hook.py.