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