AutoTest/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 
22 
60  """
61  """
62 
63 
64 
65 
79  def __init__(self, orb, name_server=None):
80  self._orb = orb
81  self._nameServer = ""
82  self._rootContext = CosNaming.NamingContext._nil
83  self._blLength = 100
84 
85  if name_server:
86  self._nameServer = "corbaloc::" + name_server + "/NameService"
87  try:
88  obj = orb.string_to_object(self._nameServer)
89  self._rootContext = obj._narrow(CosNaming.NamingContext)
90  if CORBA.is_nil(self._rootContext):
91  print "CorbaNaming: Failed to narrow the root naming context."
92 
93  except CORBA.ORB.InvalidName:
94  print "Service required is invalid [does not exist]."
95 
96  return
97 
98 
99 
111  def __del__(self):
112  return
113 
114 
115 
128  def init(self, name_server):
129  self._nameServer = "corbaloc::" + name_server + "/NameService"
130  obj = self._orb.string_to_object(self._nameServer)
131  self._rootContext = obj._narrow(CosNaming.NamingContext)
132  if CORBA.is_nil(self._rootContext):
133  raise MemoryError
134 
135  return
136 
137 
138 
184  def bind(self, name_list, obj, force=None):
185  if force is None :
186  force = True
187 
188  try:
189  self._rootContext.bind(name_list, obj)
190  except CosNaming.NamingContext.NotFound:
191  if force:
192  self.bindRecursive(self._rootContext, name_list, obj)
193  else:
194  raise
195  except CosNaming.NamingContext.CannotProceed, err:
196  if force:
197  self.bindRecursive(err.cxt, err.rest_of_name, obj)
198  else:
199  raise
200  except CosNaming.NamingContext.AlreadyBound:
201  self._rootContext.rebind(name_list, obj)
202 
203 
204 
228  def bindByString(self, string_name, obj, force=True):
229  self.bind(self.toName(string_name), obj, force)
230 
231 
232 
267  def bindRecursive(self, context, name_list, obj):
268  length = len(name_list)
269  cxt = context
270  for i in range(length):
271  if i == length -1:
272  try:
273  cxt.bind(self.subName(name_list, i, i), obj)
274  except CosNaming.NamingContext.AlreadyBound:
275  cxt.rebind(self.subName(name_list, i, i), obj)
276  return
277  else:
278  if self.objIsNamingContext(cxt):
279  cxt = self.bindOrResolveContext(cxt,self.subName(name_list, i, i))
280  else:
281  raise CosNaming.NamingContext.CannotProceed(cxt, self.subName(name_list, i))
282  return
283 
284 
285 
309  def rebind(self, name_list, obj, force=True):
310  if force is None:
311  force = True
312 
313  try:
314  self._rootContext.rebind(name_list, obj)
315 
316  except CosNaming.NamingContext.NotFound:
317  if force:
318  self.rebindRecursive(self._rootContext, name_list, obj)
319  else:
320  raise
321 
322  except CosNaming.NamingContext.CannotProceed, err:
323  if force:
324  self.rebindRecursive(err.cxt, err,rest_of_name, obj)
325  else:
326  raise
327 
328  return
329 
330 
331 
354  def rebindByString(self, string_name, obj, force=True):
355  self.rebind(self.toName(string_name), obj, force)
356 
357  return
358 
359 
360 
384  def rebindRecursive(self, context, name_list, obj):
385  length = len(name_list)
386  for i in range(length):
387  if i == length - 1:
388  context.rebind(self.subName(name_list, i, i), obj)
389  return
390  else:
391  if self.objIsNamingContext(context):
392  try:
393  context = context.bind_new_context(self.subName(name_list, i, i))
394  except CosNaming.NamingContext.AlreadyBound:
395  obj_ = context.resolve(self.subName(name_list, i, i))
396  context = obj_._narrow(CosNaming.NamingContext)
397  else:
398  raise CosNaming.NamingContext.CannotProceed(context, self.subName(name_list, i))
399  return
400 
401 
402 
426  def bindContext(self, name, name_cxt, force=True):
427  if isinstance(name, basestring):
428  self.bind(self.toName(name), name_cxt, force)
429  else:
430  self.bind(name, name_cxt, force)
431  return
432 
433 
434 
452  def bindContextRecursive(self, context, name_list, name_cxt):
453  self.bindRecursive(context, name_list, name_cxt)
454  return
455 
456 
457 
480  def rebindContext(self, name, name_cxt, force=True):
481  if isinstance(name, basestring):
482  self.rebind(self.toName(name), name_cxt, force)
483  else:
484  self.rebind(name, name_cxt, force)
485  return
486 
487 
488 
505  def rebindContextRecursive(self, context, name_list, name_cxt):
506  self.rebindRecursive(context, name_list, name_cxt)
507  return
508 
509 
510 
537  def resolve(self, name):
538  if isinstance(name, basestring):
539  name_ = self.toName(name)
540  else:
541  name_ = name
542 
543  try:
544  obj = self._rootContext.resolve(name_)
545  return obj
546  except CosNaming.NamingContext.NotFound, ex:
547  return None
548 
549 
550 
577  def unbind(self, name):
578  if isinstance(name, basestring):
579  name_ = self.toName(name)
580  else:
581  name_ = name
582 
583  self._rootContext.unbind(name_)
584  return
585 
586 
587 
602  def newContext(self):
603  return self._rootContext.new_context()
604 
605 
606 
632  def bindNewContext(self, name, force=True):
633  if force is None:
634  force = True
635 
636  if isinstance(name, basestring):
637  name_ = self.toName(name)
638  else:
639  name_ = name
640 
641  try:
642  return self._rootContext.bind_new_context(name_)
643  except CosNaming.NamingContext.NotFound:
644  if force:
645  self.bindRecursive(self._rootContext, name_, self.newContext())
646  else:
647  raise
648  except CosNaming.NamingContext.CannotProceed, err:
649  if force:
650  self.bindRecursive(err.cxt, err.rest_of_name, self.newContext())
651  else:
652  raise
653  return None
654 
655 
656 
687  def destroy(self, context):
688  context.destroy()
689 
690 
691 
710  def destroyRecursive(self, context):
711  cont = True
712  bl = []
713  bi = 0
714  bl, bi = context.list(self._blLength)
715  while cont:
716  for i in range(len(bl)):
717  if bl[i].binding_type == CosNaming.ncontext:
718  obj = context.resolve(bl[i].binding_name)
719  next_context = obj._narrow(CosNaming.NamingContext)
720 
721  self.destroyRecursive(next_context)
722  context.unbind(bl[i].binding_name)
723  next_context.destroy()
724  elif bl[i].binding_type == CosNaming.nobject:
725  context.unbind(bl[i].binding_name)
726  else:
727  assert(0)
728  if CORBA.is_nil(bi):
729  cont = False
730  else:
731  bi.next_n(self._blLength, bl)
732 
733  if not (CORBA.is_nil(bi)):
734  bi.destroy()
735  return
736 
737 
738 
749  def clearAll(self):
750  self.destroyRecursive(self._rootContext)
751  return
752 
753 
754 
768  def list(self, name_cxt, how_many, rbl, rbi):
769  bl, bi = name_cxt.list(how_many)
770 
771  for i in bl:
772  rbl.append(bl)
773 
774  rbi.append(bi)
775 
776 
777  #============================================================
778  # interface of NamingContext
779  #============================================================
780 
781 
797  def toString(self, name_list):
798  if len(name_list) == 0:
799  raise CosNaming.NamingContext.InvalidName
800 
801  slen = self.getNameLength(name_list)
802  string_name = [""]
803  self.nameToString(name_list, string_name, slen)
804 
805  return string_name
806 
807 
808 
824  def toName(self, sname):
825  if not sname:
826  raise CosNaming.NamingContext.InvalidName
827 
828  string_name = sname
829  name_comps = []
830 
831  nc_length = 0
832  nc_length = self.split(string_name, "/", name_comps)
833  if not (nc_length > 0):
834  raise CosNaming.NamingContext.InvalidName
835 
836  name_list = [CosNaming.NameComponent("","") for i in range(nc_length)]
837 
838  for i in range(nc_length):
839  pos = string.rfind(name_comps[i][0:],".")
840  if pos == -1:
841  name_list[i].id = name_comps[i]
842  name_list[i].kind = ""
843  else:
844  name_list[i].id = name_comps[i][0:pos]
845  name_list[i].kind = name_comps[i][(pos+1):]
846 
847  return name_list
848 
849 
850 
868  def toUrl(self, addr, string_name):
869  return self._rootContext.to_url(addr, string_name)
870 
871 
872 
891  def resolveStr(self, string_name):
892  return self.resolve(self.toName(string_name))
893 
894 
895  #============================================================
896  # Find functions
897  #============================================================
898 
899 
919  def bindOrResolve(self, context, name_list, obj):
920  try:
921  context.bind_context(name_list, obj)
922  return obj
923  except CosNaming.NamingContext.AlreadyBound:
924  obj = context.resolve(name_list)
925  return obj
926  return CORBA.Object._nil
927 
928 
929 
950  def bindOrResolveContext(self, context, name_list, new_context=None):
951  if new_context is None:
952  new_cxt = self.newContext()
953  else:
954  new_cxt = new_context
955 
956  obj = self.bindOrResolve(context, name_list, new_cxt)
957  return obj._narrow(CosNaming.NamingContext)
958 
959 
960 
973  def getNameServer(self):
974  return self._nameServer
975 
976 
977 
990  def getRootContext(self):
991  return self._rootContext
992 
993 
994 
1008  def objIsNamingContext(self, obj):
1009  nc = obj._narrow(CosNaming.NamingContext)
1010  if CORBA.is_nil(nc):
1011  return False
1012  else:
1013  return True
1014 
1015 
1016 
1031  def nameIsNamingContext(self, name_list):
1032  return self.objIsNamingContext(self.resolve(name_list))
1033 
1034 
1035 
1053  def subName(self, name_list, begin, end = None):
1054  if end is None or end < 0:
1055  end = len(name_list) - 1
1056 
1057  sub_len = end - (begin -1)
1058  objId = ""
1059  kind = ""
1060 
1061  sub_name = []
1062  for i in range(sub_len):
1063  sub_name.append(name_list[begin + i])
1064 
1065  return sub_name
1066 
1067 
1068 
1087  def nameToString(self, name_list, string_name, slen):
1088  for i in range(len(name_list)):
1089  for id_ in name_list[i].id:
1090  if id_ == "/" or id_ == "." or id_ == "\\":
1091  string_name[0] += "\\"
1092  string_name[0] += id_
1093 
1094  if name_list[i].id == "" or name_list[i].kind != "":
1095  string_name[0] += "."
1096 
1097  for kind_ in name_list[i].kind:
1098  if kind_ == "/" or kind_ == "." or kind_ == "\\":
1099  string_name[0] += "\\"
1100  string_name[0] += kind_
1101 
1102  string_name[0] += "/"
1103 
1104 
1105 
1122  def getNameLength(self, name_list):
1123  slen = 0
1124 
1125  for i in range(len(name_list)):
1126  for id_ in name_list[i].id:
1127  if id_ == "/" or id_ == "." or id_ == "\\":
1128  slen += 1
1129  slen += 1
1130  if name_list[i].id == "" or name_list[i].kind == "":
1131  slen += 1
1132 
1133  for kind_ in name_list[i].kind:
1134  if kind_ == "/" or kind_ == "." or kind_ == "\\":
1135  slen += 1
1136  slen += 1
1137 
1138  slen += 1
1139 
1140  return slen
1141 
1142 
1143 
1159  def split(self, input, delimiter, results):
1160  delim_size = len(delimiter)
1161  found_pos = begin_pos = pre_pos = substr_size = 0
1162 
1163  if input[0:delim_size] == delimiter:
1164  begin_pos = pre_pos = delim_size
1165 
1166  while 1:
1167  found_pos = string.find(input[begin_pos:],delimiter)
1168 
1169  if found_pos == -1:
1170  results.append(input[pre_pos:])
1171  break
1172 
1173  if found_pos > 0 and input[found_pos - 1] == "\\":
1174  begin_pos += found_pos + delim_size
1175  else:
1176  substr_size = found_pos + (begin_pos - pre_pos)
1177  if substr_size > 0:
1178  results.append(input[pre_pos:(pre_pos+substr_size)])
1179  begin_pos += found_pos + delim_size
1180  pre_pos = begin_pos
1181 
1182  return len(results)
def nameToString(self, name_list, string_name, slen)
Get string representation of name component.
def bindNewContext(self, name, force=True)
def bindByString(self, string_name, obj, force=True)
def toUrl(self, addr, string_name)
Get URL representation from given addr and string_name.
def unbind(self, name)
void unbind(const CosNaming::Name& name) throw(NotFound, CannotProceed, InvalidName); ...
def nameIsNamingContext(self, name_list)
Whether the given name component is NamingContext.
def subName(self, name_list, begin, end=None)
Get subset of given name component.
def bindContext(self, name, name_cxt, force=True)
def bindOrResolveContext(self, context, name_list, new_context=None)
Bind of resolve the given name component.
def getRootContext(self)
Get the root context.
def getNameServer(self)
Get the name of naming server.
def clearAll(self)
Destroy all binding.
def rebindContext(self, name, name_cxt, force=True)
def objIsNamingContext(self, obj)
Whether the object is NamingContext.
def toName(self, sname)
Get NameComponent from gien string name representation.
def toString(self, name_list)
Get string representation of given NameComponent.
def rebind(self, name_list, obj, force=True)
def split(self, input, delimiter, results)
Split of string.
def list(self, name_cxt, how_many, rbl, rbi)
def init(self, name_server)
def __init__(self, orb, name_server=None)
Consructor.
def rebindContextRecursive(self, context, name_list, name_cxt)
def resolveStr(self, string_name)
Resolve from name of string representation and get object.
def bind(self, name_list, obj, force=None)
def bindContextRecursive(self, context, name_list, name_cxt)
def rebindByString(self, string_name, obj, force=True)
def getNameLength(self, name_list)
Get string length of the name component&#39;s string representation.
def __del__(self)
destructor
def bindRecursive(self, context, name_list, obj)
def bindOrResolve(self, context, name_list, obj)
Bind of resolve the given name component.
def destroyRecursive(self, context)
Destroy the naming context recursively.
def rebindRecursive(self, context, name_list, obj)


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58