Comments (4)
Here is what works for me:
import yaml
class RecursiveLoader(yaml.FullLoader): # pylint: disable=too-many-ancestors
"""YAML Loader with recursive `!include` constructor.
https://gist.github.com/joshbode/569627ced3076931b02f
https://stackoverflow.com/q/528281/850781"""
def __init__(self, stream):
try:
self._root = os.path.dirname(stream.name)
except AttributeError:
self._root = os.path.curdir
super().__init__(stream)
def include(self, node):
"""Include the file referenced at the node."""
filename = os.path.join(self._root, self.construct_scalar(node))
extension = os.path.splitext(filename)[1]
with open(filename, 'r') as fd:
if extension in ('.yaml', '.yml'):
return yaml.load(fd, Loader=self.__class__)
if extension in ('.json', ):
return json.load(fd)
return fd.readlines()
yaml.add_constructor('!include', RecursiveLoader.include, RecursiveLoader)
Thanks!
from pyyaml-include.
Recursive includes are complex ...
But if the problem is that the Tag can not get the pathname correctly, maybe absolute absolute pathname or use a base_dir
for the Tag could solve:
The constructor searchs including files with iglob
, from CWD
.
And it's construct function has an argument base_dir
, default to None
.
-
When it's set, the tag will load other YAML files with
base_dir
as prefix dir, eg:new a construcor:
import yaml from yamlinclude import YamlIncludeConstructor constructor = YamlIncludeConstructor(base_dir='/your/conf/dir') yaml.FullLoader.add_constructor('!inc', constructor)
sub-config files are in dir
/your/conf/dir
The entry YAML file's path is
/path/of/app/config.yml
, content is:bar: !inc bar.yml
The constructor does things like this:
base_dir = "/your/conf/dir" pathname = "bar.yml" pathname = os.path.join(base_dir, pathname) iterable = iglob(pathname, recursive=recursive) for path in filter(os.path.isfile, iterable): # load and parse bar.yml # ... ...
-
When it's None, the tag will load other YAML files from
CWD
,
can this help?
Thanks!
from pyyaml-include.
Thank you for your reply.
I think every include should be taken relative to the including file, not the top-level file.
Abs path in YamlIncludeConstructor(base_dir=..)
will not help.
from pyyaml-include.
Emm, each including file is relative to the file includes them ... it's more like XML Pointer or $ref
in OpenAPI spec.
Also i met a similar situation recently - seperated YAML config files on S3.
i think https://tools.ietf.org/html/rfc6901 describes things for that.
from pyyaml-include.
Related Issues (20)
- Python 3.6 unsupported after releasing `1.2.post1` HOT 2
- Publish sdist to PyPI HOT 2
- two files in one dict? HOT 8
- 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.