本文整理汇总了Python中configparser.SafeConfigParser方法的典型用法代码示例。如果您正苦于以下问题:Python configparser.SafeConfigParser方法的具体用法?Python configparser.SafeConfigParser怎么用?Python configparser.SafeConfigParser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类configparser
的用法示例。
在下文中一共展示了configparser.SafeConfigParser方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_from_file
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def load_from_file(path):
"""
Load settings from an INI file.
This will check the configuration file for a lowercase version of all of
the settings in this module. It will look in a section called "base".
The example below will set PORTAL_SERVER_PORT.
[base]
portal_server_port = 4444
"""
config = configparser.SafeConfigParser()
config.read(path)
mod = sys.modules[__name__]
for name, _ in iterate_module_attributes(mod):
# Check if lowercase version exists in the file and load the
# appropriately-typed value.
key = name.lower()
if config.has_option(constants.BASE_SETTINGS_SECTION, key):
value = config.get(constants.BASE_SETTINGS_SECTION, key)
setattr(mod, name, parseValue(value))
示例2: get_config_from_root
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def get_config_from_root(root):
# This might raise EnvironmentError (if setup.cfg is missing), or
# configparser.NoSectionError (if it lacks a [versioneer] section), or
# configparser.NoOptionError (if it lacks "VCS="). See the docstring at
# the top of versioneer.py for instructions on writing your setup.cfg .
setup_cfg = os.path.join(root, "setup.cfg")
parser = configparser.SafeConfigParser()
with open(setup_cfg, "r") as f:
parser.readfp(f)
VCS = parser.get("versioneer", "VCS") # mandatory
def get(parser, name):
if parser.has_option("versioneer", name):
return parser.get("versioneer", name)
return None
cfg = VersioneerConfig()
cfg.VCS = VCS
cfg.style = get(parser, "style") or ""
cfg.versionfile_source = get(parser, "versionfile_source")
cfg.versionfile_build = get(parser, "versionfile_build")
cfg.tag_prefix = get(parser, "tag_prefix")
cfg.parentdir_prefix = get(parser, "parentdir_prefix")
cfg.verbose = get(parser, "verbose")
return cfg
示例3: __init__
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def __init__(self):
if sys.version_info < (3, 2):
self.parser = ConfigParser.SafeConfigParser(os.environ)
else:
self.parser = ConfigParser.ConfigParser(defaults=os.environ)
if 'RUCIO_CONFIG' in os.environ:
self.configfile = os.environ['RUCIO_CONFIG']
else:
configs = [os.path.join(confdir, 'rucio.cfg') for confdir in get_config_dirs()]
self.configfile = next(iter(filter(os.path.exists, configs)), None)
if self.configfile is None:
raise RuntimeError('Could not load Rucio configuration file. '
'Rucio looked in the following paths for a configuration file, in order:'
'\n\t' + '\n\t'.join(configs))
if not self.parser.read(self.configfile) == [self.configfile]:
raise RuntimeError('Could not load Rucio configuration file. '
'Rucio tried loading the following configuration file:'
'\n\t' + self.configfile)
示例4: get_config_from_root
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def get_config_from_root(root):
"""Read the project setup.cfg file to determine Versioneer config."""
# This might raise EnvironmentError (if setup.cfg is missing), or
# configparser.NoSectionError (if it lacks a [versioneer] section), or
# configparser.NoOptionError (if it lacks "VCS="). See the docstring at
# the top of versioneer.py for instructions on writing your setup.cfg .
setup_cfg = os.path.join(root, "setup.cfg")
parser = configparser.SafeConfigParser()
with open(setup_cfg, "r") as f:
parser.readfp(f)
VCS = parser.get("versioneer", "VCS") # mandatory
def get(parser, name):
if parser.has_option("versioneer", name):
return parser.get("versioneer", name)
return None
cfg = VersioneerConfig()
cfg.VCS = VCS
cfg.style = get(parser, "style") or ""
cfg.versionfile_source = get(parser, "versionfile_source")
cfg.versionfile_build = get(parser, "versionfile_build")
cfg.tag_prefix = get(parser, "tag_prefix")
if cfg.tag_prefix in ("''", '""'):
cfg.tag_prefix = ""
cfg.parentdir_prefix = get(parser, "parentdir_prefix")
cfg.verbose = get(parser, "verbose")
return cfg
示例5: create
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def create():
this.config = SafeConfigParser()
return this.config
示例6: load
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def load(filename):
this.config = SafeConfigParser()
load_result = this.config.read(os.path.expanduser(filename))
if len(load_result) != 1:
raise ConfigFileNotFound("The configuration file %s does not exist" % filename)
this.filename = os.path.expanduser(filename)
示例7: get_config_map
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def get_config_map():
mysql_config_map = {}
if_config_map = {}
home_path = os.getcwd()
try:
if os.path.exists(os.path.join(home_path, Constant.CONFIG_FILE)):
parser = SafeConfigParser()
parser.read(os.path.join(home_path, Constant.CONFIG_FILE))
# MySql config
mysql_config_map[Constant.HOST_CONFIG] = parser.get(Constant.MYSQL_TAG, Constant.HOST_CONFIG)
mysql_config_map[Constant.DATABASE_CONFIG] = parser.get(Constant.MYSQL_TAG, Constant.DATABASE_CONFIG)
mysql_config_map[Constant.USER_CONFIG] = parser.get(Constant.MYSQL_TAG, Constant.USER_CONFIG)
mysql_config_map[Constant.PASSWORD_CONFIG] = parser.get(Constant.MYSQL_TAG, Constant.PASSWORD_CONFIG)
mysql_config_map[Constant.TABLE_CONFIG] = parser.get(Constant.MYSQL_TAG, Constant.TABLE_CONFIG)
mysql_config_map[Constant.INSTANCE_NAME_FIELD_CONFIG] = parser.get(Constant.MYSQL_TAG, Constant.INSTANCE_NAME_FIELD_CONFIG)
mysql_config_map[Constant.TIMESTAMP_FIELD_CONFIG] = parser.get(Constant.MYSQL_TAG, Constant.TIMESTAMP_FIELD_CONFIG)
mysql_config_map[Constant.TIMESTAMP_FORMAT_CONFIG] = parser.get(Constant.MYSQL_TAG, Constant.TIMESTAMP_FORMAT_CONFIG, raw=True)
check_config_parameters(mysql_config_map)
# InsightFinder config
if_config_map[Constant.LICENSE_KEY_CONFIG] = parser.get(Constant.IF_TAG, Constant.LICENSE_KEY_CONFIG)
if_config_map[Constant.PROJECT_NAME_CONFIG] = parser.get(Constant.IF_TAG, Constant.PROJECT_NAME_CONFIG)
if_config_map[Constant.USER_NAME_CONFIG] = parser.get(Constant.IF_TAG, Constant.USER_NAME_CONFIG)
if_config_map[Constant.SERVER_URL_CONFIG] = parser.get(Constant.IF_TAG, Constant.SERVER_URL_CONFIG)
if_config_map[Constant.SAMPLING_INTERVAL] = parser.get(Constant.IF_TAG, Constant.SAMPLING_INTERVAL)
check_config_parameters(if_config_map)
print("Loaded all parameters from config file")
except IOError:
print("config.ini file is missing")
return mysql_config_map, if_config_map
示例8: get_config_map
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def get_config_map():
mssql_config_map = {}
if_config_map = {}
home_path = os.getcwd()
try:
if os.path.exists(os.path.join(home_path, Constant.CONFIG_FILE)):
parser = SafeConfigParser()
parser.read(os.path.join(home_path, Constant.CONFIG_FILE))
# MSSql config
mssql_config_map[Constant.HOST_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.HOST_CONFIG)
mssql_config_map[Constant.DATABASE_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.DATABASE_CONFIG)
mssql_config_map[Constant.USER_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.USER_CONFIG)
mssql_config_map[Constant.PASSWORD_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.PASSWORD_CONFIG)
mssql_config_map[Constant.TABLE_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.TABLE_CONFIG)
mssql_config_map[Constant.DRIVER_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.DRIVER_CONFIG)
mssql_config_map[Constant.INSTANCE_NAME_FIELD_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.INSTANCE_NAME_FIELD_CONFIG)
mssql_config_map[Constant.TIMESTAMP_FIELD_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.TIMESTAMP_FIELD_CONFIG)
mssql_config_map[Constant.TIMESTAMP_FORMAT_CONFIG] = parser.get(Constant.MSSQL_TAG, Constant.TIMESTAMP_FORMAT_CONFIG, raw=True)
check_config_parameters(mssql_config_map)
# InsightFinder config
if_config_map[Constant.LICENSE_KEY_CONFIG] = parser.get(Constant.IF_TAG, Constant.LICENSE_KEY_CONFIG)
if_config_map[Constant.PROJECT_NAME_CONFIG] = parser.get(Constant.IF_TAG, Constant.PROJECT_NAME_CONFIG)
if_config_map[Constant.USER_NAME_CONFIG] = parser.get(Constant.IF_TAG, Constant.USER_NAME_CONFIG)
if_config_map[Constant.SERVER_URL_CONFIG] = parser.get(Constant.IF_TAG, Constant.SERVER_URL_CONFIG)
if_config_map[Constant.SAMPLING_INTERVAL] = parser.get(Constant.IF_TAG, Constant.SAMPLING_INTERVAL)
check_config_parameters(if_config_map)
print("Loaded all parameters from config file")
except IOError:
print("config.ini file is missing")
return mssql_config_map, if_config_map
示例9: get
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def get(self, key, filepath):
"""Get configuration parameter.
Reads 'key' configuration parameter from the configuration file given
in 'filepath'. Configuration parameter in 'key' must follow the schema
<section>.<option> .
:param key: key to get
:param filepath: configuration file
"""
if not filepath:
raise RuntimeError("Configuration file not given")
if not self.__check_config_key(key):
raise RuntimeError("%s parameter does not exists" % key)
if not os.path.isfile(filepath):
raise RuntimeError("%s config file does not exist" % filepath)
section, option = key.split('.')
config = configparser.SafeConfigParser()
config.read(filepath)
try:
option = config.get(section, option)
self.display('config.tmpl', key=key, option=option)
except (configparser.NoSectionError, configparser.NoOptionError):
pass
return CMD_SUCCESS
示例10: set
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def set(self, key, value, filepath):
"""Set configuration parameter.
Writes 'value' on 'key' to the configuration file given in
'filepath'. Configuration parameter in 'key' must follow the schema
<section>.<option> .
:param key: key to set
:param value: value to set
:param filepath: configuration file
"""
if not filepath:
raise RuntimeError("Configuration file not given")
if not self.__check_config_key(key):
raise RuntimeError("%s parameter does not exists or cannot be set" % key)
config = configparser.SafeConfigParser()
if os.path.isfile(filepath):
config.read(filepath)
section, option = key.split('.')
if section not in config.sections():
config.add_section(section)
try:
config.set(section, option, value)
except TypeError as e:
raise RuntimeError(str(e))
try:
with open(filepath, 'w') as f:
config.write(f)
except IOError as e:
raise RuntimeError(str(e))
return CMD_SUCCESS
示例11: test_set_value
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def test_set_value(self):
"""Check set method"""
import shutil
import tempfile
# Copy the reference config file to a temporary directory
testpath = tempfile.mkdtemp(prefix='sortinghat_')
shutil.copy(MOCK_CONFIG_FILE, testpath)
filepath = os.path.join(testpath, 'mock_config_file.cfg')
# First read initial values
config = configparser.SafeConfigParser()
config.read(filepath)
self.assertEqual(config.get('db', 'user'), 'root')
self.assertEqual(config.get('db', 'database'), 'testdb')
# Set the new values
retcode = self.cmd.set('db.user', 'jsmith', filepath)
self.assertEqual(retcode, CMD_SUCCESS)
retcode = self.cmd.set('db.database', 'mydb', filepath)
self.assertEqual(retcode, CMD_SUCCESS)
# Check the new values
config.read(filepath)
self.assertEqual(config.get('db', 'user'), 'jsmith')
self.assertEqual(config.get('db', 'password'), '****')
self.assertEqual(config.get('db', 'database'), 'mydb')
shutil.rmtree(testpath)
示例12: main
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def main():
print('Usage: Pressing just the [Enter] key will display the latest voltage readings, type ^C to exit')
print(' Type in the cell # that you want to calibrate followed by [Enter]')
print(' Then type in the mesured voltage for that cell followed by [Enter]')
print()
print(' Make sure the Battery Monitoring software is running otherwise the summary file will not be current')
print(' This program uses the data from the log file generated by the Battery Monitoring software')
print(' Make sure that the Battery Monitoring software is logging to a real file and not /dev/null')
print(' This program averages the data from the log file for the last 100 seconds.')
print(' To make sure that the calibration is accurate make sure the input voltages are not varying')
avvolts=[]
while True:
try:
loadconfig()
avvolts=avv()
# time.sleep(60.0)
what = input(">")
if len(what)>0:
realvolts = eval(input("Cell " + what + " reading "))
what=int(what)
config['calibrate']['measured'][what] = round(realvolts,3)
config['calibrate']['displayed'][what] = round(avvolts[what],3)
batconfigdata=SafeConfigParser()
batconfigdata.read('battery.cfg')
batconfigdata.set('calibrate','measured',str(config['calibrate']['measured']))
batconfigdata.set('calibrate','displayed',str(config['calibrate']['displayed']))
with open('battery.cfg', 'w') as batconfig:
batconfigdata.write(batconfig)
batconfig.closed
except KeyboardInterrupt:
break
示例13: main
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def main():
print('Usage: Pressing just the [Enter] key will display the latest voltage readings, type ^C to exit')
print(' Type in the cell # that you want to calibrate followed by [Enter]')
print(' Then type in the mesured voltage for that cell followed by [Enter]')
print()
print(' Make sure the Battery Monitoring software is running otherwise the summary file will not be current')
print(' This program uses the data from the log file generated by the Battery Monitoring software')
print(' Make sure that the Battery Monitoring software is logging to a real file and not /dev/null')
print(' This program averages the data from the log file for the last 100 seconds.')
print(' To make sure that the calibration is accurate make sure the input voltages are not varying')
avvolts=[]
while True:
try:
loadconfig()
avvolts=avv()
# time.sleep(60.0)
what = input(">")
if len(what)>0:
realvolts = eval(input("Cell " + what + " reading "))
what=int(what)
config['calibrate']['delta'][what-1] = round(avvolts[what-1]-realvolts+config['calibrate']['delta'][what-1],3)
batconfigdata=SafeConfigParser()
batconfigdata.read('battery.cfg')
batconfigdata.set('calibrate','delta',str(config['calibrate']['delta']))
with open('battery.cfg', 'w') as batconfig:
batconfigdata.write(batconfig)
batconfig.closed
except KeyboardInterrupt:
break
示例14: main
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def main():
print('Usage: Pressing just the [Enter] key will display the latest voltage readings, type ^C to exit')
print(' Press any other key to change the voltage calibration of all the voltage readings')
print()
print(' Make sure the Battery Monitoring software is running otherwise the summary file will not be current')
print(' This program uses the data from the log file generated by the Battery Monitoring software')
print(' Make sure that the Battery Monitoring software is logging to a real file and not /dev/null')
print(' This program averages the data from the log file for the last 100 seconds.')
print(' To make sure that the calibration is accurate make sure the input voltages are not varying')
avvolts=[]
while True:
try:
loadconfig()
avvolts=avv()
# time.sleep(60.0)
what = input(">")
if len(what)>0:
realvolts = eval(input("All Cell readings "))
for i in range(numcells):
config['calibrate']['delta'][i] = round(avvolts[i]-realvolts+config['calibrate']['delta'][i],3)
print(config['calibrate']['delta'])
batconfigdata=SafeConfigParser()
batconfigdata.read('battery.cfg')
batconfigdata.set('calibrate','delta',str(config['calibrate']['delta']))
with open('battery.cfg', 'w') as batconfig:
batconfigdata.write(batconfig)
batconfig.closed
except KeyboardInterrupt:
break
示例15: main
# 需要导入模块: import configparser [as 别名]
# 或者: from configparser import SafeConfigParser [as 别名]
def main():
print('Usage: At > prompt enter current input channel number to change then [Enter] key')
print(' Pressing just the [Enter] key will display the latest current readings, type ^C to exit')
print()
print(' Make sure the Battery Monitoring software is running otherwise the summary file will not be current')
print(' This program uses the data from the log file generated by the Battery Monitoring software')
print(' Make sure that the Battery Monitoring software is logging to a real file and not /dev/null')
print(' This program averages the data from the log file for the last 100 seconds.')
print(' To make sure that the calibartion is accurate make sure the current on the input being calibrated is not varying')
while True:
try:
# time.sleep(60.0)
geti()
what = input(">")
if len(what)>0:
avi=getavi()
reali = eval(input("Input " + what + " multimeter reading "))
if reali == 0:
print("Input Error: Zero not valid current input")
else:
what=int(what)
config['calibrate']['currentgain'][what-1] = config['calibrate']['currentgain'][what-1]*reali/avi[what-1]
print('Recalculated gain: ' + str(config['calibrate']['currentgain']))
batconfigdata=SafeConfigParser()
batconfigdata.read('battery.cfg')
batconfigdata.set('calibrate','currentgain',str(config['calibrate']['currentgain']))
with open('battery.cfg', 'w') as batconfig:
batconfigdata.write(batconfig)
batconfig.closed
except KeyboardInterrupt:
break