Re: using yaml_constructors attribute of the Loader class
Kirill Simonov <[email protected]>
| Newsgroups | gmane.text.yaml.general |
|---|---|
| Message-ID | <[email protected]> |
Manlio Perillo wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi.
>
> In a framework I'm writing I have a custom YAML Loader, with several
> custom constructors.
>
> Now, I would like to allow applications to define their own
> constructors, and have them be used by my framework.
>
> Using setuptools entry points, the code is something like this:
>
>
> from pkg_resources import iter_entry_points
> from yaml.loader import Loader
>
> class MyLoader(Loader):
> def factory(cls)
> yaml_constructors = cls.yaml_constructors
> for ep in iter_entry_points('my.entrypoint'):
> yl = ep.load()
>
> yaml_constructors.update(yl.yaml_constructors)
>
> d = dict(yaml_constructors=yaml_constructors)
> return type('MyLoader', (cls,), d)
>
> Applications just need to create a custom loader class, derived from
> yaml.loader.Loader, and register their own constructors.
>
> Is it "safe" (that is, is the API stable) to access the
> yaml_constructors attribute of the Loader class?
>
Well, `yaml_constructors` is not a part of the documented API, but I
don't think it's going to be removed any time soon. In the worst case,
you could always add a check for yaml.__version__:
if yaml.__version__ < 'X.Y':
# Use yaml_constructors here
else:
# Use the brand new API here.
Thanks,
Kirill
------------------------------------------------------------------------------