diff --git a/CMakeLists.txt b/CMakeLists.txt index aca3a2acc839f56f0219b8efcfd48f9391a0bc75..b22f0e959613054056b526bf1cbdf35431c87802 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,10 +32,10 @@ endif( ) add_custom_command( TARGET VAPython POST_BUILD COMMAND "distutils_build.${BATCH_SCRIPT_EXTENSION}" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMENT "Running distutils" VERBATIM ) # configure -vista_configure_lib( VAPython ) -vista_install( VAPython ) -vista_create_cmake_configs( VAPython ) -vista_create_default_info_file( VAPython ) +#vista_configure_lib( VAPython ) +#vista_install( VAPython ) +#vista_create_cmake_configs( VAPython ) +#vista_create_default_info_file( VAPython ) set_property( TARGET VAPython PROPERTY FOLDER "VA/Bindings" ) diff --git a/distutils_build.bat b/distutils_build.bat index 64ed95abffded9714da5b0976b928e3404143d3c..936fc7f04fe16e2f1db7bf4641d2511d5a18351c 100644 --- a/distutils_build.bat +++ b/distutils_build.bat @@ -1,7 +1,7 @@ python.exe setup.py clean -python.exe setup.py build +python.exe setup.py build --force python.exe setup.py install --prefix dist python.exe setup.py sdist --formats=zip -# later: -#python.exe setup.py bdist_wininst +rem later: +rem python.exe setup.py bdist_wininst python.exe setup.py check diff --git a/src/vasingletonmethods.hpp b/src/vasingletonmethods.hpp index 3379a53d1984741e15867a1f7d90400811c5b7bb..dedd6d3f13701af2dcec0fe4542eb3764b257b65 100644 --- a/src/vasingletonmethods.hpp +++ b/src/vasingletonmethods.hpp @@ -11,117 +11,121 @@ static IVANetClient* g_pVANetClient = nullptr; //!< Static pointer to VANetClient instance static PyObject* g_pVAError = nullptr; //!< Static pointer to error instance +// Ugly definitions to ease try-catching VA exceptions +#define VAPY_REQUIRE_CONN_TRY try { RequireCoreAvailable(); +#define VAPY_CATCH_RETURN } catch (const CVAException& oError) { PyErr_SetString(PyExc_Exception, oError.ToString().c_str()); return NULL; } + //! Helper for API dev -static PyObject* va_not_implemented( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) +static PyObject* va_not_implemented(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { VA_EXCEPT_NOT_IMPLEMENTED; return NULL; -} +}; //! Raises an exception if core is not available static void RequireCoreAvailable() { - if( !g_pVANetClient ) - VA_EXCEPT2( CVAException::NETWORK_ERROR, "VA client not available, please connect first" ); + if (!g_pVANetClient) + VA_EXCEPT2(CVAException::NETWORK_ERROR, "VA client not available, please connect first"); - if( !g_pVANetClient->GetCoreInstance() ) - VA_EXCEPT2( CVAException::NETWORK_ERROR, "VA client available, but access to VA interface failed. Please reconnect." ); -} + if (!g_pVANetClient->GetCoreInstance()) + VA_EXCEPT2(CVAException::NETWORK_ERROR, "VA client available, but access to VA interface failed. Please reconnect."); +}; -static PyObject* va_connect( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) +static PyObject* va_connect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { - if( !g_pVANetClient ) + if (!g_pVANetClient) g_pVANetClient = IVANetClient::Create(); - if( g_pVANetClient->IsConnected() ) + if (g_pVANetClient->IsConnected()) { - PyErr_Warn( pSelf, "Was still connected, forced disconnect." ); + PyErr_Warn(pSelf, "Was still connected, forced disconnect."); g_pVANetClient->Disconnect(); } static const char * const _keywords[] = { "server", "port", NULL }; - static _PyArg_Parser _parser = { "Oi:connect", _keywords, 0 }; - PyObject* pServer; - PyObject* pPort; - - if( !_PyArg_ParseStack( ppArgs, nArgs, pKeywordNames, &_parser, &pServer, &pPort ) ) - return PyBool_FromLong( 0 ); - - std::string sServerIP = "localhost"; - char* pcServerIP = nullptr; - if( PyArg_ParseTuple( pPort, "i", &pcServerIP ) ) - sServerIP = std::string( pcServerIP ); - + static _PyArg_Parser _parser = { "|si:connect", _keywords, 0 }; + char* pcServerIP; int iServerPort = 12340; - int* piServerPort = nullptr; - if( PyArg_ParseTuple( pPort, "i", &piServerPort ) ) - iServerPort = *piServerPort; - if( IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize( sServerIP, iServerPort ) ) - return PyBool_FromLong( 1 ); + if (!_PyArg_ParseStack(ppArgs, nArgs, pKeywordNames, &_parser, pcServerIP, &iServerPort)) + return NULL; - PyErr_SetString( PyExc_ConnectionError, std::string( "Could not connect to " + sServerIP + " on " + std::to_string( ( long ) iServerPort ) ).c_str() ); + std::string sServerIP = pcServerIP ? std::string(pcServerIP) : "localhost"; + if (IVANetClient::VA_NO_ERROR == g_pVANetClient->Initialize(sServerIP, iServerPort)) + return PyBool_FromLong(1); + + PyErr_SetString(PyExc_ConnectionError, std::string("Could not connect to " + sServerIP + " on " + std::to_string((long)iServerPort)).c_str()); return NULL; -} +}; -static PyObject* va_disconnect( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) +static PyObject* va_disconnect(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { - if( g_pVANetClient ) + if (g_pVANetClient) g_pVANetClient->Disconnect(); + else + PyErr_Warn(pSelf, "Was not connected, doing nothing. Use is_connected to avoid this message."); + + return PyBool_FromLong(1); +}; - return PyBool_FromLong( 1 ); -} -static PyObject* va_is_connected( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) +static PyObject* va_is_connected(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { - if( !g_pVANetClient ) - return PyBool_FromLong( 0 ); + if (!g_pVANetClient) + return PyBool_FromLong(0); else - return PyBool_FromLong( g_pVANetClient->IsConnected() ); -} + return PyBool_FromLong(g_pVANetClient->IsConnected()); +}; -static PyObject* va_reset( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) +static PyObject* va_reset(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { - RequireCoreAvailable(); + VAPY_REQUIRE_CONN_TRY; g_pVANetClient->GetCoreInstance()->Reset(); + return PyBool_FromLong(1); - return PyBool_FromLong( 1 ); -} + VAPY_CATCH_RETURN; +}; -static PyObject* va_enumerate_modules( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) +static PyObject* va_enumerate_modules(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { - RequireCoreAvailable(); + VAPY_REQUIRE_CONN_TRY; VA_EXCEPT_NOT_IMPLEMENTED; - return NULL; -} -static PyObject* va_call_module( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) + VAPY_CATCH_RETURN; +}; + +static PyObject* va_call_module(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { - RequireCoreAvailable(); + VAPY_REQUIRE_CONN_TRY; VA_EXCEPT_NOT_IMPLEMENTED; - return NULL; -} -static PyObject* va_add_search_path( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) + VAPY_CATCH_RETURN; +}; + +static PyObject* va_add_search_path(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { - RequireCoreAvailable(); + VAPY_REQUIRE_CONN_TRY; VA_EXCEPT_NOT_IMPLEMENTED; - return NULL; -} -static PyObject* va_create_listener( PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames ) + VAPY_CATCH_RETURN; +}; + +static PyObject* va_create_listener(PyObject* pSelf, PyObject** ppArgs, Py_ssize_t nArgs, PyObject* pKeywordNames) { - RequireCoreAvailable(); + VAPY_REQUIRE_CONN_TRY; std::string sName = "PyListener"; - int iID = g_pVANetClient->GetCoreInstance()->CreateListener( sName, IVACore::VA_AURAMODE_ALL ); + int iID = g_pVANetClient->GetCoreInstance()->CreateListener(sName, IVACore::VA_AURAMODE_ALL); - return PyLong_FromLong( iID ); -} + return PyLong_FromLong(iID); + + VAPY_CATCH_RETURN; +}; diff --git a/tests/va_test_connection.py b/tests/va_test_connection.py index 4f9707b0c14bc1cc9d996f864882422b8b1f4dbb..c4e6f27dfc7d9e5e700787f9023e84072227e156 100644 --- a/tests/va_test_connection.py +++ b/tests/va_test_connection.py @@ -1,16 +1,20 @@ # VA is used as a singleton. # You can access va in every script, function and method. -print( "Testing va extension connection." ) +# Add va module if it was not installed +import sys +sys.path.append( '../Lib/site-packages' ) # deploy structure import va -if va.connect( "localhost", 12340 ) == 1 : +print( "Testing va extension connection." ) + +if va.connect( "pc-jst" ) : print( "Successfully connected" ) else : print( "Connection failed" ) -if va.is_connected() == 1: +if va.is_connected() : va.disconnect() print( "Test done." )