Index: NetworkEditor/src/MainWindow.hxx =================================================================== --- NetworkEditor/src/MainWindow.hxx (revision 14445) +++ NetworkEditor/src/MainWindow.hxx (working copy) @@ -19,10 +19,7 @@ #include #include "NetworkEditorVersion.hxx" #include "RichTextEditor.hxx" -//#include -//CLAM_EMBEDDED_FILE(genderChange,"PluginExamples/ClamLadspaPluginExample/genderChange.clamnetwork") - // copied from Annotator: #include "TaskRunner.hxx" @@ -50,7 +47,21 @@ #include #include +#include "Python.h" +#include +#include +CLAM_EMBEDDED_FILE(migrationScript,"migrationScript") +class ClamrefactorException : public std::exception { +public: + ClamrefactorException(char * msg) : _msg(msg) {} + const char * what() const throw () { + return _msg; + } +private: + char * _msg; +}; + //#define AFTER13RELEASE class PlaybackIndicator : public QLabel @@ -265,13 +276,9 @@ return; } int comparison = compareVersions(networkVersion, CLAM::GetVersion()); - - // Fake to disable upgrades while they depend on file locations - if (comparison==-1) comparison=0; - - if (comparison==-1) // Older version + if (comparison==-1) { - int response = QMessageBox::warning(this, + QMessageBox::warning(this, tr("NetworkEditor"), tr("This network was created with an older version, %1, of NetworkEditor.\n" "It will be updated to version %2 when saved.") @@ -279,17 +286,25 @@ .arg(CLAM::GetVersion()) , QMessageBox::Ok); - const char * translatedNetwork = updatedNetworkVersion(qPrintable(filename)); - if (translatedNetwork) - { - loadFromString(translatedNetwork, filename); + try { + const char * translatedNetwork = updatedNetworkVersion(qPrintable(filename)); + if (translatedNetwork) + { + loadFromString(translatedNetwork, filename); + return; + } + } + catch(ClamrefactorException & e) { + QMessageBox::warning(this, + tr("NetworkEditor"), + tr(qPrintable(QString("An error ocurred while updating the network:\n") + + e.what())) + , + QMessageBox::Ok); return; } - QMessageBox::warning(this, - tr("NetworkEditor"), - tr("Updating procedure failed, trying to load old version file.")); } - if (comparison==+1) // Newer version + if (comparison==+1) { int response = QMessageBox::question(this, tr("NetworkEditor"), @@ -304,7 +319,78 @@ } loadFromFile(filename); } - const char * updatedNetworkVersion(const std::string & filename); + void getExceptionMessage() + { + PyObject *pType, *pValue, *pTrace; + PyErr_Fetch(&pType, &pValue, &pTrace); + char *strErrorMessage = PyString_AsString(pValue); + throw(ClamrefactorException(strErrorMessage)); + } + const char * updatedNetworkVersion(const std::string & filename) + { + Py_Initialize(); + PyObject * pModule = PyImport_ImportModule("clamrefactor"); + if (pModule == NULL) + { + getExceptionMessage(); + } + PyObject * pDict = PyModule_GetDict(pModule); + // Build the name of a callable class + PyObject * pClassClamNetwork = PyDict_GetItemString(pDict, "ClamNetwork"); + // Create an instance of the class + if (!PyCallable_Check(pClassClamNetwork)) + { + getExceptionMessage(); + } + PyObject * pClamNetwork = Py_BuildValue( "(s)", filename.c_str() ); + PyObject * pInstanceClamNetwork = PyObject_CallObject(pClassClamNetwork, pClamNetwork); + Py_DECREF(pClamNetwork); + if (pInstanceClamNetwork == NULL) + { + getExceptionMessage(); + } + std::stringstream ss(migrationScript); + std::string command; + PyObject * pValue; + while( std::getline(ss, command) ) + { + pValue = PyObject_CallMethod( pInstanceClamNetwork, "runCommand", "(s)", command.c_str() ); + } + + PyObject * pModule2 = PyImport_ImportModule("StringIO"); + if (pModule2 == NULL) + { + getExceptionMessage(); + return 0; + } + PyObject * pDict2 = PyModule_GetDict(pModule2); + PyObject * pClassStringIO = PyDict_GetItemString(pDict2, "StringIO"); + PyObject * pStringIO = Py_BuildValue( "(s)", "" ); + PyObject * pInstanceStringIO = PyObject_CallObject(pClassStringIO, pStringIO); + if (!PyCallable_Check(pClassStringIO)) + { + getExceptionMessage(); + } + PyObject_CallMethod(pInstanceClamNetwork, "dump", "(O)", pInstanceStringIO); + pValue = PyObject_CallMethod(pInstanceStringIO, "getvalue", NULL); + + char * cstring; + if (pValue == NULL) + { + getExceptionMessage(); + } + + PyArg_Parse(pValue, "s", &cstring); + Py_DECREF(pModule); + Py_DECREF(pModule2); + Py_DECREF(pStringIO); + Py_DECREF(pInstanceClamNetwork); + Py_DECREF(pInstanceStringIO); + Py_DECREF(pValue); + Py_Finalize(); + + return cstring; + } void loadFromFile(const QString & filename) { _network.ResetConnectionReport(); @@ -342,7 +428,7 @@ _networkFile = filename; updateCaption(); } - void loadFromString(const char *strNetwork, const QString & filename) + void loadFromString(const char * strNetwork, const QString & filename) { _network.ResetConnectionReport(); Index: NetworkEditor/src/MainWindow.cxx =================================================================== --- NetworkEditor/src/MainWindow.cxx (revision 14445) +++ NetworkEditor/src/MainWindow.cxx (working copy) @@ -1,8 +1,6 @@ #include #include "ui_LadspaMetadataEditor.hxx" #include "LadspaPluginCompilationTask.hxx" -#include "Python.h" -#include MainWindow::~MainWindow() { @@ -102,78 +100,3 @@ _processingTree->RePopulateTree(); #endif } - - -//CLAM_EMBEDEDFILE(migrationScript, "migrationScript"); - -const char * MainWindow::updatedNetworkVersion(const std::string & filename) -{ - Py_Initialize(); - PyObject * pModule = PyImport_ImportModule("clamrefactor"); - if (pModule == NULL) - { - PyErr_Print(); - return 0; - } - PyObject * pDict = PyModule_GetDict(pModule); - // Build the name of a callable class - PyObject * pClassClamNetwork = PyDict_GetItemString(pDict, "ClamNetwork"); - // Create an instance of the class - if (! PyCallable_Check(pClassClamNetwork)) - { - //TODO Manage error - return 0; - } - PyObject * pClamNetwork = Py_BuildValue( "(s)", filename.c_str() ); - PyObject * pInstanceClamNetwork = PyObject_CallObject(pClassClamNetwork, pClamNetwork); - Py_DECREF(pClamNetwork); - - if (pInstanceClamNetwork == NULL) - { - PyErr_Print(); - return 0; - } - std::ifstream ifs( "migrationScript" ); - std::string command; - PyObject *pValue; - while( getline(ifs, command) ) - { - pValue = PyObject_CallMethod( pInstanceClamNetwork, "runCommand", "(s)", command.c_str() ); - } - - PyObject *pModule2, *pDict2, *pClassStringIO, *pStringIO, *pInstanceStringIO; - pModule2 = PyImport_ImportModule("StringIO"); - if (pModule2 == NULL) - { - PyErr_Print(); - return 0; - } - pDict2 = PyModule_GetDict(pModule2); - pClassStringIO = PyDict_GetItemString(pDict2, "StringIO"); - if (PyCallable_Check(pClassStringIO)) - { - pStringIO = Py_BuildValue( "(s)", "" ); - pInstanceStringIO = PyObject_CallObject(pClassStringIO, pStringIO); - } - - PyObject_CallMethod(pInstanceClamNetwork, "dump", "(O)", pInstanceStringIO); - pValue = PyObject_CallMethod(pInstanceStringIO, "getvalue", NULL); - - char *cstring; - if (pValue == NULL) - { - PyErr_Print(); - return 0; - } - - PyArg_Parse(pValue, "s", &cstring); - Py_DECREF(pModule); - Py_DECREF(pModule2); - Py_DECREF(pStringIO); - Py_DECREF(pInstanceClamNetwork); - Py_DECREF(pInstanceStringIO); - Py_DECREF(pValue); - Py_Finalize(); - - return cstring; -}