Salome HOME
Base implementation of Notebook
[modules/kernel.git] / src / KERNEL_PY / omnipatch.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 """
24   This patch of omniORB is made to make it work with multiple interpreters
25   and to correct the problem of incomplete import of CORBA packages
26   in some situations common in SALOME
27
28   This patch add or modify functions in omniORB module.
29
30   In multiple interpreters context, omniORB module is meant to be shared among
31   all interpreters
32 """
33 import sys,string,imp
34 import omniORB
35 # Map of partially-opened modules
36 _partialModules = {}
37 # Map of modules to share
38 shared_imported={}
39
40 # Function to return a Python module for the required IDL module name
41 def openModule(mname, fname=None):
42     # Salome modification start
43     # Be sure to use the right module dictionnary
44     import sys
45     # Salome modification end
46
47     if mname == "CORBA":
48         mod = sys.modules["omniORB.CORBA"]
49         # Salome modification start
50         shared_imported[mname]=mod
51         # Salome modification end
52
53     elif sys.modules.has_key(mname):
54         mod = sys.modules[mname]
55
56         if _partialModules.has_key(mname):
57             pmod = _partialModules[mname]
58             mod.__dict__.update(pmod.__dict__)
59             del _partialModules[mname]
60         # Salome modification start
61         shared_imported[mname]=mod
62         # Salome modification end
63
64     elif _partialModules.has_key(mname):
65         mod = _partialModules[mname]
66
67     # Salome modification start
68     elif shared_imported.get(mname) :
69         mod = shared_imported[mname]
70     # Salome modification end
71
72     else:
73         mod = newModule(mname)
74
75
76     if not hasattr(mod, "__doc__") or mod.__doc__ is None:
77         mod.__doc__ = "omniORB IDL module " + mname + "\n\n" + \
78                       "Generated from:\n\n"
79
80     if fname is not None:
81         mod.__doc__ = mod.__doc__ + "  " + fname + "\n"
82
83     return mod
84
85 # Function to create a new module, and any parent modules which do not
86 # already exist
87 def newModule(mname):
88     # Salome modification start
89     # Be sure to use the right module dictionnary
90     import sys
91     # Salome modification end
92
93     mlist   = string.split(mname, ".")
94     current = ""
95     mod     = None
96
97     for name in mlist:
98         current = current + name
99
100         if sys.modules.has_key(current):
101             mod = sys.modules[current]
102
103         elif _partialModules.has_key(current):
104             mod = _partialModules[current]
105
106         else:
107             newmod = imp.new_module(current)
108             if mod: setattr(mod, name, newmod)
109             _partialModules[current] = mod = newmod
110
111         current = current + "."
112
113     return mod
114
115 # Function to update a module with the partial module store in the
116 # partial module map
117 def updateModule(mname):
118     # Salome modification start
119     # Be sure to use the right module dictionnary
120     import sys
121     # Salome modification end
122     if _partialModules.has_key(mname):
123         pmod = _partialModules[mname]
124         mod  = sys.modules[mname]
125         mod.__dict__.update(pmod.__dict__)
126         del _partialModules[mname]
127
128     # Salome modification start
129     shared_imported[mname]=sys.modules[mname]
130     # Salome modification end
131
132 omniORB.updateModule=updateModule
133 omniORB.newModule=newModule
134 omniORB.openModule=openModule
135