]> SALOME platform Git repositories - modules/kernel.git/blob - src/NamingService/SALOME_NamingServicePy.py
Salome HOME
bos #20365 Problem with killSalomeWithPort --spy on Windows
[modules/kernel.git] / src / NamingService / SALOME_NamingServicePy.py
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
4 #\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
7 #\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
12 #\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
17 #\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
21 #\r
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com\r
23 #\r
24 \r
25 #  SALOME NamingService : wrapping NamingService services\r
26 #  File   : SALOME_NamingServicePy.py\r
27 #  Author : Estelle Deville, CEA\r
28 #  Module : SALOME\r
29 #  $Header$\r
30 ## @package SALOME_NamingServicePy\r
31 # \brief Module to manage SALOME naming service from python\r
32 #\r
33 import sys\r
34 import time\r
35 from omniORB import CORBA\r
36 import CosNaming\r
37 import string\r
38 from string import *\r
39 \r
40 from SALOME_utilities import *\r
41 #=============================================================================\r
42 \r
43 class SALOME_NamingServicePy_i(object):\r
44     """\r
45       A class to manage SALOME naming service from python code\r
46     """\r
47     _orb = None\r
48     _root_context=None\r
49     _current_context=None\r
50     _obj=None\r
51     \r
52     #-------------------------------------------------------------------------\r
53 \r
54     def __init__(self, orb=None, steps=240, spy=False):\r
55         """\r
56         Standard Constructor, with ORB reference.\r
57  \r
58         Initializes the naming service root context\r
59         """\r
60         #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )\r
61         if orb is None:\r
62           orb=CORBA.ORB_init([''], CORBA.ORB_ID)\r
63         self._orb = orb\r
64         # initialize root context and current context\r
65         ok = 0\r
66         while steps > 0 and ok == 0:\r
67           try:\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
71 \r
72         \r
73             if self._root_context is None :\r
74               #MESSAGE ( "Name Service Reference is invalid" )\r
75               #sys.exit(1)\r
76               MESSAGE(" Name service not found")\r
77             else:\r
78               ok = 1\r
79           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):\r
80             MESSAGE(" Name service not found")\r
81           time.sleep(0.25)\r
82           steps = steps - 1\r
83         if steps == 0 and self._root_context is None: \r
84           MESSAGE ( "Name Service Reference is invalid" )\r
85           if spy:\r
86             raise ValueError("Name Service Reference is invalid")\r
87           else:\r
88             sys.exit(1)\r
89 \r
90     #-------------------------------------------------------------------------\r
91 \r
92     def Register(self,ObjRef, Path):\r
93         """ ns.Register(object,pathname )  \r
94         \r
95         register a CORBA object under a pathname\r
96         """\r
97 \r
98         MESSAGE ( "SALOME_NamingServicePy_i::Register" )\r
99         _not_exist = 0\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
104             Path=Path[1:]\r
105 \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
110             # is already done\r
111             # If not, the new context has to be created\r
112             _context_name = []\r
113             for i in range(len(result_resolve_path)-1):\r
114                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))\r
115             \r
116             try:\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
120                 _not_exist = 1\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
127 \r
128             if _not_exist:\r
129                 # at least one context of the complete path is not created, we had\r
130                 # to create it or them\r
131                 _context_name = []\r
132                 for i in range(len(result_resolve_path)-1):\r
133                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]\r
134 \r
135                     try:\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
141 \r
142         #The current directory is now the directory where the object should \r
143         #be recorded\r
144          \r
145         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]\r
146         try:\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
159 \r
160     #-------------------------------------------------------------------------\r
161 \r
162     def Resolve(self, Path):\r
163         """ ns.Resolve(pathname) -> object\r
164 \r
165         find a CORBA object (ior) by its pathname\r
166         """\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
172             Path=Path[1:]\r
173 \r
174         result_resolve_path = Path.split('/')\r
175         _context_name=[]\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
179         try:\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
183             self._obj = None\r
184         except CosNaming.NamingContext.InvalidName as ex:\r
185             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )\r
186             self._obj = None\r
187         except CosNaming.NamingContext.CannotProceed as ex:\r
188             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )\r
189             self._obj = None\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
192             self._obj = None\r
193         return self._obj\r
194 \r
195     #-------------------------------------------------------------------------\r
196 \r
197     def Resolve_Dir(self, Path):\r
198         """ ns.Resolve_Dir(pathname) -> dir\r
199 \r
200         find a CORBA object (ior) by its pathname\r
201         """\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
207             Path=Path[1:]\r
208 \r
209         result_resolve_path = Path.split('/')\r
210         _context_name=[]\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
215         return None\r
216         try:\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
220             self._obj = None\r
221         except CosNaming.NamingContext.InvalidName as ex:\r
222             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )\r
223             self._obj = None\r
224         except CosNaming.NamingContext.CannotProceed as ex:\r
225             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )\r
226             self._obj = None\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
229             self._obj = None\r
230         return self._obj\r
231 \r
232 \r
233     #-------------------------------------------------------------------------\r
234 \r
235     def Create_Directory(self,ObjRef, Path):\r
236         """ ns.Create_Directory(ObjRef, Path) \r
237 \r
238         create a sub directory \r
239         """\r
240         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )\r
241         _not_exist = 0\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
246             Path=Path[1:]\r
247 \r
248         result_resolve_path = Path.split('/')\r
249         _context_name = []\r
250         for i in range(len(result_resolve_path)):\r
251             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            \r
252             try:\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
263  \r
264     def Destroy_Name(self,Path):\r
265       """ ns.Destroy_Name(Path) \r
266 \r
267         remove a name in naming service\r
268       """\r
269       resolve_path=Path.split('/')\r
270       if resolve_path[0] == '': del resolve_path[0]\r
271       dir_path=resolve_path[:-1]\r
272       context_name=[]\r
273       for e in dir_path:\r
274          context_name.append(CosNaming.NameComponent(e,"dir"))\r
275       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))\r
276       \r
277       try:\r
278         self._root_context.unbind(context_name)\r
279       except CosNaming.NamingContext.NotFound as ex:\r
280         return\r
281       except CORBA.Exception as ex:\r
282         return\r
283 \r
284     def Destroy_FullDirectory(self,Path):\r
285       """ ns.Destroy_FullDirectory(Path)\r
286 \r
287         remove recursively a directory\r
288       """\r
289       context_name=[]\r
290       for e in Path.split('/'):\r
291         if e == '':continue\r
292         context_name.append(CosNaming.NameComponent(e,"dir"))\r
293 \r
294       try:\r
295         context=self._root_context.resolve(context_name)\r
296       except CosNaming.NamingContext.NotFound as ex:\r
297         return\r
298       except CORBA.Exception as ex:\r
299         return\r
300 \r
301       bl,bi=context.list(0)\r
302       if bi is not None:\r
303          ok,b=bi.next_one()\r
304          while(ok):\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
310             ok,b=bi.next_one()\r
311 \r
312       context.destroy()\r
313       self._root_context.unbind(context_name)\r