On Sun, 12 Jun 2005, Jamie C wrote:

Doesn't work - error message:

Cannot write to user settings file ""

That might be because I forgot to remove the write part of the old settings. I've copied the new code below.

Oh and Jeremy thinks that the postage-stamp sized window might be a result of Qt/Windows returning an incorrect value for the screen resolution (I guess this is the code in setting/setting.py/_calcPixPerPt() I don't know if there is a good solution (all the painting code really me), but an off-the-top of-my-head hack would be to try reading a fixed value from a config file and use that if it exists. Of course one still has to get the fixed value there in the first place...

Anyway setting/settingdb.py:


#    Copyright (C) 2005 Jeremy S. Sanders
#    Email: Jeremy Sanders <[EMAIL PROTECTED]>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
###############################################################################

# $Id: settingdb.py,v 1.3 2005/05/18 16:30:58 jeremysanders Exp $

"""A database for default values of settings."""

import sys
import os
import os.path
import qt

class _settings(object):
    def __init__(self, settings):
        self.settings = settings
        self.basePath = "/" + settings.domain + "/" + settings.name + "/"
        self.path = self.basePath
    def setPath(self, path):
        self.path = self.basePath + path
    def resetPath(self):
        self.path = self.basePath
    def __getitem__(self, key):
        try:
            value, ok = self.settings.readEntry(self.path + key)
            value = eval(unicode(value))
            return value
        except:
            print "Warning: error reading setting " + key
    def __setitem__(self, key, value):
        rv = self.settings.writeEntry(self.path+ key, repr(value))
        self.settings.sync()
        return rv
    def __contains__(self, key):
        return key in self.settings.entryList(self.basePath)
    def __del__(self, key):
        self.settings.removeEntry(self.basePath + key)

class _SettingDB(qt.QSettings):
    def __init__(self):
        #XXX - This domain name is fictional...
        self.domain = "veusz.org"
        self.name = "veusz"

        qt.QSettings.__init__(self)
        self.setPath(self.domain, self.name)
        self.database = _settings(self)

        #Import old settings.
        oldSettings = _OldSettingDB()
if oldSettings.fileFound and not 'importPerformed' in self.database:
            for key, value in oldSettings.database.iteritems():
                self.database[key] = str(value)
            self.database['importPerformed'] = True
print "Imported settings from $HOME/.veusz.def and /etc/veusz.conf"

class _OldSettingDB:
    """A singleton class to handle the settings file.

    Reads the settings file on activation, and updates the settings
    file on destruction.
    """

    def __init__(self):
        """Read the default settings.

        This reads the settings from a global configuration file,
        and then from a user configuration file.

        FIXME: Unix specific, fix for other OS."""

        self.systemdefaultfile = '/etc/veusz.conf'
        try:
            self.userdefaultfile = os.path.join(os.environ['HOME'],
                                                '.veusz.def')
        except KeyError:
            self.userdefaultfile = ''

        self.database = {}

        # FIXME: Unix specific
        defaults = self.importFile(self.systemdefaultfile)
        self.sysdefaults = self.database.copy()
        user = self.importFile(self.userdefaultfile)
        #Did we manage to find either settings file?
        self.fileFound = (defaults or user)

    def importFile(self, filename):
        """Read in a configuration file made up of a=b strings."""

        try:
            f = open(filename, 'r')
        except IOError:
            # no error if the file does not exist
            return False

        for l in f:
            l = l.strip()

            # ignore comment and blank lines
            if len(l) == 0 or l[0] == '#':
                continue

            # lines should be A=B
            pos = l.find('=')

            # in case of error
            if pos == -1:
sys.stderr.write('Error in configuration file "%s", line is:\n'
                                 '>>>%s<<<\n' % (filename, l))
                continue

            key = l[:pos].strip()
            val = l[pos+1:].strip()
            self.database[key] = eval(val)
        retrun True

# create the SettingDB singleton
settingdb = _SettingDB()

Répondre à