I need to create a custom ConfigParser which adds a few features including, most relevantly here, a default value for unset keys of None. This seems to be supported through custom dict types and accordingly I wrote something like this:
class SyncDict(collections.UserDict):
...
def __getitem__(self, key):
if key in self.data:
return self.data[key]
return None
...
class SyncConfig(ConfigParser):
...
def __init__(self, filename):
super().__init__(allow_no_value=True, dict_type = SyncDict)
...
However, this does not work as it still raises KeyError in SectionProxy. For example,
>>> a = SyncConfig('aaa.cfg')
>>> a.add_section('b')
>>> b = a['b']
>>> b['c']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python38\lib\configparser.py", line 1254, in __getitem__
raise KeyError(key)
KeyError: 'c'
Am I missing something, or this really not supposed to be possible?
PS: Bonus points for a way to make SyncDict return a value of an empty SyncDict when asked for a section and None when asked for an option.