increase the use of python in mussa
authorDiane Trout <diane@caltech.edu>
Wed, 10 May 2006 01:39:33 +0000 (01:39 +0000)
committerDiane Trout <diane@caltech.edu>
Wed, 10 May 2006 01:39:33 +0000 (01:39 +0000)
This patch does let the python extension launch the gui successfully.
I tried to get the C++ gui app to also be able to talk to the python extension
but that didn't work so well. (causes a crash on os x when I statically
link the python extension module in).

alg/parse_options.cpp
alg/parse_options.hpp
py/CMakeLists.txt
py/MussaWindow.cpp [new file with mode: 0644]
py/module.cpp
qui/CMakeLists.txt
qui/mussagl.cpp
qui/seqbrowser/SequenceBrowser.hpp

index ce253246fb3d162b9c7d80d27c7d85ebee3fa8a9..8a777d4b740c3d90ac25a43bd5ce4ebb892042f4 100644 (file)
@@ -9,7 +9,8 @@ namespace po = boost::program_options;
 
 MussaOptions::MussaOptions()
   : analysis(0),
-    useGUI(true)
+    useGUI(true),
+    runAsPythonInterpeter(false)
 {
 }
 
@@ -26,6 +27,7 @@ void initialize_mussa(MussaOptions& opts, int argc, char **argv)
     ("view-analysis", po::value<std::string>(),
      "load a previously run analysis")
     ("no-gui", "terminate without running an analysis")
+    ("python", "launch as a python interpreter")
   ;
 
   po::variables_map vm;
@@ -52,4 +54,7 @@ void initialize_mussa(MussaOptions& opts, int argc, char **argv)
   if (vm.count("no-gui")) {
     opts.useGUI=false;
   }
+  if (vm.count("python")) {
+    opts.runAsPythonInterpeter = true;
+  }
 }
index f048b8e955a9b5708ea2b28c41e25b36b1545eb6..bd08ef526cf43be1b0b47acf303e1a6225318015 100644 (file)
@@ -13,6 +13,8 @@ struct MussaOptions
   Mussa *analysis;
   //! should we use the gui?
   bool useGUI;
+  //! launch as python
+  bool runAsPythonInterpeter;
 };
 
 //! initialize a mussa analysis from command line arguments
index 2c45fa8ad9beff2c322be9a23a5d04774658ec0c..44465f720c6b6f4aba2a2cd4138d3a4abfedcedf 100644 (file)
@@ -2,14 +2,19 @@ FIND_PACKAGE(Boost)
 FIND_PACKAGE(OpenGL)
 FIND_PACKAGE(PythonLibs)
 FIND_PACKAGE(PythonInterp)
+SET(QT_USE_QTOPENGL 1)
+FIND_PACKAGE(Qt4)
+INCLUDE( ${QT_USE_FILE} )
+
 
 # IF(BOOST_PYTHON_LIB)
-  INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
+  INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH} ${QT_INCLUDES})
   SET(SOURCES 
         conserved_path.cpp 
         glsequence.cpp
         module.cpp 
         mussa.cpp
+        MussaWindow.cpp
         nway_paths.cpp
         sequence.cpp
         )
@@ -23,10 +28,14 @@ FIND_PACKAGE(PythonInterp)
   LINK_DIRECTORIES(${MUSSA_BINARY_DIR}/alg)
   TARGET_LINK_LIBRARIES(mussa 
                           mussa_core
+                          mussa_qui
                           ${BOOST_PYTHON_LIBRARY}
                           ${BOOST_FILESYSTEM_LIBRARY}
                           ${OPENGL_gl_LIBRARY}
                           ${PYTHON_LIBRARIES}
+                          ${QT_QTCORE_LIBRARY}
+                          ${QT_QTGUI_LIBRARY}
+                          ${QT_QTOPENGL_LIBRARY}
                         )
 
   IF(PYTHON_EXECUTABLE)
diff --git a/py/MussaWindow.cpp b/py/MussaWindow.cpp
new file mode 100644 (file)
index 0000000..9fe6b77
--- /dev/null
@@ -0,0 +1,49 @@
+
+#include <boost/python.hpp>
+
+#include <QApplication>
+#include <QPushButton>
+#include "alg/mussa.hpp"
+#include "qui/MussaWindow.hpp"
+
+struct gui {
+  QApplication *app;
+  QPushButton *b;
+  MussaWindow *mw;
+
+  gui() : b(0), mw(0) { 
+    char *argv[] = {"mussagl"};
+    int argc = 1;
+    app = new QApplication(argc, (char **)argv);
+    Q_INIT_RESOURCE(icons);
+  }
+
+  void button() {
+    b = new QPushButton("hi");
+    b->show();
+  }
+
+  void mussa() {
+    Mussa *analysis = new Mussa();
+    mw = new MussaWindow(analysis);
+    mw->show();
+  }
+
+  int run() {
+    return app->exec();
+  }
+};
+
+void export_mussa_window()
+{
+  /*
+  class_<MussaWindow>("MussaWindow", init<Mussa *, QObject *>)
+    .def("show", &MussaWindow::show)
+  ;
+  */
+  boost::python::class_<gui>("gui")
+    .def("button", &gui::button)
+    .def("mussa", &gui::mussa)
+    .def("run", &gui::run)
+  ;
+}
index 1f6fd0280150cce15b0dbc2e7a31c7cf2860dd4c..448b523b89db0f516480495c20c2d9e97c37fa42 100644 (file)
@@ -6,6 +6,7 @@ void export_glsequence();
 void export_mussa();
 void export_nway_paths();
 void export_sequence();
+void export_mussa_window();
 
 BOOST_PYTHON_MODULE(mussa)
 {
@@ -14,4 +15,5 @@ BOOST_PYTHON_MODULE(mussa)
   export_mussa();
   export_nway_paths();
   export_sequence();
+  export_mussa_window();
 }
index 6cb7d1cd47220666754b1f67f9f5901268abdde4..e134342d9fb73e5ae0cb10741b82be3dc991c965 100644 (file)
@@ -7,6 +7,7 @@ SET(QT_USE_QTOPENGL 1)
 INCLUDE( ${QT_USE_FILE} )
 FIND_PACKAGE(OpenGL)
 FIND_PACKAGE(Boost)
+FIND_PACKAGE(PythonLibs)
 
 SET(MOC_HEADERS
       ImageSaveDialog.hpp     
@@ -30,12 +31,11 @@ SET(MOC_HEADERS
       seqbrowser/SequenceBrowserWidget.hpp
       seqbrowser/SequenceDescription.hpp
     )
-SET(SOURCES 
+SET(GUI_SOURCES 
       ImageSaveDialog.cpp      
       ImageScaler.cpp          
       IntAction.cpp            
       MussaAlignedWindow.cpp
-      mussagl.cpp
       MussaWindow.cpp
       ThresholdWidget.cpp
       ZoomWidget.cpp
@@ -52,20 +52,34 @@ SET(SOURCES
       seqbrowser/SequenceBrowserSidebar.cpp
       seqbrowser/SequenceBrowserWidget.cpp
       seqbrowser/SequenceDescription.cpp
+
      )
+SET(SOURCES
+      mussagl.cpp)
 SET(RCCS ../icons.qrc)
 
 QT4_ADD_RESOURCES(RCC_SOURCE ${RCCS})
 QT4_WRAP_CPP(MOC_SOURCES ${MOC_HEADERS})
-ADD_EXECUTABLE(mussagl WIN32 MACOSX_BUNDLE ${SOURCES} ${MOC_SOURCES} ${RCC_SOURCE})
-LINK_DIRECTORIES(${MUSSA_BINARY_DIR}/alg)
+INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH} 
+                    ${QT_INCLUDES})
+ADD_LIBRARY(mussa_qui STATIC ${MOC_SOURCES} ${GUI_SOURCES} ${RCC_SOURCE})
+TARGET_LINK_LIBRARIES(mussa_qui 
+                        mussa_core)
+ADD_EXECUTABLE(mussagl WIN32 MACOSX_BUNDLE ${SOURCES} )
+# ${SOURCES} ${MOC_SOURCES} ${RCC_SOURCE} )
+LINK_DIRECTORIES(${MUSSA_BINARY_DIR}/alg
+                 ${MUSSA_BINARY_DIR}/py)
 TARGET_LINK_LIBRARIES(mussagl 
                         mussa_core
+                        mussa_qui
                         ${QT_QTCORE_LIBRARY}
                         ${QT_QTGUI_LIBRARY}
                         ${QT_QTOPENGL_LIBRARY}
                         ${OPENGL_gl_LIBRARY}
                         ${BOOST_PROGRAM_OPTIONS_LIBRARY}
                         ${BOOST_FILESYSTEM_LIBRARY}
+                        ${BOOST_PYTHON_LIBRARY}
+                        optimized ${PYTHON_LIBRARIES}
+                        debug ${PYTHON_DEBUG_LIBRARIES}
                         )
 
index 919c3293b8d8fcb7d62b933d66cb394ce42b0c84..1b6dd95464f83a4ed851d23f95705e0dd79a904b 100644 (file)
@@ -1,7 +1,10 @@
 #include <boost/filesystem/operations.hpp>
+#include <boost/python.hpp>
+
 using namespace boost::filesystem;
 
 #include <stdlib.h>
+#include <iostream>
 #include <QApplication>
 
 #include "qui/MussaWindow.hpp"
@@ -19,6 +22,8 @@ int main(int argc, char **argv)
 
   QApplication app(argc, argv, opts.useGUI);
   Q_INIT_RESOURCE(icons);
+  //PyImport_AppendInittab("mussa", initmussa);
+  Py_Initialize();
 
   initialize_mussa(opts, argc, argv);
 
@@ -27,7 +32,27 @@ int main(int argc, char **argv)
   }
 
   try {
-    if (opts.useGUI) { 
+    if (opts.runAsPythonInterpeter) {
+      boost::python::object main_module((boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__")))));
+      boost::python::object main_namespace = main_module.attr("__dict__");
+
+      boost::python::handle<> ignored((PyRun_String(
+                        //"from IPython.Shell import IPShellEmbed\n"
+                        //"shell = IPShellEmbed(['mussa'], '')\n",
+                        //"import mussa\n"
+                        "def square(x):\n"
+                        "  r = x * x\n"
+                        "  print 'the square is %d' % (r)\n"
+                        "  return r\n",
+                        Py_file_input, 
+                        main_namespace.ptr(),
+                        main_namespace.ptr())
+                      ));
+      boost::python::object square = main_module.attr("square");
+      int result = boost::python::extract<int>(square(3));
+      std::cout << "C++ has " << result << std::endl;
+      PyRun_InteractiveLoop(stdin, "mussa");
+    } else if (opts.useGUI) { 
       MussaWindow win(opts.analysis);
       win.show();
       app.exec();
@@ -37,10 +62,13 @@ int main(int argc, char **argv)
     qFatal(e.what());
   } catch (boost::filesystem::filesystem_error e) {
     qFatal(e.what());
-  } catch (std::runtime_error e) {
+  } catch (boost::python::error_already_set e) {
+    PyErr_Print();
+  }  catch (std::runtime_error e) {
     qFatal(e.what());
   } catch (...) {
     qFatal("unrecognized exception");
   }
-  return 1;
+  Py_Finalize();
+  return 1; 
 }
index 219d9d4c84e62ec40c9c773f2b0bb7c441c9012c..33d6f70ebba3d22b0f1d5dc61f65a8f2f086ea0c 100644 (file)
@@ -25,7 +25,6 @@ public:
   SequenceBrowser(QWidget *parent=0);
 
   QSize sizeHint() const;
-
   void clear();
   void push_sequence(const Sequence &s);
   void push_sequence(GlSequence &);