tanbro / pyyaml-include

yaml include other yaml
https://pypi.org/project/pyyaml-include/
GNU General Public License v3.0
78 stars 20 forks source link

Use tag with anchors across a single yaml config file #34

Closed gorun2git closed 1 year ago

gorun2git commented 1 year ago

Hi,

I've started playing around with this interesting library and I have a question. I'm not sure if this can be an issue or maybe I missed the point. So let me explain what I want to build.

My environment: python=3.10.10 PyYAML=6.0 pyyaml-include=1.3

What I've tried so far: I want to create some defaults that would be use across the YAML config files without need to copy it multiple times in different blocks with possibly with anchors. So, if I have /test/path/custom-defaults/person.yaml file:

.default_description: &default-description
    hair_color: grey
    eye_color: blue
    age: 123
    gender: N/A

And I'd like to include it in other config files, e.g. in /test/path/defaults/describe-people.yaml:

!include ../custom-defaults/person.yaml

persons:
   person.with.custom.defaults: 
       <<: *default-description
   person.with.overrided.defaults: 
       <<: *default-description
       hair_color: blue

When try this I'm getting an error: raise ParserError(None, None, yaml.parser.ParserError: expected '<document start>', but found '<block mapping start>'

On the other side, I can fix it by:

  1. Updating person.yaml file a bit, by removing anchors
  2. Use !include tag only in blocks, like person.with.custom.defaults: !include ../custom-defaults/person.yaml

Questions: Is it even possible to use it as a document tag and if yes, can someone provide an example?

Thank you.

NoahFeinberg commented 1 year ago

For the anchor issue I found that by default pyyaml doesn't easily allow you to carry anchors between files. I actually have an open PR to get this exact functionality working https://github.com/tanbro/pyyaml-include/pull/32

For the !include document tag I don't think !include can be used as a key by default only as a value.

gorun2git commented 1 year ago

@NoahFeinberg, thank you for your quick reply.

I think your pr will be great addition to the library.