Comments (5)
Sorry for that, the doc-comments are not quite well.
In fact, the reader table is not a dictionary. It is a list of two-elements tuples. First part of the tuple is a regular expression for matching file names; second part is the reader class.
The custom reader table could be:
readers_table = [
(re.compile(r'^.+\.(([yY][mM][lL])|([Yy][aA][mM][lL]))$'), YamlReader),
(re.compile(r'^.+\.[jJ]2$', YamlReader), # *.j2
(re.compile(r'^.+\.[jJ][sS][oO][nN]$'), JsonReader),
(re.compile(r'^.+\.[iI][nN][iI]$'), IniReader),
(re.compile(r'^.+\.[tT][oO][mL][lL]$'), TomlReader),
(re.compile(r'^.+\.[tT][xX][tT]$'), PlainTextReader),
]
from pyyaml-include.
With the above code, I'm not having success. I'm still getting the same "un-support file name" error.
I had to add a few more classes for the reader_table tuple. With that code, the normal ".yaml" files parse, but not the .j2 files. It seems the readers_table is till not getting picked up. Here is my code:
import argparse
import os
import yaml
import sys
import subprocess
import re
# Required for new reader_table
class Reader(object):
# pylint: disable=too-few-public-methods
def __init__(self, path, encoding, *args, **kwargs): # pylint:disable=unused-argument
self._path = path
self._encoding = encoding
def __call__(self):
raise NotImplementedError()
# Required for new reader_table
class YamlReader(Reader):
# pylint: disable=too-few-public-methods
def __init__(self, path, encoding, loader_class, *args, **kwargs): # pylint:disable=unused-argument
super(YamlReader, self).__init__(path, encoding)
self._loader_class = loader_class
def __call__(self):
with io.open(self._path, encoding=self._encoding) as fp:
return yaml.load(fp, self._loader_class)
def main():
from yamlinclude import YamlIncludeConstructor
import in_place
# yaml and j2 files only
reader_table = [
(re.compile(r'^.+\.(([yY][mM][lL])|([Yy][aA][mM][lL]))$'), YamlReader),
(re.compile(r'^.+\.(([j][2]))$'), YamlReader),
]
# Notice the reader_map argument in the constructor. Is this correct?
YamlIncludeConstructor(reader_map=reader_table).add_to_loader_class(
loader_class=yaml.FullLoader)
# yaml.add_constructor('!include', YamlIncludeConstructor())
# Read the main yaml file and include the other yaml files.
with open(file_path) as f:
data = yaml.load(f, Loader=yaml.FullLoader)
return data
Does this look right? What am I missing here?
from pyyaml-include.
Emm... i found the problem.
YamlIncludeConstructor.add_to_loader_class()
is a classmethod. The method creates a new YamlIncludeConstructor
instance and add it to a yaml
Loader class.
And the class's constructor arguments shall be passed to the method as **kwargs
.
So, if we modify above code to:
# ....
reader_table = [
# (regex-pattern, ReaderClass),
# ...
]
constructor = YamlIncludeConstructor(reader_map=reader_table)
yaml.FullLoader.add_constructor('!include', constructor)
# ....
yaml.load(data_or_file, yaml.FullLoader)
# ...
It should work.
from pyyaml-include.
Thank you sir, I'll give that a try.
from pyyaml-include.
Thanks @tanbro, that worked perfectly!
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
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.