File: io.cpp

package info (click to toggle)
avogadrolibs 1.101.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,480 kB
  • sloc: cpp: 139,780; ansic: 2,212; python: 1,435; perl: 321; sh: 90; makefile: 46
file content (99 lines) | stat: -rw-r--r-- 3,692 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <avogadro/core/molecule.h>
#include <avogadro/io/fileformatmanager.h>

#include <avogadro/quantumio/gamessus.h>
#include <avogadro/quantumio/gaussiancube.h>
#include <avogadro/quantumio/gaussianfchk.h>
#include <avogadro/quantumio/molden.h>
#include <avogadro/quantumio/mopacaux.h>
#include <avogadro/quantumio/nwchemjson.h>
#include <avogadro/quantumio/nwchemlog.h>
#include <avogadro/quantumio/orca.h>

namespace py = pybind11;

using namespace Avogadro;
using namespace Avogadro::Core;
using namespace Avogadro::Io;
using namespace Avogadro::QuantumIO;

namespace {
// Add a proxy class for Python that exposes the file format manager singleton.
class ffm
{
public:
  ffm() : m_ffm(FileFormatManager::instance()) {}

  bool readFile(Core::Molecule& molecule, const std::string& fileName,
                const std::string& fileExtension = std::string(),
                const std::string& options = std::string()) const
  {
    return m_ffm.readFile(molecule, fileName, fileExtension, options);
  }

  bool writeFile(const Core::Molecule& molecule, const std::string& fileName,
                 const std::string& fileExtension = std::string(),
                 const std::string& options = std::string()) const
  {
    return m_ffm.writeFile(molecule, fileName, fileExtension, options);
  }

  bool readString(Core::Molecule& molecule, const std::string& string,
                  const std::string& fileExtension,
                  const std::string& options = std::string()) const
  {
    return m_ffm.readString(molecule, string, fileExtension, options);
  }

  std::string writeString(const Molecule& mol, const std::string& ext,
                          const std::string& options = std::string())
  {
    std::string fileStr;
    bool ok = m_ffm.writeString(mol, fileStr, ext, options);
    if (!ok)
      fileStr = "Error: " + FileFormatManager::instance().error();
    return fileStr;
  }

private:
  FileFormatManager& m_ffm;
};
} // namespace

PYBIND11_MODULE(io, m)
{
  m.doc() = "AvogadroIo Python binding";

  /// Add the quantum IO formats, we should probably move them over soon, but
  /// get things working for now...
  Io::FileFormatManager::registerFormat(new GAMESSUSOutput);
  Io::FileFormatManager::registerFormat(new GaussianFchk);
  Io::FileFormatManager::registerFormat(new GaussianCube);
  Io::FileFormatManager::registerFormat(new MoldenFile);
  Io::FileFormatManager::registerFormat(new MopacAux);
  Io::FileFormatManager::registerFormat(new NWChemJson);
  Io::FileFormatManager::registerFormat(new NWChemLog);
  Io::FileFormatManager::registerFormat(new ORCAOutput);

  /// This class uses a singleton pattern, make it accessible through Python.
  py::class_<ffm>(m, "FileFormatManager")
    .def(py::init<>())
    .def("read_file", &ffm::readFile,
         "Read in a molecule from the supplied file path", py::arg("molecule"),
         py::arg("file_name"), py::arg("file_extension") = std::string(),
         py::arg("options") = std::string())
    .def("write_file", &ffm::writeFile,
         "Write the molecule to the supplied file path", py::arg("molecule"),
         py::arg("file_name"), py::arg("file_extension") = std::string(),
         py::arg("options") = std::string())
    .def("read_string", &ffm::readString,
         "Read in a molecule from the supplied string", py::arg("molecule"),
         py::arg("string"), py::arg("file_extension"),
         py::arg("options") = std::string())
    .def("write_string", &ffm::writeString,
         "Write a molecule to the supplied string", py::arg("mol"),
         py::arg("file_extension"), py::arg("options") = std::string());
}