]> SALOME platform Git repositories - modules/kernel.git/blob - src/NamingService/SALOME_NamingServicePy.py
Salome HOME
CCAR: move DSC trace function from Calcium directory to DSC_Basic directory
[modules/kernel.git] / src / NamingService / SALOME_NamingServicePy.py
1 #! /usr/bin/env python
2 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
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.
11 #
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.
16 #
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
20 #
21 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 #  SALOME NamingService : wrapping NamingService services
24 #  File   : SALOME_NamingServicePy.py
25 #  Author : Estelle Deville, CEA
26 #  Module : SALOME
27 #  $Header$
28 #
29 ## @package SALOME_NamingServicePy
30 # \brief Module to manage SALOME naming service from python
31 #  
32
33
34 import sys
35 import time
36 from omniORB import CORBA
37 import CosNaming
38 import string
39 from string import *
40
41 from SALOME_utilities import *
42 #=============================================================================
43
44 class SALOME_NamingServicePy_i(object):
45     """
46       A class to manage SALOME naming service from python code
47     """
48     _orb = None
49     _root_context=None
50     _current_context=None
51     _obj=None
52     
53     #-------------------------------------------------------------------------
54
55     def __init__(self, orb=None):
56         """
57         Standard Constructor, with ORB reference.
58  
59         Initializes the naming service root context
60         """
61         #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
62         if orb is None:
63           orb=CORBA.ORB_init([''], CORBA.ORB_ID)
64         self._orb = orb
65         # initialize root context and current context
66         ok = 0
67         steps = 240
68         while steps > 0 and ok == 0:
69           try:
70             obj =self._orb.resolve_initial_references("NameService")
71             self._root_context =obj._narrow(CosNaming.NamingContext)
72             self._current_context = self._root_context
73
74         
75             if self._root_context is None :
76               #MESSAGE ( "Name Service Reference is invalid" )
77               #sys.exit(1)
78               MESSAGE(" Name service not found")
79             else:
80               ok = 1
81           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
82             MESSAGE(" Name service not found")
83           time.sleep(0.25)
84           steps = steps - 1
85         if steps == 0 and self._root_context is None: 
86           MESSAGE ( "Name Service Reference is invalid" )
87           sys.exit(1)
88
89     #-------------------------------------------------------------------------
90
91     def Register(self,ObjRef, Path):
92         """ ns.Register(object,pathname )  
93         
94         register a CORBA object under a pathname
95         """
96
97         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
98         _not_exist = 0
99         path_list = list(Path)
100         if path_list[0]=='/':
101             self._current_context = self._root_context
102             #delete first '/' before split
103             Path=Path[1:]
104
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 
109             # is already done
110             # If not, the new context has to be created
111             _context_name = []
112             for i in range(len(result_resolve_path)-1):
113                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
114             
115             try:
116                 obj = self._current_context.resolve(_context_name)
117                 self._current_context = obj._narrow(CosNaming.NamingContext)
118             except CosNaming.NamingContext.NotFound, ex:
119                 _not_exist = 1
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" )
126
127             if _not_exist:
128                 # at least one context of the complete path is not created, we had
129                 # to create it or them
130                 _context_name = []
131                 for i in range(len(result_resolve_path)-1):
132                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
133
134                     try:
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)
140
141         #The current directory is now the directory where the object should 
142         #be recorded
143          
144         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
145         try:
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" )
158
159     #-------------------------------------------------------------------------
160
161     def Resolve(self, Path):
162         """ ns.Resolve(pathname) -> object
163
164         find a CORBA object (ior) by its pathname
165         """
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
171             Path=Path[1:]
172
173         result_resolve_path = string.split(Path,'/')
174         _context_name=[]
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"))
178         try:
179             self._obj = self._current_context.resolve(_context_name)
180         except CosNaming.NamingContext.NotFound, ex:
181             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
182             self._obj = None
183         except CosNaming.NamingContext.InvalidName, ex:
184             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
185             self._obj = None
186         except CosNaming.NamingContext.CannotProceed, ex:
187             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
188             self._obj = None
189         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
190             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
191             self._obj = None
192         return self._obj
193
194
195     #-------------------------------------------------------------------------
196
197     def Create_Directory(self,ObjRef, Path):
198         """ ns.Create_Directory(ObjRef, Path) 
199
200         create a sub directory 
201         """
202         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
203         _not_exist = 0
204         path_list = list(Path)
205         if path_list[0]=='/':
206             self._current_context = self._root_context
207             #delete first '/' before split
208             Path=Path[1:]
209
210         result_resolve_path = string.split(Path,'/')
211         _context_name = []
212         for i in range(len(result_resolve_path)):
213             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
214             try:
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" )
225  
226     def Destroy_Name(self,Path):
227       """ ns.Destroy_Name(Path) 
228
229         remove a name in naming service
230       """
231       resolve_path=string.split(Path,'/')
232       if resolve_path[0] == '': del resolve_path[0]
233       dir_path=resolve_path[:-1]
234       context_name=[]
235       for e in dir_path:
236          context_name.append(CosNaming.NameComponent(e,"dir"))
237       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
238       
239       try:
240         self._root_context.unbind(context_name)
241       except CosNaming.NamingContext.NotFound, ex:
242         return
243       except CORBA.Exception,ex:
244         return
245
246     def Destroy_FullDirectory(self,Path):
247       """ ns.Destroy_FullDirectory(Path)
248
249         remove recursively a directory
250       """
251       context_name=[]
252       for e in string.split(Path,'/'):
253         if e == '':continue
254         context_name.append(CosNaming.NameComponent(e,"dir"))
255
256       try:
257         context=self._root_context.resolve(context_name)
258       except CosNaming.NamingContext.NotFound, ex:
259         return
260       except CORBA.Exception,ex:
261         return
262
263       bl,bi=context.list(0)
264       if bi is not None:
265          ok,b=bi.next_one()
266          while(ok):
267             for s in b.binding_name :
268                if s.kind == "object":
269                   context.unbind([s])
270                elif s.kind == "dir":
271                   context.unbind([s])
272             ok,b=bi.next_one()
273
274       context.destroy()
275       self._root_context.unbind(context_name)