@@ -39,6 +39,7 @@ class SysrepoSession:
3939 "cdata" ,
4040 "is_implicit" ,
4141 "subscriptions" ,
42+ "nacm_subscription" ,
4243 )
4344
4445 # begin: general
@@ -52,10 +53,33 @@ def __init__(self, cdata, implicit: bool = False):
5253 self .cdata = cdata
5354 self .is_implicit = implicit
5455 self .subscriptions = []
56+ self .nacm_subscription = None
5557
5658 def __enter__ (self ) -> "SysrepoSession" :
5759 return self
5860
61+ def init_nacm (self , nacm_user : str , no_thread : bool = False ) -> None :
62+ """
63+ Set the NACM user for this session. This is used to determine the effective user
64+ for NACM checks.
65+
66+ :arg nacm_user:
67+ The NACM user name to be set.
68+ :arg no_thread:
69+ If True, no thread will be created for handling NACM subscription meaning.
70+ Default to False.
71+ """
72+ flags = _subscribe_flags (
73+ no_thread = no_thread ,
74+ )
75+ if self .is_implicit :
76+ raise SysrepoUnsupportedError ("cannot set NACM for implicit sessions" )
77+
78+ sub_p = ffi .new ("sr_subscription_ctx_t **" )
79+ check_call (lib .sr_nacm_init , self .cdata , flags , sub_p )
80+ check_call (lib .sr_nacm_set_user , self .cdata , str2c (nacm_user ))
81+ self .nacm_subscription = sub_p
82+
5983 def __exit__ (self , * args , ** kwargs ) -> None :
6084 self .stop ()
6185
@@ -70,6 +94,10 @@ def stop(self) -> None:
7094 return # already stopped
7195 if self .is_implicit :
7296 raise SysrepoUnsupportedError ("implicit sessions cannot be stopped" )
97+ if self .nacm_subscription is not None :
98+ check_call (lib .sr_unsubscribe , self .nacm_subscription [0 ])
99+ self .nacm_subscription = None
100+ lib .sr_nacm_destroy ()
73101
74102 # clear subscriptions
75103 while self .subscriptions :
0 commit comments