Salome HOME
Issue 0020194: EDF 977 ALL: Get rid of warnings PACKAGE_VERSION already defined
[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):
56         """
57         Standard Constructor, with ORB reference.
58  
59         Initializes the naming service root context
60         """
61         #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
62         self._orb = orb
63         # initialize root context and current context
64         ok = 0
65         steps = 240
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           sys.exit(1)
86
87     #-------------------------------------------------------------------------
88
89     def Register(self,ObjRef, Path):
90         """ ns.Register(object,pathname )  
91         
92         register a CORBA object under a pathname
93         """
94
95         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
96         _not_exist = 0
97         path_list = list(Path)
98         if path_list[0]=='/':
99             self._current_context = self._root_context
100             #delete first '/' before split
101             Path=Path[1:]
102
103         result_resolve_path = string.split(Path,'/')
104         if len(result_resolve_path)>1:
105             # A directory is treated (not only an object name)
106             # We had to test if the directory where ObjRef should be recorded 
107             # is already done
108             # If not, the new context has to be created
109             _context_name = []
110             for i in range(len(result_resolve_path)-1):
111                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
112             
113             try:
114                 obj = self._current_context.resolve(_context_name)
115                 self._current_context = obj._narrow(CosNaming.NamingContext)
116             except CosNaming.NamingContext.NotFound, ex:
117                 _not_exist = 1
118             except CosNaming.NamingContext.InvalidName, ex:
119                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
120             except CosNaming.NamingContext.CannotProceed, ex:
121                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
122             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
123                 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
124
125             if _not_exist:
126                 # at least one context of the complete path is not created, we had
127                 # to create it or them
128                 _context_name = []
129                 for i in range(len(result_resolve_path)-1):
130                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
131
132                     try:
133                         obj = self._current_context.resolve(_context_name)
134                         self._current_context = obj._narrow(CosNaming.NamingContext)
135                     except CosNaming.NamingContext.NotFound, ex:
136                         #This context is not created. It will be done
137                         self._current_context = self._current_context.bind_new_context(_context_name)
138
139         #The current directory is now the directory where the object should 
140         #be recorded
141          
142         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
143         try:
144             self._current_context.bind(_context_name,ObjRef)
145         except CosNaming.NamingContext.NotFound, ex:
146             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
147         except CosNaming.NamingContext.InvalidName, ex:
148             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
149         except CosNaming.NamingContext.CannotProceed, ex:
150             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
151         except CosNaming.NamingContext.AlreadyBound, ex:
152             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
153             self._current_context.rebind(_context_name,ObjRef)
154         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
155             MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
156
157     #-------------------------------------------------------------------------
158
159     def Resolve(self, Path):
160         """ ns.Resolve(pathname) -> object
161
162         find a CORBA object (ior) by its pathname
163         """
164         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
165         path_list = list(Path)
166         if path_list[0]=='/':
167             self._current_context = self._root_context
168             #delete first '/' before split
169             Path=Path[1:]
170
171         result_resolve_path = string.split(Path,'/')
172         _context_name=[]
173         for i in range(len(result_resolve_path)-1):
174             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
175         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
176         try:
177             self._obj = self._current_context.resolve(_context_name)
178         except CosNaming.NamingContext.NotFound, ex:
179             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
180             self._obj = None
181         except CosNaming.NamingContext.InvalidName, ex:
182             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
183             self._obj = None
184         except CosNaming.NamingContext.CannotProceed, ex:
185             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
186             self._obj = None
187         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
188             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
189             self._obj = None
190         return self._obj
191
192
193     #-------------------------------------------------------------------------
194
195     def Create_Directory(self,ObjRef, Path):
196         """ ns.Create_Directory(ObjRef, Path) 
197
198         create a sub directory 
199         """
200         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
201         _not_exist = 0
202         path_list = list(Path)
203         if path_list[0]=='/':
204             self._current_context = self._root_context
205             #delete first '/' before split
206             Path=Path[1:]
207
208         result_resolve_path = string.split(Path,'/')
209         _context_name = []
210         for i in range(len(result_resolve_path)):
211             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
212             try:
213                 obj = self._current_context.resolve(_context_name)
214                 self._current_context = obj._narrow(CosNaming.NamingContext)
215             except CosNaming.NamingContext.NotFound, ex:
216                 self._current_context = self._current_context.bind_new_context(_context_name)
217             except CosNaming.NamingContext.InvalidName, ex:
218                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
219             except CosNaming.NamingContext.CannotProceed, ex:
220                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
221             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
222                 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
223  
224     def Destroy_Name(self,Path):
225       """ ns.Destroy_Name(Path) 
226
227         remove a name in naming service
228       """
229       resolve_path=string.split(Path,'/')
230       if resolve_path[0] == '': del resolve_path[0]
231       dir_path=resolve_path[:-1]
232       context_name=[]
233       for e in dir_path:
234          context_name.append(CosNaming.NameComponent(e,"dir"))
235       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
236       
237       try:
238         self._root_context.unbind(context_name)
239       except CosNaming.NamingContext.NotFound, ex:
240         return
241       except CORBA.Exception,ex:
242         return
243
244     def Destroy_FullDirectory(self,Path):
245       """ ns.Destroy_FullDirectory(Path)
246
247         remove recursively a directory
248       """
249       context_name=[]
250       for e in string.split(Path,'/'):
251         if e == '':continue
252         context_name.append(CosNaming.NameComponent(e,"dir"))
253
254       try:
255         context=self._root_context.resolve(context_name)
256       except CosNaming.NamingContext.NotFound, ex:
257         return
258       except CORBA.Exception,ex:
259         return
260
261       bl,bi=context.list(0)
262       if bi is not None:
263          ok,b=bi.next_one()
264          while(ok):
265             for s in b.binding_name :
266                if s.kind == "object":
267                   context.unbind([s])
268                elif s.kind == "dir":
269                   context.unbind([s])
270             ok,b=bi.next_one()
271
272       context.destroy()
273       self._root_context.unbind(context_name)