本文整理汇总了Python中six.moves.configparser.SafeConfigParser.items方法的典型用法代码示例。如果您正苦于以下问题:Python SafeConfigParser.items方法的具体用法?Python SafeConfigParser.items怎么用?Python SafeConfigParser.items使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.configparser.SafeConfigParser
的用法示例。
在下文中一共展示了SafeConfigParser.items方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Config
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
class Config(object):
def __init__(self):
self.config = SafeConfigParser()
self.name = ''
def parse(self, fname, override):
if override:
override = [x for x in csv.reader(
' '.join(override).split(','), delimiter='.')]
logger.info('Reading configuration file: {}'.format(fname))
if not os.path.isfile(fname):
logger.interrupt('File doesn\'t exist: {}'.format(fname))
self.config.optionxform = str
self.config.read(fname)
for section, option, value in override:
if not self.config.has_section(section):
self.config.add_section(section)
self.config.set(section, option, value)
basename = os.path.basename(fname)
self.name = os.path.splitext(basename)[0]
@safe
def _get_options_as_dict(self, section):
if section in self.config.sections():
return {p: v for p, v in self.config.items(section)}
else:
return {}
示例2: getManifest
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
def getManifest(fp, format, defaults=None):
"""Read the manifest from the given open file pointer according to the
given ManifestFormat. Pass a dict as ``defaults`` to override the defaults
from the manifest format.
"""
if defaults is None:
defaults = format.defaults
parser = SafeConfigParser()
if six.PY2:
parser.readfp(fp)
else:
data = fp.read()
if isinstance(data, six.binary_type):
data = data.decode()
parser.read_string(data)
results = {}
for key in format.keys:
if parser.has_option(format.resourceType, key):
results[key] = parser.get(format.resourceType, key)
else:
results[key] = defaults.get(key, None)
for key in format.parameterSections:
sectionName = "%s:%s" % (format.resourceType, key,)
if parser.has_section(sectionName):
results[key] = dict(parser.items(sectionName))
else:
results[key] = {}
return results
示例3: Config
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
class Config(object):
"""A ConfigParser wrapper to support defaults when calling instance
methods, and also tied to a single section"""
SECTION = 'scrapyd'
def __init__(self, values=None, extra_sources=()):
if values is None:
sources = self._getsources()
default_config = get_data(__package__, 'default_scrapyd.conf').decode('utf8')
self.cp = SafeConfigParser()
self.cp.readfp(io.StringIO(default_config))
sources.extend(extra_sources)
for fname in sources:
try:
with io.open(fname) as fp:
self.cp.readfp(fp)
except (IOError, OSError):
pass
else:
self.cp = SafeConfigParser(values)
self.cp.add_section(self.SECTION)
def _getsources(self):
sources = ['/etc/scrapyd/scrapyd.conf', r'c:\scrapyd\scrapyd.conf']
sources += sorted(glob.glob('/etc/scrapyd/conf.d/*'))
sources += ['scrapyd.conf']
sources += [expanduser('~/.scrapyd.conf')]
scrapy_cfg = closest_scrapy_cfg()
if scrapy_cfg:
sources.append(scrapy_cfg)
return sources
def _getany(self, method, option, default):
try:
return method(self.SECTION, option)
except (NoSectionError, NoOptionError):
if default is not None:
return default
raise
def get(self, option, default=None):
return self._getany(self.cp.get, option, default)
def getint(self, option, default=None):
return self._getany(self.cp.getint, option, default)
def getfloat(self, option, default=None):
return self._getany(self.cp.getfloat, option, default)
def getboolean(self, option, default=None):
return self._getany(self.cp.getboolean, option, default)
def items(self, section, default=None):
try:
return self.cp.items(section)
except (NoSectionError, NoOptionError):
if default is not None:
return default
raise
示例4: get_scrapycfg_targets
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
def get_scrapycfg_targets(cfgfiles=None):
cfg = SafeConfigParser()
cfg.read(cfgfiles or [])
baset = dict(cfg.items('deploy')) if cfg.has_section('deploy') else {}
targets = {}
targets['default'] = baset
for x in cfg.sections():
if x.startswith('deploy:'):
t = baset.copy()
t.update(cfg.items(x))
targets[x[7:]] = t
for tname, t in list(targets.items()):
try:
int(t.get('project', 0))
except ValueError:
# Don't import non-numeric project IDs, and also throw away the
# URL and credentials associated with these projects (since the
# project ID does not belong to SH, neither do the endpoint or the
# auth information)
del targets[tname]
if t.get('url', "").endswith('scrapyd/'):
t['url'] = t['url'][:-8]
targets.setdefault('default', {})
return targets
示例5: read_ini
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
def read_ini(cls, path, section=None):
"""read preferences from an .ini file"""
parser = ConfigParser()
parser.optionxform = str
parser.readfp(mozfile.load(path))
if section:
if section not in parser.sections():
raise PreferencesReadError("No section '%s' in %s" % (section, path))
retval = parser.items(section, raw=True)
else:
retval = parser.defaults().items()
# cast the preferences since .ini is just strings
return [(i, cls.cast(j)) for i, j in retval]
示例6: _update_settings_from_file
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
def _update_settings_from_file(section, settings):
tries = 0
current_directory = os.path.normpath(os.getcwd())
config_file = None
while current_directory and tries < MAX_CONFIG_SEARCH_DEPTH:
potential_path = os.path.join(current_directory, 'setup.cfg')
if os.path.exists(potential_path):
config_file = potential_path
break
new_directory = os.path.split(current_directory)[0]
if current_directory == new_directory:
break
current_directory = new_directory
tries += 1
if config_file and os.path.exists(config_file):
with open(config_file, 'rU') as fp:
config = SafeConfigParser()
config.readfp(fp)
if config.has_section('tool:multilint'):
settings.update(sanitize(config.items('tool:multilint')))
示例7: PackageConfigHandler
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
class PackageConfigHandler(object):
"""
Manager class for packages files for tracking installation of modules
"""
def __init__(self):
# noinspection PyUnresolvedReferences
from six.moves.configparser import SafeConfigParser
self.package_cfg = os.path.expanduser('~/Documents/site-packages/.pypi_packages')
if not os.path.isfile(self.package_cfg):
print('Creating package file')
with open(self.package_cfg, 'w') as outs:
outs.close()
self.parser = SafeConfigParser()
self.parser.read(self.package_cfg)
def save(self):
with open(self.package_cfg, 'w') as outs:
self.parser.write(outs)
def add_module(self, pkg_info):
"""
:param pkg_info: A dict that has name, url, version, summary
:return:
"""
if not self.parser.has_section(pkg_info['name']):
self.parser.add_section(pkg_info['name'])
self.parser.set(pkg_info['name'], 'url', pkg_info['url'])
self.parser.set(pkg_info['name'], 'version', pkg_info['version'])
self.parser.set(pkg_info['name'], 'summary', pkg_info['summary'])
self.parser.set(pkg_info['name'], 'files', pkg_info['files'])
self.parser.set(pkg_info['name'], 'dependency', pkg_info['dependency'])
self.save()
def list_modules(self):
return [module for module in self.parser.sections()]
def module_exists(self, name):
return self.parser.has_section(name)
def get_info(self, name):
if self.parser.has_section(name):
tbl = {}
for opt, value in self.parser.items(name):
tbl[opt] = value
return tbl
def remove_module(self, name):
self.parser.remove_section(name)
self.save()
def get_files_installed(self, section_name):
if self.parser.has_option(section_name, 'files'):
files = self.parser.get(section_name, 'files').strip()
return files.split(',')
else:
return None
def get_dependencies(self, section_name):
if self.parser.has_option(section_name, 'dependency'):
dependencies = self.parser.get(section_name, 'dependency').strip()
return set(dependencies.split(',')) if dependencies != '' else set()
else:
return None
def get_all_dependencies(self, exclude_module=()):
all_dependencies = set()
for section_name in self.parser.sections():
if section_name not in exclude_module and self.parser.has_option(section_name, 'dependency'):
dependencies = self.parser.get(section_name, 'dependency').strip()
if dependencies != '':
for dep in dependencies.split(','):
all_dependencies.add(dep)
return all_dependencies
示例8: _parse_args
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
def _parse_args(self, args=None):
self.context.original_begin = time.time()
self.context.original_args = args if args is not None else sys.argv[1:]
self.option_parser.disable_interspersed_args()
try:
options, args = self.option_parser.parse_args(
self.context.original_args)
except UnboundLocalError:
# Happens sometimes with an error handler that doesn't raise its
# own exception. We'll catch the error below with
# error_encountered.
pass
self.option_parser.enable_interspersed_args()
if self.option_parser.error_encountered:
return None, None
if options.version:
self.option_parser.print_version()
return None, None
if not args or options.help:
self.option_parser.print_help()
return None, None
self.context.original_main_args = self.context.original_args[
:-len(args)]
self.context.conf = {}
if options.conf is None:
options.conf = os.environ.get('SWIFTLY_CONF', '~/.swiftly.conf')
try:
conf_parser = SafeConfigParser()
conf_parser.read(os.path.expanduser(options.conf))
for section in conf_parser.sections():
self.context.conf[section] = dict(conf_parser.items(section))
except ConfigParserError:
pass
for option_name in (
'auth_url', 'auth_user', 'auth_key', 'auth_tenant',
'auth_methods', 'region', 'direct', 'local', 'proxy', 'snet',
'no_snet', 'retries', 'cache_auth', 'no_cache_auth', 'cdn',
'no_cdn', 'concurrency', 'eventlet', 'no_eventlet', 'verbose',
'no_verbose', 'direct_object_ring'):
self._resolve_option(options, option_name, 'swiftly')
for option_name in (
'snet', 'no_snet', 'cache_auth', 'no_cache_auth', 'cdn',
'no_cdn', 'eventlet', 'no_eventlet', 'verbose', 'no_verbose'):
if isinstance(getattr(options, option_name), six.string_types):
setattr(
options, option_name,
getattr(options, option_name).lower() in TRUE_VALUES)
for option_name in ('retries', 'concurrency'):
if isinstance(getattr(options, option_name), six.string_types):
setattr(
options, option_name, int(getattr(options, option_name)))
if options.snet is None:
options.snet = False
if options.no_snet is None:
options.no_snet = False
if options.retries is None:
options.retries = 4
if options.cache_auth is None:
options.cache_auth = False
if options.no_cache_auth is None:
options.no_cache_auth = False
if options.cdn is None:
options.cdn = False
if options.no_cdn is None:
options.no_cdn = False
if options.concurrency is None:
options.concurrency = 1
if options.eventlet is None:
options.eventlet = False
if options.no_eventlet is None:
options.no_eventlet = False
if options.verbose is None:
options.verbose = False
if options.no_verbose is None:
options.no_verbose = False
self.context.eventlet = None
if options.eventlet:
self.context.eventlet = True
if options.no_eventlet:
self.context.eventlet = False
if self.context.eventlet is None:
self.context.eventlet = False
try:
import eventlet
# Eventlet 0.11.0 fixed the CPU bug
if eventlet.__version__ >= '0.11.0':
self.context.eventlet = True
except ImportError:
pass
subprocess_module = None
if self.context.eventlet:
try:
import eventlet.green.subprocess
subprocess_module = eventlet.green.subprocess
except ImportError:
pass
#.........这里部分代码省略.........
示例9: items
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
def items(self, name):
section_name = self._get_section_name(name)
return SafeConfigParser.items(self, section_name)
示例10: configparser2yaml
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
def configparser2yaml(cpfile):
dict_ = {}
cp = ConfigParser()
with codecs.open(cpfile, encoding='utf-8') as fh:
cp.readfp(fh)
for section in cp.sections():
if section.startswith('contact:'): # contacts are now nested
if 'contact' not in dict_:
dict_['contact'] = {}
section2 = dict_['contact'][section.split(':')[1]] = {}
elif section.startswith('distribution:'): # distributions now nested
if 'distribution' not in dict_:
dict_['distribution'] = {}
section2 = dict_['distribution'][section.split(':')[1]] = {}
else:
section2 = dict_[section] = {}
for k, v in cp.items(section):
if section == 'identification': # keywords are now nested
if 'keywords' not in section2:
section2['keywords'] = {}
if 'default' not in section2['keywords']:
section2['keywords']['default'] = {}
if 'gc_cst' not in section2['keywords']:
section2['keywords']['gc_cst'] = {}
if 'wmo' not in section2['keywords']:
section2['keywords']['wmo'] = {}
if 'hnap_category_information' not in section2['keywords']:
section2['keywords']['hnap_category_information'] = {}
if 'hnap_category_geography' not in section2['keywords']:
section2['keywords']['hnap_category_geography'] = {}
if 'hnap_category_content' not in section2['keywords']:
section2['keywords']['hnap_category_content'] = {}
if k in ['topiccategory']:
section2['topiccategory'] = [v]
if k in ['keywords_en', 'keywords_fr']:
section2['keywords']['default'][k] = [k2.strip() for k2 in v.split(',')] # noqa
if k in ['keywords_gc_cst_en']:
section2['keywords']['gc_cst']['keywords_en'] = [k2.strip() for k2 in v.split(',')] # noqa
if k in ['keywords_gc_cst_fr']:
section2['keywords']['gc_cst']['keywords_fr'] = [k2.strip() for k2 in v.split(',')] # noqa
if k in ['keywords_wmo']:
section2['keywords']['wmo']['keywords_en'] = [k2.strip() for k2 in v.split(',')] # noqa
if k in ['hnap_category_information_en']:
section2['keywords']['hnap_category_information']['keywords_en'] = [v] # noqa
section2['keywords']['hnap_category_information']['keywords_fr'] = [v] # noqa
if k in ['hnap_category_geography_en']:
section2['keywords']['hnap_category_geography']['keywords_en'] = [v] # noqa
if k in ['hnap_category_geography_fr']:
section2['keywords']['hnap_category_geography']['keywords_fr'] = [v] # noqa
if k in ['hnap_category_content_en']:
section2['keywords']['hnap_category_content']['keywords_en'] = [v] # noqa
if k in ['hnap_category_content_fr']:
section2['keywords']['hnap_category_content']['keywords_fr'] = [v] # noqa
if k == 'keywords_type':
section2['keywords']['default'][k] = v
section2['keywords']['gc_cst'][k] = v
section2['keywords']['wmo'][k] = v
section2['keywords']['hnap_category_geography'][k] = v
section2['keywords']['hnap_category_information'][k] = v
section2['keywords']['hnap_category_content'][k] = v
else:
section2[k] = v
return yaml.safe_dump(dict_, default_flow_style=False, allow_unicode=True)
示例11: deploy_django
# 需要导入模块: from six.moves.configparser import SafeConfigParser [as 别名]
# 或者: from six.moves.configparser.SafeConfigParser import items [as 别名]
#.........这里部分代码省略.........
cfg.get(CFG_SECTION, 'src'),
path(cfg.get(CFG_SECTION, 'dst')),
])
# Build
activate = path('bin/activate')
build = os.path.join(
cfg.get(CFG_SECTION, 'dst'),
cfg.get(CFG_SECTION, 'build')
)
subprocess.check_call([build],
cwd=proj_base,
env={'BASH_ENV': activate})
# Install Deploy Requiremts
deploy_requires = cfg.get(CFG_SECTION, 'deploy_requires')
if deploy_requires:
logger.debug('Installing: %s', deploy_requires)
cmd = [os.path.join(virtualenv.path_locations(proj_base)[-1],
'pip')
, 'install']
cmd.extend(parse_list(deploy_requires))
subprocess.check_call(cmd)
# Create settings
settings_file = path(cfg.get(CFG_SECTION, 'settings'))+'.py'
slock = LockFile(settings_file)
slock.acquire()
if os.path.exists(settings_file):
slock.release()
raise IOError([17, 'File exists'])
try:
sfp = open(settings_file, 'w')
print(DJANGO_SETTINGS_TEMPLATE % dict(cfg.items(CFG_SECTION)),
file=sfp)
sfp.close()
finally:
slock.release()
# Create wsgi script
wsgi_file = path(cfg.get(CFG_SECTION, 'wsgi'))
slock = LockFile(wsgi_file)
slock.acquire()
if os.path.exists(wsgi_file):
slock.release()
raise IOError([17, 'File exists'])
try:
wfp = open(wsgi_file, 'w')
print(WSGI_TEMPLATE % dict(cfg.items(CFG_SECTION)),
file=wfp)
wfp.close()
finally:
slock.release()
# Create apache conf
conf_file = os.path.join(httpd_conf_dir,
cfg.get(CFG_SECTION, 'name'))+'.conf'
slock = LockFile(conf_file)
slock.acquire()
if os.path.exists(conf_file):
slock.release()
raise IOError([17, 'File exists'])
try:
sfp = open(conf_file, 'w')
conf = dict(cfg.items(CFG_SECTION))
conf['site_libs'] = os.path.join(
virtualenv.path_locations(proj_base)[1],
'site-packages')
http_conf = HTTPD_CONF_TEMPLATE % conf
print(http_conf,
file=sfp)
sfp.close()
finally:
slock.release()
# Perform django commands
deploy_commands = cfg.get(CFG_SECTION, 'deploy_commands')
if deploy_commands:
manage = [os.path.join(virtualenv.path_locations(proj_base)[-1],
virtualenv.expected_exe)
, 'manage.py']
os.chdir(path(cfg.get(CFG_SECTION, 'dst')))
# Deployment django environment
dep_env = os.environ.copy()
dep_env['DJANGO_SETTINGS_MODULE'] = cfg.get(CFG_SECTION, 'settings')
dep_env['PYTHONPATH'] = path('.')
logger.debug('Environment for commands: PYTHONPATH=%s',
dep_env['PYTHONPATH'])
logger.debug(' Django settings: %s', dep_env['DJANGO_SETTINGS_MODULE'])
for cmd in parse_list(deploy_commands):
logger.debug("Executing '%s'", ' '.join(manage+[cmd]))
subprocess.check_call(manage+cmd.split(), env=dep_env)
# That's it. Remember to reload apache
print('You should reload apache:\n', '\t', 'systemctl reload httpd')
return True