1 #! /usr/bin/env python3
\r
2 # -*- coding: iso-8859-1 -*-
\r
3 # Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
\r
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
\r
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
\r
8 # This library is free software; you can redistribute it and/or
\r
9 # modify it under the terms of the GNU Lesser General Public
\r
10 # License as published by the Free Software Foundation; either
\r
11 # version 2.1 of the License, or (at your option) any later version.
\r
13 # This library is distributed in the hope that it will be useful,
\r
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
16 # Lesser General Public License for more details.
\r
18 # You should have received a copy of the GNU Lesser General Public
\r
19 # License along with this library; if not, write to the Free Software
\r
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
\r
25 # SALOME NamingService : wrapping NamingService services
\r
26 # File : SALOME_NamingServicePy.py
\r
27 # Author : Estelle Deville, CEA
\r
30 ## @package SALOME_NamingServicePy
\r
31 # \brief Module to manage SALOME naming service from python
\r
35 from omniORB import CORBA
\r
38 from string import *
\r
40 from SALOME_utilities import *
\r
41 #=============================================================================
\r
43 class SALOME_NamingServicePy_i(object):
\r
45 A class to manage SALOME naming service from python code
\r
49 _current_context=None
\r
52 #-------------------------------------------------------------------------
\r
54 def __init__(self, orb=None, steps=240, spy=False):
\r
56 Standard Constructor, with ORB reference.
\r
58 Initializes the naming service root context
\r
60 #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
\r
62 orb=CORBA.ORB_init([''], CORBA.ORB_ID)
\r
64 # initialize root context and current context
\r
66 while steps > 0 and ok == 0:
\r
68 obj =self._orb.resolve_initial_references("NameService")
\r
69 self._root_context =obj._narrow(CosNaming.NamingContext)
\r
70 self._current_context = self._root_context
\r
73 if self._root_context is None :
\r
74 #MESSAGE ( "Name Service Reference is invalid" )
\r
76 MESSAGE(" Name service not found")
\r
79 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
\r
80 MESSAGE(" Name service not found")
\r
83 if steps == 0 and self._root_context is None:
\r
84 MESSAGE ( "Name Service Reference is invalid" )
\r
86 raise ValueError("Name Service Reference is invalid")
\r
90 #-------------------------------------------------------------------------
\r
92 def Register(self,ObjRef, Path):
\r
93 """ ns.Register(object,pathname )
\r
95 register a CORBA object under a pathname
\r
98 MESSAGE ( "SALOME_NamingServicePy_i::Register" )
\r
100 path_list = list(Path)
\r
101 if path_list[0]=='/':
\r
102 self._current_context = self._root_context
\r
103 #delete first '/' before split
\r
106 result_resolve_path = Path.split('/')
\r
107 if len(result_resolve_path)>1:
\r
108 # A directory is treated (not only an object name)
\r
109 # We had to test if the directory where ObjRef should be recorded
\r
111 # If not, the new context has to be created
\r
113 for i in range(len(result_resolve_path)-1):
\r
114 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
\r
117 obj = self._current_context.resolve(_context_name)
\r
118 self._current_context = obj._narrow(CosNaming.NamingContext)
\r
119 except CosNaming.NamingContext.NotFound as ex:
\r
121 except CosNaming.NamingContext.InvalidName as ex:
\r
122 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
\r
123 except CosNaming.NamingContext.CannotProceed as ex:
\r
124 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
\r
125 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
\r
126 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
\r
129 # at least one context of the complete path is not created, we had
\r
130 # to create it or them
\r
132 for i in range(len(result_resolve_path)-1):
\r
133 _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
\r
136 obj = self._current_context.resolve(_context_name)
\r
137 self._current_context = obj._narrow(CosNaming.NamingContext)
\r
138 except CosNaming.NamingContext.NotFound as ex:
\r
139 #This context is not created. It will be done
\r
140 self._current_context = self._current_context.bind_new_context(_context_name)
\r
142 #The current directory is now the directory where the object should
\r
145 _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
\r
147 self._current_context.bind(_context_name,ObjRef)
\r
148 except CosNaming.NamingContext.NotFound as ex:
\r
149 MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
\r
150 except CosNaming.NamingContext.InvalidName as ex:
\r
151 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
\r
152 except CosNaming.NamingContext.CannotProceed as ex:
\r
153 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
\r
154 except CosNaming.NamingContext.AlreadyBound as ex:
\r
155 MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
\r
156 self._current_context.rebind(_context_name,ObjRef)
\r
157 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
\r
158 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
\r
160 #-------------------------------------------------------------------------
\r
162 def Resolve(self, Path):
\r
163 """ ns.Resolve(pathname) -> object
\r
165 find a CORBA object (ior) by its pathname
\r
167 #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
\r
168 path_list = list(Path)
\r
169 if path_list[0]=='/':
\r
170 self._current_context = self._root_context
\r
171 #delete first '/' before split
\r
174 result_resolve_path = Path.split('/')
\r
176 for i in range(len(result_resolve_path)-1):
\r
177 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
\r
178 _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
\r
180 self._obj = self._current_context.resolve(_context_name)
\r
181 except CosNaming.NamingContext.NotFound as ex:
\r
182 MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
\r
184 except CosNaming.NamingContext.InvalidName as ex:
\r
185 MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
\r
187 except CosNaming.NamingContext.CannotProceed as ex:
\r
188 MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
\r
190 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
\r
191 MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
\r
195 #-------------------------------------------------------------------------
\r
197 def Resolve_Dir(self, Path):
\r
198 """ ns.Resolve_Dir(pathname) -> dir
\r
200 find a CORBA object (ior) by its pathname
\r
202 #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
\r
203 path_list = list(Path)
\r
204 if path_list[0]=='/':
\r
205 self._current_context = self._root_context
\r
206 #delete first '/' before split
\r
209 result_resolve_path = Path.split('/')
\r
211 for i in range(len(result_resolve_path)-1):
\r
212 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
\r
213 _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"dir"))
\r
214 print(_context_name)
\r
217 self._obj = self._current_context.resolve(_context_name)
\r
218 except CosNaming.NamingContext.NotFound as ex:
\r
219 MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
\r
221 except CosNaming.NamingContext.InvalidName as ex:
\r
222 MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
\r
224 except CosNaming.NamingContext.CannotProceed as ex:
\r
225 MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
\r
227 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
\r
228 MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
\r
233 #-------------------------------------------------------------------------
\r
235 def Create_Directory(self,ObjRef, Path):
\r
236 """ ns.Create_Directory(ObjRef, Path)
\r
238 create a sub directory
\r
240 MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
\r
242 path_list = list(Path)
\r
243 if path_list[0]=='/':
\r
244 self._current_context = self._root_context
\r
245 #delete first '/' before split
\r
248 result_resolve_path = Path.split('/')
\r
250 for i in range(len(result_resolve_path)):
\r
251 _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]
\r
253 obj = self._current_context.resolve(_context_name)
\r
254 self._current_context = obj._narrow(CosNaming.NamingContext)
\r
255 except CosNaming.NamingContext.NotFound as ex:
\r
256 self._current_context = self._current_context.bind_new_context(_context_name)
\r
257 except CosNaming.NamingContext.InvalidName as ex:
\r
258 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
\r
259 except CosNaming.NamingContext.CannotProceed as ex:
\r
260 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
\r
261 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
\r
262 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
\r
264 def Destroy_Name(self,Path):
\r
265 """ ns.Destroy_Name(Path)
\r
267 remove a name in naming service
\r
269 resolve_path=Path.split('/')
\r
270 if resolve_path[0] == '': del resolve_path[0]
\r
271 dir_path=resolve_path[:-1]
\r
274 context_name.append(CosNaming.NameComponent(e,"dir"))
\r
275 context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
\r
278 self._root_context.unbind(context_name)
\r
279 except CosNaming.NamingContext.NotFound as ex:
\r
281 except CORBA.Exception as ex:
\r
284 def Destroy_FullDirectory(self,Path):
\r
285 """ ns.Destroy_FullDirectory(Path)
\r
287 remove recursively a directory
\r
290 for e in Path.split('/'):
\r
291 if e == '':continue
\r
292 context_name.append(CosNaming.NameComponent(e,"dir"))
\r
295 context=self._root_context.resolve(context_name)
\r
296 except CosNaming.NamingContext.NotFound as ex:
\r
298 except CORBA.Exception as ex:
\r
301 bl,bi=context.list(0)
\r
305 for s in b.binding_name :
\r
306 if s.kind == "object":
\r
307 context.unbind([s])
\r
308 elif s.kind == "dir":
\r
309 context.unbind([s])
\r
313 self._root_context.unbind(context_name)
\r