Saving application configuration to the ini file respecting order of sections/options and providing mechanism for (nested) complex objects

zljubisic at gmail.com zljubisic at gmail.com
Tue Aug 21 12:00:51 EDT 2018



Hi,

I am looking for a way to save ini file with application section/option combination. Something like the following:

[Default]
ROOT_DIR= /home/...
RUN = True
COUNTER = 5
LAST_VALUE = 5.6
TEST_FILES_LIST=['a.txt', 'b.txt', 'c.txt']
TEST_FILES_DICT={'CASE1' : ['a.txt', 'b.txt'], 'CASE2': ['c.txt']}

[Dates]
CREATE_DATE="2018-01-01"

[DateTimes]
LAST_MODIFIED_DATE="2018-01-01 14:23:45"


For that purpose I decided to use json as a quite readable fomat that can be edited with external text editor.
So I have created dict with all options hoping that I will be able to save it with json.dumps and read it with json.loads, but I have run to several problems:

1. saving json will result in unordered ini file. It could be solved by using OrderedDict that will than with json.dumps be saved to file in the same order as options are added to the ordered dict.

2. there is a problem with datatime values because json format serialize everything to string. I have found the solution on page https://gist.github.com/abhinav-upadhyay/5300137, but I dislike the idea that datetime is represented as unordered dict. When you have to change datetime manually in the ini file by using text editor, you first have to sort year,month,day... in order to be sure that you are setting correct date. 

3. reading with json.loads will get unordered dict again, so new save will be unordered.


Maybe json/(un)OrderedDict is not a proper tool for what I am trying to do. 
How you are saving your (nested) configuration to the ini file?

Regards.



More information about the Python-list mailing list