From 158358f7959e6e764404c4da8be8c82bef58909d Mon Sep 17 00:00:00 2001
From: Marco Biasini <marco.biasini@unibas.ch>
Date: Tue, 14 Aug 2012 13:54:50 +0200
Subject: [PATCH] added python export for RuleBasedProcessor

---
 modules/conop/pymod/CMakeLists.txt       |  4 ++
 modules/conop/pymod/export_diag.cc       | 44 +++++++++++++++
 modules/conop/pymod/export_heuristic.cc  |  0
 modules/conop/pymod/export_processor.cc  | 70 ++++++++++++++++++++++++
 modules/conop/pymod/export_rule_based.cc | 33 +++++++++--
 modules/conop/pymod/wrap_conop.cc        |  5 +-
 modules/conop/src/rule_based.hh          |  4 ++
 modules/conop/tests/CMakeLists.txt       |  1 +
 modules/conop/tests/test_processor.py    | 21 +++++++
 9 files changed, 176 insertions(+), 6 deletions(-)
 create mode 100644 modules/conop/pymod/export_diag.cc
 create mode 100644 modules/conop/pymod/export_heuristic.cc
 create mode 100644 modules/conop/pymod/export_processor.cc
 create mode 100644 modules/conop/tests/test_processor.py

diff --git a/modules/conop/pymod/CMakeLists.txt b/modules/conop/pymod/CMakeLists.txt
index 2ceff8be3..3af928719 100644
--- a/modules/conop/pymod/CMakeLists.txt
+++ b/modules/conop/pymod/CMakeLists.txt
@@ -2,8 +2,12 @@ set(OST_CONOP_PYMOD_SOURCES
   wrap_conop.cc
   export_builder.cc
   export_compound.cc
+  export_processor.cc
+  export_rule_based.cc
+  export_heuristic.cc
   export_amino_acids.cc
   export_conop.cc
+  export_diag.cc
   export_rule_based.cc
   export_non_standard.cc
   export_ring_finder.cc
diff --git a/modules/conop/pymod/export_diag.cc b/modules/conop/pymod/export_diag.cc
new file mode 100644
index 000000000..6155d63c7
--- /dev/null
+++ b/modules/conop/pymod/export_diag.cc
@@ -0,0 +1,44 @@
+//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2011 by the OpenStructure authors
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation; either version 3.0 of the License, or (at your option)
+// any later version.
+// This library is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//------------------------------------------------------------------------------
+//
+#include <boost/python.hpp>
+#include <ost/conop/diag.hh>
+using namespace ost;
+
+using namespace boost::python;
+using namespace ost::conop;
+
+
+void export_diag() {
+
+  enum_<DiagType>("DiagType")
+    .value("DIAG_UNK_ATOM", DIAG_UNK_ATOM)
+    .value("DIAG_UNK_RESIDUE", DIAG_UNK_RESIDUE)
+    .value("(DIAG_MISSING_ATOM", DIAG_MISSING_ATOM)
+    .value("DIAG_NONSTD_RESIDUE", DIAG_NONSTD_RESIDUE)
+    .export_values()
+  ;
+
+  class_<Diag>("Diag", init<DiagType,const char*>())
+    .def("__str__(self)__", &Diag::Format)
+  ;
+  class_<Diagnostics, DiagnosticsPtr>("Diagnostics")
+  ;
+}
+
diff --git a/modules/conop/pymod/export_heuristic.cc b/modules/conop/pymod/export_heuristic.cc
new file mode 100644
index 000000000..e69de29bb
diff --git a/modules/conop/pymod/export_processor.cc b/modules/conop/pymod/export_processor.cc
new file mode 100644
index 000000000..b69c0d1d8
--- /dev/null
+++ b/modules/conop/pymod/export_processor.cc
@@ -0,0 +1,70 @@
+//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2011 by the OpenStructure authors
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation; either version 3.0 of the License, or (at your option)
+// any later version.
+// This library is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//------------------------------------------------------------------------------
+//
+#include <boost/python.hpp>
+using namespace boost::python;
+#include <ost/conop/processor.hh>
+using namespace ost;
+
+using namespace ost::conop;
+
+struct PyProcessor : public Processor {};
+struct WrappedProcessor : public PyProcessor, public wrapper<WrappedProcessor> {
+  WrappedProcessor(PyObject* self): self_(self)
+  { }
+  virtual void DoProcess(DiagnosticsPtr diag, mol::EntityHandle ent) const
+  {
+    call_method<void>(self_, "DoProcess", diag, ent);
+  }
+  DiagnosticsPtr DoProcessDefault(DiagnosticsPtr diag, 
+                                  mol::EntityHandle ent) const {
+    return DiagnosticsPtr();
+  }
+  ProcessorPtr Copy() const { 
+    return call_method<ProcessorPtr>(self_, "Copy"); 
+  }
+  ProcessorPtr CopyDefault() const { return ProcessorPtr(); }
+  
+  PyObject* self_;
+};
+
+void export_processor() {
+  
+  class_<Processor, ProcessorPtr, boost::noncopyable>("_Processor", no_init)
+    .def("Copy", &Processor::Copy)
+    .add_property("strict_hydrogens", &Processor::GetStrictHydrogens,
+                 &Processor::SetStrictHydrogens)
+    .add_property("connect", &Processor::GetConnect,
+                 &Processor::SetConnect)
+    .add_property("assign_torsions", &Processor::GetAssignTorsions,
+                 &Processor::SetAssignTorsions)
+    .add_property("unk_res_treatment", &Processor::GetUnkResidueTreatment,
+                 &Processor::SetUnkResidueTreatment)
+    .add_property("unk_atom_treatment", &Processor::GetUnkAtomTreatment,
+                 &Processor::SetUnkAtomTreatment)
+    .def("Process", &Processor::Process)
+  ;
+  class_<PyProcessor, boost::noncopyable, 
+         boost::shared_ptr<WrappedProcessor>, 
+         bases<Processor> >("Processor")
+    .def("Copy", &WrappedProcessor::CopyDefault)
+    .def("DoProcess", &WrappedProcessor::DoProcessDefault)
+  ;
+}
+
diff --git a/modules/conop/pymod/export_rule_based.cc b/modules/conop/pymod/export_rule_based.cc
index 06b0e7b26..ab3aefa41 100644
--- a/modules/conop/pymod/export_rule_based.cc
+++ b/modules/conop/pymod/export_rule_based.cc
@@ -1,12 +1,35 @@
+//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2011 by the OpenStructure authors
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation; either version 3.0 of the License, or (at your option)
+// any later version.
+// This library is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+//------------------------------------------------------------------------------
+
 #include <boost/python.hpp>
+using namespace boost::python;
 #include <ost/conop/rule_based.hh>
 
-using namespace boost::python;
 using namespace ost::conop;
 
-void export_rule_based()
-{
-  class_<RuleBasedProcessor>("RuleBasedProcessor", init<CompoundLibPtr>())
-    .def("Process", &RuleBasedProcessor::Process)
+void export_rule_based() {
+  
+  class_<RuleBasedProcessor, RuleBasedProcessorPtr, 
+         boost::noncopyable, bases<Processor> >("RuleBasedProcessor", 
+         init<CompoundLibPtr>())
+    .add_property("fix_element", &RuleBasedProcessor::GetFixElement,
+                 &RuleBasedProcessor::SetFixElement)
   ;
 }
+
diff --git a/modules/conop/pymod/wrap_conop.cc b/modules/conop/pymod/wrap_conop.cc
index 99ceec316..fc19e341c 100644
--- a/modules/conop/pymod/wrap_conop.cc
+++ b/modules/conop/pymod/wrap_conop.cc
@@ -26,15 +26,18 @@ void export_Conop();
 void export_RingFinder();
 void export_AminoAcids();
 void export_NonStandard();
-
+void export_processor();
 void export_rule_based();
+void export_diag();
 BOOST_PYTHON_MODULE(_ost_conop)
 {
   export_Builder();
   export_Conop();
+  export_processor();
   export_rule_based();
   export_Compound();
   export_RingFinder();
   export_AminoAcids();
   export_NonStandard();
+  export_diag();
 }
diff --git a/modules/conop/src/rule_based.hh b/modules/conop/src/rule_based.hh
index 4ea5246a6..f014fe9d5 100644
--- a/modules/conop/src/rule_based.hh
+++ b/modules/conop/src/rule_based.hh
@@ -30,6 +30,10 @@ namespace ost { namespace conop {
 mol::AtomHandleList DLLEXPORT_OST_CONOP GetUnknownAtoms(mol::ResidueHandle res, 
                                                         CompoundPtr compound);
 
+class RuleBasedProcessor;
+
+typedef boost::shared_ptr<RuleBasedProcessor> RuleBasedProcessorPtr;
+
 class DLLEXPORT_OST_CONOP RuleBasedProcessor  : public Processor {
 public:
   RuleBasedProcessor(CompoundLibPtr compound_lib): 
diff --git a/modules/conop/tests/CMakeLists.txt b/modules/conop/tests/CMakeLists.txt
index a318766e1..ebc0be35c 100644
--- a/modules/conop/tests/CMakeLists.txt
+++ b/modules/conop/tests/CMakeLists.txt
@@ -6,6 +6,7 @@ set(OST_CONOP_UNIT_TESTS
   helper.cc
   test_compound.py
   test_cleanup.py
+  test_processor.py
   test_nonstandard.py
 )
 
diff --git a/modules/conop/tests/test_processor.py b/modules/conop/tests/test_processor.py
new file mode 100644
index 000000000..b8662c442
--- /dev/null
+++ b/modules/conop/tests/test_processor.py
@@ -0,0 +1,21 @@
+import unittest
+from ost import conop
+
+
+class TestProcessor(unittest.TestCase):
+  def testPyWrap(self):
+    class MyProc(conop.Processor):
+      def __init__(self):
+        conop.Processor.__init__(self)
+        self.count =0
+      def DoProcess(self, diag, ent):
+        self.count+=1
+    p = MyProc()
+    ent = mol.CreateEntity()
+    p.Process(ent)
+    self.assertEqual(p.count, 1)
+if __name__ == "__main__":
+  from ost import testutils
+  testutils.RunTests()
+
+
-- 
GitLab