-2

I am using Python function to create my config file and it is working fine once it is created successfully. I am opening that file to update the password key value and it is changing but it is changing other things as well. It is changing all CAPS keys into small and also changing ":" to "=". I am not sure why it is doing this.

nice.ini:

[FILENAME]
FILE:nice.ini

[LOGFILE]
LOGFILE:*.log

[ENVIORNMENT]
KEEP_DEV:DEV
KEEP_IT:IT
KEEP_APP:APP

[NYCDEF]
NICE:
KEEP:0
dbname = NYCDEF
username = NICE_MAN
password = HELLO
[KGHINP]
NICE:
KEEP:0
dbname = KGHINP
username = NICE_MAN
password = HELLO
[NICDEF]
NICE:
KEEP:0
dbname = NICDEF
username = NICE_MAN
password = HELLO

function I am using to change the value of key password is:

from configparser import ConfigParser
parser = ConfigParser()
parser.read('nice.ini')

def update_Val():
    while (True):
        a = input("Do you want to update the pass of env?yes/no: ")
        if a.lower() == "yes":
            sect = input("Enter env name to update pass or q to exit : ")
            if sect == 'q':
                exit(120)
            passwd = input("Enter pass to overwrite or q to exit : ")
            if passwd == 'q':
                exit(120)
            parser.set(sect, 'password', passwd)
            with open('nice.ini', 'w') as configfile:
                parser.write(configfile)
                configfile.close()
        elif a.lower() == "no":
            print("file updated")
            break

update_value()

Once I run this function and change the value then it modify the file in below way. Here you can see all keys name turn to small and ":" changed to "=":

[FILENAME]
configfile = nice.ini

[LOGNAME]
logfile = delete.log

[ENVIORNMENT]
keep_dev = DEV
keep_it = IT
keeo_app = APP

[NYCDEF]
nice =
keep = 0
dbname = NYCDEF
username = NICE_MAN
password = HELLO
[KGHINP]
nice =
keep = 0
dbname = KGHINP
username = NICE_MAN
password = HELLO
[NICDEF]
nice =
keep = 0
dbname = NICDEF
username = NICE_MAN
password = HELLO
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
unknown
  • 1,815
  • 3
  • 26
  • 51
  • @Tomerikoo I updated my original post.yes it is updating the file but changing the key names to small and ":" to "=" – unknown Aug 19 '20 at 15:51

1 Answers1

1

Regarding the case change:

What you are looking for is to change the optionxform method of the parser:

This method transforms option names on every read, get, or set operation. The default converts the name to lowercase.

So you just need to override it with an identity function:

from configparser import ConfigParser
parser = ConfigParser()
parser.optionxform = lambda option: option
parser.read('nice.ini')

Regarding the changing delimiter:

The problem is that the parser doesn't save the delimiter for each option:

mo = self._optcre.match(value)
if mo:
    optname, vi, optval = mo.group('option', 'vi', 'value')

In the above code from the _read method, vi is the regex group of the delimiter which is never used again in the code.

During write, it writes using the first given delimiter (which by default is =):

if space_around_delimiters:
    d = " {} ".format(self._delimiters[0])
else:
    d = self._delimiters[0]

Theoretically, you could subclass ConfigParser and implement your own mechanism, but that seems like a lot of work and loses the point of using an existing library. It means you will have to rewrite the _read method to save the delimiter for each option, and then override the write method to use that delimiter. I don't know the reason for your mixed delimiters, but I think it would be far easier to compromise for a single-type delimiter.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    I am going to use single delimiter("=") to avoid the unnecessary changes. when I used mix delimiter I was not aware about the behavior of configparser. Nice to see your explanation that is causing this. – unknown Aug 19 '20 at 16:41
  • @unknown I admit that I just learned about it because I was also curious :) I thought it might be easy to sub-class and do what you wanted, but it seems like a nightmare. BTW, I didn't mention it but if you prefer you can make it all `:` instead of `=` (when writing) – Tomerikoo Aug 19 '20 at 16:44