|
1 | 1 | #include <pybind11/pybind11.h>
|
2 | 2 |
|
| 3 | +#include <odr/document.hpp> |
| 4 | +#include <odr/document_element.hpp> |
| 5 | +#include <odr/file.hpp> |
| 6 | +#include <odr/filesystem.hpp> |
| 7 | +#include <odr/html.hpp> |
| 8 | +#include <odr/open_document_reader.hpp> |
| 9 | + |
3 | 10 | namespace py = pybind11;
|
4 | 11 |
|
5 | 12 | PYBIND11_MODULE(core, m) {
|
6 |
| - m.attr("__version__") = "dev"; |
| 13 | + { |
| 14 | + m.attr("__version__") = odr::OpenDocumentReader::version(); |
| 15 | + m.attr("version") = odr::OpenDocumentReader::version(); |
| 16 | + m.attr("commit") = odr::OpenDocumentReader::commit(); |
| 17 | + } |
| 18 | + |
| 19 | + { |
| 20 | + py::enum_<odr::FileType>(m, "FileType") |
| 21 | + .value("unknown", odr::FileType::unknown) |
| 22 | + .value("opendocument_text", odr::FileType::opendocument_text) |
| 23 | + .value("opendocument_presentation", |
| 24 | + odr::FileType::opendocument_presentation) |
| 25 | + .value("opendocument_spreadsheet", |
| 26 | + odr::FileType::opendocument_spreadsheet) |
| 27 | + .value("opendocument_graphics", odr::FileType::opendocument_graphics) |
| 28 | + .value("office_open_xml_document", |
| 29 | + odr::FileType::office_open_xml_document) |
| 30 | + .value("office_open_xml_presentation", |
| 31 | + odr::FileType::office_open_xml_presentation) |
| 32 | + .value("office_open_xml_workbook", |
| 33 | + odr::FileType::office_open_xml_workbook) |
| 34 | + .value("office_open_xml_encrypted", |
| 35 | + odr::FileType::office_open_xml_encrypted) |
| 36 | + .value("legacy_word_document", odr::FileType::legacy_word_document) |
| 37 | + .value("legacy_powerpoint_presentation", |
| 38 | + odr::FileType::legacy_powerpoint_presentation) |
| 39 | + .value("legacy_excel_worksheets", |
| 40 | + odr::FileType::legacy_excel_worksheets) |
| 41 | + .value("word_perfect", odr::FileType::word_perfect) |
| 42 | + .value("rich_text_format", odr::FileType::rich_text_format) |
| 43 | + .value("portable_document_format", |
| 44 | + odr::FileType::portable_document_format) |
| 45 | + .value("text_file", odr::FileType::text_file) |
| 46 | + .value("comma_separated_values", odr::FileType::comma_separated_values) |
| 47 | + .value("javascript_object_notation", |
| 48 | + odr::FileType::javascript_object_notation) |
| 49 | + .value("markdown", odr::FileType::markdown) |
| 50 | + .value("zip", odr::FileType::zip) |
| 51 | + .value("compound_file_binary_format", |
| 52 | + odr::FileType::compound_file_binary_format) |
| 53 | + .value("portable_network_graphics", |
| 54 | + odr::FileType::portable_network_graphics) |
| 55 | + .value("graphics_interchange_format", |
| 56 | + odr::FileType::graphics_interchange_format) |
| 57 | + .value("jpeg", odr::FileType::jpeg) |
| 58 | + .value("bitmap_image_file", odr::FileType::bitmap_image_file) |
| 59 | + .value("starview_metafile", odr::FileType::starview_metafile); |
| 60 | + |
| 61 | + py::enum_<odr::FileCategory>(m, "FileCategory") |
| 62 | + .value("unknown", odr::FileCategory::unknown) |
| 63 | + .value("text", odr::FileCategory::text) |
| 64 | + .value("image", odr::FileCategory::image) |
| 65 | + .value("archive", odr::FileCategory::archive) |
| 66 | + .value("document", odr::FileCategory::document); |
| 67 | + |
| 68 | + py::enum_<odr::FileLocation>(m, "FileLocation") |
| 69 | + .value("memory", odr::FileLocation::memory) |
| 70 | + .value("disk", odr::FileLocation::disk); |
| 71 | + |
| 72 | + py::enum_<odr::EncryptionState>(m, "EncryptionState") |
| 73 | + .value("unknown", odr::EncryptionState::unknown) |
| 74 | + .value("not_encrypted", odr::EncryptionState::not_encrypted) |
| 75 | + .value("encrypted", odr::EncryptionState::encrypted) |
| 76 | + .value("decrypted", odr::EncryptionState::decrypted); |
| 77 | + |
| 78 | + py::class_<odr::FileMeta>(m, "FileMeta") |
| 79 | + .def_readwrite("type", &odr::FileMeta::type) |
| 80 | + .def_readwrite("password_encrypted", &odr::FileMeta::password_encrypted) |
| 81 | + .def_readwrite("document_meta", &odr::FileMeta::document_meta); |
| 82 | + |
| 83 | + py::class_<odr::DocumentMeta>(m, "DocumentMeta") |
| 84 | + .def_readwrite("document_type", &odr::DocumentMeta::document_type) |
| 85 | + .def_readwrite("entry_count", &odr::DocumentMeta::entry_count); |
| 86 | + |
| 87 | + py::class_<odr::File>(m, "File") |
| 88 | + .def(py::init<const std::string &>()) |
| 89 | + .def("is_valid", &odr::File::operator bool) |
| 90 | + .def("location", &odr::File::location) |
| 91 | + .def("size", &odr::File::size); |
| 92 | + |
| 93 | + py::class_<odr::DecodedFile>(m, "DecodedFile") |
| 94 | + .def(py::init<const odr::File &>()) |
| 95 | + .def(py::init<const std::string &>()) |
| 96 | + .def("is_valid", &odr::DecodedFile::operator bool) |
| 97 | + .def("file_type", &odr::DecodedFile::file_type) |
| 98 | + .def("file_category", &odr::DecodedFile::file_category) |
| 99 | + .def("file_meta", &odr::DecodedFile::file_meta) |
| 100 | + .def("file", &odr::DecodedFile::file) |
| 101 | + .def("is_text_file", &odr::DecodedFile::is_text_file) |
| 102 | + .def("is_image_file", &odr::DecodedFile::is_image_file) |
| 103 | + .def("is_archive_file", &odr::DecodedFile::is_archive_file) |
| 104 | + .def("is_document_file", &odr::DecodedFile::is_document_file) |
| 105 | + .def("is_pdf_file", &odr::DecodedFile::is_pdf_file) |
| 106 | + .def("text_file", &odr::DecodedFile::text_file) |
| 107 | + .def("image_file", &odr::DecodedFile::image_file) |
| 108 | + .def("archive_file", &odr::DecodedFile::archive_file) |
| 109 | + .def("document_file", &odr::DecodedFile::document_file) |
| 110 | + .def("pdf_file", &odr::DecodedFile::pdf_file); |
| 111 | + |
| 112 | + py::class_<odr::TextFile>(m, "TextFile") |
| 113 | + .def("is_valid", &odr::TextFile::operator bool) |
| 114 | + .def("charset", &odr::TextFile::charset) |
| 115 | + .def("text", &odr::TextFile::text); |
| 116 | + |
| 117 | + py::class_<odr::ImageFile>(m, "ImageFile") |
| 118 | + .def("is_valid", &odr::ImageFile::operator bool); |
| 119 | + |
| 120 | + py::class_<odr::ArchiveFile>(m, "ArchiveFile") |
| 121 | + .def("is_valid", &odr::ArchiveFile::operator bool); |
| 122 | + |
| 123 | + py::class_<odr::DocumentFile>(m, "DocumentFile") |
| 124 | + .def("is_valid", &odr::DocumentFile::operator bool) |
| 125 | + .def("password_encrypted", &odr::DocumentFile::password_encrypted) |
| 126 | + .def("encryption_state", &odr::DocumentFile::encryption_state) |
| 127 | + .def("decrypt", &odr::DocumentFile::decrypt) |
| 128 | + .def("document_type", &odr::DocumentFile::document_type) |
| 129 | + .def("document_meta", &odr::DocumentFile::document_meta) |
| 130 | + .def("document", &odr::DocumentFile::document); |
| 131 | + |
| 132 | + py::class_<odr::PdfFile>(m, "PdfFile") |
| 133 | + .def("is_valid", &odr::PdfFile::operator bool); |
| 134 | + } |
| 135 | + |
| 136 | + { |
| 137 | + py::class_<odr::Filesystem>(m, "Filesystem") |
| 138 | + .def("is_valid", &odr::Filesystem::operator bool) |
| 139 | + .def("exists", &odr::Filesystem::exists) |
| 140 | + .def("is_file", &odr::Filesystem::is_file) |
| 141 | + .def("is_directory", &odr::Filesystem::is_directory) |
| 142 | + .def("file_walker", &odr::Filesystem::file_walker) |
| 143 | + .def("open", &odr::Filesystem::open); |
| 144 | + |
| 145 | + py::class_<odr::FileWalker>(m, "FileWalker") |
| 146 | + .def("is_valid", &odr::FileWalker::operator bool) |
| 147 | + .def("next", &odr::FileWalker::next); |
| 148 | + } |
| 149 | + |
| 150 | + { |
| 151 | + py::enum_<odr::DocumentType>(m, "DocumentType") |
| 152 | + .value("unknown", odr::DocumentType::unknown) |
| 153 | + .value("text", odr::DocumentType::text) |
| 154 | + .value("presentation", odr::DocumentType::presentation) |
| 155 | + .value("spreadsheet", odr::DocumentType::spreadsheet) |
| 156 | + .value("drawing", odr::DocumentType::drawing); |
| 157 | + |
| 158 | + py::class_<odr::Document>(m, "Document") |
| 159 | + .def("editable", &odr::Document::editable) |
| 160 | + .def("savable", &odr::Document::savable, py::arg("encrypted") = false) |
| 161 | + .def("save", py::overload_cast<const std::string &>( |
| 162 | + &odr::Document::save, py::const_)) |
| 163 | + .def("file_type", &odr::Document::file_type) |
| 164 | + .def("document_type", &odr::Document::document_type) |
| 165 | + .def("root_element", &odr::Document::root_element) |
| 166 | + .def("files", &odr::Document::files); |
| 167 | + } |
| 168 | + |
| 169 | + { |
| 170 | + py::enum_<odr::ElementType>(m, "ElementType") |
| 171 | + .value("none", odr::ElementType::none) |
| 172 | + .value("root", odr::ElementType::root) |
| 173 | + .value("slide", odr::ElementType::slide) |
| 174 | + .value("sheet", odr::ElementType::sheet) |
| 175 | + .value("page", odr::ElementType::page) |
| 176 | + .value("master_page", odr::ElementType::master_page) |
| 177 | + .value("text", odr::ElementType::text) |
| 178 | + .value("line_break", odr::ElementType::line_break) |
| 179 | + .value("page_break", odr::ElementType::page_break) |
| 180 | + .value("paragraph", odr::ElementType::paragraph) |
| 181 | + .value("span", odr::ElementType::span) |
| 182 | + .value("link", odr::ElementType::link) |
| 183 | + .value("bookmark", odr::ElementType::bookmark) |
| 184 | + .value("list", odr::ElementType::list) |
| 185 | + .value("list_item", odr::ElementType::list_item) |
| 186 | + .value("table", odr::ElementType::table) |
| 187 | + .value("table_column", odr::ElementType::table_column) |
| 188 | + .value("table_row", odr::ElementType::table_row) |
| 189 | + .value("table_cell", odr::ElementType::table_cell) |
| 190 | + .value("frame", odr::ElementType::frame) |
| 191 | + .value("image", odr::ElementType::image) |
| 192 | + .value("rect", odr::ElementType::rect) |
| 193 | + .value("line", odr::ElementType::line) |
| 194 | + .value("circle", odr::ElementType::circle) |
| 195 | + .value("custom_shape", odr::ElementType::custom_shape) |
| 196 | + .value("group", odr::ElementType::group); |
| 197 | + |
| 198 | + py::enum_<odr::AnchorType>(m, "AnchorType") |
| 199 | + .value("as_char", odr::AnchorType::as_char) |
| 200 | + .value("at_char", odr::AnchorType::at_char) |
| 201 | + .value("at_frame", odr::AnchorType::at_frame) |
| 202 | + .value("at_page", odr::AnchorType::at_page) |
| 203 | + .value("at_paragraph", odr::AnchorType::at_paragraph); |
| 204 | + |
| 205 | + py::enum_<odr::ValueType>(m, "ValueType") |
| 206 | + .value("unknown", odr::ValueType::unknown) |
| 207 | + .value("string", odr::ValueType::string) |
| 208 | + .value("float_number", odr::ValueType::float_number); |
| 209 | + |
| 210 | + py::class_<odr::Element>(m, "Element") |
| 211 | + .def("is_valid", &odr::Element::operator bool) |
| 212 | + .def("type", &odr::Element::type) |
| 213 | + .def("parent", &odr::Element::parent) |
| 214 | + .def("first_child", &odr::Element::first_child) |
| 215 | + .def("previous_sibling", &odr::Element::previous_sibling) |
| 216 | + .def("next_sibling", &odr::Element::next_sibling) |
| 217 | + .def("is_editable", &odr::Element::is_editable); |
| 218 | + } |
| 219 | + |
| 220 | + { |
| 221 | + py::enum_<odr::HtmlTableGridlines>(m, "HtmlTableGridlines") |
| 222 | + .value("none", odr::HtmlTableGridlines::none) |
| 223 | + .value("soft", odr::HtmlTableGridlines::soft) |
| 224 | + .value("hard", odr::HtmlTableGridlines::hard); |
| 225 | + |
| 226 | + py::class_<odr::HtmlConfig>(m, "HtmlConfig") |
| 227 | + .def(py::init<>()) |
| 228 | + .def_readwrite("compact_presentation", |
| 229 | + &odr::HtmlConfig::compact_presentation) |
| 230 | + .def_readwrite("compact_spreadsheet", |
| 231 | + &odr::HtmlConfig::compact_spreadsheet) |
| 232 | + .def_readwrite("compact_drawing", &odr::HtmlConfig::compact_drawing) |
| 233 | + .def_readwrite("text_document_output_file_name", |
| 234 | + &odr::HtmlConfig::text_document_output_file_name) |
| 235 | + .def_readwrite("presentation_output_file_name", |
| 236 | + &odr::HtmlConfig::presentation_output_file_name) |
| 237 | + .def_readwrite("spreadsheet_output_file_name", |
| 238 | + &odr::HtmlConfig::spreadsheet_output_file_name) |
| 239 | + .def_readwrite("drawing_output_file_name", |
| 240 | + &odr::HtmlConfig::drawing_output_file_name) |
| 241 | + .def_readwrite("slide_output_file_name", |
| 242 | + &odr::HtmlConfig::slide_output_file_name) |
| 243 | + .def_readwrite("sheet_output_file_name", |
| 244 | + &odr::HtmlConfig::sheet_output_file_name) |
| 245 | + .def_readwrite("page_output_file_name", |
| 246 | + &odr::HtmlConfig::page_output_file_name) |
| 247 | + .def_readwrite("embed_resources", &odr::HtmlConfig::embed_resources) |
| 248 | + .def_readwrite("external_resource_path", |
| 249 | + &odr::HtmlConfig::external_resource_path) |
| 250 | + .def_readwrite("relative_resource_paths", |
| 251 | + &odr::HtmlConfig::relative_resource_paths) |
| 252 | + .def_readwrite("editable", &odr::HtmlConfig::editable) |
| 253 | + .def_readwrite("text_document_margin", |
| 254 | + &odr::HtmlConfig::text_document_margin) |
| 255 | + .def_readwrite("spreadsheet_limit", &odr::HtmlConfig::spreadsheet_limit) |
| 256 | + .def_readwrite("spreadsheet_limit_by_content", |
| 257 | + &odr::HtmlConfig::spreadsheet_limit_by_content) |
| 258 | + .def_readwrite("spreadsheet_gridlines", |
| 259 | + &odr::HtmlConfig::spreadsheet_gridlines) |
| 260 | + .def_readwrite("format_html", &odr::HtmlConfig::format_html) |
| 261 | + .def_readwrite("html_indent", &odr::HtmlConfig::html_indent); |
| 262 | + |
| 263 | + py::class_<odr::Html>(m, "Html") |
| 264 | + .def("file_type", &odr::Html::file_type) |
| 265 | + .def("pages", &odr::Html::pages) |
| 266 | + .def("edit", &odr::Html::edit) |
| 267 | + .def("save", &odr::Html::save); |
| 268 | + |
| 269 | + py::class_<odr::HtmlPage>(m, "HtmlPage") |
| 270 | + .def_readwrite("name", &odr::HtmlPage::name) |
| 271 | + .def_readwrite("path", &odr::HtmlPage::path); |
| 272 | + } |
| 273 | + |
| 274 | + { |
| 275 | + m.def("open", &odr::OpenDocumentReader::open); |
| 276 | + |
| 277 | + m.def("copy_resources", &odr::OpenDocumentReader::copy_resources); |
| 278 | + } |
| 279 | + |
| 280 | + { |
| 281 | + auto m_html = m.def_submodule("html"); |
| 282 | + |
| 283 | + m_html.def( |
| 284 | + "translate", |
| 285 | + py::overload_cast<const odr::DecodedFile &, const std::string &, |
| 286 | + const odr::HtmlConfig &>(&odr::html::translate)); |
| 287 | + } |
7 | 288 | }
|
0 commit comments