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