Author: johannes Date: 2007-11-21 03:35:42 -0600 (Wed, 21 Nov 2007) New Revision: 9819
Modified: trunk/gnue-common/src/apps/RuntimeSettings.py Log: More work on PEP8 Modified: trunk/gnue-common/src/apps/RuntimeSettings.py =================================================================== --- trunk/gnue-common/src/apps/RuntimeSettings.py 2007-11-21 09:11:35 UTC (rev 9818) +++ trunk/gnue-common/src/apps/RuntimeSettings.py 2007-11-21 09:35:42 UTC (rev 9819) @@ -1,7 +1,8 @@ +# GNU Enterprise Common Library - Application Services - Runtime Settings # # Copyright 2001-2007 Free Software Foundation # -# This file is part of GNU Enterprise. +# This file is part of GNU Enterprise # # GNU Enterprise is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public @@ -18,18 +19,19 @@ # write to the Free Software Foundation, Inc., 59 Temple Place # - Suite 330, Boston, MA 02111-1307, USA. # -# FILE: -# RuntimeSettings.py -# -# DESCRIPTION: -# Saves the state of designer between runs. Saves such data as -# window positions, size, visibility, etc. -# -# NOTES: +# $Id$ +""" +Saves the state of designer between runs. Saves such data as window positions, +size, visibility, etc. +""" -import sys, os, ConfigParser +import sys +import os +import ConfigParser - +__all__ = ['init', 'registerInstance', 'get', 'getint', 'saveRuntimeSettings', + 'registerRuntimeSettingHandler'] + # TODO: Should this save into the Win32 registry if its available????? # TODO: This is, after all, session-specific information and not # TODO: user-settable configuration data. @@ -37,16 +39,32 @@ if not globals().has_key('location'): location = None + +# ----------------------------------------------------------------------------- +# Initialization +# ----------------------------------------------------------------------------- + def init(configFilename="default.ini", homeConfigDir=".gnue"): + """ + Determine which config file to use, open and read it's contents. As a side + effect the module globals 'location' and 'config' are set, where location + is the path to the config file being used and config is a ConfigParser + instance used for reading/writing the settings. + """ + global location, config + if os.environ.has_key('HOME'): try: os.makedirs(os.path.join(os.environ['HOME'], homeConfigDir)) except: pass + location = os.path.join(os.environ['HOME'], homeConfigDir, configFilename) - elif sys.platform[:3] in ('win','mac'): + + # Note: sys.platform on OS X returns 'darwin' + elif sys.platform[:3] in ('win', 'mac', 'dar'): location = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), configFilename) else: @@ -55,67 +73,107 @@ try: config = ConfigParser.ConfigParser() config.read(location) + except: config = None + +# ----------------------------------------------------------------------------- +# Register an instance +# ----------------------------------------------------------------------------- + def registerInstance(instance): + instance._runtimes = [] +# ----------------------------------------------------------------------------- +# Get an options value +# ----------------------------------------------------------------------------- + def get(section, setting, default): + + # FIXME: we should define location and config as global here in order to + # make pylint happy + # Backwards compatability if location == None: init() try: return config.get(section, setting) + except: return default +# ----------------------------------------------------------------------------- +# Get a numeric option value +# ----------------------------------------------------------------------------- + def getint(section, setting, default): + + # FIXME: we should define location and config as global here in order to + # make pylint happy + # Backwards compatability if location == None: init() try: return config.getint(section, setting) + except: return default -# + +# ----------------------------------------------------------------------------- # Save the runtime settings -# +# ----------------------------------------------------------------------------- + def saveRuntimeSettings(instance): + from gnue.common.apps.GDebug import _DEBUG_LEVELS + + # FIXME: we should define location and config as global here in order to + # make pylint happy + if location: try: - fh = open(location,'w') + fh = open(location, 'w') for h in instance._runtimes: try: section, hash = h.saveRuntimeSettings() + except: print o( \ u_("Warning: Unable to save all session data to %s") % location) if _DEBUG_LEVELS != [0]: raise + if len(hash.keys()): if not config.has_section(section): config.add_section(section) + for key in hash.keys(): config.set(section, key, "%s" % hash[key]) config.write(fh) fh.close() + except: print o(u_("\nWarning: Unable to save session data to %s\n") \ % location) if _DEBUG_LEVELS != [0]: raise -# -# Any object (class) that has settings it wants saved should -# register with this method. The object should define a -# getRuntimeSettings() method. -# + +# ----------------------------------------------------------------------------- +# Register an object for saving runtime settings +# ----------------------------------------------------------------------------- + def registerRuntimeSettingHandler(instance, object): + """ + Any object (class) that has settings it wants saved should register with + this method. The object should define a getRuntimeSettings() method. + """ instance._runtimes.append(object) _______________________________________________ commit-gnue mailing list commit-gnue@gnu.org http://lists.gnu.org/mailman/listinfo/commit-gnue