diff --git a/cffi/cdefs.h b/cffi/cdefs.h index 6e61c62..5eb9470 100644 --- a/cffi/cdefs.h +++ b/cffi/cdefs.h @@ -85,7 +85,11 @@ struct timespec { int sr_connect(const uint32_t opts, sr_conn_ctx_t **); int sr_disconnect(sr_conn_ctx_t *); void sr_cache_running(int); -uint32_t sr_context_options(uint32_t, int, uint32_t *); +int sr_context_options(uint32_t, int, uint32_t *); +const char *sr_yang_module_dir(void); +int sr_set_yang_module_dir(const char *); +const char *sr_ly_yang_module_dir(void); +int sr_set_ly_yang_module_dir(const char *); const struct ly_ctx *sr_acquire_context(sr_conn_ctx_t *); void sr_release_context(sr_conn_ctx_t *); int sr_install_module(sr_conn_ctx_t *, const char *, const char *, const char **); diff --git a/sysrepo/__init__.py b/sysrepo/__init__.py index c126ca7..b5141d5 100644 --- a/sysrepo/__init__.py +++ b/sysrepo/__init__.py @@ -9,7 +9,13 @@ ChangeMoved, update_config_cache, ) -from .connection import SysrepoConnection +from .connection import ( + SysrepoConnection, + yang_module_dir, + set_yang_module_dir, + ly_yang_module_dir, + set_ly_yang_module_dir, +) from .errors import ( SysrepoCallbackFailedError, SysrepoCallbackShelveError, @@ -106,4 +112,8 @@ "configure_logging", "get_syslog_level", "get_stderr_level", + "yang_module_dir", + "set_yang_module_dir", + "ly_yang_module_dir", + "set_ly_yang_module_dir", ] diff --git a/sysrepo/connection.py b/sysrepo/connection.py index 42d3253..7bed40a 100644 --- a/sysrepo/connection.py +++ b/sysrepo/connection.py @@ -17,6 +17,44 @@ LOG = logging.getLogger(__name__) +# ------------------------------------------------------------------------------ +def yang_module_dir() -> str: + """ + Return the directory with internal sysrepo YANG modules. + """ + return c2str(lib.sr_yang_module_dir()) + + +# ------------------------------------------------------------------------------ +def set_yang_module_dir(path: Optional[str]) -> None: + """ + Set the directory with internal sysrepo YANG modules. + + :arg path: + YANG module directory, or None to reset to the compiled default. + """ + check_call(lib.sr_set_yang_module_dir, str2c(path)) + + +# ------------------------------------------------------------------------------ +def ly_yang_module_dir() -> str: + """ + Return the directory with internal libyang YANG modules used by sysrepo. + """ + return c2str(lib.sr_ly_yang_module_dir()) + + +# ------------------------------------------------------------------------------ +def set_ly_yang_module_dir(path: Optional[str]) -> None: + """ + Set the directory with internal libyang YANG modules used by sysrepo. + + :arg path: + YANG module directory, or None to reset to the libyang default. + """ + check_call(lib.sr_set_ly_yang_module_dir, str2c(path)) + + # ------------------------------------------------------------------------------ class SysrepoConnection: """ diff --git a/tests/test_connection.py b/tests/test_connection.py index 1d4b528..d1ce0ed 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -20,6 +20,44 @@ # ------------------------------------------------------------------------------ class ConnectionTest(unittest.TestCase): + def test_yang_module_dir(self): + original = sysrepo.yang_module_dir() + self.assertIsInstance(original, str) + self.assertTrue(original) + try: + sysrepo.set_yang_module_dir(None) + default = sysrepo.yang_module_dir() + self.assertIsInstance(default, str) + self.assertTrue(default) + + path = "/tmp/sysrepo-\u00e9/modules" + sysrepo.set_yang_module_dir(path) + self.assertEqual(sysrepo.yang_module_dir(), path) + + sysrepo.set_yang_module_dir(None) + self.assertEqual(sysrepo.yang_module_dir(), default) + finally: + sysrepo.set_yang_module_dir(original) + + def test_ly_yang_module_dir(self): + original = sysrepo.ly_yang_module_dir() + self.assertIsInstance(original, str) + self.assertTrue(original) + try: + sysrepo.set_ly_yang_module_dir(None) + default = sysrepo.ly_yang_module_dir() + self.assertIsInstance(default, str) + self.assertTrue(default) + + path = "/tmp/libyang-\u00e9/modules" + sysrepo.set_ly_yang_module_dir(path) + self.assertEqual(sysrepo.ly_yang_module_dir(), path) + + sysrepo.set_ly_yang_module_dir(None) + self.assertEqual(sysrepo.ly_yang_module_dir(), default) + finally: + sysrepo.set_ly_yang_module_dir(original) + def test_conn_connect(self): conn = sysrepo.SysrepoConnection() conn.disconnect()