]>
git.proxmox.com Git - mirror_frr.git/blob - yang/embedmodel.py
3 # YANG module to C wrapper
4 # written 2018 by David Lamparter, placed in Public Domain.
14 outdir
= os
.path
.dirname(os
.path
.abspath(outname
))
17 except FileExistsError
:
20 # these are regexes to avoid a compile-time/host dependency on yang-tools
21 # or python-yang. Cross-compiling FRR is already somewhat involved, no need
22 # to make it even harder.
24 re_name
= re
.compile(r
"\bmodule\s+([^\s]+)\s+\{")
25 re_subname
= re
.compile(r
"\bsubmodule\s+([^\s]+)\s+\{")
26 re_mainname
= re
.compile(r
"\bbelongs-to\s+([^\s]+)\s+\{")
27 re_rev
= re
.compile(r
"\brevision\s+([\d-]+)\s+\{")
30 template
= """/* autogenerated by embedmodel.py. DO NOT EDIT */
35 static const char model[] =
38 static struct yang_module_embed embed = {
41 \t.sub_mod_name = "%s",
42 \t.sub_mod_rev = "%s",
47 static void embed_register(void) __attribute__((_CONSTRUCTOR(2000)));
48 static void embed_register(void)
50 \tyang_module_embed(&embed);
54 passchars
= set(string
.printable
) - set("\\'\"%\r\n\t\x0b\x0c")
66 return "\\x%02x" % (ord(char
))
70 return "".join([escapech(i
) for i
in line
])
73 with
open(inname
, "r") as fd
:
80 # XML support isn't actively used currently, but it's here in case the need
81 # arises. It does avoid the regex'ing.
83 from xml
.etree
import ElementTree
85 xml
= ElementTree
.fromstring(data
)
86 name
= xml
.get("name")
87 rev
= xml
.find("{urn:ietf:params:xml:ns:yang:yin:1}revision").get("date")
90 search_name
= re_name
.search(data
)
92 name
= search_name
.group(1)
93 rev
= re_rev
.search(data
).group(1)
95 search_name
= re_subname
.search(data
)
96 sub_name
= search_name
.group(1)
97 name
= re_mainname
.search(data
).group(1)
98 sub_rev
= re_rev
.search(data
).group(1)
101 if name
is None or rev
is None:
102 raise ValueError("cannot determine YANG module name and revision")
104 lines
= [escape(row
) for row
in data
.split("\n")]
105 text
= '\\n"\n\t"'.join(lines
)
107 with
open(outname
, "w") as fd
:
110 % (text
, escape(name
), escape(rev
), escape(sub_name
), escape(sub_rev
), fmt
)