We use YAML configuration for services scaling. Usually it goes like this:
service:
scalingPolicy:
capacity:
min: 1
max: 1
So it's easy to open with basic PyYAML and parse as an dict to get config['service']['scalingPolicy']['capacity']['min']
result as 1
. Problem is that some configs are built with dots delimiter e.g:
service.scalingPolicy.capacity:
min: 1
max: 1
Basic consumer of this configs is Java's Spring and somehow it's treated equally as the example above. But due to need to also parse these configs with Python - I get whole dot separated line as a config['service.scalingPolicy.capacity']
key.
The question is - how would I make python parse any kind of keys combinations (both separated by dots
and separated by tabulation and :
). I didn't find related parameters for Python YAML libs (I've checked standard PyYAML and ruamel.yaml
) and handling any possible combination manually seems like a crazy idea. The only possible idea I have is to write my own parser but maybe there is something I'm missing so I won't have to reinvent the bicycle.
This is not trivial, it is much more easy to split a lookup with a key with dots into recursing into a nested data structure. Here you have a nested data structure and different
[key]
lookups mean different things at different levels.If you use
ruamel.yaml
in the default round-trip mode, you can add a class-variable to the type that represents a mapping, that defines on what the keys were split and an instance variable that keeps track of the prefix already matched:which gives: