-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml.py
58 lines (48 loc) · 1.9 KB
/
yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
This plugin translate YANG module into YAML format http://yaml.org/
"""
from pyang import plugin
def pyang_plugin_init():
plugin.register_plugin(YAMLPlugin())
class YAMLPlugin(plugin.PyangPlugin):
def add_output_format(self, fmts):
self.multiple_modules = True
fmts["yaml"] = self
def setup_fmt(self, ctx):
ctx.implicit_errors = False
def emit(self, ctx, modules, fd):
fd.write("---" + "\n")
parent = None
for module in modules:
self.process_children(module, parent, fd)
def process_children(self, node, parent, fd, indent=0):
"""
Process all children of `node`, except "rpc" and "notification".
"""
is_indent = False
if parent == None:
fd.write(' ' * indent + "%s: " % (node.arg)+"\n")
indent = indent + 2
if parent is not None and parent.keyword == "case":
indent = indent + 2
for ch in node.i_children:
if ch.keyword in ["rpc", "notification"]: continue
if ch.keyword in ["container", "list", "choice", "case"]:
parent = ch
if is_indent:
indent = indent + 2
is_indent = False
fd.write(' ' * indent + "%s: " % (ch.arg)+"\n")
self.process_children(ch, parent, fd, indent+2)
elif ch.keyword in ["leaf", "leaf-list"]:
if hasattr(ch, 'i_is_key'):
fd.write(' ' * indent + "- %s:" % ch.arg + "\n")
is_indent = True
elif ch.keyword == "leaf-list":
fd.write(' ' * indent + "%s:" % ch.arg + "\n")
else:
if is_indent:
indent = indent + 2
is_indent = False
fd.write(' ' * indent + "%s:" % ch.arg + "\n")