// Copyright 2017 Global Phasing Ltd. #include "gemmi/cifdoc.hpp" #include "gemmi/tostr.hpp" #include "gemmi/to_cif.hpp" #include "gemmi/to_json.hpp" #include "gemmi/fstream.hpp" #include "common.h" #include namespace py = pybind11; using namespace gemmi::cif; std::string pyobject_to_string(py::handle handle, bool raw) { PyObject* ptr = handle.ptr(); if (ptr == Py_None) { return "?"; } else if (ptr == Py_False) { return "."; } else if (ptr == Py_True) { throw py::value_error("unexpected value True"); } else if (raw || PyFloat_Check(ptr) || PyLong_Check(ptr)) { return py::str(handle); } else { return gemmi::cif::quote(py::str(handle)); } } std::vector quote_list(py::list items) { size_t size = items.size(); std::vector ret; ret.reserve(size); for (auto handle : items) ret.push_back(pyobject_to_string(handle, false)); return ret; } template T& add_to_vector(std::vector& vec, const T& new_item, int pos) { if (pos < 0) pos = (int) vec.size(); else if (pos > (int) vec.size()) throw py::index_error(); vec.insert(vec.begin() + pos, new_item); return vec[pos]; } // for delitem_slice namespace gemmi { namespace cif { void delitem_at_index(Table& t, py::ssize_t idx) { t.remove_row((int)idx); } void delitem_range(Table& t, py::ssize_t start, py::ssize_t end) { t.remove_rows((int)start, (int)end); } } } // namespace gemmi::cif void add_cif(py::module& cif) { py::class_ cif_block(cif, "Block"); py::class_ cif_item(cif, "Item"); py::class_ cif_loop(cif, "Loop"); py::class_ cif_column(cif, "Column"); py::class_ cif_table(cif, "Table"); py::class_ cif_table_row(cif_table, "Row"); py::enum_