Commit 03616908 authored by Yakubov, Sergey's avatar Yakubov, Sergey
Browse files

add script to update config

parent 097488fa
Loading
Loading
Loading
Loading

src/update_config.py

0 → 100644
+41 −0
Original line number Diff line number Diff line
import argparse
import shlex

def update_lines(filename, update ):
  new_lines = []
  file = open(filename)
  for line in file:
    original_line = line
    line = line.strip()
    line = line.rstrip()
    if (not line.startswith('#')) and len(line) > 2:
      words = shlex.split(line)
      for word, value in update.items():
          if words[0]==word:
            original_line = original_line.replace(words[1], str(value))
            break
    new_lines.append(original_line)
  return new_lines

def update_config(args, updates):
    updated_data = update_lines(args.config_file, updates)
    with open(args.output_file, 'w') as config_file:
        for item in updated_data:
            config_file.write(item)

def main():
    parser = argparse.ArgumentParser(description='Update configuration values in a config file.')
    parser.add_argument('config_file', type=str, help='Path to the config file')
    parser.add_argument('output_file', type=str, help='Path to the output file')
    parser.add_argument('updates', nargs='+', type=str, help='Key-value pairs to update in the format key=value')
    args = parser.parse_args()

    updates = {}
    for update in args.updates:
        key, value = update.split('=')
        updates[key] = value

    update_config(args, updates)

if __name__ == '__main__':
    main()