Salome HOME
PR: retour au tag V1_2_1_debug2 (pb dans la branche de merge V1_2c)
[modules/kernel.git] / src / NamingService / SALOME_NamingServicePy.py
1 #! /usr/bin/env python
2 #
3 #  SALOME NamingService : wrapping NamingService services
4 #
5 #  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
23 #
24 #
25 #
26 #  File   : SALOME_NamingServicePy.py
27 #  Author : Estelle Deville, CEA
28 #  Module : SALOME
29 #  $Header$
30
31 import sys
32 from omniORB import CORBA
33 import CosNaming
34 from string import *
35
36 from SALOME_utilities import *
37 #=============================================================================
38
39 class SALOME_NamingServicePy_i:
40     _orb = None
41     _root_context=None
42     _current_context=None
43     _obj=None
44     
45     #-------------------------------------------------------------------------
46
47     def __init__(self, orb):
48         MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
49         self._orb = orb
50         # initialize root context and current context
51         obj =self._orb.resolve_initial_references("NameService")
52         self._root_context =obj._narrow(CosNaming.NamingContext)
53         self._current_context = self._root_context
54
55         
56         if self._root_context is None :
57             MESSAGE ( "Name Service Reference is invalid" )
58             sys.exit(1)
59  
60     #-------------------------------------------------------------------------
61     def Register(self,ObjRef, Path):
62         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
63         _not_exist = 0
64         path_list = list(Path)
65         if path_list[0]=='/':
66             self._current_context = self._root_context
67             #delete first '/' before split
68             Path=Path[1:]
69
70         result_resolve_path = split(Path,'/')
71         if len(result_resolve_path)>1:
72             # A directory is treated (not only an object name)
73             # We had to test if the directory where ObjRef should be recorded 
74             # is already done
75             # If not, the new context has to be created
76             _context_name = []
77             for i in range(len(result_resolve_path)-1):
78                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
79             
80             try:
81                 obj = self._current_context.resolve(_context_name)
82                 self._current_context = obj._narrow(CosNaming.NamingContext)
83             except CosNaming.NamingContext.NotFound, ex:
84                 _not_exist = 1
85             except CosNaming.NamingContext.InvalidName, ex:
86                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
87             except CosNaming.NamingContext.CannotProceed, ex:
88                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
89             except CORBA.COMM_FAILURE, ex:
90                 MESSAGE ( "Register : CORBA.COMM_FAILURE" )
91
92             if _not_exist:
93                 # at least one context of the complete path is not created, we had
94                 # to create it or them
95                 _context_name = []
96                 for i in range(len(result_resolve_path)-1):
97                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
98
99                     try:
100                         obj = self._current_context.resolve(_context_name)
101                         self._current_context = obj._narrow(CosNaming.NamingContext)
102                     except CosNaming.NamingContext.NotFound, ex:
103                         #This context is not created. It will be done
104                         self._current_context = self._current_context.bind_new_context(_context_name)
105
106         #The current directory is now the directory where the object should 
107         #be recorded
108          
109         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
110         try:
111             self._current_context.bind(_context_name,ObjRef)
112         except CosNaming.NamingContext.NotFound, ex:
113             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
114         except CosNaming.NamingContext.InvalidName, ex:
115             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
116         except CosNaming.NamingContext.CannotProceed, ex:
117             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
118         except CosNaming.NamingContext.AlreadyBound, ex:
119             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
120             self._current_context.rebind(_context_name,ObjRef)
121         except CORBA.COMM_FAILURE, ex:
122             MESSAGE ( "Register : CORBA.COMM_FAILURE" )
123
124             
125     #-------------------------------------------------------------------------
126     def Resolve(self, Path):
127         MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
128         path_list = list(Path)
129         if path_list[0]=='/':
130             self._current_context = self._root_context
131             #delete first '/' before split
132             Path=Path[1:]
133
134         result_resolve_path = split(Path,'/')
135         _context_name=[]
136         for i in range(len(result_resolve_path)-1):
137             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
138         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
139         try:
140             self._obj = self._current_context.resolve(_context_name)
141         except CosNaming.NamingContext.NotFound, ex:
142             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
143             self._obj = None
144         except CosNaming.NamingContext.InvalidName, ex:
145             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
146             self._obj = None
147         except CosNaming.NamingContext.CannotProceed, ex:
148             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
149             self._obj = None
150         except CORBA.COMM_FAILURE, ex:
151             MESSAGE ( "Resolve : CORBA.COMM_FAILURE" )
152             self._obj = None
153         return self._obj
154
155
156
157     #-------------------------------------------------------------------------
158     def Create_Directory(self,ObjRef, Path):
159         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
160         _not_exist = 0
161         path_list = list(Path)
162         if path_list[0]=='/':
163             self._current_context = self._root_context
164             #delete first '/' before split
165             Path=Path[1:]
166
167         result_resolve_path = split(Path,'/')
168         _context_name = []
169         for i in range(len(result_resolve_path)):
170             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
171             try:
172                 obj = self._current_context.resolve(_context_name)
173                 self._current_context = obj._narrow(CosNaming.NamingContext)
174             except CosNaming.NamingContext.NotFound, ex:
175                 self._current_context = self._current_context.bind_new_context(_context_name)
176             except CosNaming.NamingContext.InvalidName, ex:
177                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
178             except CosNaming.NamingContext.CannotProceed, ex:
179                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
180             except CORBA.COMM_FAILURE, ex:
181                 MESSAGE ( "Create_Directory : CORBA.COMM_FAILURE" )
182
183  
184
185