Back to index

salome-kernel  6.5.0
salome_shared_modules.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 #  File   : salome_shared_modules.py
00025 #  Module : SALOME
00026 #
00027 """
00028 This module with help of import_hook and *_shared_modules
00029 filters imports when using the embedded Python interpretor.
00030 
00031 Some imports can't be done more than once.
00032 This is related to the multi study feature that is implemented
00033 by using the Python multi interpreter feature.
00034 Some modules register objects or classes by calling modules
00035 implemented in C. These operations can't be done multiple times.
00036 So it's very important to control these imports.
00037 
00038 Examples:
00039   - PyQt : import qt calls a C module to register classes
00040   - OmniORB : import *_idl calls a C module to register CORBA interfaces
00041 
00042 Usage:
00043   - First : the module salome_shared_modules is imported by main Python interpretor.
00044     It will keep a dictionnary and a list that are shared among all
00045     the subinterpretors and imports import_hook module that replaces
00046     the standard import mechanism par one that prevents more than one import
00047     for some modules identified by name (call register_name) or by a 
00048     validator (call register_pattern).
00049 
00050   Calls to register_name and register_pattern are done in modules named *_shared_modules
00051   that could be found in the path SALOMEPATH
00052 
00053 """
00054 import glob,os,sys
00055 
00056 import import_hook
00057 # shared_imported, patterns, register_name, register_pattern
00058 # will be shared by all Python sub interpretors
00059 from import_hook import patterns
00060 from import_hook import register_name
00061 from import_hook import register_pattern
00062 
00063 register_name("salome_shared_modules")
00064 register_name("omniORB")
00065 register_name("omnipatch")
00066 register_pattern(lambda(x):x.endswith("_idl"))
00067 register_pattern(lambda(x):x.startswith("omniORB."))
00068 
00069 from omnipatch import shared_imported
00070 shared_imported.update(import_hook.shared_imported)
00071 import_hook.shared_imported=shared_imported
00072 
00073 # Get the SALOMEPATH if set or else use KERNEL_ROOT_DIR that should be set.
00074 salome_path=os.environ.get("SALOMEPATH",os.getenv("KERNEL_ROOT_DIR"))
00075 
00076 list_modules=[]
00077 
00078 # Import all *_shared_modules in the path and store them in list_modules
00079 splitter = ":"
00080 if sys.platform == "win32":
00081   splitter = ";"
00082 path=salome_path.split(splitter)
00083 import platform
00084 libdir = "lib"
00085 for rep in path:
00086     # Import all *_shared_modules in rep
00087     for f in glob.glob(os.path.join(rep,libdir,"python"+sys.version[:3],"site-packages","salome","shared_modules","*_shared_modules.py")):
00088         try:
00089            name=os.path.splitext(os.path.basename(f))[0]
00090            register_name(name)
00091            #print name + " REGISTERED"
00092            m=__import__(name)
00093            #print name + " IMPORTED"
00094            list_modules.append(m)
00095         except:
00096            print "Exception during register and import shared module"
00097            pass
00098 
00099 # 
00100 # If shared modules have been imported before installing import mechanism
00101 # we add them to shared_imported
00102 #
00103 for name,module in sys.modules.items():
00104     if module and import_hook.is_shared(name) and not shared_imported.has_key(name):
00105        #print "Module shared added to shared_imported: ",name
00106        shared_imported[name]=module
00107