示例#1
0
    def __init__(
            self,
            search_path: Optional[str] = None,
            disable_searchdir_cwd: bool = True,
            pointer=None,  # C type: "struct ly_ctx *"
            cdata=None,  # C type: "struct ly_ctx *"
    ):
        if pointer is not None:
            deprecated("pointer=", "cdata=", "2.0.0")
            cdata = pointer
        if cdata is not None:
            self.cdata = ffi.cast("struct ly_ctx *", cdata)
            return  # already initialized

        options = 0
        if disable_searchdir_cwd:
            options |= lib.LY_CTX_DISABLE_SEARCHDIR_CWD

        self.cdata = ffi.gc(
            lib.ly_ctx_new(ffi.NULL, options),
            lambda ctx: lib.ly_ctx_destroy(ctx, ffi.NULL),
        )
        if not self.cdata:
            raise self.error("cannot create context")

        search_dirs = []
        if "YANGPATH" in os.environ:
            search_dirs.extend(
                os.environ["YANGPATH"].strip(": \t\r\n'\"").split(":"))
        elif "YANG_MODPATH" in os.environ:
            search_dirs.extend(
                os.environ["YANG_MODPATH"].strip(": \t\r\n'\"").split(":"))
        if search_path:
            search_dirs.extend(search_path.strip(": \t\r\n'\"").split(":"))

        for path in search_dirs:
            if not os.path.isdir(path):
                continue
            if lib.ly_ctx_set_searchdir(self.cdata, str2c(path)) != 0:
                raise self.error("cannot set search dir")
示例#2
0
    def __init__(self, search_path=None,
                 options=lib.LY_CTX_DISABLE_SEARCHDIR_CWD):
        self._ctx = ffi.gc(lib.ly_ctx_new(ffi.NULL, options),
                           lambda c: lib.ly_ctx_destroy(c, ffi.NULL))
        if not self._ctx:
            raise self.error('cannot create context')

        search_dirs = []
        if 'YANGPATH' in os.environ:
            search_dirs.extend(
                os.environ['YANGPATH'].strip(': \t\r\n\'"').split(':'))
        elif 'YANG_MODPATH' in os.environ:
            search_dirs.extend(
                os.environ['YANG_MODPATH'].strip(': \t\r\n\'"').split(':'))
        if search_path:
            search_dirs.extend(search_path.strip(': \t\r\n\'"').split(':'))

        for path in search_dirs:
            if not os.path.isdir(path):
                continue
            if lib.ly_ctx_set_searchdir(self._ctx, str2c(path)) != 0:
                raise self.error('cannot set search dir')
示例#3
0
 def destroy(self):
     if self._ctx is not None:
         lib.ly_ctx_destroy(self._ctx, ffi.NULL)
         self._ctx = None
示例#4
0
 def destroy(self):
     if self.cdata is not None:
         lib.ly_ctx_destroy(self.cdata, ffi.NULL)
         self.cdata = None