pyang icon indicating copy to clipboard operation
pyang copied to clipboard

Documentation to use pyang in python ?

Open dgarros opened this issue 9 years ago • 9 comments

Hi

I would be interested to use pyang as part of a larger program written in python I'm new to pyang but I'm having trouble to find documentation of the python library itself. All documentation I found so far are using the CLI tool.

Please could someone help me to locate the documentation for the python classes for pyang

Thanks Damien

dgarros avatar Jul 24 '16 16:07 dgarros

What I did was look at what pyangbind did in their implementation.

Basically, you need to implement pyang.plugin.PyangPlugin and the pyang_plugin_init method. The emit method of your class has the hierarchy you need.

zombie-guru avatar Jul 27 '16 20:07 zombie-guru

@zombie-guru thanks for your response, I'll take a deeper look but, any chance you could share a snippet of code to help me started

It would be really great to have a better documentation on this ...

dgarros avatar Jul 29 '16 02:07 dgarros

Sure. All I learned was from reading source code of other plugins.

You'll need a plugins folder and a myplugin.py script in there. Next, you'll need to add the following code to myplugin.py

from pyang import plugin

def pyang_plugin_init():
    plugin.register_plugin(MyYangPlugin())

class MyYangPlugin(plugin.PyangPlugin):
    def add_output_format(self, fmts):
        self.multiple_modules = True
        fmts['my-plugin-output'] = self

    def emit(self, ctx, modules, fd):
        # modules has information on your pyang modules you supplied as input.
        # I just used import pdb; pdb.set_trace(), dir, and print to explore the attributes.
        import pdb
        pdb.set_trace()
        # write your output to fd. this is either stdout or a file but depends on the args sent to the
        #pyang script.
        fd.write('hello world!')

    def add_opts(self, optparser):
        # you can add additional cmd line options here.
        optlist = []
        g = optparser.add_option_group("my-plugin output specific options")
        g.add_options(optlist)

Once you have that, just supply that folder and your registered output type to the pyang script.

pyang --plugindir plugins -f my-plugin-output $MYARGS
# hello world!

Let me know if that helps.

zombie-guru avatar Jul 29 '16 03:07 zombie-guru

Hi,

Yes, we really need documentation for plugin writers! It would be great if someone that learns how it works writes some documentation at the same time!

mbj4668 avatar Aug 22 '16 14:08 mbj4668

@mbj4668 thank you I am interested in, could you please guide us, I found something like https://robot-framework.readthedocs.io/en/3.0/ is it useful?

vkosuri avatar Aug 23 '16 03:08 vkosuri

@mbj4668 I can give it a try but some guidance would be useful :) I haven't tried @zombie-guru suggestions yet .. lots of travel lately

dgarros avatar Aug 23 '16 06:08 dgarros

As for format, I an open to suggestions, but I would go for a simple structure on the wiki.

As for contents, I think all of you that have written or are about to write a plugin know best what is needed. We need to dicuss different kinds of plugins; most plugins are 'output' plugins (e.g. 'tree'), what is needed for them. Some others add additional checking (e.g, 'lint'). Some add additional grammar (e.g., 'smi').

All share the common callback interface, as @zombie-guru wrote.

Next, we need to document the API that plugins can use to the internals. Now it is getting tricky. In one sense, the plugins can use any function in pyang. But the idea was to add at least common/stable API functions to plugin.py. This has not been done. Instead these functions are mostly in statements.py. Maybe add these functions to plugin.py? In general, as you write documentation, you often find that the code that you document is not clear or it is inconsistent. I am all for changing such code.

mbj4668 avatar Aug 23 '16 06:08 mbj4668

Hi

I would be interested to use pyang as part of a larger program written in python I'm new to pyang but I'm having trouble to find documentation of the python library itself. All documentation I found so far are using the CLI tool.

Please could someone help me to locate the documentation for the python classes for pyang

Thanks Damien

Can you share how you are aware of CLI tools to me.

ksp2 avatar Oct 16 '18 09:10 ksp2

What is pyang_plugin_init method and emit method all about? Is there any documentation about this?

udhayala avatar Jun 13 '22 13:06 udhayala