2 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 # SALOME NamingService : wrapping NamingService services
24 # File : SALOME_NamingServicePy.py
25 # Author : Estelle Deville, CEA
29 ## @package SALOME_NamingServicePy
30 # \brief Module to manage SALOME naming service from python
36 from omniORB import CORBA
41 from SALOME_utilities import *
42 #=============================================================================
44 class SALOME_NamingServicePy_i(object):
46 A class to manage SALOME naming service from python code
53 #-------------------------------------------------------------------------
55 def __init__(self, orb):
57 Standard Constructor, with ORB reference.
59 Initializes the naming service root context
61 #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
63 # initialize root context and current context
66 while steps > 0 and ok == 0:
68 obj =self._orb.resolve_initial_references("NameService")
69 self._root_context =obj._narrow(CosNaming.NamingContext)
70 self._current_context = self._root_context
73 if self._root_context is None :
74 #MESSAGE ( "Name Service Reference is invalid" )
76 MESSAGE(" Name service not found")
79 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
80 MESSAGE(" Name service not found")
83 if steps == 0 and self._root_context is None:
84 MESSAGE ( "Name Service Reference is invalid" )
87 #-------------------------------------------------------------------------
89 def Register(self,ObjRef, Path):
90 """ ns.Register(object,pathname )
92 register a CORBA object under a pathname
95 MESSAGE ( "SALOME_NamingServicePy_i::Register" )
97 path_list = list(Path)
99 self._current_context = self._root_context
100 #delete first '/' before split
103 result_resolve_path = string.split(Path,'/')
104 if len(result_resolve_path)>1:
105 # A directory is treated (not only an object name)
106 # We had to test if the directory where ObjRef should be recorded
108 # If not, the new context has to be created
110 for i in range(len(result_resolve_path)-1):
111 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
114 obj = self._current_context.resolve(_context_name)
115 self._current_context = obj._narrow(CosNaming.NamingContext)
116 except CosNaming.NamingContext.NotFound, ex:
118 except CosNaming.NamingContext.InvalidName, ex:
119 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
120 except CosNaming.NamingContext.CannotProceed, ex:
121 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
122 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
123 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
126 # at least one context of the complete path is not created, we had
127 # to create it or them
129 for i in range(len(result_resolve_path)-1):
130 _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
133 obj = self._current_context.resolve(_context_name)
134 self._current_context = obj._narrow(CosNaming.NamingContext)
135 except CosNaming.NamingContext.NotFound, ex:
136 #This context is not created. It will be done
137 self._current_context = self._current_context.bind_new_context(_context_name)
139 #The current directory is now the directory where the object should
142 _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
144 self._current_context.bind(_context_name,ObjRef)
145 except CosNaming.NamingContext.NotFound, ex:
146 MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
147 except CosNaming.NamingContext.InvalidName, ex:
148 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
149 except CosNaming.NamingContext.CannotProceed, ex:
150 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
151 except CosNaming.NamingContext.AlreadyBound, ex:
152 MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
153 self._current_context.rebind(_context_name,ObjRef)
154 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
155 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
157 #-------------------------------------------------------------------------
159 def Resolve(self, Path):
160 """ ns.Resolve(pathname) -> object
162 find a CORBA object (ior) by its pathname
164 #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
165 path_list = list(Path)
166 if path_list[0]=='/':
167 self._current_context = self._root_context
168 #delete first '/' before split
171 result_resolve_path = string.split(Path,'/')
173 for i in range(len(result_resolve_path)-1):
174 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
175 _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
177 self._obj = self._current_context.resolve(_context_name)
178 except CosNaming.NamingContext.NotFound, ex:
179 MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
181 except CosNaming.NamingContext.InvalidName, ex:
182 MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
184 except CosNaming.NamingContext.CannotProceed, ex:
185 MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
187 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
188 MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
193 #-------------------------------------------------------------------------
195 def Create_Directory(self,ObjRef, Path):
196 """ ns.Create_Directory(ObjRef, Path)
198 create a sub directory
200 MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
202 path_list = list(Path)
203 if path_list[0]=='/':
204 self._current_context = self._root_context
205 #delete first '/' before split
208 result_resolve_path = string.split(Path,'/')
210 for i in range(len(result_resolve_path)):
211 _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]
213 obj = self._current_context.resolve(_context_name)
214 self._current_context = obj._narrow(CosNaming.NamingContext)
215 except CosNaming.NamingContext.NotFound, ex:
216 self._current_context = self._current_context.bind_new_context(_context_name)
217 except CosNaming.NamingContext.InvalidName, ex:
218 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
219 except CosNaming.NamingContext.CannotProceed, ex:
220 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
221 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
222 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
224 def Destroy_Name(self,Path):
225 """ ns.Destroy_Name(Path)
227 remove a name in naming service
229 resolve_path=string.split(Path,'/')
230 if resolve_path[0] == '': del resolve_path[0]
231 dir_path=resolve_path[:-1]
234 context_name.append(CosNaming.NameComponent(e,"dir"))
235 context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
238 self._root_context.unbind(context_name)
239 except CosNaming.NamingContext.NotFound, ex:
241 except CORBA.Exception,ex:
244 def Destroy_FullDirectory(self,Path):
245 """ ns.Destroy_FullDirectory(Path)
247 remove recursively a directory
250 for e in string.split(Path,'/'):
252 context_name.append(CosNaming.NameComponent(e,"dir"))
255 context=self._root_context.resolve(context_name)
256 except CosNaming.NamingContext.NotFound, ex:
258 except CORBA.Exception,ex:
261 bl,bi=context.list(0)
265 for s in b.binding_name :
266 if s.kind == "object":
268 elif s.kind == "dir":
273 self._root_context.unbind(context_name)