comparison 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
comparison
equal deleted inserted replaced
929:059b56cbd247 930:cbf4122baae7
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 21
22 import os.path 22 import os.path
23 import csv 23 import csv
24 from xdg import BaseDirectory
24 from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError 25 from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
25 from uuid import uuid4 26 from uuid import uuid4
26 from logging import debug, info, warning, error 27 from logging import debug, info, warning, error
27 from twisted.internet import defer, reactor 28 from twisted.internet import defer, reactor
28 from twisted.words.protocols.jabber import jid 29 from twisted.words.protocols.jabber import jid
115 # /!\ an entity is not necessarily in roster 116 # /!\ an entity is not necessarily in roster
116 self.subscriptions = {} 117 self.subscriptions = {}
117 self.server_features = {} # used to store discovery's informations 118 self.server_features = {} # used to store discovery's informations
118 self.server_identities = {} 119 self.server_identities = {}
119 self.config = self.parseMainConf() 120 self.config = self.parseMainConf()
121 self.__fixLocalDir()
120 database_file = os.path.expanduser(os.path.join(self.getConfig('', 'local_dir'), C.SAVEFILE_DATABASE)) 122 database_file = os.path.expanduser(os.path.join(self.getConfig('', 'local_dir'), C.SAVEFILE_DATABASE))
121 self.storage = SqliteStorage(database_file, host.__version__) 123 self.storage = SqliteStorage(database_file, host.__version__)
122 PersistentDict.storage = self.storage 124 PersistentDict.storage = self.storage
123 self.params = Params(host, self.storage) 125 self.params = Params(host, self.storage)
124 info(_("Loading default params template")) 126 info(_("Loading default params template"))
128 d.addCallback(lambda ignore: self.memory_data.load()) 130 d.addCallback(lambda ignore: self.memory_data.load())
129 d.chainDeferred(self.initialized) 131 d.chainDeferred(self.initialized)
130 132
131 def parseMainConf(self): 133 def parseMainConf(self):
132 """look for main .ini configuration file, and parse it""" 134 """look for main .ini configuration file, and parse it"""
133 _config = SafeConfigParser(defaults=C.DEFAULT_CONFIG) 135 config = SafeConfigParser(defaults=C.DEFAULT_CONFIG)
134 try: 136 try:
135 _config.read(map(os.path.expanduser, ['/etc/sat.conf', '~/sat.conf', '~/.sat.conf', 'sat.conf', '.sat.conf'])) 137 config.read(C.CONFIG_FILES)
136 except: 138 except:
137 error(_("Can't read main config !")) 139 error(_("Can't read main config !"))
138 140 return config
139 return _config 141
142 # XXX: tmp update code, will be removed in the future
143 # When you remove this, please also remove sat.core.constants.Const.DEFAULT_LOCAL_DIR
144 # and add the default value for 'local_dir' in sat.core.constants.Const.DEFAULT_CONFIG
145 def __fixLocalDir(self):
146 """Retro-compatibility with the previous local_dir default value."""
147 if self.getConfig('', 'local_dir'):
148 return # nothing to do
149 old_default = '~/.sat'
150 if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE):
151 warning(_("A database has been found in the default local_dir for previous versions (< 0.5)"))
152 config = SafeConfigParser()
153 target_file = None
154 for file_ in C.CONFIG_FILES[::-1]:
155 # we will eventually update the existing file with the highest priority, if it's a user personal file...
156 if os.path.isfile(file_):
157 if file_.startswith(os.path.expanduser('~')):
158 config.read([file_])
159 target_file = file_
160 break
161 if not target_file:
162 # ... otherwise we create a new config file for that user
163 target_file = BaseDirectory.save_config_path('sat') + '/sat.conf'
164 config.set('', 'local_dir', old_default)
165 with open(target_file, 'wb') as configfile:
166 config.write(configfile)
167 warning(_("Auto-update: local_dir set to %(path)s in the file %(config_file)s") % {'path': old_default, 'config_file': file_})
168 else: # use the new default local_dir
169 self.config.set('', 'local_dir', C.DEFAULT_LOCAL_DIR)
140 170
141 def getConfig(self, section, name): 171 def getConfig(self, section, name):
142 """Get the main configuration option 172 """Get the main configuration option
143 @param section: section of the config file (None or '' for DEFAULT) 173 @param section: section of the config file (None or '' for DEFAULT)
144 @param name: name of the option 174 @param name: name of the option