Comments (8)
Thanks for the question.
I thinks, such a YAML:
foo:
!include foo-1.yml
!include foo-2.yml
is illegal, casue it's neither a mapping nor a sequence.
If want it to be a sequence, we shall write the YAML as:
foo:
- !include foo-1.yml
- !include foo-2.yml
it wall be loaded to Python
dictionary object:
{
"foo":
[
{"one": "a"},
{"two": "b"}
]
}
from pyyaml-include.
But there "foo" is not a sequence, its a list of sequences. That is exactly my problem.
The result I want is
{
"foo":
{
"one": "a",
"two": "b"
}
}
from pyyaml-include.
@grauschnabel Do you mean that:
we have two YAML files to be included:
-
file
foo-1.yml
:one: a
-
file
foo-2.yml
:two: b
and when we write :
foo:
!include foo-1.yml
!include foo-2.yml
the excepted data is:
{
"foo":
{
"one": "a",
"two": "b"
}
}
Is that?
But the YAML
including constructor can not union two mappings into one.
Because the constructor will first parse a including file into a YAML
node, then PyYAML
puts the node into the doc tree.
So, the including parts of above example are two separated mappings, and can not be joined into one mapping as what they are in the included files literally
i think they will be parsed to something like:
{
"foo":
{"one": "a"}
{"two": "b"}
}
That's illegal.
What's more, PyYAML takes the fragment
!include foo-1.yml
!include foo-2.yml
like:
include("foo-1.yml !include foo-2.yml")
i did not expect that.
from pyyaml-include.
I think that
foo:
bar: 1
baz: 2
is absolutly legal in yaml. But now I understand how you have built this tool and the I think that it's a bit like a design issue. Maybe a colon could solve the problem, but that would possibly break compatibility to like its now. I thought about
foo:
!include: bar.yml
!include: baz.yml
Would do the trick to make pyyaml parse it. Otherwise
include("foo-1.yml !include foo-2.yml")
Could be reparsed. by str.split("!include")
.
In both cases: We could load bar and baz and then combine it via dict.update()
so that it is clear that later loaded keys overwrite earlyer definitions of the same key.
What do you think about that?
from pyyaml-include.
Thanks for @grauschnabel
foo:
bar: 1
baz: 2
is a valid YAML.
i'll try to avoid pyyaml parsing
foo:
!include: bar.yml
!include: baz.yml
to include("foo-1.yml !include foo-2.yml")
...
from pyyaml-include.
You should be able to do it with merging, if you can tolerate some cruft lying around.
one: &one
!include bar.yml
two: &two
!include baz.yml
foo:
<<: [ *one, *two ]
Disclaimer: untested.
from pyyaml-include.
Thanks @smurfix
But i think that won't work.
Because what returns by the !include
constructor is a python object, not a YAML
mapping
, and can't inherit
from pyyaml-include.
I think it's not easy to do such a thing in PyYAML
's constructor.
Because we can't make it return a document literally.
Perhaps a template engine (eg: Jinja2) is better.
For this issue, we can write a main YAML
file with Jinja2
templates in it, and include other files using the template engine.
Here we use Jinja2
together with jinjyaml:
-
main.yml
:foo: !j2 | {% include "child-1.yml" %} {% include "child-2.yml" %}
-
child-1.yml
:'1.1': one '1.2': two
-
child-2.yml
:'2.1': 2.1.1: three 2.1.2: four
then parse and render it:
from pprint import pprint
import jinja2
import yaml
from jinjyaml import JinjyamlConstructor, jinjyaml_render
TAG = 'j2'
j2_env = jinja2.Environment(
loader=jinja2.FileSystemLoader('path/of/the/yaml/files')
)
constructor = JinjyamlConstructor(env=j2_env)
yaml.add_constructor('!{}'.format(TAG), constructor)
with open('main.yml') as fp:
doc = yaml.load(fp, yaml.Loader)
print(doc)
data = jinjyaml_render(doc, yaml.Loader)
pprint(data)
we'll get
{'foo': <jinjyaml.tagobject.JinjyamlObject object at 0x0000027AB80F0250>}
{'foo': {'1.1': 'one',
'1.2': 'two',
'2.1': {'2.1.1': 'three', '2.1.2': 'four'}}}
from pyyaml-include.
Related Issues (20)
- Python 3.6 unsupported after releasing `1.2.post1` HOT 2
- Publish sdist to PyPI HOT 2
- Include and achors HOT 3
- include specific components from other yamls? HOT 2
- Does not support top level include with other keys HOT 2
- Relative include: relativeto the current file? HOT 8
- Could you update the PyPI to new version to add PyYAML 6.0 support? HOT 2
- How to concatenate two yaml files? HOT 1
- Ability for default value HOT 1
- Wildcard with Hash/Dict/Map HOT 1
- Idea: include from remote storages (notably git) HOT 3
- Yaml anchor support for include yaml files HOT 1
- Use tag with anchors across a single yaml config file HOT 2
- It seems that I can't rewrite the inherited argments from base `yaml` file. HOT 5
- Python 3.10 unsopported? HOT 2
- yaml writer? HOT 1
- How to make the package work with setuptools lower than 61.0.0 HOT 4
- The library looks to by typed, but the `py.typed` file is missing HOT 2
- Can we use alias with include? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyyaml-include.