本文整理汇总了Python中six.moves.configparser.ConfigParser.readfp方法的典型用法代码示例。如果您正苦于以下问题:Python ConfigParser.readfp方法的具体用法?Python ConfigParser.readfp怎么用?Python ConfigParser.readfp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.configparser.ConfigParser
的用法示例。
在下文中一共展示了ConfigParser.readfp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_configuration
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def get_configuration(configuration=None):
"""
Parse a configuration file
Parameters
----------
configuration : str or list, optional
A configuration file or list of configuration files to parse,
defaults to the deploy_default.conf file in the package and
deploy.conf in the current working directory.
Returns
-------
configparser
The parsed configuration
"""
if not configuration: # pragma: no cover
configuration = [
# Config file that is part of the package
# PACKAGE_DEFAULT_CONFIG,
# Any deploy.conf files in the current directory
'deploy.conf'
]
config = ConfigParser()
# Set the config defaults
try:
config.read_string(config_defaults())
except AttributeError:
config.readfp(io.BytesIO(config_defaults()))
logger.debug('Working with default dict: %r', config_defaults())
config.read(configuration)
return config
示例2: handleSection
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def handleSection(self, section, items):
locales = items['locales']
if locales == 'all':
inipath = '/'.join((
items['repo'], items['mozilla'],
'raw-file', 'default',
items['l10n.ini']
))
ini = ConfigParser()
ini.readfp(urlopen(inipath))
allpath = urljoin(
urljoin(inipath, ini.get('general', 'depth')),
ini.get('general', 'all'))
locales = urlopen(allpath).read()
locales = locales.split()
obs = (Active.objects
.filter(run__tree__code=section)
.exclude(run__locale__code__in=locales)
.order_by('run__locale__code'))
obslocs = ' '.join(obs.values_list('run__locale__code', flat=True))
if not obslocs:
self.stdout.write(' OK\n')
return
s = input('Remove %s? [Y/n] ' % obslocs)
if s.lower() == 'y' or s == '':
obs.delete()
示例3: _get_attach_points
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def _get_attach_points(self, info, size_request):
has_attach_points_, attach_points = info.get_attach_points()
attach_x = attach_y = 0
if attach_points:
# this works only for Gtk < 3.14
# https://developer.gnome.org/gtk3/stable/GtkIconTheme.html
# #gtk-icon-info-get-attach-points
attach_x = float(attach_points[0].x) / size_request
attach_y = float(attach_points[0].y) / size_request
elif info.get_filename():
# try read from the .icon file
icon_filename = info.get_filename().replace('.svg', '.icon')
if icon_filename != info.get_filename() and \
os.path.exists(icon_filename):
try:
with open(icon_filename) as config_file:
cp = ConfigParser()
cp.readfp(config_file)
attach_points_str = cp.get('Icon Data', 'AttachPoints')
attach_points = attach_points_str.split(',')
attach_x = float(attach_points[0].strip()) / 1000
attach_y = float(attach_points[1].strip()) / 1000
except Exception as e:
logging.exception('Exception reading icon info: %s', e)
return attach_x, attach_y
示例4: get_config
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def get_config(p):
"""Read a config file.
:return: dict of ('section.option', value) pairs.
"""
cfg = {}
parser = ConfigParser()
if hasattr(parser, 'read_file'):
parser.read_file(Path(p).open(encoding='utf8'))
else: # pragma: no cover
assert PY2
# The `read_file` method is not available on ConfigParser in py2.7!
parser.readfp(Path(p).open(encoding='utf8'))
for section in parser.sections():
getters = {
'int': partial(parser.getint, section),
'boolean': partial(parser.getboolean, section),
'float': partial(parser.getfloat, section),
'list': lambda option: parser.get(section, option).split(),
}
default = partial(parser.get, section)
for option in parser.options(section):
type_ = option.rpartition('_')[2] if '_' in option else None
value = getters.get(type_, default)(option)
cfg['{0}.{1}'.format(section, option)] = value
return cfg
示例5: read_config_file
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def read_config_file(cfgfile, options):
config = ConfigParser()
config.readfp(open(cfgfile))
if config.has_option('testflo', 'skip_dirs'):
skips = config.get('testflo', 'skip_dirs')
options.skip_dirs = [s.strip() for s in skips.split(',') if s.strip()]
示例6: read_mcf
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def read_mcf(mcf):
"""returns dict of ConfigParser object"""
mcf_list = []
def makelist(mcf2):
"""recursive function for MCF by reference inclusion"""
c = ConfigParser()
LOGGER.debug('reading {}'.format(mcf2))
with codecs.open(mcf2, encoding='utf-8') as fh:
c.readfp(fh)
mcf_dict = c.__dict__['_sections']
for section in mcf_dict.keys():
if 'base_mcf' in mcf_dict[section]:
base_mcf_path = get_abspath(mcf,
mcf_dict[section]['base_mcf'])
makelist(base_mcf_path)
mcf_list.append(mcf2)
else: # leaf
mcf_list.append(mcf2)
makelist(mcf)
c = ConfigParser()
for mcf_file in mcf_list:
LOGGER.debug('reading {}'.format(mcf))
with codecs.open(mcf_file, encoding='utf-8') as fh:
c.readfp(fh)
mcf_dict = c.__dict__['_sections']
return mcf_dict
示例7: INIReader
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
class INIReader(object):
"""ConfigParser wrapper able to cast value when reading INI options."""
# Helper casters
cast_boolean = casts.Boolean()
cast_dict = casts.Dict()
cast_list = casts.List()
cast_logging_level = casts.LoggingLevel()
cast_tuple = casts.Tuple()
cast_webdriver_desired_capabilities = casts.WebdriverDesiredCapabilities()
def __init__(self, path):
self.config_parser = ConfigParser()
with open(path) as handler:
self.config_parser.readfp(handler)
if sys.version_info[0] < 3:
# ConfigParser.readfp is deprecated on Python3, read_file
# replaces it
self.config_parser.readfp(handler)
else:
self.config_parser.read_file(handler)
def get(self, section, option, default=None, cast=None):
"""Read an option from a section of a INI file.
The default value will return if the look up option is not available.
The value will be cast using a callable if specified otherwise a string
will be returned.
:param section: Section to look for.
:param option: Option to look for.
:param default: The value that should be used if the option is not
defined.
:param cast: If provided the value will be cast using the cast
provided.
"""
try:
value = self.config_parser.get(section, option)
if cast is not None:
if cast is bool:
value = self.cast_boolean(value)
elif cast is dict:
value = self.cast_dict(value)
elif cast is list:
value = self.cast_list(value)
elif cast is tuple:
value = self.cast_tuple(value)
else:
value = cast(value)
except (NoSectionError, NoOptionError):
value = default
return value
def has_section(self, section):
"""Check if section is available."""
return self.config_parser.has_section(section)
示例8: handleApps
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def handleApps(self, **kwargs):
l10nbuilds = urlopen(
'https://raw.githubusercontent.com/Pike/master-ball/'
'master/l10n-master/l10nbuilds.ini')
cp = ConfigParser()
cp.readfp(l10nbuilds)
for section in cp.sections():
self.stdout.write(section + '\n')
self.handleSection(section, dict(cp.items(section)))
示例9: get_configuration_dict
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def get_configuration_dict(configuration=None, value_types=None):
"""
Parse the configuration files
Parameters
----------
configuration : str or list, optional
A configuration file or list of configuration files to parse,
defaults to the deploy_default.conf file in the package and
deploy.conf in the current working directory.
value_types : dict, optional
Dictionary containing classes to apply to specific items
Returns
-------
dict
Configuration dictionary
"""
if not value_types: # pragma: no cover
value_types = config_types()
if configuration is None or configuration is '': # pragma: no cover
configuration = [
# Config file that is part of the package
# PACKAGE_DEFAULT_CONFIG,
# Any deploy.conf files in the current directory
'deploy.conf'
]
config = ConfigParser()
# Set the config defaults
try:
config.read_string(config_defaults())
except AttributeError:
config.readfp(io.BytesIO(config_defaults()))
logger.debug('Working with default dict: %r', config_defaults())
config.read(configuration)
result_dict = {}
for section in config.sections():
result_dict[section] = {}
for key, val in config.items(section):
result_dict[section][key] = str_format_env(val)
config_update(result_dict)
if 'locations' not in result_dict.keys():
result_dict['locations'] = {}
result_dict['locations']['package_scripts'] = package_scripts_directory()
if not result_dict['global'].get('virtualenv_dir', None):
result_dict['global']['virtualenv_dir'] = \
default_virtualenv_directory()
cast_types(result_dict)
return result_dict
示例10: _parseINI
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def _parseINI(text):
from six.moves.configparser import ConfigParser
from six.moves import cStringIO
parser = ConfigParser()
try:
parser.read_file(cStringIO(text))
except AttributeError: # Python 2
parser.readfp(cStringIO(text))
return parser
示例11: _load
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def _load(self):
self.files = self.options
config = ConfigParser()
for filepath in self.files:
with codecs.open(filepath, 'r', encoding='utf-8') as stream:
fakefile = StringIO("[top]\n" + stream.read())
config.readfp(fakefile)
return [self._l10n2rec(key, config.get('top', key))
for key in config.options('top')]
示例12: load_config
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def load_config(filename):
section = "root"
try:
config_text = "[%s]\n%s" % (section, open(filename).read())
except IOError as e:
sys.stderr.write("load_config: %s\n" % e)
config_text = "[%s]\n" % section
config = ConfigParser()
config.readfp(StringIO(config_text))
return config.items(section)
示例13: load_theme
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def load_theme(struct, path, colors, default_colors):
theme = ConfigParser()
with open(path, 'r') as f:
theme.readfp(f)
for k, v in chain(theme.items('syntax'), theme.items('interface')):
if theme.has_option('syntax', k):
colors[k] = theme.get('syntax', k)
else:
colors[k] = theme.get('interface', k)
# Check against default theme to see if all values are defined
for k, v in iteritems(default_colors):
if k not in colors:
colors[k] = v
示例14: put_ini
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def put_ini(self, text):
"""
"""
context = self.context
parser = ConfigParser()
try:
parser.read_file(cStringIO(text))
except AttributeError: # Python 2
parser.readfp(cStringIO(text))
for option, value in parser.defaults().items():
prop_type = context.getPropertyType(option)
if prop_type is None:
context._setProperty(option, value, 'string')
else:
context._updateProperty(option, value)
示例15: _load_config
# 需要导入模块: from six.moves.configparser import ConfigParser [as 别名]
# 或者: from six.moves.configparser.ConfigParser import readfp [as 别名]
def _load_config(no_cfgfile=False):
config = ConfigParser()
config.optionxform = str # make it preserve case
# defaults
if not six.PY3:
config.readfp(BytesIO(_DEFAULT_CONFIG))
else:
config.read_file(StringIO(_DEFAULT_CONFIG))
# update from config file
if not no_cfgfile:
config.read(os.path.join(_STASH_ROOT, f) for f in _STASH_CONFIG_FILES)
return config