Salome HOME
NRI : Merge from V1_2.
[modules/kernel.git] / src / SALOME_SWIG / salome_shared_modules.py
1 #  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3
4 #  This library is free software; you can redistribute it and/or 
5 #  modify it under the terms of the GNU Lesser General Public 
6 #  License as published by the Free Software Foundation; either 
7 #  version 2.1 of the License. 
8
9 #  This library is distributed in the hope that it will be useful, 
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 #  Lesser General Public License for more details. 
13
14 #  You should have received a copy of the GNU Lesser General Public 
15 #  License along with this library; if not, write to the Free Software 
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17
18 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 #
20 #
21 #
22 #  File   : salome_shared_modules.py
23 #  Module : SALOME
24
25 from SALOME_utilities import *
26
27 """
28 """
29 This module with help of import_hook and *_shared_modules
30 filters imports when using the embedded Python interpretor.
31
32 Some imports can't be done more than once.
33 This is related to the multi study feature that is implemented
34 by using the Python multi interpreter feature.
35 Some modules register objects or classes by calling modules
36 implemented in C. These operations can't be done multiple times.
37 So it's very important to control these imports.
38
39 Examples:
40   - PyQt : import qt calls a C module to register classes
41   - OmniORB : import *_idl calls a C module to register CORBA interfaces
42
43 Usage:
44   - First : the module salome_shared_modules is imported by main Python interpretor.
45     It will keep a dictionnary and a list that are shared among all
46     the subinterpretors and imports import_hook module that replaces
47     the standard import mechanism par one that prevents more than one import
48     for some modules identified by name (call register_name) or by a 
49     validator (call register_pattern).
50
51   Calls to register_name and register_pattern are done in modules named *_shared_modules
52   that could be found in the path SALOMEPATH
53
54 """
55 import glob,os,sys
56
57 import import_hook
58 # shared_imported, patterns, register_name, register_pattern
59 # will be shared by all Python sub interpretors
60 from import_hook import shared_imported
61 from import_hook import patterns
62 from import_hook import register_name
63 from import_hook import register_pattern
64
65 register_name("salome_shared_modules")
66
67 # Get the SALOMEPATH if set or else use KERNEL_ROOT_DIR that should be set.
68 salome_path=os.environ.get("SALOMEPATH",os.getenv("KERNEL_ROOT_DIR"))
69
70 list_modules=[]
71
72 # Import all *_shared_modules in the path and store them in list_modules
73 path=salome_path.split(":")
74 for rep in path:
75     # Import all *_shared_modules in rep
76     for f in glob.glob(os.path.join(rep,"lib","python"+sys.version[:3],"site-packages","salome","shared_modules","*_shared_modules.py")):
77         try:
78            m=__import__(os.path.splitext(os.path.basename(f))[0])
79            list_modules.append(m)
80         except:
81            pass
82
83
84 # If shared modules have been imported before installing import mechanism
85 # we add them to shared_imported
86 #
87 for name,module in sys.modules.items():
88     if import_hook.is_shared(name) and shared_imported.get(name) is None:
89        #print "Module shared added to shared_imported: ",name
90        shared_imported[name]=module
91