Droopy4096
Droopy4096

Reputation: 311

PyYAML customized yaml processing

I would like to extend YAML with some custom macros so that I can "reuse" parts of definitions within the same file. Sample:

DEFAULTS:
- a
- b
- c
CUSTOM1:
- %DEFAULTS
- d
CUSTOM2:
- %DEFAULTS
- e

resulting in

CUSTOM1==['a','b','c','d']
CUSTOM2==['a','b','c','e']

Doesn't have to be exact same syntax, as long as I can get same functionality out of it. what are my options?

P.S. I do realize that it is possible to just walk the dictionary after parsing and re-adjust the values, however I'd like to do it while loading.

Upvotes: 1

Views: 417

Answers (1)

Anthon
Anthon

Reputation: 76842

There are no options within the YAML specification. The only thing that comes close is the merge syntax, but that is for merging mappings and doesn't work for sequences.

If you cannot switch to using mappings in your context (and use the << merge), the cleanest way, IMO, to implement this is to make the values of CUSTOM1 and CUSTOM2 specific types, e.g. expander:

CUSTOM1: !expander
- %DEFAULTS
- d    

that map to objects that interpret the first sequence element as a replaceable value when it starts with %.

Upvotes: 1

Related Questions