NVUtil.py

説明を見る。
00001 #!/usr/bin/env python
00002 # -*- coding: euc-jp -*- 
00003 
00004 ##
00005 # @file NVUtil.py
00006 # @brief NameValue and NVList utility functions
00007 # @date $Date: 2007/09/11$
00008 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara
00009 # 
00010 # Copyright (C) 2006-2008
00011 #     Noriaki Ando
00012 #     Task-intelligence Research Group,
00013 #     Intelligent Systems Research Institute,
00014 #     National Institute of
00015 #         Advanced Industrial Science and Technology (AIST), Japan
00016 #     All rights reserved.
00017 
00018 import sys
00019 import traceback
00020 from omniORB import any
00021 
00022 import OpenRTM
00023 import SDOPackage, SDOPackage__POA
00024 
00025 
00026 ##
00027 # @if jp
00028 #
00029 # @brief NameValue を生成する
00030 #
00031 # このオペレーションはNameValueを作成する。
00032 #
00033 # @param name NameValue の name
00034 # @param value NameValue の value
00035 #
00036 # @return NameValue
00037 #
00038 # @else
00039 #
00040 # @brief Create NameVale
00041 #
00042 # This operation creates NameVale.
00043 #
00044 # @param name name of NameValue
00045 # @param value value of NameValue
00046 #
00047 # @return NameValue
00048 #
00049 # @endif
00050 def newNV(name, value):
00051   try:
00052     any_val = any.to_any(value)
00053   except:
00054     print "ERROR  NVUtil.newNV : Can't convert to any. ", type(value)
00055     raise
00056 
00057     
00058   nv = SDOPackage.NameValue(name, any_val)
00059   return nv
00060 
00061 
00062 ##
00063 # @if jp
00064 #
00065 # @brief Properties を NVList へコピーする
00066 #
00067 # このオペレーションは Properties を NVList へコピーする。
00068 # NVList の value は全て CORBA::string 型としてコピーする。
00069 #
00070 # @param nv Properties の値を格納する NVList
00071 # @param prop コピー元の Properties
00072 #
00073 # @else
00074 #
00075 # @brief Copy to NVList from Proeprties
00076 #
00077 # This operation copies Properties to NVList.
00078 # Created NVList's values are CORBA::string.
00079 #
00080 # @param nv NVList to store Properties values
00081 # @param prop Properties that is copies from
00082 #
00083 # @endif
00084 def copyFromProperties(nv, prop):
00085   keys = prop.propertyNames()
00086   keys_len = len(keys)
00087   nv_len = len(nv)
00088   if nv_len > 0:
00089     for i in range(nv_len):
00090       del nv[-1]
00091 
00092   for i in range(keys_len):
00093     nv.append(newNV(keys[i], prop.getProperty(keys[i])))
00094 
00095 
00096 ##
00097 # @if jp
00098 #
00099 # @brief NVList を Properties へコピーする
00100 #
00101 # このオペレーションは NVList を Properties へコピーする。
00102 #
00103 # @param prop NVList の値を格納する Properties
00104 # @param nv コピー元の NVList
00105 #
00106 # @else
00107 #
00108 # @brief Copy to Proeprties from NVList
00109 #
00110 # This operation copies NVList to Properties.
00111 #
00112 # @param prop Properties to store NVList values
00113 # @param nv NVList that is copies from
00114 #
00115 # @endif
00116 def copyToProperties(prop, nvlist):
00117   for nv in nvlist:
00118     try:
00119       val = str(any.from_any(nv.value, keep_structs=True))
00120       prop.setProperty(str(nv.name),val)
00121     except:
00122       traceback.print_exception(*sys.exc_info())
00123       pass
00124 
00125 
00126 
00127 ##
00128 # @if jp
00129 # @class to_prop
00130 # @brief NVList → Properties 変換用ファンクタ
00131 # @endif
00132 class to_prop:
00133   def __init__(self):
00134     self._prop = OpenRTM.Properties()
00135     
00136   def __call__(self, nv):
00137     self._prop.setProperty(nv.name, nv.value)
00138 
00139 
00140 
00141 ##
00142 # @if jp
00143 #
00144 # @brief NVList を Properties へ変換する
00145 #
00146 # このオペレーションは NVList を Properties へ変換する。
00147 #
00148 # @param nv 変換元の NVList
00149 #
00150 # @return 変換結果Property
00151 #
00152 # @else
00153 #
00154 # @endif
00155 def toProperties(nv):
00156   p = OpenRTM.CORBA_SeqUtil.for_each(nv, to_prop())
00157   return p._prop
00158 
00159 
00160 
00161 ##
00162 # @if jp
00163 # @class nv_find
00164 # @brief NVList 検索用ファンクタ
00165 # @endif
00166 class nv_find:
00167   """
00168   """
00169 
00170   def __init__(self, name):
00171     self._name = name
00172 
00173   def __call__(self, nv):
00174     return str(self._name) == str(nv.name)
00175 
00176 
00177 ##
00178 # @if jp
00179 #
00180 # @brief NVList から name で指定された value を返す
00181 #
00182 # このオペレーションは name で指定された value を Any 型で返す。
00183 # 指定した名称の要素が存在しない場合は例外を発生させる。
00184 #
00185 # @param nv 検索対象の NVList
00186 # @param name 検索する名前
00187 #
00188 # @return 検索結果
00189 #
00190 # @else
00191 #
00192 # @brief Get value in NVList specified by name
00193 #
00194 # This operation returns Any type of value specified by name.
00195 # Created NVList's values are CORBA::string.
00196 #
00197 # @param nv NVList to be searched
00198 # @param prop name to seartch in NVList
00199 #
00200 # @endif
00201 def find(nv, name):
00202   index = OpenRTM.CORBA_SeqUtil.find(nv, nv_find(name))
00203 
00204   if index < 0:
00205     raise "Not found."
00206 
00207   return nv[index].value
00208 
00209 
00210 ##
00211 # @if jp
00212 #
00213 # @brief name で指定された要素のインデックスを返す
00214 #
00215 # このオペレーションは name で指定された要素が格納されている位置の
00216 # インデックスを返す。
00217 #
00218 # @param nv 検索対象の NVList
00219 # @param name 検索する名前
00220 #
00221 # @return 検索対象のインデックス
00222 #
00223 # @else
00224 #
00225 # @endif
00226 def find_index(nv, name):
00227   return OpenRTM.CORBA_SeqUtil.find(nv, nv_find(name))
00228 
00229 
00230 ##
00231 # @if jp
00232 #
00233 # @brief 指定された name の value の型が string であるか検証する
00234 #
00235 # このオペレーションは name で指定された value の型が CORBA::string
00236 # かどうかを bool 値で返す。
00237 #
00238 # @param nv 検索対象の NVList
00239 # @param name 検索する名前
00240 #
00241 # @return string検証結果(string:true、それ以外:false)
00242 #
00243 # @else
00244 #
00245 # @endif
00246 def isString(nv, name):
00247   try:
00248     value = find(nv, name)
00249     val = any.from_any(value, keep_structs=True)
00250     return type(val) == str
00251   except:
00252     return False
00253 
00254 
00255 ##
00256 # @if jp
00257 #
00258 # @brief 指定された name の value の型が指定した文字列と一致するか検証する
00259 #
00260 # このオペレーションは name で指定された value の型が CORBA::string
00261 # かどうかを判断し、  CORBA::string である場合には指定した文字列と一致するか
00262 # をbool 値で返す。
00263 #
00264 # @param nv 検索対象の NVList
00265 # @param name 検索する名前
00266 # @param value 比較対象文字列
00267 #
00268 # @return 検証結果(文字列と一致:true、非一致:false)
00269 #
00270 # @else
00271 #
00272 # @endif
00273 def isStringValue(nv, name, value):
00274   if isString(nv, name):
00275     if toString(nv, name) == value:
00276       return True
00277   return False
00278 
00279 
00280 ##
00281 # @if jp
00282 #
00283 # @brief 指定された name の NVList を string として返す。
00284 #
00285 # このオペレーションは name で指定された NVList の値を string で返す。
00286 # もし、name で指定した value の値が CORBA::string でなければ、
00287 # 空の文字列のstringを返す。
00288 #
00289 # @param nv 検索対象の NVList
00290 # @param name 検索する名前
00291 #
00292 # @return name に対応する値のstring型の値
00293 #
00294 # @else
00295 #
00296 # @brief Get string value in NVList specified by name
00297 #
00298 # This operation returns string value in NVList specified by name.
00299 # If the value in NVList specified by name is not CORBA::string type
00300 # this operation returns empty string value.
00301 #
00302 # @param nv NVList to be searched
00303 # @param name name to to serach
00304 #
00305 # @return string value named by name
00306 #
00307 # @endif
00308 def toString(nv, name):
00309   str_value = ""
00310   try:
00311     ret_value = find(nv, name)
00312     val = any.from_any(ret_value, keep_structs=True)
00313     if type(val) == str:
00314       str_value = val
00315   except:
00316     traceback.print_exception(*sys.exc_info())
00317     pass
00318   
00319   return str_value
00320 
00321 
00322 ##
00323 # @if jp
00324 #
00325 # @brief 指定された文字列を NVList の要素に追加する。
00326 #
00327 # このオペレーションは name で指定された要素に value で指定された文字列を
00328 # 追加する。
00329 # name で指定した要素に既に value の値が設定されている場合には何もしない。
00330 # name で指定した要素に value の値が設定されていない場合は、 「,」区切りで
00331 # value の値を追加する。
00332 # 指定された値を設定する。
00333 # name で指定した要素が存在しない場合は、 NVList の最後に新たな要素を追加し、
00334 # 指定された値を設定する。
00335 #
00336 # @param nv 検索対象の NVList
00337 # @param name 追加対象要素名
00338 # @param value 追加する文字列
00339 #
00340 # @return 追加操作結果
00341 #
00342 # @else
00343 #
00344 # @endif
00345 def appendStringValue(nv, name, value):
00346   index = find_index(nv, name)
00347 
00348   if index >= 0:
00349     tmp_str = nv[index].value.value()
00350 
00351     values = OpenRTM.split(tmp_str,",")
00352     find_flag = False
00353     for val in values:
00354       if val == value:
00355         find_flag = True
00356 
00357     if not find_flag:
00358       tmp_str += ", "
00359       tmp_str += value
00360       nv[index].value = tmp_str
00361   else:
00362     OpenRTM.CORBA_SeqUtil.push_back(nv, newNV(name, value))
00363 
00364   return True
00365 
00366 
00367 ##
00368 # @if jp
00369 #
00370 # @brief NVList に要素を追加する。
00371 #
00372 # このオペレーションは dest で指定された NVList に src で指定された要素を
00373 # 追加する。
00374 #
00375 # @param dest 追加される NVList
00376 # @param src 追加する NVList
00377 #
00378 # @else
00379 #
00380 # @endif
00381 def append(dest, src):
00382   for i in range(len(src)):
00383     OpenRTM.CORBA_SeqUtil.push_back(dest, src[i])
00384 
00385 
00386 ##
00387 # @if jp
00388 #
00389 # @brief NVList に設定されている内容を文字列として出力する。
00390 #
00391 # 指定された NVList に設定された内容を文字列として出力する。
00392 # なお、設定されている要素が文字列型以外の場合には、その旨(文字列ではない)を
00393 # 出力する。
00394 #
00395 # @param nv 出力対象 NVList
00396 #
00397 # @else
00398 #
00399 # @endif
00400 def dump(nv):
00401   for i in range(len(nv)):
00402     if type(nv[i].value) == str:
00403       print nv[i].name, ": ", nv[i].value
00404     else:
00405       print nv[i].name, ": not a string value"

OpenRTMに対してMon Mar 17 15:11:05 2008に生成されました。  doxygen 1.5.4