@@ -39,6 +39,7 @@ class SysrepoSession:
39
39
"cdata" ,
40
40
"is_implicit" ,
41
41
"subscriptions" ,
42
+ "nacm_subscription" ,
42
43
)
43
44
44
45
# begin: general
@@ -52,10 +53,33 @@ def __init__(self, cdata, implicit: bool = False):
52
53
self .cdata = cdata
53
54
self .is_implicit = implicit
54
55
self .subscriptions = []
56
+ self .nacm_subscription = None
55
57
56
58
def __enter__ (self ) -> "SysrepoSession" :
57
59
return self
58
60
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
+
59
83
def __exit__ (self , * args , ** kwargs ) -> None :
60
84
self .stop ()
61
85
@@ -70,6 +94,10 @@ def stop(self) -> None:
70
94
return # already stopped
71
95
if self .is_implicit :
72
96
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 ()
73
101
74
102
# clear subscriptions
75
103
while self .subscriptions :
0 commit comments