Salome HOME
9b5b06db1dc9cba38c0f26208045d5cb016b5fd1
[modules/kernel.git] / src / NamingService / SALOME_NamingServicePy.py
1 #! /usr/bin/env python3
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2020  CEA/DEN, EDF R&D, 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(object):
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):
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         steps = 240
67         while steps > 0 and ok == 0:
68           try:
69             obj =self._orb.resolve_initial_references("NameService")
70             self._root_context =obj._narrow(CosNaming.NamingContext)
71             self._current_context = self._root_context
72
73         
74             if self._root_context is None :
75               #MESSAGE ( "Name Service Reference is invalid" )
76               #sys.exit(1)
77               MESSAGE(" Name service not found")
78             else:
79               ok = 1
80           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
81             MESSAGE(" Name service not found")
82           time.sleep(0.25)
83           steps = steps - 1
84         if steps == 0 and self._root_context is None: 
85           MESSAGE ( "Name Service Reference is invalid" )
86           sys.exit(1)
87
88     #-------------------------------------------------------------------------
89
90     def Register(self,ObjRef, Path):
91         """ ns.Register(object,pathname )  
92         
93         register a CORBA object under a pathname
94         """
95
96         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
97         _not_exist = 0
98         path_list = list(Path)
99         if path_list[0]=='/':
100             self._current_context = self._root_context
101             #delete first '/' before split
102             Path=Path[1:]
103
104         result_resolve_path = Path.split('/')
105         if len(result_resolve_path)>1:
106             # A directory is treated (not only an object name)
107             # We had to test if the directory where ObjRef should be recorded 
108             # is already done
109             # If not, the new context has to be created
110             _context_name = []
111             for i in range(len(result_resolve_path)-1):
112                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
113             
114             try:
115                 obj = self._current_context.resolve(_context_name)
116                 self._current_context = obj._narrow(CosNaming.NamingContext)
117             except CosNaming.NamingContext.NotFound as ex:
118                 _not_exist = 1
119             except CosNaming.NamingContext.InvalidName as ex:
120                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
121             except CosNaming.NamingContext.CannotProceed as ex:
122                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
123             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
124                 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
125
126             if _not_exist:
127                 # at least one context of the complete path is not created, we had
128                 # to create it or them
129                 _context_name = []
130                 for i in range(len(result_resolve_path)-1):
131                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
132
133                     try:
134                         obj = self._current_context.resolve(_context_name)
135                         self._current_context = obj._narrow(CosNaming.NamingContext)
136                     except CosNaming.NamingContext.NotFound as ex:
137                         #This context is not created. It will be done
138                         self._current_context = self._current_context.bind_new_context(_context_name)
139
140         #The current directory is now the directory where the object should 
141         #be recorded
142          
143         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
144         try:
145             self._current_context.bind(_context_name,ObjRef)
146         except CosNaming.NamingContext.NotFound as ex:
147             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
148         except CosNaming.NamingContext.InvalidName as ex:
149             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
150         except CosNaming.NamingContext.CannotProceed as ex:
151             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
152         except CosNaming.NamingContext.AlreadyBound as ex:
153             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
154             self._current_context.rebind(_context_name,ObjRef)
155         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
156             MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
157
158     #-------------------------------------------------------------------------
159
160     def Resolve(self, Path):
161         """ ns.Resolve(pathname) -> object
162
163         find a CORBA object (ior) by its pathname
164         """
165         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
166         path_list = list(Path)
167         if path_list[0]=='/':
168             self._current_context = self._root_context
169             #delete first '/' before split
170             Path=Path[1:]
171
172         result_resolve_path = Path.split('/')
173         _context_name=[]
174         for i in range(len(result_resolve_path)-1):
175             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
176         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
177         try:
178             self._obj = self._current_context.resolve(_context_name)
179         except CosNaming.NamingContext.NotFound as ex:
180             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
181             self._obj = None
182         except CosNaming.NamingContext.InvalidName as ex:
183             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
184             self._obj = None
185         except CosNaming.NamingContext.CannotProceed as ex:
186             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
187             self._obj = None
188         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
189             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
190             self._obj = None
191         return self._obj
192
193     #-------------------------------------------------------------------------
194
195     def Resolve_Dir(self, Path):
196         """ ns.Resolve_Dir(pathname) -> dir
197
198         find a CORBA object (ior) by its pathname
199         """
200         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
201         path_list = list(Path)
202         if path_list[0]=='/':
203             self._current_context = self._root_context
204             #delete first '/' before split
205             Path=Path[1:]
206
207         result_resolve_path = Path.split('/')
208         _context_name=[]
209         for i in range(len(result_resolve_path)-1):
210             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
211         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"dir"))
212         print(_context_name)
213         return None
214         try:
215             self._obj = self._current_context.resolve(_context_name)
216         except CosNaming.NamingContext.NotFound as ex:
217             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
218             self._obj = None
219         except CosNaming.NamingContext.InvalidName as ex:
220             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
221             self._obj = None
222         except CosNaming.NamingContext.CannotProceed as ex:
223             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
224             self._obj = None
225         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
226             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
227             self._obj = None
228         return self._obj
229
230
231     #-------------------------------------------------------------------------
232
233     def Create_Directory(self,ObjRef, Path):
234         """ ns.Create_Directory(ObjRef, Path) 
235
236         create a sub directory 
237         """
238         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
239         _not_exist = 0
240         path_list = list(Path)
241         if path_list[0]=='/':
242             self._current_context = self._root_context
243             #delete first '/' before split
244             Path=Path[1:]
245
246         result_resolve_path = Path.split('/')
247         _context_name = []
248         for i in range(len(result_resolve_path)):
249             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
250             try:
251                 obj = self._current_context.resolve(_context_name)
252                 self._current_context = obj._narrow(CosNaming.NamingContext)
253             except CosNaming.NamingContext.NotFound as ex:
254                 self._current_context = self._current_context.bind_new_context(_context_name)
255             except CosNaming.NamingContext.InvalidName as ex:
256                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
257             except CosNaming.NamingContext.CannotProceed as ex:
258                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
259             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
260                 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
261  
262     def Destroy_Name(self,Path):
263       """ ns.Destroy_Name(Path) 
264
265         remove a name in naming service
266       """
267       resolve_path=Path.split('/')
268       if resolve_path[0] == '': del resolve_path[0]
269       dir_path=resolve_path[:-1]
270       context_name=[]
271       for e in dir_path:
272          context_name.append(CosNaming.NameComponent(e,"dir"))
273       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
274       
275       try:
276         self._root_context.unbind(context_name)
277       except CosNaming.NamingContext.NotFound as ex:
278         return
279       except CORBA.Exception as ex:
280         return
281
282     def Destroy_FullDirectory(self,Path):
283       """ ns.Destroy_FullDirectory(Path)
284
285         remove recursively a directory
286       """
287       context_name=[]
288       for e in Path.split('/'):
289         if e == '':continue
290         context_name.append(CosNaming.NameComponent(e,"dir"))
291
292       try:
293         context=self._root_context.resolve(context_name)
294       except CosNaming.NamingContext.NotFound as ex:
295         return
296       except CORBA.Exception as ex:
297         return
298
299       bl,bi=context.list(0)
300       if bi is not None:
301          ok,b=bi.next_one()
302          while(ok):
303             for s in b.binding_name :
304                if s.kind == "object":
305                   context.unbind([s])
306                elif s.kind == "dir":
307                   context.unbind([s])
308             ok,b=bi.next_one()
309
310       context.destroy()
311       self._root_context.unbind(context_name)