memory, tools (config): move special config retrieval from memory to tools
This commit is contained in:
parent
a5dd35ce65
commit
76413b3679
2 changed files with 35 additions and 20 deletions
|
|
@ -20,7 +20,6 @@
|
|||
from sat.core.i18n import _
|
||||
|
||||
import os.path
|
||||
import csv
|
||||
from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
|
||||
from uuid import uuid4
|
||||
from sat.core.log import getLogger
|
||||
|
|
@ -34,7 +33,7 @@ from sat.memory.persistent import PersistentDict
|
|||
from sat.memory.params import Params
|
||||
from sat.memory.disco import Discovery
|
||||
from sat.memory.crypto import BlockCipher
|
||||
from sat.tools.config import fixConfigOption
|
||||
from sat.tools import config as tools_config
|
||||
|
||||
|
||||
class Sessions(object):
|
||||
|
|
@ -180,7 +179,7 @@ def fixLocalDir(silent=True):
|
|||
if os.path.isfile(os.path.expanduser(old_default) + '/' + C.SAVEFILE_DATABASE):
|
||||
if not silent:
|
||||
log.warning(_("A database has been found in the default local_dir for previous versions (< 0.5)"))
|
||||
fixConfigOption('', 'local_dir', old_default, silent)
|
||||
tools_config.fixConfigOption('', 'local_dir', old_default, silent)
|
||||
|
||||
|
||||
class Memory(object):
|
||||
|
|
@ -222,22 +221,7 @@ class Memory(object):
|
|||
@param section: section of the config file (None or '' for DEFAULT)
|
||||
@param name: name of the option
|
||||
"""
|
||||
if not section:
|
||||
section = 'DEFAULT'
|
||||
try:
|
||||
value = self.config.get(section, name)
|
||||
except (NoOptionError, NoSectionError):
|
||||
value = ''
|
||||
|
||||
if name.endswith('_path') or name.endswith('_dir'):
|
||||
value = os.path.expanduser(value)
|
||||
# thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873)
|
||||
elif name.endswith('_list'):
|
||||
value = csv.reader([value], delimiter=',', quotechar='"').next()
|
||||
elif name.endswith('_dict'):
|
||||
value = dict(csv.reader([item], delimiter=':', quotechar='"').next()
|
||||
for item in csv.reader([value], delimiter=',', quotechar='"').next())
|
||||
return value
|
||||
return tools_config.getConfig(self.config, section, name, default='')
|
||||
|
||||
def load_xml(self, filename):
|
||||
"""Load parameters template from xml file
|
||||
|
|
|
|||
|
|
@ -26,9 +26,10 @@ log = getLogger(__name__)
|
|||
from sat.core.constants import Const as C
|
||||
from sat.core.i18n import _
|
||||
|
||||
from ConfigParser import SafeConfigParser, DEFAULTSECT
|
||||
from ConfigParser import SafeConfigParser, DEFAULTSECT, NoOptionError, NoSectionError
|
||||
from xdg import BaseDirectory
|
||||
import os
|
||||
import csv
|
||||
|
||||
|
||||
def fixConfigOption(section, option, value, silent=True):
|
||||
|
|
@ -64,3 +65,33 @@ def fixConfigOption(section, option, value, silent=True):
|
|||
value = '******'
|
||||
log.warning(_("Config auto-update: %(option)s set to %(value)s in the file %(config_file)s") %
|
||||
{'option': option, 'value': value, 'config_file': target_file})
|
||||
|
||||
|
||||
def getConfig(config, section, name, default=None):
|
||||
"""Get a configuration option
|
||||
|
||||
@param config (SafeConfigParser): the configuration instance
|
||||
@param section (str): section of the config file (None or '' for DEFAULT)
|
||||
@param name (str): name of the option
|
||||
@param default (str): eventually default to this value, if not None
|
||||
@return: str, list or dict
|
||||
"""
|
||||
if not section:
|
||||
section = DEFAULTSECT
|
||||
|
||||
try:
|
||||
value = config.get(section, name)
|
||||
except (NoOptionError, NoSectionError) as e:
|
||||
if default is None:
|
||||
raise e
|
||||
value = default
|
||||
|
||||
if name.endswith('_path') or name.endswith('_dir'):
|
||||
value = os.path.expanduser(value)
|
||||
# thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873)
|
||||
elif name.endswith('_list'):
|
||||
value = csv.reader([value], delimiter=',', quotechar='"').next()
|
||||
elif name.endswith('_dict'):
|
||||
value = dict(csv.reader([item], delimiter=':', quotechar='"').next()
|
||||
for item in csv.reader([value], delimiter=',', quotechar='"').next())
|
||||
return value
|
||||
|
|
|
|||
Loading…
Reference in a new issue