CorbaNaming.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
5 
17 
18 import omniORB.CORBA as CORBA
19 import CosNaming
20 import string
21 import sys
22 import traceback
23 
24 
62  """
63  """
64 
65 
66 
67 
81  def __init__(self, orb, name_server=None):
82  self._orb = orb
83  self._nameServer = ""
84  self._rootContext = CosNaming.NamingContext._nil
85  self._blLength = 100
86 
87  if name_server:
88  self._nameServer = "corbaloc::" + name_server + "/NameService"
89  try:
90  obj = orb.string_to_object(self._nameServer)
91  self._rootContext = obj._narrow(CosNaming.NamingContext)
92  if CORBA.is_nil(self._rootContext):
93  print "CorbaNaming: Failed to narrow the root naming context."
94 
95  except CORBA.ORB.InvalidName:
96  self.__print_exception()
97  print "Service required is invalid [does not exist]."
98 
99  return
100 
101 
102 
114  def __del__(self):
115  return
116 
117 
118 
131  def init(self, name_server):
132  self._nameServer = "corbaloc::" + name_server + "/NameService"
133  obj = self._orb.string_to_object(self._nameServer)
134  self._rootContext = obj._narrow(CosNaming.NamingContext)
135  if CORBA.is_nil(self._rootContext):
136  raise MemoryError
137 
138  return
139 
140 
141 
155  def isAlive(self):
156  try:
157  if self._rootContext._non_existent():
158  return False
159  return True
160  except:
161  self.__print_exception()
162  return False
163 
164  return False
165 
166 
167 
213  def bind(self, name_list, obj, force=None):
214  if force is None :
215  force = True
216 
217  try:
218  self._rootContext.bind(name_list, obj)
219  except CosNaming.NamingContext.NotFound:
220  if force:
221  self.bindRecursive(self._rootContext, name_list, obj)
222  else:
223  raise
224  except CosNaming.NamingContext.CannotProceed, err:
225  if force:
226  self.bindRecursive(err.cxt, err.rest_of_name, obj)
227  else:
228  raise
229  except CosNaming.NamingContext.AlreadyBound:
230  self._rootContext.rebind(name_list, obj)
231 
232 
233 
257  def bindByString(self, string_name, obj, force=True):
258  self.bind(self.toName(string_name), obj, force)
259 
260 
261 
296  def bindRecursive(self, context, name_list, obj):
297  length = len(name_list)
298  cxt = context
299  for i in range(length):
300  if i == length -1:
301  try:
302  cxt.bind(self.subName(name_list, i, i), obj)
303  except CosNaming.NamingContext.AlreadyBound:
304  cxt.rebind(self.subName(name_list, i, i), obj)
305  return
306  else:
307  if self.objIsNamingContext(cxt):
308  cxt = self.bindOrResolveContext(cxt,self.subName(name_list, i, i))
309  else:
310  raise CosNaming.NamingContext.CannotProceed(cxt, self.subName(name_list, i))
311  return
312 
313 
314 
338  def rebind(self, name_list, obj, force=True):
339  if force is None:
340  force = True
341 
342  try:
343  self._rootContext.rebind(name_list, obj)
344 
345  except CosNaming.NamingContext.NotFound:
346  if force:
347  self.rebindRecursive(self._rootContext, name_list, obj)
348  else:
349  self.__print_exception()
350  raise
351 
352  except CosNaming.NamingContext.CannotProceed, err:
353  if force:
354  self.rebindRecursive(err.cxt, err,rest_of_name, obj)
355  else:
356  self.__print_exception()
357  raise
358 
359  return
360 
361 
362 
385  def rebindByString(self, string_name, obj, force=True):
386  self.rebind(self.toName(string_name), obj, force)
387 
388  return
389 
390 
391 
415  def rebindRecursive(self, context, name_list, obj):
416  length = len(name_list)
417  for i in range(length):
418  if i == length - 1:
419  context.rebind(self.subName(name_list, i, i), obj)
420  return
421  else:
422  if self.objIsNamingContext(context):
423  try:
424  context = context.bind_new_context(self.subName(name_list, i, i))
425  except CosNaming.NamingContext.AlreadyBound:
426  obj_ = context.resolve(self.subName(name_list, i, i))
427  context = obj_._narrow(CosNaming.NamingContext)
428  else:
429  raise CosNaming.NamingContext.CannotProceed(context, self.subName(name_list, i))
430  return
431 
432 
433 
457  def bindContext(self, name, name_cxt, force=True):
458  if isinstance(name, basestring):
459  self.bind(self.toName(name), name_cxt, force)
460  else:
461  self.bind(name, name_cxt, force)
462  return
463 
464 
465 
483  def bindContextRecursive(self, context, name_list, name_cxt):
484  self.bindRecursive(context, name_list, name_cxt)
485  return
486 
487 
488 
511  def rebindContext(self, name, name_cxt, force=True):
512  if isinstance(name, basestring):
513  self.rebind(self.toName(name), name_cxt, force)
514  else:
515  self.rebind(name, name_cxt, force)
516  return
517 
518 
519 
536  def rebindContextRecursive(self, context, name_list, name_cxt):
537  self.rebindRecursive(context, name_list, name_cxt)
538  return
539 
540 
541 
568  def resolve(self, name):
569  if isinstance(name, basestring):
570  name_ = self.toName(name)
571  else:
572  name_ = name
573 
574  try:
575  obj = self._rootContext.resolve(name_)
576  return obj
577  except CosNaming.NamingContext.NotFound, ex:
578  self.__print_exception()
579  return None
580 
581 
582 
609  def unbind(self, name):
610  if isinstance(name, basestring):
611  name_ = self.toName(name)
612  else:
613  name_ = name
614 
615  try:
616  self._rootContext.unbind(name_)
617  except:
618  self.__print_exception()
619 
620  return
621 
622 
623 
638  def newContext(self):
639  return self._rootContext.new_context()
640 
641 
642 
668  def bindNewContext(self, name, force=True):
669  if force is None:
670  force = True
671 
672  if isinstance(name, basestring):
673  name_ = self.toName(name)
674  else:
675  name_ = name
676 
677  try:
678  return self._rootContext.bind_new_context(name_)
679  except CosNaming.NamingContext.NotFound:
680  if force:
681  self.bindRecursive(self._rootContext, name_, self.newContext())
682  else:
683  self.__print_exception()
684  raise
685  except CosNaming.NamingContext.CannotProceed, err:
686  if force:
687  self.bindRecursive(err.cxt, err.rest_of_name, self.newContext())
688  else:
689  self.__print_exception()
690  raise
691  return None
692 
693 
694 
725  def destroy(self, context):
726  context.destroy()
727 
728 
729 
748  def destroyRecursive(self, context):
749  cont = True
750  bl = []
751  bi = 0
752  bl, bi = context.list(self._blLength)
753  while cont:
754  for i in range(len(bl)):
755  if bl[i].binding_type == CosNaming.ncontext:
756  obj = context.resolve(bl[i].binding_name)
757  next_context = obj._narrow(CosNaming.NamingContext)
758 
759  self.destroyRecursive(next_context)
760  context.unbind(bl[i].binding_name)
761  next_context.destroy()
762  elif bl[i].binding_type == CosNaming.nobject:
763  context.unbind(bl[i].binding_name)
764  else:
765  assert(0)
766  if CORBA.is_nil(bi):
767  cont = False
768  else:
769  bi.next_n(self._blLength, bl)
770 
771  if not (CORBA.is_nil(bi)):
772  bi.destroy()
773  return
774 
775 
776 
787  def clearAll(self):
788  self.destroyRecursive(self._rootContext)
789  return
790 
791 
792 
806  def list(self, name_cxt, how_many, rbl, rbi):
807  bl, bi = name_cxt.list(how_many)
808 
809  for i in bl:
810  rbl.append(bl)
811 
812  rbi.append(bi)
813 
814 
815  #============================================================
816  # interface of NamingContext
817  #============================================================
818 
819 
835  def toString(self, name_list):
836  if len(name_list) == 0:
837  raise CosNaming.NamingContext.InvalidName
838 
839  slen = self.getNameLength(name_list)
840  string_name = [""]
841  self.nameToString(name_list, string_name, slen)
842 
843  return string_name[0]
844 
845 
846 
862  def toName(self, sname):
863  if not sname:
864  raise CosNaming.NamingContext.InvalidName
865 
866  string_name = sname
867  name_comps = []
868 
869  nc_length = 0
870  nc_length = self.split(string_name, "/", name_comps)
871 
872  if not (nc_length > 0):
873  raise CosNaming.NamingContext.InvalidName
874 
875  name_list = [CosNaming.NameComponent("","") for i in range(nc_length)]
876 
877  for i in range(nc_length):
878  pos = string.rfind(name_comps[i][0:],".")
879  if pos == -1:
880  name_list[i].id = name_comps[i]
881  name_list[i].kind = ""
882  else:
883  name_list[i].id = name_comps[i][0:pos]
884  name_list[i].kind = name_comps[i][(pos+1):]
885 
886  return name_list
887 
888 
889 
907  def toUrl(self, addr, string_name):
908  return self._rootContext.to_url(addr, string_name)
909 
910 
911 
930  def resolveStr(self, string_name):
931  return self.resolve(self.toName(string_name))
932 
933 
934  #============================================================
935  # Find functions
936  #============================================================
937 
938 
958  def bindOrResolve(self, context, name_list, obj):
959  try:
960  context.bind_context(name_list, obj)
961  return obj
962  except CosNaming.NamingContext.AlreadyBound:
963  obj = context.resolve(name_list)
964  return obj
965  return CORBA.Object._nil
966 
967 
968 
989  def bindOrResolveContext(self, context, name_list, new_context=None):
990  if new_context is None:
991  new_cxt = self.newContext()
992  else:
993  new_cxt = new_context
994 
995  obj = self.bindOrResolve(context, name_list, new_cxt)
996  return obj._narrow(CosNaming.NamingContext)
997 
998 
999 
1012  def getNameServer(self):
1013  return self._nameServer
1014 
1015 
1016 
1029  def getRootContext(self):
1030  return self._rootContext
1031 
1032 
1033 
1047  def objIsNamingContext(self, obj):
1048  nc = obj._narrow(CosNaming.NamingContext)
1049  if CORBA.is_nil(nc):
1050  return False
1051  else:
1052  return True
1053 
1054 
1055 
1070  def nameIsNamingContext(self, name_list):
1071  return self.objIsNamingContext(self.resolve(name_list))
1072 
1073 
1074 
1092  def subName(self, name_list, begin, end = None):
1093  if end is None or end < 0:
1094  end = len(name_list) - 1
1095 
1096  sub_len = end - (begin -1)
1097  objId = ""
1098  kind = ""
1099 
1100  sub_name = []
1101  for i in range(sub_len):
1102  sub_name.append(name_list[begin + i])
1103 
1104  return sub_name
1105 
1106 
1107 
1126  def nameToString(self, name_list, string_name, slen):
1127  for i in range(len(name_list)):
1128  for id_ in name_list[i].id:
1129  if id_ == "/" or id_ == "." or id_ == "\\":
1130  string_name[0] += "\\"
1131  string_name[0] += id_
1132 
1133  if name_list[i].id == "" or name_list[i].kind != "":
1134  string_name[0] += "."
1135 
1136  for kind_ in name_list[i].kind:
1137  if kind_ == "/" or kind_ == "." or kind_ == "\\":
1138  string_name[0] += "\\"
1139  string_name[0] += kind_
1140 
1141  string_name[0] += "/"
1142 
1143 
1144 
1161  def getNameLength(self, name_list):
1162  slen = 0
1163 
1164  for i in range(len(name_list)):
1165  for id_ in name_list[i].id:
1166  if id_ == "/" or id_ == "." or id_ == "\\":
1167  slen += 1
1168  slen += 1
1169  if name_list[i].id == "" or name_list[i].kind == "":
1170  slen += 1
1171 
1172  for kind_ in name_list[i].kind:
1173  if kind_ == "/" or kind_ == "." or kind_ == "\\":
1174  slen += 1
1175  slen += 1
1176 
1177  slen += 1
1178 
1179  return slen
1180 
1181 
1182 
1198  def split(self, input, delimiter, results):
1199  delim_size = len(delimiter)
1200  found_pos = 0
1201  begin_pos = 0
1202  pre_pos = 0
1203  substr_size = 0
1204 
1205  if input[0:delim_size] == delimiter:
1206  begin_pos = delim_size
1207  pre_pos = delim_size
1208 
1209  while 1:
1210  found_pos = string.find(input[begin_pos:],delimiter)
1211  if found_pos == -1:
1212  results.append(input[pre_pos:])
1213  break
1214 
1215  if found_pos > 0 and input[found_pos + begin_pos - 1] == "\\":
1216  begin_pos += found_pos + delim_size
1217  else:
1218  substr_size = found_pos + (begin_pos - pre_pos)
1219  if substr_size > 0:
1220  results.append(input[pre_pos:(pre_pos+substr_size)])
1221  begin_pos += found_pos + delim_size
1222  pre_pos = begin_pos
1223 
1224  return len(results)
1225 
1226 
1227 
1239  if sys.version_info[0:3] >= (2, 4, 0):
1240  print traceback.format_exc()
1241  else:
1242  _exc_list = traceback.format_exception(*sys.exc_info())
1243  _exc_str = "".join(_exc_list)
1244  print _exc_str
1245 
1246  return
def rebindContextRecursive(self, context, name_list, name_cxt)
Definition: CorbaNaming.py:536
def resolveStr(self, string_name)
Resolve from name of string representation and get object.
Definition: CorbaNaming.py:930
def __init__(self, orb, name_server=None)
Consructor.
Definition: CorbaNaming.py:81
def rebindRecursive(self, context, name_list, obj)
Definition: CorbaNaming.py:415
def bindOrResolveContext(self, context, name_list, new_context=None)
Bind of resolve the given name component.
Definition: CorbaNaming.py:989
def rebind(self, name_list, obj, force=True)
Definition: CorbaNaming.py:338
def rebindByString(self, string_name, obj, force=True)
Definition: CorbaNaming.py:385
def rebindContext(self, name, name_cxt, force=True)
Definition: CorbaNaming.py:511
def objIsNamingContext(self, obj)
Whether the object is NamingContext.
def bindNewContext(self, name, force=True)
Definition: CorbaNaming.py:668
def getNameLength(self, name_list)
Get string length of the name component&#39;s string representation.
def isAlive(self)
Check on whether the root context is alive.
Definition: CorbaNaming.py:155
def toName(self, sname)
Get NameComponent from gien string name representation.
Definition: CorbaNaming.py:862
def bindContext(self, name, name_cxt, force=True)
Definition: CorbaNaming.py:457
def getNameServer(self)
Get the name of naming server.
CORBA Naming Service helper class.
Definition: CorbaNaming.py:61
def toUrl(self, addr, string_name)
Get URL representation from given addr and string_name.
Definition: CorbaNaming.py:907
def nameToString(self, name_list, string_name, slen)
Get string representation of name component.
def subName(self, name_list, begin, end=None)
Get subset of given name component.
def bindOrResolve(self, context, name_list, obj)
Bind of resolve the given name component.
Definition: CorbaNaming.py:958
def __print_exception(self)
Print exception information Print exception information.
def destroyRecursive(self, context)
Destroy the naming context recursively.
Definition: CorbaNaming.py:748
def bindRecursive(self, context, name_list, obj)
Definition: CorbaNaming.py:296
def bindByString(self, string_name, obj, force=True)
Definition: CorbaNaming.py:257
def toString(self, name_list)
Get string representation of given NameComponent.
Definition: CorbaNaming.py:835
def split(self, input, delimiter, results)
Split of string.
def clearAll(self)
Destroy all binding.
Definition: CorbaNaming.py:787
def bind(self, name_list, obj, force=None)
Definition: CorbaNaming.py:213
def bindContextRecursive(self, context, name_list, name_cxt)
Definition: CorbaNaming.py:483
def list(self, name_cxt, how_many, rbl, rbi)
Definition: CorbaNaming.py:806
def nameIsNamingContext(self, name_list)
Whether the given name component is NamingContext.
def unbind(self, name)
void unbind(const CosNaming::Name& name) throw(NotFound, CannotProceed, InvalidName); ...
Definition: CorbaNaming.py:609
def getRootContext(self)
Get the root context.


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:34