Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ sr_log_level_t sr_log_get_syslog(void);
typedef struct sr_conn_ctx_s sr_conn_ctx_t;
typedef struct sr_session_ctx_s sr_session_ctx_t;
typedef enum sr_conn_flag_e {
SR_CONN_CACHE_RUNNING,
SR_CONN_CTX_SET_PRIV_PARSED,
SR_CONN_DEFAULT,
...
} sr_conn_flag_t;
typedef uint32_t sr_conn_options_t;

typedef enum {
SR_CTX_DEFAULT,
SR_CTX_NO_PRINTED,
SR_CTX_SET_PRIV_PARSED,
...
} sr_context_flag_t;

typedef enum sr_datastore_e {
SR_DS_STARTUP,
SR_DS_RUNNING,
Expand Down Expand Up @@ -76,8 +82,10 @@ struct timespec {
long tv_nsec;
};

int sr_connect(const sr_conn_options_t, sr_conn_ctx_t **);
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 *);
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 **);
Expand Down
7 changes: 2 additions & 5 deletions cffi/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#include <sysrepo/version.h>
#include <sysrepo/netconf_acm.h>

#if (SR_VERSION_MAJOR != 7)
#error "This version of sysrepo bindings only works with libsysrepo.so.7"
#endif
#if (SR_VERSION_MINOR < 10)
#error "Need at least libsysrepo.so.7.10"
#if (SR_VERSION_MAJOR != 8)
#error "This version of sysrepo bindings only works with libsysrepo.so.8"
#endif
18 changes: 12 additions & 6 deletions sysrepo/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,32 @@ class SysrepoConnection:

__slots__ = ("cdata",)

def __init__(self, cache_running: bool = False):
def __init__(self, cache_running: bool = False, not_printed: bool = True):
"""
:arg cache_running:
Always cache running datastore data which makes mainly repeated retrieval of
data much faster. Affects all sessions created on this connection.
:arg not_printed:
The context is created from a sysrepo shared memory and is loaded faster.
Some operations on the context do not work.
"""
flags = 0
if cache_running:
flags |= lib.SR_CONN_CACHE_RUNNING
ctx_flags = 0
if not_printed:
ctx_flags |= lib.SR_CTX_NO_PRINTED

# mandatory flag to work with libyang-python
flags |= lib.SR_CONN_CTX_SET_PRIV_PARSED
ctx_flags |= lib.SR_CTX_SET_PRIV_PARSED

conn_p = ffi.new("sr_conn_ctx_t **")
# valid_signals() is only available since python 3.8
valid_signals = getattr(signal, "valid_signals", lambda: range(1, signal.NSIG))
sigmask = signal.pthread_sigmask(signal.SIG_BLOCK, valid_signals())
try:
check_call(lib.sr_connect, flags, conn_p)
lib.sr_cache_running(cache_running)
lib.sr_context_options(ctx_flags, 0, ffi.NULL)
check_call(lib.sr_connect, 0, conn_p)
self.cdata = ffi.gc(conn_p[0], lib.sr_disconnect)

finally:
signal.pthread_sigmask(signal.SIG_SETMASK, sigmask)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ def test_conn_get_module_infos(self):
self.assertEqual(pwd.getpwnam(owner).pw_uid, os.geteuid())
self.assertEqual(grp.getgrnam(group).gr_gid, os.getegid())
self.assertEqual(perm, 0o600)

def test_conn_start_session_with_printed_context(self):
with sysrepo.SysrepoConnection(not_printed=False) as conn:
sess = conn.start_session()
self.assertEqual(sess.get_datastore(), "running")
sess.stop()
Loading