Mercurial > libervia-backend
diff src/memory/memory.py @ 930:cbf4122baae7
core, memory: use XDG recommended paths as the defaults for the config and local directories
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 23 Mar 2014 22:43:43 +0100 |
parents | e77948faaef3 |
children | 5b2d2f1f05d0 |
line wrap: on
line diff
--- a/src/memory/memory.py Mon Mar 24 15:18:53 2014 +0100 +++ b/src/memory/memory.py Sun Mar 23 22:43:43 2014 +0100 @@ -21,6 +21,7 @@ import os.path import csv +from xdg import BaseDirectory from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError from uuid import uuid4 from logging import debug, info, warning, error @@ -117,6 +118,7 @@ self.server_features = {} # used to store discovery's informations self.server_identities = {} self.config = self.parseMainConf() + self.__fixLocalDir() database_file = os.path.expanduser(os.path.join(self.getConfig('', 'local_dir'), C.SAVEFILE_DATABASE)) self.storage = SqliteStorage(database_file, host.__version__) PersistentDict.storage = self.storage @@ -130,13 +132,41 @@ def parseMainConf(self): """look for main .ini configuration file, and parse it""" - _config = SafeConfigParser(defaults=C.DEFAULT_CONFIG) + config = SafeConfigParser(defaults=C.DEFAULT_CONFIG) try: - _config.read(map(os.path.expanduser, ['/etc/sat.conf', '~/sat.conf', '~/.sat.conf', 'sat.conf', '.sat.conf'])) + config.read(C.CONFIG_FILES) except: error(_("Can't read main config !")) + return config - return _config + # XXX: tmp update code, will be removed in the future + # When you remove this, please also remove sat.core.constants.Const.DEFAULT_LOCAL_DIR + # and add the default value for 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG + def __fixLocalDir(self): + """Retro-compatibility with the previous local_dir default value.""" + if self.getConfig('', 'local_dir'): + return # nothing to do + old_default = '~/.sat' + if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE): + warning(_("A database has been found in the default local_dir for previous versions (< 0.5)")) + config = SafeConfigParser() + target_file = None + for file_ in C.CONFIG_FILES[::-1]: + # we will eventually update the existing file with the highest priority, if it's a user personal file... + if os.path.isfile(file_): + if file_.startswith(os.path.expanduser('~')): + config.read([file_]) + target_file = file_ + break + if not target_file: + # ... otherwise we create a new config file for that user + target_file = BaseDirectory.save_config_path('sat') + '/sat.conf' + config.set('', 'local_dir', old_default) + with open(target_file, 'wb') as configfile: + config.write(configfile) + warning(_("Auto-update: local_dir set to %(path)s in the file %(config_file)s") % {'path': old_default, 'config_file': file_}) + else: # use the new default local_dir + self.config.set('', 'local_dir', C.DEFAULT_LOCAL_DIR) def getConfig(self, section, name): """Get the main configuration option