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