00001 #!/usr/bin/env python 00002 # -*- coding: euc-jp -*- 00003 00004 ## 00005 # @file ManagerConfig.py 00006 # @brief RTC manager configuration 00007 # @date $Date: $ 00008 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara 00009 # 00010 # Copyright (C) 2003-2008 00011 # Task-intelligence Research Group, 00012 # Intelligent Systems Research Institute, 00013 # National Institute of 00014 # Advanced Industrial Science and Technology (AIST), Japan 00015 # All rights reserved. 00016 00017 00018 import sys 00019 import os 00020 import re 00021 import getopt 00022 import platform 00023 00024 import OpenRTM 00025 00026 00027 ## 00028 # @if jp 00029 # 00030 # @class ManagerConfig 00031 # @brief Manager configuration クラス 00032 # 00033 # Manager のコンフィギュレーションを行う、コマンドライン引数を受け取り、 00034 # (あるいは引数なしで)インスタンス化される。 00035 # コマンドライン引数で指定された設定ファイル、環境変数などから Manager の 00036 # プロパティ情報を設定する。 00037 # 00038 # 各設定の優先度は以下のとおりである。 00039 # <OL> 00040 # <LI>コマンドラインオプション "-f" 00041 # <LI>環境変数 "RTC_MANAGER_CONFIG" 00042 # <LI>デフォルト設定ファイル "./rtc.conf" 00043 # <LI>デフォルト設定ファイル "/etc/rtc.conf" 00044 # <LI>デフォルト設定ファイル "/etc/rtc/rtc.conf" 00045 # <LI>デフォルト設定ファイル "/usr/local/etc/rtc.conf" 00046 # <LI>デフォルト設定ファイル "/usr/local/etc/rtc/rtc.conf" 00047 # <LI>埋め込みコンフィギュレーション値 00048 #</OL> 00049 # ただし、コマンドラインオプション "-d" が指定された場合は、 00050 # (たとえ -f で設定ファイルを指定しても)埋め込みコンフィギュレーション値 00051 # が使用される。 00052 # 00053 # @since 0.4.0 00054 # 00055 # @else 00056 # 00057 # @brief 00058 # 00059 # @endif 00060 class ManagerConfig : 00061 """ 00062 """ 00063 00064 00065 00066 ## 00067 # @if jp 00068 # @brief Manager コンフィギュレーションのデフォルト・ファイル・パス 00069 # @else 00070 # @endif 00071 config_file_path = ["./rtc.conf", 00072 "/etc/rtc.conf", 00073 "/etc/rtc/rtc.conf", 00074 "/usr/local/etc/rtc.conf", 00075 "/usr/local/etc/rtc/rtc.conf", 00076 None] 00077 00078 00079 ## 00080 # @if jp 00081 # @brief デフォルト・コンフィギュレーションのファイル・パスを識別する 00082 # 環境変数 00083 # @else 00084 # @endif 00085 config_file_env = "RTC_MANAGER_CONFIG" 00086 00087 00088 ## 00089 # @if jp 00090 # 00091 # @brief コンストラクタ 00092 # 00093 # 与えられた引数によりコンフィギュレーション情報の初期化を行う。 00094 # 00095 # @param self 00096 # @param argv コマンドライン引数(デフォルト値:None) 00097 # 00098 # @else 00099 # 00100 # @brief ManagerConfig constructor 00101 # 00102 # The constructor that performs initialization at the same time with 00103 # given arguments. 00104 # 00105 # @param argv The command line arguments 00106 # 00107 # @endif 00108 def __init__(self, argv=None): 00109 00110 self._configFile = "" 00111 if argv: 00112 self.init(argv) 00113 00114 00115 ## 00116 # @if jp 00117 # 00118 # @brief 初期化 00119 # 00120 # コマンドライン引数に応じて初期化を実行する。コマンドラインオプションは 00121 # 以下のものが使用可能である。 00122 # 00123 # -f file : コンフィギュレーションファイルを指定する。<br> 00124 # -l module : ロードするモジュールを指定する。(未実装)<br> 00125 # -o options: その他オプションを指定する。(未実装)<br> 00126 # -d : デフォルトのコンフィギュレーションを使う。(未実装)<br> 00127 # 00128 # @param self 00129 # @param argv コマンドライン引数 00130 # 00131 # @else 00132 # 00133 # @brief Initialization 00134 # 00135 # Initialize with command line options. The following command options 00136 # are available. 00137 # 00138 # -f file : Specify a configuration file. <br> 00139 # -l module : Specify modules to be loaded at the beginning. <br> 00140 # -o options: Other options. <br> 00141 # -d : Use default static configuration. <br> 00142 # 00143 # @endif 00144 def init(self, argv): 00145 self.parseArgs(argv) 00146 00147 00148 ## 00149 # @if jp 00150 # @brief Configuration 情報を Property に設定する 00151 # 00152 # Manager のConfiguration 情報を指定された Property に設定する。 00153 # 00154 # @param self 00155 # @param prop Configuration 設定対象 Property 00156 # 00157 # @else 00158 # @brief Apply configuration results to Property 00159 # @endif 00160 def configure(self, prop): 00161 prop.setDefaults(OpenRTM.default_config) 00162 if self.findConfigFile(): 00163 try: 00164 fd = file(self._configFile,"r") 00165 prop.load(fd) 00166 fd.close() 00167 except: 00168 print "Error: file open." 00169 return self.setSystemInformation(prop) 00170 00171 ####### 00172 # \if jp 00173 # 00174 # \brief コンフィギュレーションを取得する(未実装) 00175 # 00176 # コンフィギュレーションを取得する。init()呼び出し前に呼ぶと、 00177 # 静的に定義されたデフォルトのコンフィギュレーションを返す。 00178 # init() 呼び出し後に呼ぶと、コマンドライン引数、環境変数等に 00179 # 基づいた初期化されたコンフィギュレーションを返す。 00180 # 00181 # \else 00182 # 00183 # \brief Get configuration value. 00184 # 00185 # This operation returns default configuration statically defined, 00186 # when before calling init() function. When after calling init() function, 00187 # this operation returns initialized configuration value according to 00188 # command option, environment value and so on. 00189 # 00190 # \endif 00191 #def getConfig(self): 00192 #pass 00193 00194 00195 ## 00196 # @if jp 00197 # 00198 # @brief コマンド引数をパースする 00199 # 00200 # -f file : コンフィギュレーションファイルを指定する。<br> 00201 # -l module : ロードするモジュールを指定する。(未実装)<br> 00202 # -o options: その他オプションを指定する。(未実装)<br> 00203 # -d : デフォルトのコンフィギュレーションを使う。(未実装)<br> 00204 # 00205 # @param self 00206 # @param argv コマンドライン引数 00207 # 00208 # @else 00209 # 00210 # @brief Parse command arguments 00211 # 00212 # -f file : Specify a configuration file. <br> 00213 # -l module : Specify modules to be loaded at the beginning. <br> 00214 # -o options: Other options. <br> 00215 # -d : Use default static configuration. <br> 00216 # 00217 # @endif 00218 def parseArgs(self, argv): 00219 try: 00220 opts, args = getopt.getopt(argv[1:], "f:l:o:d:") 00221 except getopt.GetoptError: 00222 print "Error: getopt error!" 00223 return 00224 00225 for opt, arg in opts: 00226 if opt == "-f": 00227 self._configFile = arg 00228 00229 if opt == "-l": 00230 pass 00231 00232 if opt == "-o": 00233 pass 00234 00235 if opt == "-d": 00236 pass 00237 00238 return 00239 00240 00241 ## 00242 # @if jp 00243 # 00244 # @brief Configuration file の検索 00245 # 00246 # Configuration file を検索し、設定する。 00247 # 既に Configuration file が設定済みの場合は、ファイルの存在確認を行う。 00248 # 00249 # Configuration file の優先順位<br> 00250 # コマンドオプション指定>環境変数>デフォルトファイル>デフォルト設定 00251 # 00252 # デフォルト強制オプション(-d): デフォルトファイルがあっても無視して 00253 # デフォルト設定を使う 00254 # 00255 # @param self 00256 # 00257 # @return Configuration file 検索結果 00258 # 00259 # @else 00260 # 00261 # @brief Find configuration file 00262 # 00263 # @endif 00264 def findConfigFile(self): 00265 if self._configFile != "": 00266 if self.fileExist(self._configFile): 00267 return True 00268 00269 env = os.getenv(self.config_file_env) 00270 if env: 00271 if self.fileExist(env): 00272 self._configFile = env 00273 return True 00274 00275 i = 0 00276 while (self.config_file_path[i]): 00277 if self.fileExist(self.config_file_path[i]): 00278 self._configFile = self.config_file_path[i] 00279 return True 00280 i += 1 00281 00282 return False 00283 00284 00285 ## 00286 # @if jp 00287 # 00288 # @brief システム情報を設定する 00289 # 00290 # システム情報を取得しプロパティにセットする。設定されるキーは以下の通り。 00291 # - manager.os.name : OS名 00292 # - manager.os.release : OSリリース名 00293 # - maanger.os.version : OSバージョン名 00294 # - manager.os.arch : OSアーキテクチャ 00295 # - manager.os.hostname: ホスト名 00296 # - manager.pid : プロセスID 00297 # 00298 # @param self 00299 # @param prop システム情報を設定したプロパティ 00300 # 00301 # @else 00302 # 00303 # @brief Set system information 00304 # 00305 # Get the following system info and set them to Manager's properties. 00306 # - manager.os.name : OS name 00307 # - manager.os.release : OS release name 00308 # - manager.os.version : OS version 00309 # - manager.os.arch : OS architecture 00310 # - manager.os.hostname: Hostname 00311 # - manager.pid : process ID 00312 # 00313 # @endif 00314 def setSystemInformation(self, prop): 00315 sysinfo = platform.uname() 00316 00317 prop.setProperty("manager.os.name", sysinfo[0]) 00318 prop.setProperty("manager.os.hostname", sysinfo[1]) 00319 prop.setProperty("manager.os.release", sysinfo[2]) 00320 prop.setProperty("manager.os.version", sysinfo[3]) 00321 prop.setProperty("manager.os.arch", sysinfo[4]) 00322 prop.setProperty("manager.pid", os.getpid()) 00323 00324 return prop 00325 00326 00327 ## 00328 # @if jp 00329 # @brief ファイルの存在確認 00330 # 00331 # 指定されたファイルが存在するか確認する。 00332 # 00333 # @param self 00334 # @param filename 確認対象ファイル名称 00335 # 00336 # @return 対象ファイル確認結果(存在する場合にtrue) 00337 # 00338 # @else 00339 # @brief Check file existance 00340 # @endif 00341 def fileExist(self, filename): 00342 try: 00343 fp = open(filename) 00344 except: 00345 print "Can't open file:", filename 00346 return False 00347 else: 00348 fp.close() 00349 return True 00350 00351 return False 00352 00353