Skip to content

Commit 6772e31

Browse files
committed
connection: add printed context support
Add the possibility to add the no_printed flag when creating the sysrepo session to avoid loading the context from the shared memory. Apply it by default to keep the behavior as before. The printed context is valuable to get a the sysrepo config/state faster but some operations on the context itself do not work. For instance printing and parsing a yang model will fail. Link: https://github.com/sysrepo/sysrepo/blob/v4.2.10/README.md#printed-context Signed-off-by: Jeremie Leska <[email protected]>
1 parent 58c9bb9 commit 6772e31

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

sysrepo/connection.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@ class SysrepoConnection:
3636

3737
__slots__ = ("cdata",)
3838

39-
def __init__(self, cache_running: bool = False):
39+
def __init__(self, cache_running: bool = False, not_printed: bool = True):
4040
"""
4141
:arg cache_running:
4242
Always cache running datastore data which makes mainly repeated retrieval of
4343
data much faster. Affects all sessions created on this connection.
44+
:arg not_printed:
45+
The context is created from a sysrepo shared memory and is loaded faster.
46+
Some operations on the context do not work.
4447
"""
4548
ctx_flags = 0
49+
if not_printed:
50+
ctx_flags |= lib.SR_CTX_NO_PRINTED
4651

4752
# mandatory flag to work with libyang-python
4853
ctx_flags |= lib.SR_CTX_SET_PRIV_PARSED

tests/test_connection.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ def test_conn_get_module_infos(self):
8787
self.assertEqual(pwd.getpwnam(owner).pw_uid, os.geteuid())
8888
self.assertEqual(grp.getgrnam(group).gr_gid, os.getegid())
8989
self.assertEqual(perm, 0o600)
90+
91+
def test_conn_start_session_with_printed_context(self):
92+
with sysrepo.SysrepoConnection(not_printed=False) as conn:
93+
sess = conn.start_session()
94+
self.assertEqual(sess.get_datastore(), "running")
95+
sess.stop()

0 commit comments

Comments
 (0)