comparison sat/tools/config.py @ 3031:98d1f34ce5b9

tools (config), memory: renamed SafeConfigParser following Python 3 port
author Goffi <goffi@goffi.org>
date Fri, 16 Aug 2019 17:06:43 +0200
parents ab2696e34d29
children 8b36e5c3f28f
comparison
equal deleted inserted replaced
3030:e80694d536d7 3031:98d1f34ce5b9
21 """ Configuration related useful methods """ 21 """ Configuration related useful methods """
22 22
23 import os 23 import os
24 import csv 24 import csv
25 import json 25 import json
26 from configparser import SafeConfigParser, DEFAULTSECT, NoOptionError, NoSectionError 26 from configparser import ConfigParser, DEFAULTSECT, NoOptionError, NoSectionError
27 from xdg import BaseDirectory 27 from xdg import BaseDirectory
28 from sat.core.log import getLogger 28 from sat.core.log import getLogger
29 from sat.core.constants import Const as C 29 from sat.core.constants import Const as C
30 from sat.core.i18n import _ 30 from sat.core.i18n import _
31 from sat.core import exceptions 31 from sat.core import exceptions
40 @param section (str): the config section 40 @param section (str): the config section
41 @param option (str): the config option 41 @param option (str): the config option
42 @param value (str): the new value 42 @param value (str): the new value
43 @param silent (boolean): toggle logging output (must be True when called from sat.sh) 43 @param silent (boolean): toggle logging output (must be True when called from sat.sh)
44 """ 44 """
45 config = SafeConfigParser() 45 config = ConfigParser()
46 target_file = None 46 target_file = None
47 for file_ in C.CONFIG_FILES[::-1]: 47 for file_ in C.CONFIG_FILES[::-1]:
48 # we will eventually update the existing file with the highest priority, 48 # we will eventually update the existing file with the highest priority,
49 # if it's a user personal file... 49 # if it's a user personal file...
50 if not silent: 50 if not silent:
70 config_file=target_file)) 70 config_file=target_file))
71 71
72 72
73 def parseMainConf(): 73 def parseMainConf():
74 """look for main .ini configuration file, and parse it""" 74 """look for main .ini configuration file, and parse it"""
75 config = SafeConfigParser(defaults=C.DEFAULT_CONFIG) 75 config = ConfigParser(defaults=C.DEFAULT_CONFIG)
76 try: 76 try:
77 config.read(C.CONFIG_FILES) 77 config.read(C.CONFIG_FILES)
78 except Exception as e: 78 except Exception as e:
79 log.error(_("Can't read main config: {msg}").format(msg=e), exc_info=True) 79 log.error(_("Can't read main config: {msg}").format(msg=e), exc_info=True)
80 return config 80 return config
81 81
82 82
83 def getConfig(config, section, name, default=None): 83 def getConfig(config, section, name, default=None):
84 """Get a configuration option 84 """Get a configuration option
85 85
86 @param config (SafeConfigParser): the configuration instance 86 @param config (ConfigParser): the configuration instance
87 @param section (str): section of the config file (None or '' for DEFAULT) 87 @param section (str): section of the config file (None or '' for DEFAULT)
88 @param name (str): name of the option 88 @param name (str): name of the option
89 @param default: value to use if not found, or Exception to raise an exception 89 @param default: value to use if not found, or Exception to raise an exception
90 @return (unicode, list, dict): parsed value 90 @return (unicode, list, dict): parsed value
91 @raise: NoOptionError if option is not present and default is Exception 91 @raise: NoOptionError if option is not present and default is Exception