X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FNamingService%2FSALOME_NamingServicePy.py;h=450b262c3181e31b223bf229566aeb1f38a02609;hb=2454295251ef7ee0d71a100597d9428da1247f1e;hp=f43df5aebb5a97337311e8bf3f8dd6a72fca38ad;hpb=0fac7040be417aab7228a4e23904b4cf22a8dda4;p=modules%2Fkernel.git diff --git a/src/NamingService/SALOME_NamingServicePy.py b/src/NamingService/SALOME_NamingServicePy.py index f43df5aeb..450b262c3 100644 --- a/src/NamingService/SALOME_NamingServicePy.py +++ b/src/NamingService/SALOME_NamingServicePy.py @@ -1,6 +1,6 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE # # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -40,7 +40,7 @@ from string import * from SALOME_utilities import * #============================================================================= -class SALOME_NamingServicePy_i(object): +class SALOME_NamingServicePy_i: """ A class to manage SALOME naming service from python code """ @@ -51,7 +51,7 @@ class SALOME_NamingServicePy_i(object): #------------------------------------------------------------------------- - def __init__(self, orb=None): + def __init__(self, orb=None, steps=240, spy=False): """ Standard Constructor, with ORB reference. @@ -63,7 +63,6 @@ class SALOME_NamingServicePy_i(object): self._orb = orb # initialize root context and current context ok = 0 - steps = 240 while steps > 0 and ok == 0: try: obj =self._orb.resolve_initial_references("NameService") @@ -83,7 +82,10 @@ class SALOME_NamingServicePy_i(object): steps = steps - 1 if steps == 0 and self._root_context is None: MESSAGE ( "Name Service Reference is invalid" ) - sys.exit(1) + if spy: + raise ValueError("Name Service Reference is invalid") + else: + sys.exit(1) #------------------------------------------------------------------------- @@ -101,7 +103,7 @@ class SALOME_NamingServicePy_i(object): #delete first '/' before split Path=Path[1:] - result_resolve_path = string.split(Path,'/') + result_resolve_path = Path.split('/') if len(result_resolve_path)>1: # A directory is treated (not only an object name) # We had to test if the directory where ObjRef should be recorded @@ -114,11 +116,11 @@ class SALOME_NamingServicePy_i(object): try: obj = self._current_context.resolve(_context_name) self._current_context = obj._narrow(CosNaming.NamingContext) - except CosNaming.NamingContext.NotFound, ex: + except CosNaming.NamingContext.NotFound as ex: _not_exist = 1 - except CosNaming.NamingContext.InvalidName, ex: + except CosNaming.NamingContext.InvalidName as ex: MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" ) - except CosNaming.NamingContext.CannotProceed, ex: + except CosNaming.NamingContext.CannotProceed as ex: MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" ) except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE): MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" ) @@ -133,7 +135,7 @@ class SALOME_NamingServicePy_i(object): try: obj = self._current_context.resolve(_context_name) self._current_context = obj._narrow(CosNaming.NamingContext) - except CosNaming.NamingContext.NotFound, ex: + except CosNaming.NamingContext.NotFound as ex: #This context is not created. It will be done self._current_context = self._current_context.bind_new_context(_context_name) @@ -143,13 +145,13 @@ class SALOME_NamingServicePy_i(object): _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")] try: self._current_context.bind(_context_name,ObjRef) - except CosNaming.NamingContext.NotFound, ex: + except CosNaming.NamingContext.NotFound as ex: MESSAGE ( "Register : CosNaming.NamingContext.NotFound" ) - except CosNaming.NamingContext.InvalidName, ex: + except CosNaming.NamingContext.InvalidName as ex: MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" ) - except CosNaming.NamingContext.CannotProceed, ex: + except CosNaming.NamingContext.CannotProceed as ex: MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" ) - except CosNaming.NamingContext.AlreadyBound, ex: + except CosNaming.NamingContext.AlreadyBound as ex: MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" ) self._current_context.rebind(_context_name,ObjRef) except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE): @@ -169,20 +171,57 @@ class SALOME_NamingServicePy_i(object): #delete first '/' before split Path=Path[1:] - result_resolve_path = string.split(Path,'/') + result_resolve_path = Path.split('/') _context_name=[] for i in range(len(result_resolve_path)-1): _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir")) _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")) try: self._obj = self._current_context.resolve(_context_name) - except CosNaming.NamingContext.NotFound, ex: + except CosNaming.NamingContext.NotFound as ex: MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" ) self._obj = None - except CosNaming.NamingContext.InvalidName, ex: + except CosNaming.NamingContext.InvalidName as ex: MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" ) self._obj = None - except CosNaming.NamingContext.CannotProceed, ex: + except CosNaming.NamingContext.CannotProceed as ex: + MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" ) + self._obj = None + except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE): + MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" ) + self._obj = None + return self._obj + + #------------------------------------------------------------------------- + + def Resolve_Dir(self, Path): + """ ns.Resolve_Dir(pathname) -> dir + + find a CORBA object (ior) by its pathname + """ + #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" ) + path_list = list(Path) + if path_list[0]=='/': + self._current_context = self._root_context + #delete first '/' before split + Path=Path[1:] + + result_resolve_path = Path.split('/') + _context_name=[] + for i in range(len(result_resolve_path)-1): + _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir")) + _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"dir")) + print(_context_name) + return None + try: + self._obj = self._current_context.resolve(_context_name) + except CosNaming.NamingContext.NotFound as ex: + MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" ) + self._obj = None + except CosNaming.NamingContext.InvalidName as ex: + MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" ) + self._obj = None + except CosNaming.NamingContext.CannotProceed as ex: MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" ) self._obj = None except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE): @@ -206,18 +245,18 @@ class SALOME_NamingServicePy_i(object): #delete first '/' before split Path=Path[1:] - result_resolve_path = string.split(Path,'/') + result_resolve_path = Path.split('/') _context_name = [] for i in range(len(result_resolve_path)): _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")] try: obj = self._current_context.resolve(_context_name) self._current_context = obj._narrow(CosNaming.NamingContext) - except CosNaming.NamingContext.NotFound, ex: + except CosNaming.NamingContext.NotFound as ex: self._current_context = self._current_context.bind_new_context(_context_name) - except CosNaming.NamingContext.InvalidName, ex: + except CosNaming.NamingContext.InvalidName as ex: MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" ) - except CosNaming.NamingContext.CannotProceed, ex: + except CosNaming.NamingContext.CannotProceed as ex: MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" ) except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE): MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" ) @@ -227,7 +266,7 @@ class SALOME_NamingServicePy_i(object): remove a name in naming service """ - resolve_path=string.split(Path,'/') + resolve_path=Path.split('/') if resolve_path[0] == '': del resolve_path[0] dir_path=resolve_path[:-1] context_name=[] @@ -237,9 +276,9 @@ class SALOME_NamingServicePy_i(object): try: self._root_context.unbind(context_name) - except CosNaming.NamingContext.NotFound, ex: + except CosNaming.NamingContext.NotFound as ex: return - except CORBA.Exception,ex: + except CORBA.Exception as ex: return def Destroy_FullDirectory(self,Path): @@ -248,15 +287,15 @@ class SALOME_NamingServicePy_i(object): remove recursively a directory """ context_name=[] - for e in string.split(Path,'/'): + for e in Path.split('/'): if e == '':continue context_name.append(CosNaming.NameComponent(e,"dir")) try: context=self._root_context.resolve(context_name) - except CosNaming.NamingContext.NotFound, ex: + except CosNaming.NamingContext.NotFound as ex: return - except CORBA.Exception,ex: + except CORBA.Exception as ex: return bl,bi=context.list(0)