root/ossiedev/branches/0.7.x/tools/WaveDev/wavedev/cfg.py @ 9384

Revision 9384, 10.2 KB (checked in by stedwar2, 4 years ago)

Updated to work with OEF v1.1.2.

Line 
1## Copyright 2005, 2006, 2007 Virginia Polytechnic Institute and State University
2##
3## This file is part of the OSSIE Waveform Developer.
4##
5## OSSIE Waveform Developer is free software; you can redistribute it and/or modify
6## it under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 2 of the License, or
8## (at your option) any later version.
9##
10## OSSIE Waveform Developer is distributed in the hope that it will be useful,
11## but WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13## GNU General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with OSSIE Waveform Developer; if not, write to the Free Software
17## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19##-------------------------------------------------------------------------
20## This file defines basic methods for retrieving information from the
21## wavedev.cfg file.
22##-------------------------------------------------------------------------
23import xml.dom.minidom
24import os
25import traceback
26
27##-------------------------------------------------------------------------
28## Doc
29##-------------------------------------------------------------------------
30"""
31This module provides functions for accessing configuration settings
32stored in the wavedev.cfg file (in xml).  The primary functions provided
33for use in other modules are defined in the "public" section of the
34module below.
35
36The configuration file is a simple XML file where values are stored
37in specialized tags.  Each value has a name, and is encoded in the
38XML file as a child tag of the top-level <owdconfiguration> tag that
39looks like this:
40    <name>value</name>
41
42To look up a configuration value, simply provide the corresponding name
43as an argument to ossieCfgValue().  The XML in the config file is parsed
44only once, and results are cached internally within this module.  Provisions
45are also made to override configuration settings loaded from the config
46file using the overrideCfgValue() function.  Special methods for retrieving
47the version() and ossieInstallDir() are provided as separate methods, since
48they are used more frequently.  These methods are shallow wrappers around
49ossieCfgValue().
50
51Names in this file that begin with double-underscores ("__") are intended
52to be private and should not be referenced outside this file.
53"""
54
55##-------------------------------------------------------------------------
56## Public functions
57##-------------------------------------------------------------------------
58
59def 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
69def 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
78def 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
93def 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
102def 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
112def 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
121def 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
132
133def LoadConfiguration(frame_obj):
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
142
143    # install path
144    frame_obj.installPath = ossieInstallDir()
145
146    # standard IDL path
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:
152            print "warning: wavedev.cfg stdidl path " + frame_obj.stdIdlPath + " does not exist"
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/'
160        else:
161            tmpstr = "StandardInterfaces does not appear to be installed!\n"
162            tmpstr += "You will not be able to add ports to components.\n\n"
163            tmpstr += "If you have standardinterfaces installed in a location\n"
164            tmpstr += "other than the default, please specify that location\n"
165            tmpstr += "in the wavedev.cfg file located in the top directory."
166            errorMsg(frame_obj,tmpstr)
167
168
169    # custom IDL path
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:
175            print "warning: wavedev.cfg customidl path " + frame_obj.customIdlPath + " does not exist"
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/'
183        else:
184            tmpstr = "CustomInterfaces does not appear to be installed!\n"
185            tmpstr += "You will not be able to add ports to components.\n\n"
186            tmpstr += "If you have standardinterfaces installed in a location\n"
187            tmpstr += "other than the default, please specify that location\n"
188            tmpstr += "in the wavedev.cfg file located in the top directory."
189            errorMsg(frame_obj,tmpstr)
190
191    # ossie include path
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/'
198        else:
199            tmpstr = "OSSIE does not appear to be installed!\n"
200            tmpstr += "You will not be able to add ports to components\n"
201            tmpstr += "using CF interfaces.\n\n"
202            tmpstr += "If you have ossie installed in a location other than\n"
203            tmpstr += "the default please specify that location in the\n"
204            tmpstr += "wavedev.cfg file located in the top directory."
205            errorMsg(frame_obj,tmpstr)
206
207    # home directory
208    frame_obj.homeDir = ensureDirHasTrailingSlash(ossieCfgValue('homedir'))
209
210    # generate source preamble
211    frame_obj.sourcepreamble = ossieCfgValue('sourcepreamble')
212    if frame_obj.sourcepreamble == None or not os.path.exists(frame_obj.sourcepreamble):
213        frame_obj.sourcepreamble = ossieInstallDir() + 'WaveDev/wavedev/generate/generic_preamble'
214
215    # generate license file
216    frame_obj.licensefile = ossieCfgValue('licensefile')
217    if frame_obj.licensefile == None or not os.path.exists(frame_obj.licensefile):
218        frame_obj.licensefile = ''
219
220    # developer
221    frame_obj.developer = ossieCfgValue('developer')
222    if frame_obj.developer == None or not os.path.exists(frame_obj.developer):
223        frame_obj.developer = "your_name_or_organization"
224
225
226##-------------------------------------------------------------------------
227## Internal
228##-------------------------------------------------------------------------
229
230def __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
247def __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
261def __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
Note: See TracBrowser for help on using the browser.