我正在尝试使用config.ini
更新ConfigParser
文件中的特定选项。
这是我的config.ini
文件:
[remote-server-details]
MACHINE_NAME =
MACHINE_USERNAME =
MACHINE_PASSWORD = Welcome!23
这是我在Python中的步骤文件:
@step('I change the following config options in section "{section_name}" in the {config_file_type} config file')
def set_dag_config_section_values(context, section_name, config_file_type):
parameters = ast.literal_eval(context.text)
context.conf[config_file_type].read(context.dag_config_file[config_file_type])
context.conf[config_file_type].remove_section(section_name)
context.conf[config_file_type].add_section(section_name)
for option, value in parameters.iteritems():
context.conf[config_file_type].set(section_name, option, value)
with open(context.dag_config_file[config_file_type], 'w+') as configfile:
context.conf[config_file_type].write(configfile)
context.conf[config_file_type].read(context.dag_config_file[config_file_type])
readin_parameters = dict(context.conf[config_file_type].items(section_name))
sort_dict(parameters)
sort_dict(readin_parameters)
assert readin_parameters == parameters, 'Value could not be set. %s does not match config contents %s' % (parameters, readin_parameters)
我只需要更新MACHINE_NAME
和MACHINE_USERNAME
,因为MACHINE_PASSWORD
已经包含在配置文件中,但是出现此错误:
INFO - Subtask: remote_server_details['MACHINE_PASSWORD'],
[2018-08-17 17:40:04,723] {{base_task_runner.py:98}} INFO - Subtask: KeyError: 'MACHINE_PASSWORD'