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=None):
57 Standard Constructor, with ORB reference.
59 Initializes the naming service root context
61 #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
63 orb=CORBA.ORB_init([''], CORBA.ORB_ID)
65 # initialize root context and current context
68 while steps > 0 and ok == 0:
70 obj =self._orb.resolve_initial_references("NameService")
71 self._root_context =obj._narrow(CosNaming.NamingContext)
72 self._current_context = self._root_context
75 if self._root_context is None :
76 #MESSAGE ( "Name Service Reference is invalid" )
78 MESSAGE(" Name service not found")
81 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
82 MESSAGE(" Name service not found")
85 if steps == 0 and self._root_context is None:
86 MESSAGE ( "Name Service Reference is invalid" )
89 #-------------------------------------------------------------------------
91 def Register(self,ObjRef, Path):
92 """ ns.Register(object,pathname )
94 register a CORBA object under a pathname
97 MESSAGE ( "SALOME_NamingServicePy_i::Register" )
99 path_list = list(Path)
100 if path_list[0]=='/':
101 self._current_context = self._root_context
102 #delete first '/' before split
105 result_resolve_path = string.split(Path,'/')
106 if len(result_resolve_path)>1:
107 # A directory is treated (not only an object name)
108 # We had to test if the directory where ObjRef should be recorded
110 # If not, the new context has to be created
112 for i in range(len(result_resolve_path)-1):
113 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
116 obj = self._current_context.resolve(_context_name)
117 self._current_context = obj._narrow(CosNaming.NamingContext)
118 except CosNaming.NamingContext.NotFound, ex:
120 except CosNaming.NamingContext.InvalidName, ex:
121 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
122 except CosNaming.NamingContext.CannotProceed, ex:
123 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
124 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
125 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
128 # at least one context of the complete path is not created, we had
129 # to create it or them
131 for i in range(len(result_resolve_path)-1):
132 _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
135 obj = self._current_context.resolve(_context_name)
136 self._current_context = obj._narrow(CosNaming.NamingContext)
137 except CosNaming.NamingContext.NotFound, ex:
138 #This context is not created. It will be done
139 self._current_context = self._current_context.bind_new_context(_context_name)
141 #The current directory is now the directory where the object should
144 _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
146 self._current_context.bind(_context_name,ObjRef)
147 except CosNaming.NamingContext.NotFound, ex:
148 MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
149 except CosNaming.NamingContext.InvalidName, ex:
150 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
151 except CosNaming.NamingContext.CannotProceed, ex:
152 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
153 except CosNaming.NamingContext.AlreadyBound, ex:
154 MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
155 self._current_context.rebind(_context_name,ObjRef)
156 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
157 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
159 #-------------------------------------------------------------------------
161 def Resolve(self, Path):
162 """ ns.Resolve(pathname) -> object
164 find a CORBA object (ior) by its pathname
166 #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
167 path_list = list(Path)
168 if path_list[0]=='/':
169 self._current_context = self._root_context
170 #delete first '/' before split
173 result_resolve_path = string.split(Path,'/')
175 for i in range(len(result_resolve_path)-1):
176 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
177 _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
179 self._obj = self._current_context.resolve(_context_name)
180 except CosNaming.NamingContext.NotFound, ex:
181 MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
183 except CosNaming.NamingContext.InvalidName, ex:
184 MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
186 except CosNaming.NamingContext.CannotProceed, ex:
187 MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
189 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
190 MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
195 #-------------------------------------------------------------------------
197 def Create_Directory(self,ObjRef, Path):
198 """ ns.Create_Directory(ObjRef, Path)
200 create a sub directory
202 MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
204 path_list = list(Path)
205 if path_list[0]=='/':
206 self._current_context = self._root_context
207 #delete first '/' before split
210 result_resolve_path = string.split(Path,'/')
212 for i in range(len(result_resolve_path)):
213 _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]
215 obj = self._current_context.resolve(_context_name)
216 self._current_context = obj._narrow(CosNaming.NamingContext)
217 except CosNaming.NamingContext.NotFound, ex:
218 self._current_context = self._current_context.bind_new_context(_context_name)
219 except CosNaming.NamingContext.InvalidName, ex:
220 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
221 except CosNaming.NamingContext.CannotProceed, ex:
222 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
223 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
224 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
226 def Destroy_Name(self,Path):
227 """ ns.Destroy_Name(Path)
229 remove a name in naming service
231 resolve_path=string.split(Path,'/')
232 if resolve_path[0] == '': del resolve_path[0]
233 dir_path=resolve_path[:-1]
236 context_name.append(CosNaming.NameComponent(e,"dir"))
237 context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
240 self._root_context.unbind(context_name)
241 except CosNaming.NamingContext.NotFound, ex:
243 except CORBA.Exception,ex:
246 def Destroy_FullDirectory(self,Path):
247 """ ns.Destroy_FullDirectory(Path)
249 remove recursively a directory
252 for e in string.split(Path,'/'):
254 context_name.append(CosNaming.NameComponent(e,"dir"))
257 context=self._root_context.resolve(context_name)
258 except CosNaming.NamingContext.NotFound, ex:
260 except CORBA.Exception,ex:
263 bl,bi=context.list(0)
267 for s in b.binding_name :
268 if s.kind == "object":
270 elif s.kind == "dir":
275 self._root_context.unbind(context_name)