Back to index

salome-kernel  6.5.0
SALOME_NamingServicePy.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 #  SALOME NamingService : wrapping NamingService services
00026 #  File   : SALOME_NamingServicePy.py
00027 #  Author : Estelle Deville, CEA
00028 #  Module : SALOME
00029 #  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/NamingService/SALOME_NamingServicePy.py,v 1.11.2.3.14.2.12.1 2012-04-12 14:05:18 vsr Exp $
00030 ## @package SALOME_NamingServicePy
00031 # \brief Module to manage SALOME naming service from python
00032 #
00033 import sys
00034 import time
00035 from omniORB import CORBA
00036 import CosNaming
00037 import string
00038 from string import *
00039 
00040 from SALOME_utilities import *
00041 #=============================================================================
00042 
00043 class SALOME_NamingServicePy_i(object):
00044     """
00045       A class to manage SALOME naming service from python code
00046     """
00047     _orb = None
00048     _root_context=None
00049     _current_context=None
00050     _obj=None
00051     
00052     #-------------------------------------------------------------------------
00053 
00054     def __init__(self, orb=None):
00055         """
00056         Standard Constructor, with ORB reference.
00057  
00058         Initializes the naming service root context
00059         """
00060         #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
00061         if orb is None:
00062           orb=CORBA.ORB_init([''], CORBA.ORB_ID)
00063         self._orb = orb
00064         # initialize root context and current context
00065         ok = 0
00066         steps = 240
00067         while steps > 0 and ok == 0:
00068           try:
00069             obj =self._orb.resolve_initial_references("NameService")
00070             self._root_context =obj._narrow(CosNaming.NamingContext)
00071             self._current_context = self._root_context
00072 
00073         
00074             if self._root_context is None :
00075               #MESSAGE ( "Name Service Reference is invalid" )
00076               #sys.exit(1)
00077               MESSAGE(" Name service not found")
00078             else:
00079               ok = 1
00080           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00081             MESSAGE(" Name service not found")
00082           time.sleep(0.25)
00083           steps = steps - 1
00084         if steps == 0 and self._root_context is None: 
00085           MESSAGE ( "Name Service Reference is invalid" )
00086           sys.exit(1)
00087 
00088     #-------------------------------------------------------------------------
00089 
00090     def Register(self,ObjRef, Path):
00091         """ ns.Register(object,pathname )  
00092         
00093         register a CORBA object under a pathname
00094         """
00095 
00096         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
00097         _not_exist = 0
00098         path_list = list(Path)
00099         if path_list[0]=='/':
00100             self._current_context = self._root_context
00101             #delete first '/' before split
00102             Path=Path[1:]
00103 
00104         result_resolve_path = string.split(Path,'/')
00105         if len(result_resolve_path)>1:
00106             # A directory is treated (not only an object name)
00107             # We had to test if the directory where ObjRef should be recorded 
00108             # is already done
00109             # If not, the new context has to be created
00110             _context_name = []
00111             for i in range(len(result_resolve_path)-1):
00112                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
00113             
00114             try:
00115                 obj = self._current_context.resolve(_context_name)
00116                 self._current_context = obj._narrow(CosNaming.NamingContext)
00117             except CosNaming.NamingContext.NotFound, ex:
00118                 _not_exist = 1
00119             except CosNaming.NamingContext.InvalidName, ex:
00120                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
00121             except CosNaming.NamingContext.CannotProceed, ex:
00122                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
00123             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00124                 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
00125 
00126             if _not_exist:
00127                 # at least one context of the complete path is not created, we had
00128                 # to create it or them
00129                 _context_name = []
00130                 for i in range(len(result_resolve_path)-1):
00131                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
00132 
00133                     try:
00134                         obj = self._current_context.resolve(_context_name)
00135                         self._current_context = obj._narrow(CosNaming.NamingContext)
00136                     except CosNaming.NamingContext.NotFound, ex:
00137                         #This context is not created. It will be done
00138                         self._current_context = self._current_context.bind_new_context(_context_name)
00139 
00140         #The current directory is now the directory where the object should 
00141         #be recorded
00142          
00143         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
00144         try:
00145             self._current_context.bind(_context_name,ObjRef)
00146         except CosNaming.NamingContext.NotFound, ex:
00147             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
00148         except CosNaming.NamingContext.InvalidName, ex:
00149             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
00150         except CosNaming.NamingContext.CannotProceed, ex:
00151             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
00152         except CosNaming.NamingContext.AlreadyBound, ex:
00153             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
00154             self._current_context.rebind(_context_name,ObjRef)
00155         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00156             MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
00157 
00158     #-------------------------------------------------------------------------
00159 
00160     def Resolve(self, Path):
00161         """ ns.Resolve(pathname) -> object
00162 
00163         find a CORBA object (ior) by its pathname
00164         """
00165         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
00166         path_list = list(Path)
00167         if path_list[0]=='/':
00168             self._current_context = self._root_context
00169             #delete first '/' before split
00170             Path=Path[1:]
00171 
00172         result_resolve_path = string.split(Path,'/')
00173         _context_name=[]
00174         for i in range(len(result_resolve_path)-1):
00175             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
00176         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
00177         try:
00178             self._obj = self._current_context.resolve(_context_name)
00179         except CosNaming.NamingContext.NotFound, ex:
00180             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
00181             self._obj = None
00182         except CosNaming.NamingContext.InvalidName, ex:
00183             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
00184             self._obj = None
00185         except CosNaming.NamingContext.CannotProceed, ex:
00186             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
00187             self._obj = None
00188         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00189             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
00190             self._obj = None
00191         return self._obj
00192 
00193 
00194     #-------------------------------------------------------------------------
00195 
00196     def Create_Directory(self,ObjRef, Path):
00197         """ ns.Create_Directory(ObjRef, Path) 
00198 
00199         create a sub directory 
00200         """
00201         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
00202         _not_exist = 0
00203         path_list = list(Path)
00204         if path_list[0]=='/':
00205             self._current_context = self._root_context
00206             #delete first '/' before split
00207             Path=Path[1:]
00208 
00209         result_resolve_path = string.split(Path,'/')
00210         _context_name = []
00211         for i in range(len(result_resolve_path)):
00212             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
00213             try:
00214                 obj = self._current_context.resolve(_context_name)
00215                 self._current_context = obj._narrow(CosNaming.NamingContext)
00216             except CosNaming.NamingContext.NotFound, ex:
00217                 self._current_context = self._current_context.bind_new_context(_context_name)
00218             except CosNaming.NamingContext.InvalidName, ex:
00219                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
00220             except CosNaming.NamingContext.CannotProceed, ex:
00221                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
00222             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
00223                 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
00224  
00225     def Destroy_Name(self,Path):
00226       """ ns.Destroy_Name(Path) 
00227 
00228         remove a name in naming service
00229       """
00230       resolve_path=string.split(Path,'/')
00231       if resolve_path[0] == '': del resolve_path[0]
00232       dir_path=resolve_path[:-1]
00233       context_name=[]
00234       for e in dir_path:
00235          context_name.append(CosNaming.NameComponent(e,"dir"))
00236       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
00237       
00238       try:
00239         self._root_context.unbind(context_name)
00240       except CosNaming.NamingContext.NotFound, ex:
00241         return
00242       except CORBA.Exception,ex:
00243         return
00244 
00245     def Destroy_FullDirectory(self,Path):
00246       """ ns.Destroy_FullDirectory(Path)
00247 
00248         remove recursively a directory
00249       """
00250       context_name=[]
00251       for e in string.split(Path,'/'):
00252         if e == '':continue
00253         context_name.append(CosNaming.NameComponent(e,"dir"))
00254 
00255       try:
00256         context=self._root_context.resolve(context_name)
00257       except CosNaming.NamingContext.NotFound, ex:
00258         return
00259       except CORBA.Exception,ex:
00260         return
00261 
00262       bl,bi=context.list(0)
00263       if bi is not None:
00264          ok,b=bi.next_one()
00265          while(ok):
00266             for s in b.binding_name :
00267                if s.kind == "object":
00268                   context.unbind([s])
00269                elif s.kind == "dir":
00270                   context.unbind([s])
00271             ok,b=bi.next_one()
00272 
00273       context.destroy()
00274       self._root_context.unbind(context_name)