]>
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
))
15 if not os
.path
.isdir(outdir
):
18 # these are regexes to avoid a compile-time/host dependency on yang-tools
19 # or python-yang. Cross-compiling FRR is already somewhat involved, no need
20 # to make it even harder.
22 re_name
= re
.compile(r
'\bmodule\s+([^\s]+)\s+\{')
23 re_rev
= re
.compile(r
'\brevision\s+([\d-]+)\s+\{')
26 template
= '''/* autogenerated by embedmodel.py. DO NOT EDIT */
31 static const char model[] =
34 static struct yang_module_embed embed = {
41 static void embed_register(void) __attribute__((_CONSTRUCTOR(2000)));
42 static void embed_register(void)
44 \tyang_module_embed(&embed);
48 passchars
= set(string
.printable
) - set('\\\'"%\r\n\t\x0b\x0c')
58 return '\\x%02x' % (ord(char
))
60 return ''.join([escapech(i
) for i
in line
])
62 with
open(inname
, 'r') as fd
:
65 # XML support isn't actively used currently, but it's here in case the need
66 # arises. It does avoid the regex'ing.
68 from xml
.etree
import ElementTree
69 xml
= ElementTree
.fromstring(data
)
70 name
= xml
.get('name')
71 rev
= xml
.find('{urn:ietf:params:xml:ns:yang:yin:1}revision').get('date')
74 name
= re_name
.search(data
).group(1)
75 rev
= re_rev
.search(data
).group(1)
78 if name
is None or rev
is None:
79 raise ValueError('cannot determine YANG module name and revision')
81 lines
= [escape(row
) for row
in data
.split('\n')]
82 text
= '\\n"\n\t"'.join(lines
)
84 with
open(outname
, 'w') as fd
:
85 fd
.write(template
% (text
, escape(name
), escape(rev
), fmt
))