diff src/tools/memory.py @ 364:312ca6f9d84a

core: configuration file SàT's main options can now be put in a configuration file which can be in different locations Bridge: new getConfig option
author Goffi <goffi@goffi.org>
date Sat, 18 Jun 2011 16:22:50 +0200
parents ca3a041fed30
children efbfccfed623
line wrap: on
line diff
--- a/src/tools/memory.py	Sun Jun 12 23:43:36 2011 +0200
+++ b/src/tools/memory.py	Sat Jun 18 16:22:50 2011 +0200
@@ -24,12 +24,14 @@
 import os.path
 import time
 import cPickle as pickle
+from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
 from xml.dom import minidom
 from logging import debug, info, warning, error
 import pdb
 from twisted.internet import defer
 from twisted.words.protocols.jabber import jid
 from sat.tools.xml_tools import paramsXml2xmlUI
+from sat.core.default_config import default_config
 
 SAVEFILE_PARAM_XML="/param" #xml parameters template
 SAVEFILE_PARAM_DATA="/param" #individual & general parameters; _ind and _gen suffixes will be added
@@ -399,10 +401,31 @@
         self.private={}  #used to store private value
         self.server_features={} #used to store discovery's informations
         self.server_identities={}
+        self.config = self.parseMainConf()
         host.set_const('savefile_history', SAVEFILE_HISTORY)
         host.set_const('savefile_private', SAVEFILE_PRIVATE)
         self.load()
 
+    def parseMainConf(self):
+        """look for main .ini configuration file, and parse it"""
+        _config = SafeConfigParser(defaults=default_config)
+        _config.read(['/etc/sat.conf', os.path.expanduser('~/sat.conf'), 'sat.conf'])
+        return _config
+
+    def getConfig(self, section, name, profile):
+        """Get the main configuration option
+        @param section: section of the config file (None or '' for DEFAULT)
+        @param name: name of the option
+        @param profile: %(doc_profile_key)s"""
+        if not section:
+            section='DEFAULT'
+        try:
+            _value = self.config.get(section, name)
+        except NoOptionError, NoSectionError:
+            _value = ''
+
+        return os.path.expanduser(_value) if name.endswith('_path') or name.endswith('_dir') else _value
+
     def load(self):
         """Load parameters and all memory things from file/db"""
         param_file_xml = os.path.expanduser(self.host.get_const('local_dir')+