GithubHelp home page GithubHelp logo

Comments (5)

tanbro avatar tanbro commented on August 10, 2024

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.

elvis2 avatar elvis2 commented on August 10, 2024

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.

tanbro avatar tanbro commented on August 10, 2024

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.

elvis2 avatar elvis2 commented on August 10, 2024

Thank you sir, I'll give that a try.

from pyyaml-include.

elvis2 avatar elvis2 commented on August 10, 2024

Thanks @tanbro, that worked perfectly!

from pyyaml-include.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.