0

I am trying to update a particular option in my config.ini file using ConfigParser.

This is my config.ini file:

[remote-server-details]                                                                                                 

MACHINE_NAME =                                                                                                   

MACHINE_USERNAME =                                                                                               

MACHINE_PASSWORD = Welcome!23

And this is my step file in 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)

I need to update only MACHINE_NAME and MACHINE_USERNAME, as MACHINE_PASSWORD is already contained in the config file, but I'm getting this error:

 INFO - Subtask:     remote_server_details['MACHINE_PASSWORD'],
      [2018-08-17 17:40:04,723] {{base_task_runner.py:98}} INFO - Subtask: KeyError: 'MACHINE_PASSWORD'
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Ankur Sharma
  • 45
  • 3
  • 10
  • Where in your code do you make a distinction between `MACHINE_NAME`/`MACHINE_USERNAME`, and `MACHINE_PASSWORD`? I think you also need to show us how you call that function. Also, if you have more detailed error messages than the two lines you've shown, that would be helpful. – mkrieger1 Aug 17 '18 at 18:45
  • Ah, I think I understand: You want to write options to the file based on whether they are already contained or not (instead of directly based on their name). – mkrieger1 Aug 17 '18 at 18:48
  • @mkrieger1:ahh yes,you got the point ,password is already present in the config file ,i just need to update the machine name and user name – Ankur Sharma Aug 18 '18 at 14:48

0 Answers0