Re: using yaml_constructors attribute of the Loader class
Manlio Perillo <[email protected]>
| Newsgroups | gmane.text.yaml.general |
|---|---|
| Message-ID | <[email protected]> |
-----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.
By the way, is yaml_constructors in old versions?
Thanks Manlio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkvV+/AACgkQscQJ24LbaURXdQCePDYrhFTprsAiUyZQljwEu7hQ
XyoAnjyZp7IQV4qqdy/fW63gvCi57GTW
=kMCU
-----END PGP SIGNATURE-----
------------------------------------------------------------------------------