介绍
ConfigParser模块介绍
实例
@ getini.py
'''
@author:xxx
'''
import os
import configparser
class Config:
def __init__(self, configPath=None):
if configPath is None:
configPath = os.path.dirname(__file__)
configPath = os.path.join(configPath, 'conf.ini')
self.cfg = configparser.ConfigParser()
self.cfg.read(configPath, encoding="utf-8")
def GetAllSections(self):
return self.cfg.sections()
def GetAllOptions(self,section):
return self.cfg.options(section)
def HasSection(self,section):
return self.cfg.has_section(section)
def HasOption(self,section,option):
return self.cfg.has_option(section,option)
def GetConfig(self, section, option):
return self.cfg.get(section, option)
def GetConfig():
try:
objConfig = Config()
dictConfig = { "cdhIp" : objConfig.GetConfig('install',"cdhIp"),
"cdhInstallPath" : objConfig.GetConfig('install', "cdhInstallPath"),
"versionMysql" : objConfig.GetConfig('install', "versionMysql"),
"createDB" : objConfig.GetConfig('install', "createDB"),
"mysqlInstallPath" : objConfig.GetConfig('install', "mysqlInstallPath"),
"pathMysqlData" : objConfig.GetConfig('install', "pathMysqlData"),
"mysqlIp" : objConfig.GetConfig('install', "mysqlIp"),
"mysqlPort" : objConfig.GetConfig('install', "mysqlPort"),
"mysqlUserName" : objConfig.GetConfig('install', "mysqlUserName"),
"mysqlPwd" : objConfig.GetConfig('install', "mysqlPwd"),
"packageMysql" : objConfig.GetConfig('install', "packageMysql")
}
print('*** ', objConfig.GetAllSections())
print('*** ', objConfig.GetAllOptions('install'))
print('*** ', objConfig.HasSection('install'))
print('*** ', objConfig.HasOption('install', 'cdhIp'))
for k,v in dictConfig.items():
print('dictConfig: %s - %s' % (k,v))
return dictConfig
except Exception:
print("Get configuration failed")
return None
def PrepareConfig(dictConfig):
cdhIp = dictConfig['cdhIp']
cdhInstallPath = dictConfig['cdhInstallPath']
createDB = dictConfig['createDB']
mysqlPort = dictConfig['mysqlPort']
versionMysql= dictConfig['versionMysql']
mysqlInstallPath = dictConfig['mysqlInstallPath']
print('cdhIp: %s\ncdhInstallPath: %s\ncreateDB: %s\nmysqlPort: %s\nversionMysql: %s\nmysqlInstallPath: %s' % (cdhIp,cdhInstallPath,createDB,mysqlPort,versionMysql,mysqlInstallPath))
dictConfig = GetConfig()
PrepareConfig(dictConfig)
@conf.ini
[deploy]
cdhIp=10.10.85.111
[install]
cdhIp=10.10.85.112
cdhInstallPath=/home/cdh/cdhmanager
versionMysql=5.7
packageMysql="mysql-5.7.21-linux-glibc2.12-x86_64 x a d f 1212 -23| 12121123123"
createDB=1
mysqlInstallPath=/home/cdh/mysql
pathMysqlData=/home/cdh/mysql/data
mysqlIp=10.10.85.112
mysqlPort=3306
mysqlUserName=root
mysqlPwd=root
@输出
dictConfig: cdhIp - 10.10.85.112
dictConfig: cdhInstallPath - /home/cdh/cdhmanager
dictConfig: versionMysql - 5.7
dictConfig: createDB - 1
dictConfig: mysqlInstallPath - /home/cdh/mysql
dictConfig: pathMysqlData - /home/cdh/mysql/data
dictConfig: mysqlIp - 10.10.85.112
dictConfig: mysqlPort - 3306
dictConfig: mysqlUserName - root
dictConfig: mysqlPwd - root
dictConfig: packageMysql - "mysql-5.7.21-linux-glibc2.12-x86_64 x a d f 1212 -23| 12121123123"
cdhIp: 10.10.85.112
cdhInstallPath: /home/cdh/cdhmanager
createDB: 1
mysqlPort: 3306
versionMysql: 5.7
mysqlInstallPath: /home/cdh/mysql