]>
git.proxmox.com Git - mirror_frr.git/blob - yang/embedmodel.py
2 # SPDX-License-Identifier: NONE
4 # YANG module to C wrapper
5 # written 2018 by David Lamparter, placed in Public Domain.
15 outdir
= os
.path
.dirname(os
.path
.abspath(outname
))
18 except FileExistsError
:
21 # these are regexes to avoid a compile-time/host dependency on yang-tools
22 # or python-yang. Cross-compiling FRR is already somewhat involved, no need
23 # to make it even harder.
25 re_name
= re
.compile(r
"\bmodule\s+([^\s]+)\s+\{")
26 re_subname
= re
.compile(r
"\bsubmodule\s+([^\s]+)\s+\{")
27 re_mainname
= re
.compile(r
"\bbelongs-to\s+([^\s]+)\s+\{")
28 re_rev
= re
.compile(r
"\brevision\s+([\d-]+)\s+\{")
31 template
= """/* autogenerated by embedmodel.py. DO NOT EDIT */
36 static const char model[] =
39 static struct yang_module_embed embed = {
42 \t.sub_mod_name = "%s",
43 \t.sub_mod_rev = "%s",
48 static void embed_register(void) __attribute__((_CONSTRUCTOR(2000)));
49 static void embed_register(void)
51 \tyang_module_embed(&embed);
55 passchars
= set(string
.printable
) - set("\\'\"%\r\n\t\x0b\x0c")
67 return "\\x%02x" % (ord(char
))
71 return "".join([escapech(i
) for i
in line
])
74 with
open(inname
, "r") as fd
:
81 # XML support isn't actively used currently, but it's here in case the need
82 # arises. It does avoid the regex'ing.
84 from xml
.etree
import ElementTree
86 xml
= ElementTree
.fromstring(data
)
87 name
= xml
.get("name")
88 rev
= xml
.find("{urn:ietf:params:xml:ns:yang:yin:1}revision").get("date")
91 search_name
= re_name
.search(data
)
93 name
= search_name
.group(1)
94 rev
= re_rev
.search(data
).group(1)
96 search_name
= re_subname
.search(data
)
97 sub_name
= search_name
.group(1)
98 name
= re_mainname
.search(data
).group(1)
99 sub_rev
= re_rev
.search(data
).group(1)
102 if name
is None or rev
is None:
103 raise ValueError("cannot determine YANG module name and revision")
105 lines
= [escape(row
) for row
in data
.split("\n")]
106 text
= '\\n"\n\t"'.join(lines
)
108 with
open(outname
, "w") as fd
:
111 % (text
, escape(name
), escape(rev
), escape(sub_name
), escape(sub_rev
), fmt
)