Salome HOME
a5d9faa65a00480494f5332cd72186c6c117cbee
[modules/kernel.git] / src / NamingService / SALOME_NamingServicePy.py
1 #! /usr/bin/env python3
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 #  SALOME NamingService : wrapping NamingService services
26 #  File   : SALOME_NamingServicePy.py
27 #  Author : Estelle Deville, CEA
28 #  Module : SALOME
29 #  $Header$
30 ## @package SALOME_NamingServicePy
31 # \brief Module to manage SALOME naming service from python
32 #
33 import sys
34 import time
35 from omniORB import CORBA
36 import CosNaming
37 import string
38 from string import *
39
40 from SALOME_utilities import *
41 #=============================================================================
42
43 class SALOME_NamingServicePy_i:
44     """
45       A class to manage SALOME naming service from python code
46     """
47     _orb = None
48     _root_context=None
49     _current_context=None
50     _obj=None
51     
52     #-------------------------------------------------------------------------
53
54     def __init__(self, orb=None, steps=240, spy=False):
55         """
56         Standard Constructor, with ORB reference.
57  
58         Initializes the naming service root context
59         """
60         #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
61         if orb is None:
62           orb=CORBA.ORB_init([''], CORBA.ORB_ID)
63         self._orb = orb
64         # initialize root context and current context
65         ok = 0
66         while steps > 0 and ok == 0:
67           try:
68             obj =self._orb.resolve_initial_references("NameService")
69             self._root_context =obj._narrow(CosNaming.NamingContext)
70             self._current_context = self._root_context
71
72         
73             if self._root_context is None :
74               #MESSAGE ( "Name Service Reference is invalid" )
75               #sys.exit(1)
76               MESSAGE(" Name service not found")
77             else:
78               ok = 1
79           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
80             MESSAGE(" Name service not found")
81           time.sleep(0.25)
82           steps = steps - 1
83         if steps == 0 and self._root_context is None: 
84           MESSAGE ( "Name Service Reference is invalid" )
85           if spy:
86             raise ValueError("Name Service Reference is invalid")
87           else:
88             sys.exit(1)
89
90     #-------------------------------------------------------------------------
91
92     def Register(self,ObjRef, Path):
93         """ ns.Register(object,pathname )  
94         
95         register a CORBA object under a pathname
96         """
97
98         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
99         _not_exist = 0
100         path_list = list(Path)
101         if path_list[0]=='/':
102             self._current_context = self._root_context
103             #delete first '/' before split
104             Path=Path[1:]
105
106         result_resolve_path = Path.split('/')
107         if len(result_resolve_path)>1:
108             # A directory is treated (not only an object name)
109             # We had to test if the directory where ObjRef should be recorded 
110             # is already done
111             # If not, the new context has to be created
112             _context_name = []
113             for i in range(len(result_resolve_path)-1):
114                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
115             
116             try:
117                 obj = self._current_context.resolve(_context_name)
118                 self._current_context = obj._narrow(CosNaming.NamingContext)
119             except CosNaming.NamingContext.NotFound as ex:
120                 _not_exist = 1
121             except CosNaming.NamingContext.InvalidName as ex:
122                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
123             except CosNaming.NamingContext.CannotProceed as ex:
124                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
125             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
126                 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
127
128             if _not_exist:
129                 # at least one context of the complete path is not created, we had
130                 # to create it or them
131                 _context_name = []
132                 for i in range(len(result_resolve_path)-1):
133                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
134
135                     try:
136                         obj = self._current_context.resolve(_context_name)
137                         self._current_context = obj._narrow(CosNaming.NamingContext)
138                     except CosNaming.NamingContext.NotFound as ex:
139                         #This context is not created. It will be done
140                         self._current_context = self._current_context.bind_new_context(_context_name)
141
142         #The current directory is now the directory where the object should 
143         #be recorded
144          
145         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
146         try:
147             self._current_context.bind(_context_name,ObjRef)
148         except CosNaming.NamingContext.NotFound as ex:
149             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
150         except CosNaming.NamingContext.InvalidName as ex:
151             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
152         except CosNaming.NamingContext.CannotProceed as ex:
153             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
154         except CosNaming.NamingContext.AlreadyBound as ex:
155             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
156             self._current_context.rebind(_context_name,ObjRef)
157         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
158             MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
159
160     #-------------------------------------------------------------------------
161
162     def Resolve(self, Path):
163         """ ns.Resolve(pathname) -> object
164
165         find a CORBA object (ior) by its pathname
166         """
167         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
168         path_list = list(Path)
169         if path_list[0]=='/':
170             self._current_context = self._root_context
171             #delete first '/' before split
172             Path=Path[1:]
173
174         result_resolve_path = Path.split('/')
175         _context_name=[]
176         for i in range(len(result_resolve_path)-1):
177             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
178         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
179         try:
180             self._obj = self._current_context.resolve(_context_name)
181         except CosNaming.NamingContext.NotFound as ex:
182             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
183             self._obj = None
184         except CosNaming.NamingContext.InvalidName as ex:
185             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
186             self._obj = None
187         except CosNaming.NamingContext.CannotProceed as ex:
188             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
189             self._obj = None
190         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
191             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
192             self._obj = None
193         return self._obj
194
195     #-------------------------------------------------------------------------
196
197     def Resolve_Dir(self, Path):
198         """ ns.Resolve_Dir(pathname) -> dir
199
200         find a CORBA object (ior) by its pathname
201         """
202         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
203         path_list = list(Path)
204         if path_list[0]=='/':
205             self._current_context = self._root_context
206             #delete first '/' before split
207             Path=Path[1:]
208
209         result_resolve_path = Path.split('/')
210         _context_name=[]
211         for i in range(len(result_resolve_path)-1):
212             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
213         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"dir"))
214         print(_context_name)
215         return None
216         try:
217             self._obj = self._current_context.resolve(_context_name)
218         except CosNaming.NamingContext.NotFound as ex:
219             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
220             self._obj = None
221         except CosNaming.NamingContext.InvalidName as ex:
222             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
223             self._obj = None
224         except CosNaming.NamingContext.CannotProceed as ex:
225             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
226             self._obj = None
227         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
228             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
229             self._obj = None
230         return self._obj
231
232
233     #-------------------------------------------------------------------------
234
235     def Create_Directory(self,ObjRef, Path):
236         """ ns.Create_Directory(ObjRef, Path) 
237
238         create a sub directory 
239         """
240         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
241         _not_exist = 0
242         path_list = list(Path)
243         if path_list[0]=='/':
244             self._current_context = self._root_context
245             #delete first '/' before split
246             Path=Path[1:]
247
248         result_resolve_path = Path.split('/')
249         _context_name = []
250         for i in range(len(result_resolve_path)):
251             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
252             try:
253                 obj = self._current_context.resolve(_context_name)
254                 self._current_context = obj._narrow(CosNaming.NamingContext)
255             except CosNaming.NamingContext.NotFound as ex:
256                 self._current_context = self._current_context.bind_new_context(_context_name)
257             except CosNaming.NamingContext.InvalidName as ex:
258                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
259             except CosNaming.NamingContext.CannotProceed as ex:
260                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
261             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
262                 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
263  
264     def Destroy_Name(self,Path):
265       """ ns.Destroy_Name(Path) 
266
267         remove a name in naming service
268       """
269       resolve_path=Path.split('/')
270       if resolve_path[0] == '': del resolve_path[0]
271       dir_path=resolve_path[:-1]
272       context_name=[]
273       for e in dir_path:
274          context_name.append(CosNaming.NameComponent(e,"dir"))
275       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
276       
277       try:
278         self._root_context.unbind(context_name)
279       except CosNaming.NamingContext.NotFound as ex:
280         return
281       except CORBA.Exception as ex:
282         return
283
284     def Destroy_FullDirectory(self,Path):
285       """ ns.Destroy_FullDirectory(Path)
286
287         remove recursively a directory
288       """
289       context_name=[]
290       for e in Path.split('/'):
291         if e == '':continue
292         context_name.append(CosNaming.NameComponent(e,"dir"))
293
294       try:
295         context=self._root_context.resolve(context_name)
296       except CosNaming.NamingContext.NotFound as ex:
297         return
298       except CORBA.Exception as ex:
299         return
300
301       bl,bi=context.list(0)
302       if bi is not None:
303          ok,b=bi.next_one()
304          while(ok):
305             for s in b.binding_name :
306                if s.kind == "object":
307                   context.unbind([s])
308                elif s.kind == "dir":
309                   context.unbind([s])
310             ok,b=bi.next_one()
311
312       context.destroy()
313       self._root_context.unbind(context_name)