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
>
> Kirill Simonov ha scritto:
>> 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.
>>
>
> My concern is that the new API will not allow me to do what I'm doing now.
>
> As an example, if add_constructor is written in C and will store the
> dictionary in C code, so that registered constructors are no more
> accessible from Python code.
>
I think using yaml_constructors is reasonably safe.
In any case, I don't have any time to refactor the pyyaml api, which is
a shame; there are several problems that I'd love to have fixed.
>
> By the way, is yaml_constructors in old versions?
>
Yes, yaml_constructors is in pyyaml since the initial release.
Thanks,
Kirill
------------------------------------------------------------------------------