Repository navigation
Expand file tree
/
Copy pathextension.py
More file actions
216 lines (187 loc) · 8.22 KB
/
Copy pathextension.py
File metadata and controls
216 lines (187 loc) · 8.22 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright (c) 2018-2019 Robin Jarry
# Copyright (c) 2020 6WIND S.A.
# Copyright (c) 2021 RACOM s.r.o.
# SPDX-License-Identifier: MIT
from typing import Callable, Optional
from _libyang import ffi, lib
from .context import Context
from .log import get_libyang_level
from .schema import ExtensionCompiled, ExtensionParsed, Module
from .util import LibyangError, c2str, str2c
# -------------------------------------------------------------------------------------
extensions_plugins = {}
class LibyangExtensionError(LibyangError):
def __init__(self, message: str, ret: int, log_level: int) -> None:
super().__init__(message)
self.ret = ret
self.log_level = log_level
@ffi.def_extern(name="lypy_lyplg_ext_parse_clb")
def libyang_c_lyplg_ext_parse_clb(pctx, pext):
plugin = extensions_plugins[pext.plugin_ref]
module_cdata = lib.lyplg_ext_parse_get_cur_pmod(pctx).mod
context = Context(cdata=module_cdata.ctx)
module = Module(context, module_cdata)
parsed_ext = ExtensionParsed(context, pext, module)
plugin.set_parse_ctx(pctx)
try:
plugin.parse_clb(module, parsed_ext)
return lib.LY_SUCCESS
except LibyangExtensionError as e:
ly_level = get_libyang_level(e.log_level)
if ly_level is None:
ly_level = lib.LY_EPLUGIN
e_str = str(e)
plugin.add_error_message(e_str)
lib.lyplg_ext_parse_log(pctx, pext, ly_level, e.ret, str2c(e_str))
return e.ret
@ffi.def_extern(name="lypy_lyplg_ext_compile_clb")
def libyang_c_lyplg_ext_compile_clb(cctx, pext, cext):
plugin = extensions_plugins[pext.plugin_ref]
context = Context(cdata=lib.lyplg_ext_compile_get_ctx(cctx))
module = Module(context, cext.module)
parsed_ext = ExtensionParsed(context, pext, module)
compiled_ext = ExtensionCompiled(context, cext)
plugin.set_compile_ctx(cctx)
try:
plugin.compile_clb(parsed_ext, compiled_ext)
return lib.LY_SUCCESS
except LibyangExtensionError as e:
ly_level = get_libyang_level(e.log_level)
if ly_level is None:
ly_level = lib.LY_EPLUGIN
e_str = str(e)
plugin.add_error_message(e_str)
lib.lyplg_ext_compile_log(cctx, cext, ly_level, e.ret, str2c(e_str))
return e.ret
@ffi.def_extern(name="lypy_lyplg_ext_parse_free_clb")
def libyang_c_lyplg_ext_parse_free_clb(ctx, pext):
plugin = extensions_plugins[pext.plugin_ref]
context = Context(cdata=ctx)
parsed_ext = ExtensionParsed(context, pext, None)
plugin.parse_free_clb(parsed_ext)
@ffi.def_extern(name="lypy_lyplg_ext_compile_free_clb")
def libyang_c_lyplg_ext_compile_free_clb(ctx, cext):
plugin = extensions_plugins[getattr(cext, "def").plugin_ref]
context = Context(cdata=ctx)
compiled_ext = ExtensionCompiled(context, cext)
plugin.compile_free_clb(compiled_ext)
class ExtensionPlugin:
ERROR_SUCCESS = lib.LY_SUCCESS
ERROR_MEM = lib.LY_EMEM
ERROR_INVALID_INPUT = lib.LY_EINVAL
ERROR_NOT_VALID = lib.LY_EVALID
ERROR_DENIED = lib.LY_EDENIED
ERROR_NOT = lib.LY_ENOT
def __init__(
self,
module_name: str,
name: str,
id_str: str,
context: Optional[Context] = None,
parse_clb: Optional[Callable[[Module, ExtensionParsed], None]] = None,
compile_clb: Optional[
Callable[[ExtensionParsed, ExtensionCompiled], None]
] = None,
parse_free_clb: Optional[Callable[[ExtensionParsed], None]] = None,
compile_free_clb: Optional[Callable[[ExtensionCompiled], None]] = None,
) -> None:
"""
Set the callback functions, which will be called if libyang will be processing
given extension defined by name from module defined by module_name.
:arg self:
This instance of extension plugin
:arg module_name:
The name of module in which the extension is defined
:arg name:
The name of extension itself
:arg id_str:
The unique ID of extension plugin within the libyang context
:arg context:
The context in which the extension plugin will be used. If set to None,
the plugin will be used for all existing and even future contexts
:arg parse_clb:
The optional callback function of which will be called during extension parsing
Expected arguments are:
module: The module which is being parsed
extension: The exact extension instance
Expected raises:
LibyangExtensionError in case of processing error
:arg compile_clb:
The optional callback function of which will be called during extension compiling
Expected arguments are:
extension_parsed: The parsed extension instance
extension_compiled: The compiled extension instance
Expected raises:
LibyangExtensionError in case of processing error
:arg parse_free_clb
The optional callback function of which will be called during freeing of parsed extension
Expected arguments are:
extension: The parsed extension instance to be freed
:arg compile_free_clb
The optional callback function of which will be called during freeing of compiled extension
Expected arguments are:
extension: The compiled extension instance to be freed
"""
self.context = context
self.module_name = module_name
self.module_name_cstr = str2c(self.module_name)
self.name = name
self.name_cstr = str2c(self.name)
self.id_str = id_str
self.id_cstr = str2c(self.id_str)
self.parse_clb = parse_clb
self.compile_clb = compile_clb
self.parse_free_clb = parse_free_clb
self.compile_free_clb = compile_free_clb
self._error_messages = []
self._pctx = ffi.NULL
self._cctx = ffi.NULL
self.cdata = ffi.new("struct lyplg_ext_record[2]")
self.cdata[0].module = self.module_name_cstr
self.cdata[0].name = self.name_cstr
self.cdata[0].plugin.id = self.id_cstr
if self.parse_clb is not None:
self.cdata[0].plugin.parse = lib.lypy_lyplg_ext_parse_clb
if self.compile_clb is not None:
self.cdata[0].plugin.compile = lib.lypy_lyplg_ext_compile_clb
if self.parse_free_clb is not None:
self.cdata[0].plugin.pfree = lib.lypy_lyplg_ext_parse_free_clb
if self.compile_free_clb is not None:
self.cdata[0].plugin.cfree = lib.lypy_lyplg_ext_compile_free_clb
ret = lib.lyplg_add_extension_plugin(
context.cdata if context is not None else ffi.NULL,
lib.LYPLG_EXT_API_VERSION,
ffi.cast("const void *", self.cdata),
)
if ret != lib.LY_SUCCESS:
raise LibyangError("Unable to add extension plugin")
if self.cdata[0].plugin not in extensions_plugins:
extensions_plugins[self.cdata[0].plugin] = self
def __del__(self) -> None:
if self.cdata[0].plugin in extensions_plugins:
del extensions_plugins[self.cdata[0].plugin]
@staticmethod
def stmt2str(stmt: int) -> str:
return c2str(lib.lyplg_ext_stmt2str(stmt))
def add_error_message(self, err_msg: str) -> None:
self._error_messages.append(err_msg)
def clear_error_messages(self) -> None:
self._error_messages.clear()
def set_parse_ctx(self, pctx) -> None:
self._pctx = pctx
def set_compile_ctx(self, cctx) -> None:
self._cctx = cctx
def parse_substmts(self, ext: ExtensionParsed) -> int:
return lib.lyplg_ext_parse_extension_instance(self._pctx, ext.cdata)
def compile_substmts(self, pext: ExtensionParsed, cext: ExtensionCompiled) -> int:
return lib.lyplg_ext_compile_extension_instance(
self._cctx, pext.cdata, cext.cdata
)
def free_parse_substmts(self, ext: ExtensionParsed) -> None:
lib.lyplg_ext_pfree_instance_substatements(
self.context.cdata, ext.cdata.substmts
)
def free_compile_substmts(self, ext: ExtensionCompiled) -> None:
lib.lyplg_ext_cfree_instance_substatements(
self.context.cdata, ext.cdata.substmts
)