-2

I want to use if statement and variable assignment in my config file. I have tried but it seems impossible. Right now the "begin" value is read in and the application runs it using python system call. So it will be system( if %(USER_MODE)s=="ADMIN"; then PASSWD: %(ADMIN_PASSWD)s else PASSWD: %(ROOT_PASSWD)s; fi).

config file:

[DEFAULT]
ADMIN_PASSWD: ADMIN
ROOT_PASSWD: ROOT
[setup:password]
begin: if %(USER_MODE)s=="ADMIN"; then PASSWD: %(ADMIN_PASSWD)s else PASSWD: %(ROOT_PASSWD)s; fi
end: echo %(PASSWD)s
user2719735
  • 97
  • 2
  • 10
  • 2
    Typically, the logic goes inside your application, not in the config file. – glenn jackman Aug 24 '22 at 22:35
  • The requirement that we were given is to minimize touching existing code due to the big test matrix. I have tried various ways but it seems impossible, that is why I am asking for ideas here :) – user2719735 Aug 25 '22 at 16:49
  • I would consider this a minimal exercise to touch existing code, since a config file doesn't contain any way to execute or determine logic. – Makoto Aug 25 '22 at 16:56

1 Answers1

0

You can't do if statement or manipulate environment variables in a ConfigParser config file.

You haven't provided what the code looks like in the "big test matrix".

Here is one scenario where you could provide a default password that is used if one is not present for a user.

Config file 'pwd.ini':

[DEFAULT]
password = root_password123

[admin]
password = admin_password123
email = admin@xyz.com

[user1]
email = user1@xyz.com

user1 does not have a password entry. So configparser will supply a the default value (root_password123):

import configparser
config = configparser.ConfigParser()
config.read('pwd.ini')

print(config['admin']['password'])
print(config['user1']['password'])

gives

admin_password123
root_password123
bfris
  • 5,272
  • 1
  • 20
  • 37