[cig-commits] r13093 - in cs/cigma/trunk: . src tests tests/libcigma tests/pytests

luis at geodynamics.org luis at geodynamics.org
Wed Oct 15 13:26:49 PDT 2008


Author: luis
Date: 2008-10-15 13:26:49 -0700 (Wed, 15 Oct 2008)
New Revision: 13093

Added:
   cs/cigma/trunk/src/py_FileWriter.h
   cs/cigma/trunk/tests/libcigma/WriterTest.cpp
   cs/cigma/trunk/tests/libcigma/WriterTest.h
   cs/cigma/trunk/tests/pytests/test_writer.py
Modified:
   cs/cigma/trunk/Makefile.am
   cs/cigma/trunk/src/io_file_writer.cpp
   cs/cigma/trunk/src/io_file_writer.h
   cs/cigma/trunk/src/io_text_writer.cpp
   cs/cigma/trunk/src/io_text_writer.h
   cs/cigma/trunk/src/py_FileWriter.cpp
   cs/cigma/trunk/src/py_cigma_module.cpp
   cs/cigma/trunk/src/py_cigma_setup.py
   cs/cigma/trunk/tests/test_registry.cpp
Log:
Python bindings for abstract class cigma::FileWriter

Modified: cs/cigma/trunk/Makefile.am
===================================================================
--- cs/cigma/trunk/Makefile.am	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/Makefile.am	2008-10-15 20:26:49 UTC (rev 13093)
@@ -136,6 +136,8 @@
 	src/io_file_writer.cpp \
 	src/io_null_writer.h \
 	src/io_null_writer.cpp \
+	src/io_text_writer.h \
+	src/io_text_writer.cpp \
 	src/core_base_op.h \
 	src/core_base_op.cpp \
 	src/core_list_op.h \
@@ -217,6 +219,7 @@
 _cigma_so_LDADD =
 _cigma_so_LDADD += libcigma.a
 
+# XXX: add header files to this variable
 _cigma_so_SOURCES = \
 	src/numpy_util.h \
 	src/numpy_util.cpp \
@@ -231,6 +234,7 @@
 	src/py_NodeCoordinates.cpp \
 	src/py_Function.cpp \
 	src/py_FileReader.cpp \
+	src/py_FileWriter.cpp \
 	src/py_Misc.cpp \
 	src/py_cigma_module.cpp
 
@@ -279,6 +283,8 @@
 	tests/libcigma/FunctionTest.cpp \
 	tests/libcigma/ReaderTest.h \
 	tests/libcigma/ReaderTest.cpp \
+	tests/libcigma/WriterTest.h \
+	tests/libcigma/WriterTest.cpp \
 	tests/libcigma/MiscTest.h \
 	tests/libcigma/MiscTest.cpp \
 	tests/test_registry.cpp \

Modified: cs/cigma/trunk/src/io_file_writer.cpp
===================================================================
--- cs/cigma/trunk/src/io_file_writer.cpp	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/src/io_file_writer.cpp	2008-10-15 20:26:49 UTC (rev 13093)
@@ -3,7 +3,7 @@
 
 #include "Filesystem.h"
 #include "io_file_writer.h"
-//#include "io_null_writer.h"
+#include "io_null_writer.h"
 //#include "io_text_writer.h"
 
 /*
@@ -22,11 +22,21 @@
 
 // ----------------------------------------------------------------------------
 
-FileWriter *New(std::string filename, std::string mode)
+FileWriter::FileWriter()
 {
+}
+
+FileWriter::~FileWriter()
+{
+}
+
+// ----------------------------------------------------------------------------
+
+FileWriter* FileWriter::New(std::string filename, std::string mode)
+{
     fs::path p(filename);
-    //return new NullWriter();
-    return 0;
+    return new NullWriter();
+    //return 0;
 }
 
 // ----------------------------------------------------------------------------

Modified: cs/cigma/trunk/src/io_file_writer.h
===================================================================
--- cs/cigma/trunk/src/io_file_writer.h	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/src/io_file_writer.h	2008-10-15 20:26:49 UTC (rev 13093)
@@ -17,9 +17,10 @@
 class cigma::FileWriter : private boost::noncopyable
 {
 public:
+    FileWriter();
     virtual ~FileWriter();
 
-    virtual int open(const char *filename) = 0;
+    virtual int open(const char *filename) = 0; // add "const char *mode" argument
     virtual int close() = 0;
 
     virtual int writeDataset(const char *loc, double *data, int nno, int ndim) = 0;

Modified: cs/cigma/trunk/src/io_text_writer.cpp
===================================================================
--- cs/cigma/trunk/src/io_text_writer.cpp	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/src/io_text_writer.cpp	2008-10-15 20:26:49 UTC (rev 13093)
@@ -1,6 +1,6 @@
 #include <cassert>
 #include <cstdlib>
-#include "TextWriter.h"
+#include "io_text_writer.h"
 
 using namespace cigma;
 

Modified: cs/cigma/trunk/src/io_text_writer.h
===================================================================
--- cs/cigma/trunk/src/io_text_writer.h	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/src/io_text_writer.h	2008-10-15 20:26:49 UTC (rev 13093)
@@ -2,7 +2,7 @@
 #define __TEXT_WRITER_H__
 
 #include <cstdio>
-#include "FileWriter.h"
+#include "io_file_writer.h"
 
 namespace cigma
 {

Modified: cs/cigma/trunk/src/py_FileWriter.cpp
===================================================================
--- cs/cigma/trunk/src/py_FileWriter.cpp	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/src/py_FileWriter.cpp	2008-10-15 20:26:49 UTC (rev 13093)
@@ -1,67 +1,124 @@
-#include "io_file_writer.h"
-#include "io_null_writer.h"
-#include "io_text_writer.h"
+#include "py_FileWriter.h"
+#include "numpy_util.h"
 
-#ifdef HAVE_HDF5
-#include "io_hdf5_writer.h"
-#endif
+using namespace cigma;
+using namespace boost::python;
 
-#ifdef HAVE_VTK
-#include "io_vtk_writer.h"
-#endif
+// ----------------------------------------------------------------------------
 
-#include <boost/python.hpp>
+FileWriter::WriterType pyFileWriter::getWriterType()
+{
+    return this->get_override("getWriterType")();
+}
 
-class pyNullWriter : public NullWriter
+int pyFileWriter::open(const char *filename)
 {
-};
+    return this->get_override("open")(filename);
+}
 
-class pyTextWriter : public TextWriter
+int pyFileWriter::close()
 {
-};
+    return this->get_override("close")();
+}
 
-/* Wrapper for HDF5 */
+int pyFileWriter::writeDataset(const char *loc, double *data, int nno, int ndim)
+{
+    return -1;
+}
+
+int pyFileWriter::writeCoordinates(const char *loc, double *coordinates, int nno, int nsd)
+{
+    return -1;
+}
+
+int pyFileWriter::writeConnectivity(const char *loc, int *connectivity, int nel, int ndofs)
+{
+    return -1;
+}
+
+
+// ----------------------------------------------------------------------------
+
+int pyNullWriter::open(const char *filename)
+{
+    // XXX: include filename in error string
+    PyErr_SetString(PyExc_IOError, "Cannot open <filename> with NullWriter");
+    throw_error_already_set();
+    return -1;
+}
+
+int pyNullWriter::close()
+{
+    PyErr_SetString(PyExc_IOError, "Cannot close a NullWriter instance");
+    throw_error_already_set();
+    return -1;
+}
+
+
+// ----------------------------------------------------------------------------
+
+int pyTextWriter::open(const char *filename)
+{
+    // anything to validate? -- throw exception based on return error code
+    return TextWriter::open(filename);
+}
+
+int pyTextWriter::close()
+{
+    // anything to validate?
+    return TextWriter::close();
+}
+
+// ----------------------------------------------------------------------------
+
 #ifdef HAVE_HDF5
-class pyHDF5Writer : public HDF5_Writer
-{
-};
 #endif
 
-/* Wrapper for VTK */
+// ----------------------------------------------------------------------------
+
 #ifdef HAVE_VTK
-class pyVTKWriter : public VtkWriter
-{
-};
 #endif
 
+// ----------------------------------------------------------------------------
 
-void export_FileWriter
+void export_FileWriter()
 {
+    using namespace cigma;
     using namespace boost::python;
 
-    class_<pyNullWriter, bases<NullWriter> >("NullWriter")
-        .def(init<std::string>())
+    enum_<FileWriter::WriterType>("FileWriterType")
+        .value("NULL_WRITER", FileWriter::NULL_FILE_WRITER)
+        .value("HDF5_WRITER", FileWriter::HDF5_FILE_WRITER)
+        .value("VTK_WRITER", FileWriter::VTK_FILE_WRITER)
+        .value("TEXT_WRITER", FileWriter::TEXT_FILE_WRITER)
+        //.export_values()
+        ;
+
+    class_<pyFileWriter, boost::noncopyable>("FileWriter")
+        .def("open", pure_virtual(&FileWriter::open))
+        .def("close", pure_virtual(&FileWriter::close))
+        .def("type", pure_virtual(&FileWriter::getWriterType))
+        ;
+
+    class_<pyNullWriter, boost::noncopyable>("NullWriter")
         .def("open", &pyNullWriter::open)
         .def("close", &pyNullWriter::close)
         ;
 
-    class_<pyTextWriter, bases<TextWriter> >("TextWriter")
-        .def(init<std::string>())
+    class_<pyTextWriter, boost::noncopyable>("TextWriter")
         .def("open", &pyTextWriter::open)
         .def("close", &pyTextWriter::close)
         ;
 
 #ifdef HAVE_HDF5
-    class_<pyHDF5Writer, bases<HDF5_Writer> >("HDF5Writer")
-        .def(init<std::string>())
+    class_<pyHDF5Writer, boost::noncopyable>("HDF5Writer")
         .def("open", &pyHDF5Writer::open)
         .def("close", &pyHDF5Writer::close)
         ;
 #endif
 
 #ifdef HAVE_VTK
-    class_<pyVTKWriter, bases<VtkWriter> >("VTKWriter")
-        .def(init<std::string>())
+    class_<pyVTKWriter, boost::noncopyable>("VTKWriter")
         .def("open", &pyVTKWriter::open)
         .def("close", &pyVTKWriter::close)
         ;

Added: cs/cigma/trunk/src/py_FileWriter.h
===================================================================
--- cs/cigma/trunk/src/py_FileWriter.h	                        (rev 0)
+++ cs/cigma/trunk/src/py_FileWriter.h	2008-10-15 20:26:49 UTC (rev 13093)
@@ -0,0 +1,60 @@
+#ifndef __PY_FILE_WRITER_H__
+#define __PY_FILE_WRITER_H__
+
+#include "io_file_writer.h"
+#include "io_null_writer.h"
+#include "io_text_writer.h"
+
+#ifdef HAVE_HDF5
+#include "io_hdf5_writer.h"
+#endif
+
+#ifdef HAVE_VTK
+#include "io_vtk_writer.h"
+#endif
+
+#include <boost/python.hpp>
+
+
+struct pyFileWriter : cigma::FileWriter, boost::python::wrapper<cigma::FileWriter>
+{
+    cigma::FileWriter::WriterType getWriterType();
+
+    int open(const char *filename);
+    int close();
+
+    int writeDataset(const char *loc, double *data, int nno, int ndim);
+    int writeCoordinates(const char *loc, double *coordinates, int nno, int nsd);
+    int writeConnectivity(const char *loc, int *connectivity, int nel, int ndofs);
+};
+
+struct pyNullWriter : cigma::NullWriter
+{
+    int open(const char *filename);
+    int close();
+};
+
+struct pyTextWriter : cigma::TextWriter
+{
+    int open(const char *filename);
+    int close();
+};
+
+#ifdef HAVE_HDF5
+class pyHDF5Writer : cigma::HDF5_Writer
+{
+    int open(const char *filename);
+    int close();
+};
+#endif
+
+#ifdef HAVE_VTK
+struct pyVTKWriter : cigma::VtkWriter
+{
+    int open(const char *filename);
+    int close();
+};
+#endif
+
+
+#endif /* __PY_FILE_WRITER_H__ */

Modified: cs/cigma/trunk/src/py_cigma_module.cpp
===================================================================
--- cs/cigma/trunk/src/py_cigma_module.cpp	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/src/py_cigma_module.cpp	2008-10-15 20:26:49 UTC (rev 13093)
@@ -16,6 +16,7 @@
 extern void export_NodeCoordinates();
 extern void export_Function();
 extern void export_FileReader();
+extern void export_FileWriter();
 extern void export_Misc();
 
 using namespace boost::python;
@@ -38,6 +39,7 @@
     export_NodeCoordinates();
     export_Function();
     export_FileReader();
+    export_FileWriter();
     export_Misc();
 }
 

Modified: cs/cigma/trunk/src/py_cigma_setup.py
===================================================================
--- cs/cigma/trunk/src/py_cigma_setup.py	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/src/py_cigma_setup.py	2008-10-15 20:26:49 UTC (rev 13093)
@@ -30,6 +30,7 @@
     'py_NodeCoordinates.cpp',
     'py_Function.cpp',
     'py_FileReader.cpp',
+    'py_FileWriter.cpp',
     'py_Misc.cpp',
 ]
 

Added: cs/cigma/trunk/tests/libcigma/WriterTest.cpp
===================================================================
--- cs/cigma/trunk/tests/libcigma/WriterTest.cpp	                        (rev 0)
+++ cs/cigma/trunk/tests/libcigma/WriterTest.cpp	2008-10-15 20:26:49 UTC (rev 13093)
@@ -0,0 +1,20 @@
+#include "WriterTest.h"
+using namespace libcigma;
+
+#include "io_file_writer.h"
+#include "io_null_writer.h"
+#include "io_text_writer.h"
+using namespace cigma;
+
+
+void WriterTest::test_null_writer()
+{
+    NullWriter writer;
+    CPPUNIT_ASSERT_EQUAL(writer.getWriterType(), FileWriter::NULL_FILE_WRITER);
+}
+
+void WriterTest::test_text_writer()
+{
+    TextWriter writer;
+    CPPUNIT_ASSERT_EQUAL(writer.getWriterType(), FileWriter::TEXT_FILE_WRITER);
+}

Added: cs/cigma/trunk/tests/libcigma/WriterTest.h
===================================================================
--- cs/cigma/trunk/tests/libcigma/WriterTest.h	                        (rev 0)
+++ cs/cigma/trunk/tests/libcigma/WriterTest.h	2008-10-15 20:26:49 UTC (rev 13093)
@@ -0,0 +1,26 @@
+#ifndef __WRITER_TEST_H__
+#define __WRITER_TEST_H__
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+
+namespace libcigma
+{
+    class WriterTest : public CPPUNIT_NS::TestFixture
+    {
+        CPPUNIT_TEST_SUITE(WriterTest);
+        CPPUNIT_TEST(test_null_writer);
+        CPPUNIT_TEST(test_text_writer);
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+        WriterTest() {}
+        ~WriterTest() {}
+
+        void test_null_writer();
+        void test_text_writer();
+    };
+};
+
+#endif

Added: cs/cigma/trunk/tests/pytests/test_writer.py
===================================================================
--- cs/cigma/trunk/tests/pytests/test_writer.py	                        (rev 0)
+++ cs/cigma/trunk/tests/pytests/test_writer.py	2008-10-15 20:26:49 UTC (rev 13093)
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+from _cigma import FileWriter, NullWriter, TextWriter
+
+class MyFileWriter(FileWriter):
+    def __init__(self):
+        FileWriter.__init__(self)
+    def open(self, filename):
+        return 0
+    def close(self):
+        return 0
+
+import unittest
+class WriterTest(unittest.TestCase):
+    def __init__(self, *args):
+        unittest.TestCase.__init__(self, *args)
+    def test_writer_custom(self):
+        writer = MyFileWriter()
+    def test_writer_null(self):
+        writer = NullWriter()
+    def test_writer_text(self):
+        writer = TextWriter()
+
+def create_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(WriterTest))
+    return suite
+

Modified: cs/cigma/trunk/tests/test_registry.cpp
===================================================================
--- cs/cigma/trunk/tests/test_registry.cpp	2008-10-15 20:26:47 UTC (rev 13092)
+++ cs/cigma/trunk/tests/test_registry.cpp	2008-10-15 20:26:49 UTC (rev 13093)
@@ -27,6 +27,9 @@
 #include <libcigma/ReaderTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION(libcigma::ReaderTest);
 
+#include <libcigma/WriterTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION(libcigma::WriterTest);
+
 #include <libcigma/MiscTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION(libcigma::MiscTest);
 



More information about the cig-commits mailing list