def __config2cp(self): config = ConfigParser.RawConfigParser(dict_type=OrderedDict) keys = self.config.keys() keys.sort() for k in keys: if k.find('.') > 0: tmp = k.split('.') section = tmp[0] name = ''.join(tmp[1:]) else: section = 'unknown' name = k if not config.has_section(section): config.add_section(section) #print('adding sec ',section) config.set(section, name, self.config[k]) self.cp = config
# not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from configparser import ConfigParser import os configfile = '/etc/nova/nova.conf' config = ConfigParser.RawConfigParser() config.read(configfile) section = 'database' if not set([section]).issubset(config.sections()): config.add_section(section) config.set(section, 'connection', 'mysql://*****:*****@%s/nova' % (os.environ.get('NOVA_DBPASS'), os.environ.get('HOST_IP'))) section = 'keystone_authtoken' if not set([section]).issubset(config.sections()): config.add_section(section) config.set(section, 'auth_uri', 'http://%s:5000/v2.0' % os.environ.get('HOST_IP'))
def get_login_details(self): ''' This parses the config file, environment variables and command line options and returns the config values Order of parsing: command line options, ~/.pepperrc, environment, defaults ''' # setting default values results = { 'SALTAPI_URL': 'https://localhost:8000/', 'SALTAPI_USER': None, 'SALTAPI_PASS': None, 'SALTAPI_EAUTH': 'auto', } try: config = ConfigParser(interpolation=None) except TypeError as e: config = ConfigParser.RawConfigParser() config.read(self.options.config) # read file profile = 'main' if config.has_section(profile): for key, value in config.items(profile): key = key.upper() results[key] = config.get(profile, key) # get environment values for key, value in list(results.items()): results[key] = os.environ.get(key, results[key]) # get eauth prompt options if self.options.saltapiurl: results['SALTAPI_URL'] = self.options.saltapiurl if results['SALTAPI_EAUTH'] == 'kerberos': results['SALTAPI_PASS'] = None if self.options.eauth: results['SALTAPI_EAUTH'] = self.options.eauth if self.options.username is None and results['SALTAPI_USER'] is None: if self.options.interactive: results['SALTAPI_USER'] = input('Username: '******'SALTAPI_USER'] = self.options.username if self.options.password is None and results['SALTAPI_PASS'] is None: if self.options.interactive: results['SALTAPI_PASS'] = getpass.getpass(prompt='Password: '******'SALTAPI_PASS'] = self.options.password return results
def __configuration(self): test_config = filesystem.getabsname( os.path.join('tests', 'data', 'setup.cfg')) config = ConfigParser.RawConfigParser() config.read(test_config) return config
#!/usr/bin/env python # _*_ coding:utf-8 _*_ #from six.moves.configparser import ConfigParser from configparser import ConfigParser test_cfg = "./default.cfg" config_raw = ConfigParser.RawConfigParser() config_raw.read(test_cfg) # 读取配置文件中 [DEFAULT] defaults = config_raw.defaults() print(defaults) # 读取指定section下的value值 a_float = config_raw.getfloat('Section1', 'a_float') print("-- number : %f type is : %s" % (a_float, type(a_float))) # 设置指定section下的value值 # 此时没有写入文件,保存在内存实例中 a_float = 2.14159 config_raw.set('Section1', 'a_float', a_float) a_float = config_raw.getfloat('Section1', 'a_float') print("-- number : %f type is : %s" % (a_float, type(a_float))) # 读取带有参数替换模板的变量,但是没有替换参数 print("-- RawConfigParser just get raw value") str_foo = config_raw.get('Section1', 'foo') print(str_foo)