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