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