| | 25 | import traceback |
| | 26 | |
| | 27 | ##------------------------------------------------------------------------- |
| | 28 | ## Doc |
| | 29 | ##------------------------------------------------------------------------- |
| | 30 | """ |
| | 31 | This module provides functions for accessing configuration settings |
| | 32 | stored in the wavedev.cfg file (in xml). The primary functions provided |
| | 33 | for use in other modules are defined in the "public" section of the |
| | 34 | module below. |
| | 35 | |
| | 36 | The configuration file is a simple XML file where values are stored |
| | 37 | in specialized tags. Each value has a name, and is encoded in the |
| | 38 | XML file as a child tag of the top-level <owdconfiguration> tag that |
| | 39 | looks like this: |
| | 40 | <name>value</name> |
| | 41 | |
| | 42 | To look up a configuration value, simply provide the corresponding name |
| | 43 | as an argument to ossieCfgValue(). The XML in the config file is parsed |
| | 44 | only once, and results are cached internally within this module. Provisions |
| | 45 | are also made to override configuration settings loaded from the config |
| | 46 | file using the overrideCfgValue() function. Special methods for retrieving |
| | 47 | the version() and ossieInstallDir() are provided as separate methods, since |
| | 48 | they are used more frequently. These methods are shallow wrappers around |
| | 49 | ossieCfgValue(). |
| | 50 | |
| | 51 | Names in this file that begin with double-underscores ("__") are intended |
| | 52 | to be private and should not be referenced outside this file. |
| | 53 | """ |
| | 54 | |
| | 55 | ##------------------------------------------------------------------------- |
| | 56 | ## Public functions |
| | 57 | ##------------------------------------------------------------------------- |
| | 58 | |
| | 59 | def version(): |
| | 60 | """ |
| | 61 | Get the current OSSIE version by looking up the "version" key. |
| | 62 | """ |
| | 63 | result = ossieCfgValue('version') |
| | 64 | if result == None: |
| | 65 | result = 'unknown' |
| | 66 | return result |
| | 67 | |
| | 68 | |
| | 69 | def ossieInstallDir(): |
| | 70 | """ |
| | 71 | Get the location of the OSSIE installation directory by looking |
| | 72 | up the "installpath" key. This function ensures that the return |
| | 73 | value always ends in a trailing slash. |
| | 74 | """ |
| | 75 | return ensureDirHasTrailingSlash(ossieCfgValue('installpath')) |
| | 76 | |
| | 77 | |
| | 78 | def ossieCfgValue(key): |
| | 79 | """ |
| | 80 | Look up a key in the configuration file and return its associated value. |
| | 81 | """ |
| | 82 | global __overrides |
| | 83 | if __overrides.has_key(key): |
| | 84 | return __overrides[key] |
| | 85 | else: |
| | 86 | val = __keyFromXml(key) |
| | 87 | # cache it |
| | 88 | if val != None: |
| | 89 | setCfgValueIfNecessary(key, val) |
| | 90 | return val |
| | 91 | |
| | 92 | |
| | 93 | def overrideCfgValue(key, value): |
| | 94 | """ |
| | 95 | Override a configuration value from the config file for the current |
| | 96 | process, without rewriting the configuration file. |
| | 97 | """ |
| | 98 | global __overrides |
| | 99 | __overrides['key'] = value |
| | 100 | |
| | 101 | |
| | 102 | def setCfgValueIfNecessary(key, value): |
| | 103 | """ |
| | 104 | Set an override value for a configuration setting, if no override |
| | 105 | value is already recorded. |
| | 106 | """ |
| | 107 | global __overrides |
| | 108 | if not __overrides.has_key(key): |
| | 109 | overrideCfgValue(key, value) |
| | 110 | |
| | 111 | |
| | 112 | def ensureDirHasTrailingSlash(dir): |
| | 113 | """ |
| | 114 | Add a trailing slash to the dir, if there isn't one there already. |
| | 115 | """ |
| | 116 | if dir != None and dir[len(dir) - 1] != '/': |
| | 117 | dir = dir + '/' |
| | 118 | return dir |
| | 119 | |
| | 120 | |
| | 121 | def commentLine(): |
| | 122 | """ |
| | 123 | Generate an XML comment line containing the current version number |
| | 124 | for use in automatically generated XML files. |
| | 125 | """ |
| | 126 | global __commentLine |
| | 127 | if __commentLine == None: |
| | 128 | __commentLine = u'<!--Created with OSSIE WaveDev ' + version() \ |
| | 129 | + u'-->\n<!--Powered by Python-->\n' |
| | 130 | return __commentLine |
| | 131 | |
| 27 | | '''Extracts information from configuration file''' |
| 28 | | root = __file__ |
| 29 | | if os.path.islink (root): |
| 30 | | root = os.path.realpath (root) |
| 31 | | root = os.path.dirname (os.path.abspath (root)) |
| 32 | | |
| 33 | | doc_cfg = xml.dom.minidom.parse(root + '/../wavedev.cfg') |
| 34 | | |
| 35 | | # version |
| 36 | | try: |
| 37 | | frame_obj.version = \ |
| 38 | | str(doc_cfg.getElementsByTagName("version")[0].firstChild.data) |
| 39 | | except: |
| 40 | | frame_obj.version = "unknown" |
| | 134 | """ |
| | 135 | This function used to be located in MainFrame.py, but is used by |
| | 136 | both MainFrame and ComponentFrame to load configuration settings |
| | 137 | into fields within the provided frame_obj. It was moved here, since |
| | 138 | it is used by both of those other modules. |
| | 139 | """ |
| | 140 | frame_obj.version = version() |
| | 141 | |
| 52 | | use_default_stdidlpath = False |
| 53 | | try: |
| 54 | | frame_obj.stdIdlPath = str(doc_cfg.getElementsByTagName("stdidlpath")[0].firstChild.data) |
| 55 | | except: |
| 56 | | frame_obj.stdIdlPath = "" |
| 57 | | if len(frame_obj.stdIdlPath) > 0: |
| 58 | | if frame_obj.stdIdlPath[len(frame_obj.stdIdlPath)-1] != '/': |
| 59 | | frame_obj.stdIdlPath = frame_obj.stdIdlPath + '/' |
| 60 | | # see if directory actually exists |
| 61 | | if not os.path.isdir(frame_obj.stdIdlPath): |
| | 147 | frame_obj.stdIdlPath = ensureDirHasTrailingSlash(ossieCfgValue('stdidlpath')) |
| | 148 | if frame_obj.stdIdlPath == None or not os.path.isdir(frame_obj.stdIdlPath): |
| | 149 | if frame_obj.stdIdlPath == None: |
| | 150 | print "warning: wavedev.cfg stdidl path is not set" |
| | 151 | else: |
| 63 | | print " => using default standard idl path instead" |
| 64 | | use_default_stdidlpath = True |
| 65 | | else: |
| 66 | | use_default_stdidlpath = True |
| 67 | | |
| 68 | | if use_default_stdidlpath: |
| 69 | | if os.path.isdir("/usr/include/standardinterfaces"): |
| 70 | | frame_obj.stdIdlPath = "/usr/include/standardinterfaces/" |
| 71 | | elif os.path.isdir("/usr/local/include/standardinterfaces"): |
| 72 | | frame_obj.stdIdlPath = "/usr/local/include/standardinterfaces/" |
| | 153 | print " => using default standard idl path instead" |
| | 154 | |
| | 155 | # Attempt to use a default value |
| | 156 | if os.path.isdir('/usr/include/standardinterfaces'): |
| | 157 | frame_obj.stdIdlPath = '/usr/include/standardinterfaces/' |
| | 158 | elif os.path.isdir('/usr/local/include/standardinterfaces'): |
| | 159 | frame_obj.stdIdlPath = '/usr/local/include/standardinterfaces/' |
| 83 | | use_default_customidlpath = False |
| 84 | | try: |
| 85 | | frame_obj.customIdlPath = str(doc_cfg.getElementsByTagName("customidlpath")[0].firstChild.data) |
| 86 | | except: |
| 87 | | frame_obj.customIdlPath = "" |
| 88 | | if len(frame_obj.customIdlPath) > 0: |
| 89 | | if frame_obj.customIdlPath[len(frame_obj.customIdlPath)-1] != '/': |
| 90 | | frame_obj.customIdlPath = frame_obj.customIdlPath + '/' |
| 91 | | # see if directory actually exists |
| 92 | | if not os.path.isdir(frame_obj.customIdlPath): |
| | 170 | frame_obj.customIdlPath = ensureDirHasTrailingSlash(ossieCfgValue('customidlpath')) |
| | 171 | if frame_obj.customIdlPath == None or not os.path.isdir(frame_obj.customIdlPath): |
| | 172 | if frame_obj.customIdlPath == None: |
| | 173 | print "warning: wavedev.cfg customidl path is not set" |
| | 174 | else: |
| 94 | | print " => using default custom idl path instead" |
| 95 | | use_default_customidlpath = True |
| 96 | | else: |
| 97 | | use_default_customidlpath = True |
| 98 | | |
| 99 | | if use_default_customidlpath: |
| 100 | | if os.path.isdir("/usr/include/custominterfaces"): |
| 101 | | frame_obj.customIdlPath = "/usr/include/custominterfaces/" |
| 102 | | elif os.path.isdir("/usr/local/include/custominterfaces"): |
| 103 | | frame_obj.customIdlPath = "/usr/local/include/custominterfaces/" |
| | 176 | print " => using default custom idl path instead" |
| | 177 | |
| | 178 | # Attempt to use a default value |
| | 179 | if os.path.isdir('/usr/include/custominterfaces'): |
| | 180 | frame_obj.customIdlPath = '/usr/include/custominterfaces/' |
| | 181 | elif os.path.isdir('/usr/local/include/custominterfaces'): |
| | 182 | frame_obj.customIdlPath = '/usr/local/include/custominterfaces/' |
| 113 | | try: |
| 114 | | frame_obj.ossieIncludePath = \ |
| 115 | | str(doc_cfg.getElementsByTagName("ossieincludepath")[0].firstChild.data) |
| 116 | | except: |
| 117 | | frame_obj.ossieIncludePath = "" |
| 118 | | |
| 119 | | if len(frame_obj.ossieIncludePath) > 0: |
| 120 | | if frame_obj.ossieIncludePath[len(frame_obj.ossieIncludePath)-1] != '/': |
| 121 | | frame_obj.ossieIncludePath = frame_obj.ossieIncludePath + '/' |
| 122 | | else: |
| 123 | | if os.path.isdir("/usr/include/ossie"): |
| 124 | | frame_obj.ossieIncludePath = "/usr/include/ossie/" |
| 125 | | elif os.path.isdir("/usr/local/include/ossie"): |
| 126 | | frame_obj.ossieIncludePath = "/usr/local/include/ossie/" |
| | 192 | frame_obj.ossieIncludePath = ensureDirHasTrailingSlash(ossieCfgValue('ossieincludepath')) |
| | 193 | if frame_obj.ossieIncludePath == None or not os.path.isdir(frame_obj.ossieIncludePath): |
| | 194 | if os.path.isdir('/usr/include/ossie'): |
| | 195 | frame_obj.ossieIncludePath = '/usr/include/ossie/' |
| | 196 | elif os.path.isdir('/usr/local/include/ossie'): |
| | 197 | frame_obj.ossieIncludePath = '/usr/local/include/ossie/' |
| | 225 | |
| | 226 | ##------------------------------------------------------------------------- |
| | 227 | ## Internal |
| | 228 | ##------------------------------------------------------------------------- |
| | 229 | |
| | 230 | def __relativeRoot(): |
| | 231 | """ |
| | 232 | Locate the OSSIE SDR root directory by looking relative to this file. |
| | 233 | """ |
| | 234 | root = __file__ |
| | 235 | if os.path.islink(root): |
| | 236 | root = os.path.realpath(root) |
| | 237 | root = root + '../../..' |
| | 238 | root = os.path.dirname(os.path.abspath(root)) |
| | 239 | return root |
| | 240 | |
| | 241 | |
| | 242 | __overrides = {'installpath' : __relativeRoot() } |
| | 243 | __cachedXml = None |
| | 244 | __commentLine = None |
| | 245 | |
| | 246 | |
| | 247 | def __xml(): |
| | 248 | global __cachedXml |
| | 249 | if __cachedXml == None: |
| | 250 | fileName = ossieInstallDir() + 'tools/WaveDev/wavedev.cfg' |
| | 251 | if not os.path.exists(fileName): |
| | 252 | # For OEF internals |
| | 253 | fileName = ossieInstallDir() + 'WaveDev/wavedev.cfg' |
| | 254 | if not os.path.exists(fileName): |
| | 255 | fileName = '../wavedev.cfg' |
| | 256 | if os.path.exists(fileName): |
| | 257 | __cachedXml = xml.dom.minidom.parse(fileName) |
| | 258 | return __cachedXml |
| | 259 | |
| | 260 | |
| | 261 | def __keyFromXml(key): |
| | 262 | result = None |
| | 263 | xml = __xml() |
| | 264 | if xml != None: |
| | 265 | try: |
| | 266 | result = str(xml.getElementsByTagName(key)[0].firstChild.data) |
| | 267 | except: |
| | 268 | # ignore it--the config file is missing the given setting |
| | 269 | pass |
| | 270 | return result |