CorbaNaming.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 # -*- Python -*-
00004 
00005 
00006 ##
00007 # \file CorbaNaming.py
00008 # \brief CORBA naming service helper class
00009 # \author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara
00010 #
00011 # Copyright (C) 2006-2008
00012 #     Noriaki Ando
00013 #     Task-intelligence Research Group,
00014 #     Intelligent Systems Research Institute,
00015 #     National Institute of
00016 #         Advanced Industrial Science and Technology (AIST), Japan
00017 #     All rights reserved.
00018 
00019 import omniORB.CORBA as CORBA
00020 import CosNaming
00021 import string
00022 
00023 ##
00024 # @if jp
00025 # @class CorbaNaming
00026 # @brief CORBA Naming Service ヘルパークラス
00027 #
00028 # このクラスは、CosNaming::NamingContext に対するラッパークラスである。
00029 # CosNaming::NamingContext が持つオペレーションとほぼ同じ機能の
00030 # オペレーションを提供するとともに、ネームコンポーネント CosNaming::Name
00031 # の代わりに文字列による名前表現を受け付けるオペレーションも提供する。
00032 #
00033 # オブジェクトは生成時、あるいは生成直後に CORBA ネームサーバに接続し
00034 # 以後、このネームサーバのルートコンテキストに対して種々のオペレーション
00035 # を処理する。
00036 # 深い階層のネーミングコンテキストの作成やオブジェクトのバインドにおいて、
00037 # 途中のコンテキストが存在しない場合でも、強制的にコンテキストをバインド
00038 # し目的のコンテキストやオブジェクトのバインドを行うこともできる。
00039 #
00040 # @since 0.4.0
00041 #
00042 # @else
00043 # @class CorbaNaming
00044 # @brief CORBA Naming Service helper class
00045 #
00046 # This class is a wrapper class of CosNaming::NamingContext.
00047 # Almost the same operations which CosNaming::NamingContext has are
00048 # provided, and some operation allows string naming representation of
00049 # context and object instead of CosNaming::Name.
00050 #
00051 # The object of the class would connect to a CORBA naming server at
00052 # the instantiation or immediately after instantiation.
00053 # After that the object invokes operations to the root context of it.
00054 # This class realizes forced binding to deep NamingContext, without binding
00055 # intermediate NamingContexts explicitly.
00056 #
00057 # @since 0.4.0
00058 #
00059 # @endif
00060 class CorbaNaming:
00061   """
00062   """
00063 
00064 
00065 
00066   ##
00067   # @if jp
00068   #
00069   # @brief コンストラクタ
00070   #
00071   # @param self
00072   # @param orb ORB
00073   # @param name_server ネームサーバの名称(デフォルト値:None)
00074   #
00075   # @else
00076   #
00077   # @brief Consructor
00078   #
00079   # @endif
00080   def __init__(self, orb, name_server=None):
00081     self._orb = orb
00082     self._nameServer = ""
00083     self._rootContext = CosNaming.NamingContext._nil
00084     self._blLength = 100
00085 
00086     if name_server:
00087       self._nameServer = "corbaloc::" + name_server + "/NameService"
00088       try:
00089         obj = orb.string_to_object(self._nameServer)
00090         self._rootContext = obj._narrow(CosNaming.NamingContext)
00091         if CORBA.is_nil(self._rootContext):
00092           print "CorbaNaming: Failed to narrow the root naming context."
00093 
00094       except CORBA.ORB.InvalidName:
00095         print "Service required is invalid [does not exist]."
00096 
00097     return
00098   
00099 
00100   ##
00101   # @if jp
00102   #
00103   # @brief デストラクタ
00104   # 
00105   # @param self
00106   # 
00107   # @else
00108   # 
00109   # @brief destructor
00110   # 
00111   # @endif
00112   def __del__(self):
00113     return
00114 
00115 
00116   ##
00117   # @if jp
00118   #
00119   # @brief ネーミングサービスの初期化
00120   # 
00121   # 指定されたネームサーバ上のネーミングサービスを初期化します。
00122   # 
00123   # @param self
00124   # @param name_server ネームサーバの名称
00125   # 
00126   # @else
00127   # 
00128   # @endif
00129   def init(self, name_server):
00130     self._nameServer = "corbaloc::" + name_server + "/NameService"
00131     obj = self._orb.string_to_object(self._nameServer)
00132     self._rootContext = obj._narrow(CosNaming.NamingContext)
00133     if CORBA.is_nil(self._rootContext):
00134       raise MemoryError
00135 
00136     return
00137 
00138 
00139   ##
00140   # @if jp
00141   #
00142   # @brief Object を bind する
00143   #
00144   # CosNaming::bind() とほぼ同等の働きをするが、常に与えられたネームサーバの
00145   # ルートコンテキストに対してbind()が呼び出される点が異なる。
00146   #
00147   # Name <name> と Object <obj> を当該 NamingContext 上にバインドする。
00148   # c_n が n 番目の NameComponent をあらわすとすると、
00149   # name が n 個の NameComponent から成るとき、以下のように扱われる。
00150   #
00151   # cxt->bind(<c_1, c_2, ... c_n>, obj) は以下の操作と同等である。
00152   # cxt->resolve(<c_1, ... c_(n-1)>)->bind(<c_n>, obj)
00153   #
00154   # すなわち、1番目からn-1番目のコンテキストを解決し、n-1番目のコンテキスト
00155   # 上に name <n> として obj を bind する。
00156   # 名前解決に参加する <c_1, ... c_(n-1)> の NemingContext は、
00157   # bindContext() や rebindContext() で既にバインド済みでなければならない。
00158   # もし <c_1, ... c_(n-1)> の NamingContext が存在しない場合には、
00159   # NotFound 例外が発生する。
00160   #
00161   # ただし、強制バインドフラグ force が true の時は、<c_1, ... c_(n-1)>
00162   # が存在しない場合にも、再帰的にコンテキストをバインドしながら、
00163   # 最終的に obj を名前 name <c_n> にバインドする。
00164   #
00165   # いずれの場合でも、n-1番目のコンテキスト上に name<n> のオブジェクト
00166   # (Object あるいは コンテキスト) がバインドされていれば
00167   # AlreadyBound 例外が発生する。
00168   #
00169   # @param self
00170   # @param name_list オブジェクトに付ける名前の NameComponent
00171   # @param obj 関連付けられる Object
00172   # @param force trueの場合、途中のコンテキストを強制的にバインドする
00173   #              (デフォルト値:None)
00174   #
00175   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00176   # @exception CannotProceed 何らかの理由で処理を継続できない。
00177   # @exception InvalidName 引数 name_list の名前が不正。
00178   # @exception AlreadyBound name <c_n> の Object がすでにバインドされている。
00179   #
00180   # @else
00181   #
00182   # @brief
00183   #
00184   # @endif
00185   def bind(self, name_list, obj, force=None):
00186     if force is None :
00187       force = True
00188 
00189     try:
00190       self._rootContext.bind(name_list, obj)
00191     except CosNaming.NamingContext.NotFound:
00192       if force:
00193         self.bindRecursive(self._rootContext, name_list, obj)
00194       else:
00195         raise
00196     except CosNaming.NamingContext.CannotProceed, err:
00197       if force:
00198         self.bindRecursive(err.cxt, err.rest_of_name, obj)
00199       else:
00200         raise
00201     except CosNaming.NamingContext.AlreadyBound:
00202       self._rootContext.rebind(name_list, obj)
00203 
00204 
00205   ##
00206   # @if jp
00207   #
00208   # @brief Object を bind する
00209   #
00210   # Object を bind する際に与える名前が文字列表現であること以外は、bind()
00211   # と同じである。bind(toName(string_name), obj) と等価。
00212   #
00213   # @param self
00214   # @param string_name オブジェクトに付ける名前の文字列表現
00215   # @param obj 関連付けられるオブジェクト
00216   # @param force trueの場合、途中のコンテキストを強制的にバインドする
00217   #              (デフォルト値:true)
00218   #
00219   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00220   # @exception CannotProceed 何らかの理由で処理を継続できない。
00221   # @exception InvalidName 引数 string_name の名前が不正。
00222   # @exception AlreadyBound name <n> の Object がすでにバインドされている。
00223   #
00224   # @else
00225   #
00226   # @brief
00227   #
00228   # @endif
00229   def bindByString(self, string_name, obj, force=True):
00230     self.bind(self.toName(string_name), obj, force)
00231 
00232 
00233   ##
00234   # @if jp
00235   #
00236   # @brief 途中のコンテキストを bind しながら Object を bind する
00237   #
00238   # context で与えられた NamingContext に対して、name で指定された
00239   # ネームコンポーネント <c_1, ... c_(n-1)> を NamingContext として
00240   # 解決しながら、名前 <c_n> に対して obj を bind する。
00241   # もし、<c_1, ... c_(n-1)> に対応する NamingContext がない場合には
00242   # 新たな NamingContext をバインドする。
00243   #
00244   # 最終的に <c_1, c_2, ..., c_(n-1)> に対応する NamingContext が生成
00245   # または解決された上で、CosNaming::bind(<c_n>, object) が呼び出される。
00246   # このとき、すでにバインディングが存在すれば AlreadyBound例外が発生する。
00247   #
00248   # 途中のコンテキストを解決する過程で、解決しようとするコンテキストと
00249   # 同じ名前の NamingContext ではない Binding が存在する場合、
00250   # CannotProceed 例外が発生し処理を中止する。
00251   #
00252   # @param self
00253   # @param context bind を開始する NamingContext
00254   # @param name_list オブジェクトに付ける名前のネームコンポーネント
00255   # @param obj 関連付けられるオブジェクト
00256   #
00257   # @exception CannotProceed <c_1, ..., c_(n-1)> に対応する NamingContext 
00258   #            のうちひとつが、すでに NamingContext 以外の object にバインド
00259   #            されており、処理を継続できない。
00260   # @exception InvalidName 名前 name_list が不正
00261   # @exception AlreadyBound name <c_n> にすでに何らかの object がバインド
00262   #            されている。
00263   # @else
00264   #
00265   # @brief
00266   #
00267   # @endif
00268   def bindRecursive(self, context, name_list, obj):
00269     length = len(name_list)
00270     cxt = context
00271     for i in range(length):
00272       if i == length -1:
00273         try:
00274           cxt.bind(self.subName(name_list, i, i), obj)
00275         except CosNaming.NamingContext.AlreadyBound:
00276           cxt.rebind(self.subName(name_list, i, i), obj)
00277         return
00278       else:
00279         if self.objIsNamingContext(cxt):
00280           cxt = self.bindOrResolveContext(cxt,self.subName(name_list, i, i))
00281         else:
00282           raise CosNaming.NamingContext.CannotProceed(cxt, self.subName(name_list, i))
00283     return
00284 
00285 
00286   ##
00287   # @if jp
00288   #
00289   # @brief Object を rebind する
00290   #
00291   # name_list で指定された Binding がすでに存在する場合を除いて bind() と同じ
00292   # である。バインディングがすでに存在する場合には、新しいバインディングに
00293   # 置き換えられる。
00294   #
00295   # @param self
00296   # @param name_list オブジェクトに付ける名前の NameComponent
00297   # @param obj 関連付けられるオブジェクト
00298   # @param force trueの場合、途中のコンテキストを強制的にバインドする
00299   #              (デフォルト値:true)
00300   #
00301   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00302   # @exception CannotProceed 何らかの理由で処理を継続できない。
00303   # @exception InvalidName 名前 name_list が不正
00304   #
00305   # @else
00306   #
00307   # @brief
00308   #
00309   # @endif
00310   def rebind(self, name_list, obj, force=True):
00311     if force is None:
00312       force = True
00313       
00314     try:
00315       self._rootContext.rebind(name_list, obj)
00316 
00317     except CosNaming.NamingContext.NotFound:
00318       if force:
00319         self.rebindRecursive(self._rootContext, name_list, obj)
00320       else:
00321         raise
00322 
00323     except CosNaming.NamingContext.CannotProceed, err:
00324       if force:
00325         self.rebindRecursive(err.cxt, err,rest_of_name, obj)
00326       else:
00327         raise
00328       
00329     return
00330 
00331 
00332   ##
00333   # @if jp
00334   #
00335   # @brief Object を rebind する
00336   #
00337   # Object を rebind する際に与える名前が文字列表現であること以外は rebind()
00338   # と同じである。rebind(toName(string_name), obj) と等価。
00339   #
00340   # @param self
00341   # @param string_name オブジェクトに付ける名前の文字列表現
00342   # @param obj 関連付けられるオブジェクト
00343   # @param force trueの場合、途中のコンテキストを強制的にバインドする
00344   #              (デフォルト値:true)
00345   #
00346   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00347   # @exception CannotProceed 何らかの理由で処理を継続できない。
00348   # @exception InvalidName 引数 string_name の名前が不正。
00349   #
00350   # @else
00351   #
00352   # @brief
00353   #
00354   # @endif
00355   def rebindByString(self, string_name, obj, force=True):
00356     self.rebind(self.toName(string_name), obj, force)
00357 
00358     return
00359 
00360 
00361   ##
00362   # @if jp
00363   #
00364   # @brief 途中のコンテキストを bind しながら Object を rebind する
00365   #
00366   # name_list <c_n> で指定された NamingContext もしくは Object がすでに存在する
00367   # 場合を除いて bindRecursive() と同じである。
00368   #
00369   # name_list <c_n> で指定されたバインディングがすでに存在する場合には、
00370   # 新しいバインディングに置き換えられる。
00371   #
00372   # @param self
00373   # @param context bind を開始する NamingContext
00374   # @param name_list オブジェクトに付ける名前の NameComponent
00375   # @param obj 関連付けられるオブジェクト
00376   #
00377   # @exception CannotProceed 途中のコンテキストが解決できない。
00378   # @exception InvalidName 与えられた name_list が不正。
00379   #
00380   # @else
00381   #
00382   # @brief
00383   #
00384   # @endif
00385   def rebindRecursive(self, context, name_list, obj):
00386     length = len(name_list)
00387     for i in range(length):
00388       if i == length - 1:
00389         context.rebind(self.subName(name_list, i, i), obj)
00390         return
00391       else:
00392         if self.objIsNamingContext(context):
00393           try:
00394             context = context.bind_new_context(self.subName(name_list, i, i))
00395           except CosNaming.NamingContext.AlreadyBound:
00396             obj_ = context.resolve(self.subName(name_list, i, i))
00397             context = obj_._narrow(CosNaming.NamingContext)
00398         else:
00399           raise CosNaming.NamingContext.CannotProceed(context, self.subName(name_list, i))
00400     return
00401 
00402 
00403   ##
00404   # @if jp
00405   #
00406   # @brief NamingContext を bind する
00407   #
00408   # bind 対象として指定された引数 name が文字列の場合は bindByString() と、
00409   # それ以外の場合は bind() と同じである。
00410   #
00411   # @param self
00412   # @param name オブジェクトに付ける名前
00413   # @param name_cxt 関連付けられる NamingContext
00414   # @param force trueの場合、途中のコンテキストを強制的にバインドする
00415   #              (デフォルト値:True)
00416   #
00417   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00418   # @exception CannotProceed 何らかの理由で処理を継続できない。
00419   # @exception InvalidName 引数 name の名前が不正。
00420   # @exception AlreadyBound name <c_n> の Object がすでにバインドされている。
00421   #
00422   # @else
00423   #
00424   # @brief
00425   #
00426   # @endif
00427   def bindContext(self, name, name_cxt, force=True):
00428     if isinstance(name, basestring):
00429       self.bind(self.toName(name), name_cxt, force)
00430     else:
00431       self.bind(name, name_cxt, force)
00432     return
00433 
00434 
00435   ##
00436   # @if jp
00437   #
00438   # @brief NamingContext を bind する
00439   #
00440   # bind されるオブジェクトが NamingContext であることを除いて
00441   # bindRecursive() と同じである。
00442   #
00443   # @param self
00444   # @param context bind を開始する NamingContext
00445   # @param name_list オブジェクトに付ける名前のネームコンポーネント
00446   # @param name_cxt 関連付けられる NamingContext
00447   #
00448   # @else
00449   #
00450   # @brief
00451   #
00452   # @endif
00453   def bindContextRecursive(self, context, name_list, name_cxt):
00454     self.bindRecursive(context, name_list, name_cxt)
00455     return
00456 
00457 
00458   ##
00459   # @if jp
00460   #
00461   # @brief NamingContext を rebind する
00462   #
00463   # bind 対象として指定された引数 name が文字列の場合は rebindByString() と、
00464   # それ以外の場合は rebind() と同じである。
00465   # どちらの場合もバインディングがすでに存在する場合には、
00466   # 新しいバインディングに置き換えられる。
00467   #
00468   # @param self
00469   # @param name オブジェクトに付ける名前のネームコンポーネント
00470   # @param name_cxt 関連付けられる NamingContext
00471   # @param force trueの場合、途中のコンテキストを強制的にバインドする
00472   #              (デフォルト値:true)
00473   #
00474   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00475   # @exception CannotProceed 何らかの理由で処理を継続できない。
00476   # @exception InvalidName 引数 name の名前が不正。
00477   #
00478   # @else
00479   #
00480   # @endif
00481   def rebindContext(self, name, name_cxt, force=True):
00482     if isinstance(name, basestring):
00483       self.rebind(self.toName(name), name_cxt, force)
00484     else:
00485       self.rebind(name, name_cxt, force)
00486     return
00487 
00488 
00489   ##
00490   # @if jp
00491   #
00492   # @brief 途中のコンテキストを再帰的に rebind し NamingContext を rebind する    #
00493   # bind されるオブジェクトが NamingContext であることを除いて
00494   # rebindRecursive() と同じである。
00495   #
00496   # @param self
00497   # @param context bind を開始する NamingContext
00498   # @param name_list オブジェクトに付ける名前の NameComponent
00499   # @param name_cxt 関連付けられる NamingContext
00500   #
00501   # @else
00502   #
00503   # @brief
00504   #
00505   # @endif
00506   def rebindContextRecursive(self, context, name_list, name_cxt):
00507     self.rebindRecursive(context, name_list, name_cxt)
00508     return
00509 
00510 
00511   ##
00512   # @if jp
00513   #
00514   # @brief Object を name から解決する
00515   #
00516   # name に bind されているオブジェクト参照を返す。
00517   # ネームコンポーネント <c_1, c_2, ... c_n> は再帰的に解決される。
00518   # 
00519   # 引数 name に与えられた値が文字列の場合にはまず最初に toName() によって
00520   # NameComponent に変換される。
00521   # 
00522   # CosNaming::resolve() とほぼ同等の働きをするが、常に与えられた
00523   # ネームサーバのルートコンテキストに対して resolve() が呼び出される点が
00524   # 異なる。
00525   #
00526   # @param self
00527   # @param name 解決すべきオブジェクトの名前のネームコンポーネント
00528   #
00529   # @return 解決されたオブジェクト参照
00530   #
00531   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00532   # @exception CannotProceed 何らかの理由で処理を継続できない。
00533   # @exception InvalidName 引数 name の名前が不正。
00534   #
00535   # @else
00536   #
00537   # @endif
00538   def resolve(self, name):
00539     if isinstance(name, basestring):
00540       name_ = self.toName(name)
00541     else:
00542       name_ = name
00543       
00544     try:
00545       obj = self._rootContext.resolve(name_)
00546       return obj
00547     except CosNaming.NamingContext.NotFound, ex:
00548       return None
00549 
00550 
00551   ##
00552   # @if jp
00553   #
00554   # @brief 指定された名前のオブジェクトの bind を解除する
00555   #
00556   # name に bind されているオブジェクト参照を解除する。
00557   # ネームコンポーネント <c_1, c_2, ... c_n> は再帰的に解決される。
00558   # 
00559   # 引数 name に与えられた値が文字列の場合にはまず最初に toName() によって
00560   # NameComponent に変換される。
00561   # 
00562   # CosNaming::unbind() とほぼ同等の働きをするが、常に与えられた
00563   # ネームサーバのルートコンテキストに対して unbind() が呼び出される点が
00564   # 異なる。
00565   #
00566   # @param self
00567   # @param name 削除するオブジェクトのネームコンポーネント
00568   #
00569   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00570   # @exception CannotProceed 何らかの理由で処理を継続できない。
00571   # @exception InvalidName 引数 name の名前が不正。
00572   #
00573   # @else
00574   #
00575   # @endif
00576   # void unbind(const CosNaming::Name& name)
00577   #   throw(NotFound, CannotProceed, InvalidName);
00578   def unbind(self, name):
00579     if isinstance(name, basestring):
00580       name_ = self.toName(name)
00581     else:
00582       name_ = name
00583 
00584     self._rootContext.unbind(name_)
00585     return
00586 
00587 
00588   ##
00589   # @if jp
00590   #
00591   # @brief 新しいコンテキストを生成する
00592   #
00593   # 与えられたネームサーバ上で生成された NamingContext を返す。
00594   # 返された NamingContext は bind されていない。
00595   # 
00596   # @param self
00597   # 
00598   # @return 生成された新しい NamingContext
00599   #
00600   # @else
00601   #
00602   # @endif
00603   def newContext(self):
00604     return self._rootContext.new_context()
00605 
00606 
00607   ##
00608   # @if jp
00609   #
00610   # @brief 新しいコンテキストを bind する
00611   #
00612   # 与えられた name に対して新しいコンテキストをバインドする。
00613   # 生成された NamingContext はネームサーバ上で生成されたものである。
00614   # 
00615   # 引数 name に与えられた値が文字列の場合にはまず最初に toName() によって
00616   # NameComponent に変換される。
00617   # 
00618   # @param self
00619   # @param name NamingContextに付ける名前のネームコンポーネント
00620   # @param force trueの場合、途中のコンテキストを強制的にバインドする
00621   #              (デフォルト値:true)
00622   #
00623   # @return 生成された新しい NamingContext
00624   #
00625   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00626   # @exception CannotProceed 何らかの理由で処理を継続できない。
00627   # @exception InvalidName 引数 name の名前が不正。
00628   # @exception AlreadyBound name <n> の Object がすでにバインドされている。
00629   #
00630   # @else
00631   #
00632   # @endif
00633   def bindNewContext(self, name, force=True):
00634     if force is None:
00635       force = True
00636       
00637     if isinstance(name, basestring):
00638       name_ = self.toName(name)
00639     else:
00640       name_ = name
00641 
00642     try:
00643       return self._rootContext.bind_new_context(name_)
00644     except CosNaming.NamingContext.NotFound:
00645       if force:
00646         self.bindRecursive(self._rootContext, name_, self.newContext())
00647       else:
00648         raise
00649     except CosNaming.NamingContext.CannotProceed, err:
00650       if force:
00651         self.bindRecursive(err.cxt, err.rest_of_name, self.newContext())
00652       else:
00653         raise
00654     return None
00655 
00656 
00657   ##
00658   # @if jp
00659   #
00660   # @brief NamingContext を非アクティブ化する
00661   #
00662   # context で指定された NamingContext を非アクティブ化する。
00663   # context に他のコンテキストがバインドされている場合は NotEmpty 例外が
00664   # 発生する。
00665   # 
00666   # @param self
00667   # @param context 非アクティブ化する NamingContext
00668   #
00669   # @exception NotEmpty 対象context に他のコンテキストがバインドされている。
00670   #
00671   # @else
00672   #
00673   # @else
00674   #
00675   # @brief Destroy the naming context
00676   #
00677   # Delete the specified naming context.
00678   # any bindings should be <unbind> in which the given context is bound to
00679   # some names before invoking <destroy> operation on it. 
00680   #
00681   # @param context NamingContext which is destroied.
00682   #     
00683   # @exception NotEmpty 
00684   #
00685   # @else
00686   #
00687   # @endif
00688   def destroy(self, context):
00689     context.destroy()
00690 
00691 
00692   ##
00693   # @if jp
00694   # @brief NamingContext を再帰的に下って非アクティブ化する
00695   #
00696   # context で与えられた NamingContext に対して、name で指定された
00697   # ネームコンポーネント <c_1, ... c_(n-1)> を NamingContext として
00698   # 解決しながら、名前 <c_n> に対して 非アクティブ化を行う。
00699   #
00700   # @param self
00701   # @param context 非アクティブ化する NamingContext
00702   #
00703   # @exception NotEmpty 対象context に他のコンテキストがバインドされている。
00704   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00705   # @exception CannotProceed 何らかの理由で処理を継続できない。
00706   # @exception InvalidName 引数 name の名前が不正。
00707   #
00708   # @else
00709   # @brief Destroy the naming context recursively
00710   # @endif
00711   def destroyRecursive(self, context):
00712     cont = True
00713     bl = []
00714     bi = 0
00715     bl, bi = context.list(self._blLength)
00716     while cont:
00717       for i in range(len(bl)):
00718         if bl[i].binding_type == CosNaming.ncontext:
00719           obj = context.resolve(bl[i].binding_name)
00720           next_context = obj._narrow(CosNaming.NamingContext)
00721 
00722           self.destroyRecursive(next_context)
00723           context.unbind(bl[i].binding_name)
00724           next_context.destroy()
00725         elif bl[i].binding_type == CosNaming.nobject:
00726           context.unbind(bl[i].binding_name)
00727         else:
00728           assert(0)
00729       if CORBA.is_nil(bi):
00730         cont = False
00731       else:
00732         bi.next_n(self._blLength, bl)
00733 
00734     if not (CORBA.is_nil(bi)):
00735       bi.destroy()
00736     return
00737 
00738 
00739   ##
00740   # @if jp
00741   # @brief すべての Binding を削除する
00742   #
00743   # 登録されている全てのBinding を削除する。
00744   #
00745   # @param self
00746   #
00747   # @else
00748   # @brief Destroy all binding
00749   # @endif
00750   def clearAll(self):
00751     self.destroyRecursive(self._rootContext)
00752     return
00753 
00754 
00755   ##
00756   # @if jp
00757   # @brief 与えられた NamingContext の Binding を取得する
00758   #
00759   # 指定された NamingContext の Binding を取得する。
00760   #
00761   # @param self
00762   # @param name_cxt Binding 取得対象 NamingContext
00763   # @param how_many Binding を取得する階層の深さ
00764   # @param rbl 取得した Binding を保持するホルダ
00765   # @param rbi 取得した Binding をたどるためのイテレータ
00766   #
00767   # @else
00768   # @endif
00769   def list(self, name_cxt, how_many, rbl, rbi):
00770     bl, bi = name_cxt.list(how_many)
00771 
00772     for i in bl:
00773       rbl.append(bl)
00774 
00775     rbi.append(bi)
00776   
00777 
00778   #============================================================
00779   # interface of NamingContext
00780   #============================================================
00781 
00782   ##
00783   # @if jp
00784   # @brief 与えられた NameComponent の文字列表現を返す
00785   #
00786   # 指定された NameComponent を文字に変換する。
00787   #
00788   # @param self
00789   # @param name_list 変換対象 NameComponent
00790   #
00791   # @return 文字列変換結果
00792   #
00793   # @exception InvalidName 引数 name_list の名前が不正。
00794   #
00795   # @else
00796   # @brief Get string representation of given NameComponent
00797   # @endif
00798   def toString(self, name_list):
00799     if len(name_list) == 0:
00800       raise CosNaming.NamingContext.InvalidName
00801 
00802     slen = self.getNameLength(name_list)
00803     string_name = [""]
00804     self.nameToString(name_list, string_name, slen)
00805 
00806     return string_name
00807 
00808 
00809   ##
00810   # @if jp
00811   # @brief 与えられた文字列表現を NameComponent に分解する
00812   #
00813   # 指定された文字列を NameComponent に変換する。
00814   #
00815   # @param self
00816   # @param sname 変換対象文字列
00817   #
00818   # @return NameComponent 変換結果
00819   #
00820   # @exception InvalidName 引数 sname が不正。
00821   #
00822   # @else
00823   # @brief Get NameComponent from gien string name representation
00824   # @endif
00825   def toName(self, sname):
00826     if not sname:
00827       raise CosNaming.NamingContext.InvalidName
00828 
00829     string_name = sname
00830     name_comps = []
00831 
00832     nc_length = 0
00833     nc_length = self.split(string_name, "/", name_comps)
00834     if not (nc_length > 0):
00835       raise CosNaming.NamingContext.InvalidName
00836 
00837     name_list = [CosNaming.NameComponent("","") for i in range(nc_length)]
00838 
00839     for i in range(nc_length):
00840       pos = string.rfind(name_comps[i][0:],".")
00841       if pos == -1:
00842         name_list[i].id   = name_comps[i]
00843         name_list[i].kind = ""
00844       else:
00845         name_list[i].id   = name_comps[i][0:pos]
00846         name_list[i].kind = name_comps[i][(pos+1):]
00847 
00848     return name_list
00849 
00850 
00851   ##
00852   # @if jp
00853   # @brief 与えられた addr と string_name から URL表現を取得する
00854   #
00855   # 指定されたアドレスと名称をURLに変換する。
00856   #
00857   # @param self
00858   # @param addr 変換対象アドレス
00859   # @param string_name 変換対象名称
00860   #
00861   # @return URL 変換結果
00862   #
00863   # @exception InvalidAddress 引数 addr が不正。
00864   # @exception InvalidName 引数 string_name が不正。
00865   #
00866   # @else
00867   # @brief Get URL representation from given addr and string_name
00868   # @endif
00869   def toUrl(self, addr, string_name):
00870     return self._rootContext.to_url(addr, string_name)
00871 
00872 
00873   ##
00874   # @if jp
00875   # @brief 与えられた文字列表現を resolve しオブジェクトを返す
00876   #
00877   # 指定された文字列表現をresolveし,オブジェクトを取得する。
00878   #
00879   # @param self
00880   # @param string_name 取得対象オブジェクト文字列表現
00881   #
00882   # @return 解決されたオブジェクト
00883   #
00884   # @exception NotFound 途中の <c_1, c_2, ..., c_(n-1)> が存在しない。
00885   # @exception CannotProceed 何らかの理由で処理を継続できない。
00886   # @exception InvalidName 引数 name の名前が不正。
00887   # @exception AlreadyBound name <n> の Object がすでにバインドされている。
00888   #
00889   # @else
00890   # @brief Resolve from name of string representation and get object 
00891   # @endif
00892   def resolveStr(self, string_name):
00893     return self.resolve(self.toName(string_name))
00894 
00895 
00896   #============================================================
00897   # Find functions
00898   #============================================================
00899 
00900   ##
00901   # @if jp
00902   #
00903   # @brief オブジェクトの名前をバインドまたは解決する
00904   #
00905   # 指定されたコンテキストに対してオブジェクトを NameComponent で指定された
00906   # 位置にバインドする。
00907   # 同一箇所に既に他の要素がバインド済みの場合は、既存のバインド済み要素を
00908   # 取得する。
00909   #
00910   # @param self
00911   # @param context bind もしくは resole 対象コンテキスト
00912   # @param name_list オブジェクトに付ける名前の NameComponent
00913   # @param obj 関連付けられる Object
00914   #
00915   # @return NameComponent で指定された位置にバインドされているオブジェクト
00916   #
00917   # @else
00918   # @brief Bind of resolve the given name component
00919   # @endif
00920   def bindOrResolve(self, context, name_list, obj):
00921     try:
00922       context.bind_context(name_list, obj)
00923       return obj
00924     except CosNaming.NamingContext.AlreadyBound:
00925       obj = context.resolve(name_list)
00926       return obj
00927     return CORBA.Object._nil
00928 
00929 
00930   ##
00931   # @if jp
00932   #
00933   # @brief コンテキストの名前をバインドまたは解決する
00934   #
00935   # 指定されたコンテキストに対して Contextを NameComponent で指定された位置に
00936   # バインドする。
00937   # Context が空の場合は新規コンテキストを生成してバインドする。
00938   # 同一箇所に既に他の要素がバインド済みの場合は、既存のバインド済み要素を
00939   # 取得する。
00940   #
00941   # @param self
00942   # @param context bind もしくは resole 対象コンテキスト
00943   # @param name_list コンテキストに付ける名前の NameComponent
00944   # @param new_context 関連付けられる Context(デフォルト値:None)
00945   #
00946   # @return NameComponent で指定された位置にバインドされているContext
00947   #
00948   # @else
00949   # @brief Bind of resolve the given name component
00950   # @endif
00951   def bindOrResolveContext(self, context, name_list, new_context=None):
00952     if new_context is None:
00953       new_cxt = self.newContext()
00954     else:
00955       new_cxt = new_context
00956 
00957     obj = self.bindOrResolve(context, name_list, new_cxt)
00958     return obj._narrow(CosNaming.NamingContext)
00959 
00960 
00961   ##
00962   # @if jp
00963   # @brief ネームサーバの名前を取得する
00964   #
00965   # 設定したネームサーバの名前を取得する。
00966   #
00967   # @param self
00968   #
00969   # @return ネームサーバの名前
00970   #
00971   # @else
00972   # @brief Get the name of naming server
00973   # @endif
00974   def getNameServer(self):
00975     return self._nameServer
00976 
00977 
00978   ##
00979   # @if jp
00980   # @brief ルートコンテキストを取得する
00981   #
00982   # 設定したネームサーバのルートコンテキストを取得する。
00983   #
00984   # @param self
00985   #
00986   # @return ネームサーバのルートコンテキスト
00987   #
00988   # @else
00989   # @brief Get the root context
00990   # @endif
00991   def getRootContext(self):
00992     return self._rootContext
00993 
00994 
00995   ##
00996   # @if jp
00997   # @brief オブジェクトがネーミングコンテキストか判別する
00998   #
00999   # 指定した要素がネーミングコンテキストか判別する
01000   #
01001   # @param self
01002   # @param obj 判別対象要素
01003   #
01004   # @return 判別結果(ネーミングコンテキスト:true、それ以外:false)
01005   #
01006   # @else
01007   # @brief Whether the object is NamingContext
01008   # @endif
01009   def objIsNamingContext(self, obj):
01010     nc = obj._narrow(CosNaming.NamingContext)
01011     if CORBA.is_nil(nc):
01012       return False
01013     else:
01014       return True
01015 
01016 
01017   ##
01018   # @if jp
01019   # @brief 与えられた名前がネーミングコンテキストかどうか判別する
01020   #
01021   # NameComponent もしくは文字列で指定した要素がネーミングコンテキストか
01022   # 判別する
01023   #
01024   # @param self
01025   # @param name_list 判別対象
01026   #
01027   # @return 判別結果(ネーミングコンテキスト:true、それ以外:false)
01028   #
01029   # @else
01030   # @brief Whether the given name component is NamingContext
01031   # @endif
01032   def nameIsNamingContext(self, name_list):
01033     return self.objIsNamingContext(self.resolve(name_list))
01034 
01035 
01036   ##
01037   # @if jp
01038   # @brief ネームコンポーネントの部分を返す
01039   #
01040   # 指定された範囲のネームコンポーネントを取得する。
01041   # 終了位置が指定されていない場合は、最後の要素を除いたネームコンポーネント
01042   # を返す。
01043   #
01044   # @param self
01045   # @param name_list 検索対象NameComponent
01046   # @param begin 取得範囲開始位置
01047   # @param end 取得範囲終了位置(デフォルト値:None)
01048   #
01049   # @return NameComponent 取得結果
01050   #
01051   # @else
01052   # @brief Get subset of given name component
01053   # @endif
01054   def subName(self, name_list, begin, end = None):
01055     if end is None or end < 0:
01056       end = len(name_list) - 1
01057 
01058     sub_len = end - (begin -1)
01059     objId = ""
01060     kind  = ""
01061     
01062     sub_name = []
01063     for i in range(sub_len):
01064       sub_name.append(name_list[begin + i])
01065 
01066     return sub_name
01067 
01068 
01069   ##
01070   # @if jp
01071   # @brief ネームコンポーネントの文字列表現を取得する
01072   #
01073   # 指定した範囲のネームコンポーネントの文字列表現を取得する。
01074   # 文字列表現は、NameComponentの構成が{Nc[0],Nc[1],Nc[2]・・・}の場合、
01075   #   Nc[0]id.Nc[0].kind/Nc[1]id.Nc[1].kind/Nc[2].id/Nc[2].kind・・・
01076   # という形式で取得できる。
01077   # 取得した文字列の長さが指定した長さ以上の場合は、
01078   # 指定した長さで切り捨てられる。
01079   #
01080   # @param self
01081   # @param name_list 取得対象NameComponent
01082   # @param string_name 取得結果文字列
01083   # @param slen 取得対象文字列最大値
01084   #
01085   # @else
01086   # @brief Get string representation of name component
01087   # @endif
01088   def nameToString(self, name_list, string_name, slen):
01089     for i in range(len(name_list)):
01090       for id_ in name_list[i].id:
01091         if id_ == "/" or id_ == "." or id_ == "\\":
01092           string_name[0] += "\\"
01093         string_name[0] += id_
01094 
01095       if name_list[i].id == "" or name_list[i].kind != "":
01096         string_name[0] += "."
01097 
01098       for kind_ in name_list[i].kind:
01099         if kind_ == "/" or kind_ == "." or kind_ == "\\":
01100           string_name[0] += "\\"
01101         string_name[0] += kind_
01102 
01103       string_name[0] += "/"
01104 
01105 
01106   ##
01107   # @if jp
01108   # @brief ネームコンポーネントの文字列表現時の文字長を取得する
01109   #
01110   # 指定したネームコンポーネントを文字列で表現した場合の長さを取得する。
01111   # 文字列表現は、NameComponentの構成が{Nc[0],Nc[1],Nc[2]・・・}の場合、
01112   #   Nc[0]id.Nc[0].kind/Nc[1]id.Nc[1].kind/Nc[2].id/Nc[2].kind・・・
01113   # という形式で取得できる。
01114   #
01115   # @param self
01116   # @param name_list 取得対象NameComponent
01117   #
01118   # @return 指定したネームコンポーネントの文字列長さ
01119   #
01120   # @else
01121   # @brief Get string length of the name component's string representation
01122   # @endif
01123   def getNameLength(self, name_list):
01124     slen = 0
01125 
01126     for i in range(len(name_list)):
01127       for id_ in name_list[i].id:
01128         if id_ == "/" or id_ == "." or id_ == "\\":
01129           slen += 1
01130         slen += 1
01131       if name_list[i].id == "" or name_list[i].kind == "":
01132         slen += 1
01133 
01134       for kind_ in name_list[i].kind:
01135         if kind_ == "/" or kind_ == "." or kind_ == "\\":
01136           slen += 1
01137         slen += 1
01138 
01139       slen += 1
01140 
01141     return slen
01142 
01143 
01144   ##
01145   # @if jp
01146   # @brief 文字列の分割
01147   #
01148   # 文字列を指定したデリミタで分割する。
01149   #
01150   # @param self
01151   # @param input 分割対象文字列
01152   # @param delimiter 分割用デリミタ
01153   # @param results 分割結果
01154   #
01155   # @return 分割した文字列の要素数
01156   #
01157   # @else
01158   # @brief Split of string
01159   # @endif
01160   def split(self, input, delimiter, results):
01161     delim_size = len(delimiter)
01162     found_pos = begin_pos = pre_pos = substr_size = 0
01163 
01164     if input[0:delim_size] == delimiter:
01165       begin_pos = pre_pos = delim_size
01166 
01167     while 1:
01168       found_pos = string.find(input[begin_pos:],delimiter)
01169       
01170       if found_pos == -1:
01171         results.append(input[pre_pos:])
01172         break
01173 
01174       if found_pos > 0 and input[found_pos - 1] == "\\":
01175         begin_pos += found_pos + delim_size
01176       else:
01177         substr_size = found_pos + (begin_pos - pre_pos)
01178         if substr_size > 0:
01179           results.append(input[pre_pos:(pre_pos+substr_size)])
01180         begin_pos += found_pos + delim_size
01181         pre_pos   = begin_pos
01182 
01183     return len(results)


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28