本文整理汇总了Python中six.moves.configparser.ConfigParser方法的典型用法代码示例。如果您正苦于以下问题:Python configparser.ConfigParser方法的具体用法?Python configparser.ConfigParser怎么用?Python configparser.ConfigParser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.configparser
的用法示例。
在下文中一共展示了configparser.ConfigParser方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_config
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def create_config(self, config):
"""Create a config file for for a given config fname and sections."""
self.config = config
if self.config_file and self.config_sections:
new_config = configparser.ConfigParser()
new_config_file = os.path.join(self.cmd_root, self.config_file)
for (cio_config_section, config_section) in self.config_sections:
if config.has_section(cio_config_section):
items = config.items(cio_config_section)
new_config.add_section(config_section)
for option, value in items:
new_config.set(config_section, option, value)
with open(new_config_file, 'w') as file_obj:
new_config.write(file_obj)
示例2: save_config
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def save_config(config):
"""Save configuration.
:param config: Data to be written to the configuration file.
:type config: dict
"""
config_parser = ConfigParser()
config_parser.add_section("greynoise")
config_parser.set("greynoise", "api_key", config["api_key"])
config_parser.set("greynoise", "api_server", config["api_server"])
config_parser.set("greynoise", "timeout", str(config["timeout"]))
config_dir = os.path.dirname(CONFIG_FILE)
if not os.path.isdir(config_dir):
os.makedirs(config_dir)
with open(CONFIG_FILE, "w") as config_file:
config_parser.write(config_file)
示例3: parse_config_file
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def parse_config_file(self, path, section, final=True):
config_parser = configparser.ConfigParser()
if not config_parser.read(path):
raise IOError('Config file at path "{}" not found'.format(path))
try:
config = config_parser.items(section)
except KeyError:
raise ValueError('Config file does not have [{}] section]'.format(section))
for (name, value) in config:
normalized = self._normalize_name(name)
normalized = normalized.lower()
if normalized in self._options:
option = self._options[normalized]
if option.multiple:
if not isinstance(value, (list, str)):
raise Error("Option %r is required to be a list of %s "
"or a comma-separated string" %
(option.name, option.type.__name__))
if type(value) == str and option.type != str:
option.parse(value)
else:
option.set(value)
示例4: _check_project_directory
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def _check_project_directory(project_dir):
def path_if_exists(name):
path = os.path.join(project_dir, name)
if os.path.exists(path):
return path
else:
return None
app_config = None
app_config_path = path_if_exists("app.yml")
if app_config_path:
app_config = yaml.load(open(app_config_path, "r"))
assert isinstance(app_config, dict) or (app_config is None)
ini_config = None
ini_path = path_if_exists("server.ini")
if ini_path:
ini_config = configparser.ConfigParser()
ini_config.read([ini_path])
return Project(ini_config, app_config)
示例5: configure_default_options
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def configure_default_options(options):
if not options:
return
config = configparser.ConfigParser()
config.read(LOG_CREDS_FILENAME)
if not config.has_section(GLOBAL_OPTION_SECTION):
config.add_section(GLOBAL_OPTION_SECTION)
if GLOBAL_OPTION_KEY_FORMAT_OUTPUT in options:
config.set(GLOBAL_OPTION_SECTION, GLOBAL_OPTION_KEY_FORMAT_OUTPUT, options[GLOBAL_OPTION_KEY_FORMAT_OUTPUT])
if GLOBAL_OPTION_KEY_DEFAULT_CLIENT in options:
config.set(GLOBAL_OPTION_SECTION, GLOBAL_OPTION_KEY_DEFAULT_CLIENT, options[GLOBAL_OPTION_KEY_DEFAULT_CLIENT])
if GLOBAL_OPTION_KEY_DECODE_OUTPUT in options:
config.set(GLOBAL_OPTION_SECTION, GLOBAL_OPTION_KEY_DECODE_OUTPUT, options[GLOBAL_OPTION_KEY_DECODE_OUTPUT])
with open(LOG_CREDS_FILENAME, 'w') as configfile:
config.write(configfile)
示例6: _write_profile_for_sso
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def _write_profile_for_sso(
aws_profile,
sso_start_url,
sso_region,
sso_account_id,
region):
config = configparser.ConfigParser()
config.read(os.path.realpath(AWS_CONFIG_PATH))
section = 'profile {}'.format(aws_profile)
if section not in config.sections():
config.add_section(section)
config.set(section, 'sso_start_url', sso_start_url)
config.set(section, 'sso_region', sso_region)
config.set(section, 'sso_account_id', sso_account_id)
config.set(section, 'sso_role_name', 'AWSPowerUserAccess')
config.set(section, 'region', region)
config.set(section, 'output', 'json')
with open(AWS_CONFIG_PATH, 'w') as f:
config.write(f)
示例7: test_parse_auth_settings_no_config
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def test_parse_auth_settings_no_config(self):
"""
Test that server authentication plugin settings are parsed correctly,
even when not specified.
"""
parser = configparser.ConfigParser()
parser.add_section('server')
c = config.KmipServerConfig()
c._logger = mock.MagicMock()
self.assertEqual([], c.settings['auth_plugins'])
c.parse_auth_settings(parser)
configs = c.settings['auth_plugins']
self.assertIsInstance(configs, list)
self.assertEqual(0, len(configs))
示例8: __init__
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def __init__(self, path=None):
self.logger = logging.getLogger(__name__)
# DEBUG logging here may expose passwords, so log at INFO by default.
# However, if consumers know the risks, let them go ahead and override.
if self.logger.level == logging.NOTSET:
self.logger.setLevel(logging.INFO)
self.conf = ConfigParser()
filenames = path
if not path:
filenames = CONFIG_FILE
if self.conf.read(filenames):
self.logger.debug("Using config file at {0}".format(filenames))
else:
self.logger.warning(
"Config file {0} not found".format(filenames))
示例9: parse_config_dict
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def parse_config_dict(config_dict):
"""Parse the given config from a dictionary, populating missing items and sections
Args:
config_dict (dict): the configuration dictionary to be parsed
"""
# Build a config dictionary from the defaults merged with the given dictionary
config = copy.deepcopy(CONFIG_DEFAULTS)
for section, section_dict in config_dict.items():
if section not in config:
config[section] = {}
for option in section_dict.keys():
config[section][option] = config_dict[section][option]
# Build a ConfigParser from the merged dictionary
cfg = configparser.ConfigParser()
for section, section_dict in config.items():
cfg.add_section(section)
for option, value in section_dict.items():
cfg.set(section, option, value)
return cfg
示例10: parse_config_file
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def parse_config_file(config_file):
"""Parse the given config from a filepath, populating missing items and
sections
Args:
config_file (str): the file to be parsed
"""
# if the config file doesn't exist, prepopulate the config object
# with the defaults, in the right section.
#
# otherwise, we have to put the defaults in the DEFAULT section,
# to ensure that they don't override anyone's settings which are
# in their config file in the default section (which is likely,
# because sydent used to be braindead).
use_defaults = not os.path.exists(config_file)
cfg = configparser.ConfigParser()
for sect, entries in CONFIG_DEFAULTS.items():
cfg.add_section(sect)
for k, v in entries.items():
cfg.set(configparser.DEFAULTSECT if use_defaults else sect, k, v)
cfg.read(config_file)
return cfg
示例11: _load_config
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def _load_config():
global g_default_region, g_default_ak_id, g_default_ak_key, g_default_project, g_default_logstore
def _get_section_option(config, section_name, option_name, default=None):
if six.PY3:
return config.get(section_name, option_name, fallback=default)
else:
return config.get(section_name, option_name) if config.has_option(section_name, option_name) else default
config = configparser.ConfigParser()
config.read(CLI_CONFIG_FILENAME)
g_default_region = _get_section_option(config, MAGIC_SECTION, "region-endpoint", "")
g_default_ak_id = _get_section_option(config, MAGIC_SECTION, "access-id", "")
g_default_ak_key = _get_section_option(config, MAGIC_SECTION, "access-key", "")
g_default_project = _get_section_option(config, MAGIC_SECTION, "project", "")
g_default_logstore = _get_section_option(config, MAGIC_SECTION, "logstore", "")
示例12: _save_config
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def _save_config(region, ak_id, ak_key, project, logstore):
global g_default_region, g_default_ak_id, g_default_ak_key, g_default_project, g_default_logstore
config = configparser.ConfigParser()
config.read(CLI_CONFIG_FILENAME)
if not config.has_section(MAGIC_SECTION):
config.add_section(MAGIC_SECTION)
config.set(MAGIC_SECTION, "region-endpoint", region)
config.set(MAGIC_SECTION, "access-id", ak_id)
config.set(MAGIC_SECTION, "access-key", ak_key)
config.set(MAGIC_SECTION, "project", project)
config.set(MAGIC_SECTION, "logstore", logstore)
# save to disk
with open(CLI_CONFIG_FILENAME, 'w') as configfile:
config.write(configfile)
# save to memory
g_default_region, g_default_ak_id, g_default_ak_key, g_default_project, g_default_logstore = region, ak_id, ak_key, project, logstore
示例13: __init__
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def __init__(self, filename=None):
"""
WhichModel is a copy of initialization features inside the main class
"""
self.filename = filename
if self.filename:
try:
# only let this function imoprt things once
self.whichModel_AlreadyRun
except:
# Open parser and get what kind of model
_fileisvalid = self.config = configparser.ConfigParser()
_fileisvalid = len(_fileisvalid)
if _fileisvalid:
try:
self.config.read(filename)
# Need to change this and all slashes to be Windows compatible
self.inpath = os.path.dirname(os.path.realpath(filename)) + '/'
# Need to have these guys inside "try" to make sure it is set up OK
# (at least for them)
self.dimension = self.configGet("integer", "mode", "dimension")
self.whichModel_AlreadyRun = True
except:
sys.exit(">>>> Error: cannot locate specified configuration file. <<<<")
示例14: read_config_file
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def read_config_file(self, file="./config.ini"):
#read config file
Config = configparser.ConfigParser()
try:
Config.read(file)
except Exception as e:
self.log.warn("Error reading config file %s" %e)
self.log.info("No Roomba specified, and no config file found - "
"attempting discovery")
if Password(self.address, file):
return self.read_config_file(file)
else:
return False
self.log.info("reading info from config file %s" % file)
addresses = Config.sections()
if self.address is None:
if len(addresses) > 1:
self.log.warn("config file has entries for %d Roombas, "
"only configuring the first!")
self.address = addresses[0]
self.blid = Config.get(self.address, "blid"),
self.password = Config.get(self.address, "password")
# self.roombaName = literal_eval(
# Config.get(self.address, "data"))["robotname"]
return True
示例15: __init__
# 需要导入模块: from six.moves import configparser [as 别名]
# 或者: from six.moves.configparser import ConfigParser [as 别名]
def __init__(self):
self.config = configparser.ConfigParser({
'firsttime' : 'yes',
'style' : 'default'
})
self.config.add_section('Help Files')
self.config.add_section('Layout')
self.config.set('Help Files', 'command', 'help_dump.json')
self.config.set('Help Files', 'history', 'history.txt')
self.config.set('Layout', 'command_description', 'yes')
self.config.set('Layout', 'param_description', 'yes')
self.config.set('Layout', 'examples', 'yes')
azure_folder = get_config_dir()
if not os.path.exists(azure_folder):
os.makedirs(azure_folder)
if not os.path.exists(os.path.join(get_config_dir(), CONFIG_FILE_NAME)):
with open(os.path.join(get_config_dir(), CONFIG_FILE_NAME), 'w') as config_file:
self.config.write(config_file)
else:
with open(os.path.join(get_config_dir(), CONFIG_FILE_NAME), 'r') as config_file:
self.config.readfp(config_file) # pylint: disable=deprecated-method
self.update()