2020#include <string.h>
2121#include <libssh/libssh.h>
2222#include <libyang/libyang.h>
23+ #include <libyang/swigpyrun.h>
2324
2425#include "../src/config.h"
2526#include "netconf.h"
2627#include "session.h"
2728#include "rpc.h"
2829
30+ extern PyObject * libnetconf2Error ;
31+
2932int
3033auth_hostkey_check_pyclb (const char * hostname , ssh_session session , void * priv )
3134{
@@ -418,6 +421,40 @@ ncSessionGetVersion(ncSessionObject *self, void *closure)
418421 }
419422}
420423
424+ static PyObject *
425+ ncSessionGetContext (ncSessionObject * self , void * closure )
426+ {
427+ PyObject * context , * result , * module ;
428+
429+
430+ /* process the received data */
431+ context = SWIG_NewPointerObj (self -> ctx , SWIG_Python_TypeQuery ("ly_ctx*" ), 0 );
432+ if (!context ) {
433+ PyErr_SetString (libnetconf2Error , "Building Python object from context structure failed." );
434+ goto error ;
435+ }
436+
437+ module = PyImport_ImportModule ("libyang" );
438+ if (module == NULL ) {
439+ PyErr_SetString (libnetconf2Error , "Could not import libyang module" );
440+ goto error ;
441+ }
442+
443+ result = PyObject_CallMethod (module , "create_new_Context" , "(O)" , context , NULL );
444+ Py_DECREF (module );
445+ Py_DECREF (context );
446+ if (!result ) {
447+ PyErr_SetString (libnetconf2Error , "Could not create Context object." );
448+ goto error ;
449+ }
450+
451+ return result ;
452+
453+ error :
454+ Py_XDECREF (context );
455+ return NULL ;
456+ }
457+
421458/*
422459 * Callback structures
423460 */
@@ -430,6 +467,7 @@ static PyGetSetDef ncSessionGetSetters[] = {
430467 {"transport" , (getter )ncSessionGetTransport , NULL , "Transport protocol used for the NETCONF Session." , NULL },
431468 {"version" , (getter )ncSessionGetVersion , NULL , "NETCONF Protocol version used for the NETCONF Session." , NULL },
432469 {"capabilities" , (getter )ncSessionGetCapabilities , NULL , "Capabilities of the NETCONF Session." , NULL },
470+ {"context" , (getter )ncSessionGetContext , NULL , "libyang context of the NETCONF Session." , NULL },
433471 {NULL } /* Sentinel */
434472};
435473
0 commit comments