| 1 | #include <cstdio> |
| 2 | #include <cerrno> |
| 3 | #include <cstring> |
| 4 | #include <pybind11/pybind11.h> |
| 5 | #include <pybind11/stl.h> |
| 6 | |
| 7 | #include <boost/nowide/cstdio.hpp> |
| 8 | |
| 9 | #include "convert/convert.hpp" |
| 10 | |
| 11 | namespace py = pybind11; |
| 12 | |
| 13 | bgcode::binarize::BinarizerConfig get_config() |
| 14 | { |
| 15 | using namespace bgcode; |
| 16 | |
| 17 | binarize::BinarizerConfig config; |
| 18 | config.checksum = core::EChecksumType::CRC32; |
| 19 | config.compression.file_metadata = core::ECompressionType::None; |
| 20 | config.compression.print_metadata = core::ECompressionType::None; |
| 21 | config.compression.printer_metadata = core::ECompressionType::None; |
| 22 | config.compression.slicer_metadata = core::ECompressionType::Deflate; |
| 23 | config.compression.gcode = core::ECompressionType::Heatshrink_12_4; |
| 24 | config.gcode_encoding = core::EGCodeEncodingType::MeatPackComments; |
| 25 | config.metadata_encoding = core::EMetadataEncodingType::INI; |
| 26 | |
| 27 | return config; |
| 28 | } |
| 29 | |
| 30 | struct FILEWrapper { |
| 31 | FILE *fptr = nullptr; |
| 32 | |
| 33 | explicit FILEWrapper (FILE *f = nullptr) : fptr{f} {} |
| 34 | FILEWrapper(FILEWrapper &&) = delete; |
| 35 | FILEWrapper& operator=(FILEWrapper &&) = delete; |
| 36 | |
| 37 | void close() |
| 38 | { |
| 39 | if (fptr) { |
| 40 | std::fclose(fptr); |
| 41 | fptr = nullptr; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | ~FILEWrapper() { close(); } |
| 46 | }; |
| 47 | |
| 48 | PYBIND11_MODULE(MODULE_NAME, m) { |
| 49 | using namespace bgcode; |
| 50 | |
| 51 | static constexpr size_t MaxBuffSz = 2048; |
| 52 | |
| 53 | m.doc() = R"pbdoc( |
| 54 | Python binding of the libbgcode conversion functions |
| 55 | ----------------------- |
| 56 | |
| 57 | .. currentmodule:: pygcode |
| 58 | |
| 59 | .. autosummary:: |
| 60 | :toctree: _generate |
| 61 | |
| 62 | open |
| 63 | close |
| 64 | is_open |
| 65 | translate_result |
| 66 | is_valid_binary_gcode |
| 67 | read_header |
| 68 | get_config |
| 69 | from_ascii_to_binary |
| 70 | from_binary_to_ascii |
| 71 | )pbdoc"; |
| 72 | |
| 73 | // Auxiliary FILE API: |
| 74 | |
| 75 | py::class_<FILEWrapper> file_io_binding(m, |
| 76 | "FILEWrapper", |
| 77 | R"pbdoc(A FILE* wrapper to pass around in Python code)pbdoc"); |
| 78 | |
| 79 | m.def("open", [](const char * name, const char *mode) { |
| 80 | FILE * fptr = boost::nowide::fopen(name, mode); |
| 81 | |
| 82 | if (!fptr) { |
| 83 | #if _MSC_VER |
| 84 | static constexpr size_t bufsz = 100; |
| 85 | char buf[bufsz]; |
| 86 | strerror_s(buf, bufsz, errno); |
| 87 | throw std::runtime_error(buf); |
| 88 | #else |
| 89 | throw std::runtime_error(std::strerror(errno)); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | return std::make_unique<FILEWrapper>(fptr); |
| 94 | }, |
| 95 | R"pbdoc(Open a gcode file to process using pybgcode)pbdoc", |
| 96 | py::arg("name"), |
| 97 | py::arg("mode")); |
| 98 | |
| 99 | // Opening a byte buffer in python. This will only be available on Unix platforms |
| 100 | #ifndef _MSC_VER |
| 101 | m.def("memopen", [](py::bytes buffer) { |
| 102 | std::string_view cbuf(buffer); |
| 103 | |
| 104 | // Ugly but will work, despite the const_cast, the buffer will not |
| 105 | // be modified. Open mode is "rb" |
| 106 | FILE *fptr = fmemopen(const_cast<void *>(static_cast<const void *>(cbuf.data())), |
| 107 | cbuf.size(), |
| 108 | "rb"); |
| 109 | |
| 110 | if (!fptr) { |
| 111 | throw std::runtime_error(std::strerror(errno)); |
| 112 | } |
| 113 | |
| 114 | return std::make_unique<FILEWrapper>(fptr); |
| 115 | }, |
| 116 | R"pbdoc(Open a gcode memory buffer to process using pybgcode)pbdoc", |
| 117 | py::arg("buffer")); |
| 118 | #endif |
| 119 | |
| 120 | m.def("close", [](FILEWrapper &f) { f.close(); }, |
| 121 | R"pbdoc(Close a previously opened file)pbdoc", py::arg("file")); |
| 122 | m.def("is_open", [](const FILEWrapper &f) { return f.fptr != nullptr; }, |
| 123 | R"pbdoc(Check if file is open)pbdoc", py::arg("file")); |
| 124 | m.def("rewind", [](FILEWrapper &f) { if (f.fptr) std::rewind(f.fptr); }, |
| 125 | R"pbdoc(Moves the file position indicator to the beginning of the given file stream)pbdoc"); |
| 126 | |
| 127 | // Core API: |
| 128 | |
| 129 | py::enum_<core::EResult>(m, "EResult") |
| 130 | .value("Success", core::EResult::Success) |
| 131 | .value("ReadError", core::EResult::ReadError) |
| 132 | .value("WriteError", core::EResult::WriteError) |
| 133 | .value("InvalidMagicNumber", core::EResult::InvalidMagicNumber) |
| 134 | .value("InvalidVersionNumber", core::EResult::InvalidVersionNumber) |
| 135 | .value("InvalidChecksumType", core::EResult::InvalidChecksumType) |
| 136 | .value("InvalidBlockType", core::EResult::InvalidBlockType) |
| 137 | .value("InvalidCompressionType", core::EResult::InvalidCompressionType) |
| 138 | .value("InvalidMetadataEncodingType", core::EResult::InvalidMetadataEncodingType) |
| 139 | .value("InvalidGCodeEncodingType", core::EResult::InvalidGCodeEncodingType) |
| 140 | .value("DataCompressionError", core::EResult::DataCompressionError) |
| 141 | .value("DataUncompressionError", core::EResult::DataUncompressionError) |
| 142 | .value("MetadataEncodingError", core::EResult::MetadataEncodingError) |
| 143 | .value("MetadataDecodingError", core::EResult::MetadataDecodingError) |
| 144 | .value("GCodeEncodingError", core::EResult::GCodeEncodingError) |
| 145 | .value("GCodeDecodingError", core::EResult::GCodeDecodingError) |
| 146 | .value("BlockNotFound", core::EResult::BlockNotFound) |
| 147 | .value("InvalidChecksum", core::EResult::InvalidChecksum) |
| 148 | .value("InvalidThumbnailFormat", core::EResult::InvalidThumbnailFormat) |
| 149 | .value("InvalidThumbnailWidth", core::EResult::InvalidThumbnailWidth) |
| 150 | .value("InvalidThumbnailHeight", core::EResult::InvalidThumbnailHeight) |
| 151 | .value("InvalidThumbnailDataSize", core::EResult::InvalidThumbnailDataSize) |
| 152 | .value("InvalidBinaryGCodeFile", core::EResult::InvalidBinaryGCodeFile) |
| 153 | .value("InvalidAsciiGCodeFile", core::EResult::InvalidAsciiGCodeFile) |
| 154 | .value("InvalidSequenceOfBlocks", core::EResult::InvalidSequenceOfBlocks) |
| 155 | .value("InvalidBuffer", core::EResult::InvalidBuffer) |
| 156 | .value("AlreadyBinarized", core::EResult::AlreadyBinarized) |
| 157 | .value("MissingPrinterMetadata", core::EResult::MissingPrinterMetadata) |
| 158 | .value("MissingPrintMetadata", core::EResult::MissingPrintMetadata) |
| 159 | .value("MissingSlicerMetadat", core::EResult::MissingSlicerMetadata) |
| 160 | ; |
| 161 | |
| 162 | py::enum_<core::ECompressionType>(m, "CompressionType") |
| 163 | .value("none", core::ECompressionType::None) |
| 164 | .value("Deflate", core::ECompressionType::Deflate) |
| 165 | .value("Heatshrink_11_4", core::ECompressionType::Heatshrink_11_4) |
| 166 | .value("Heatshrink_12_4", core::ECompressionType::Heatshrink_12_4); |
| 167 | py::enum_<core::EGCodeEncodingType>(m, "GCodeEncodingType") |
| 168 | .value("none", core::EGCodeEncodingType::None) |
| 169 | .value("MeatPack", core::EGCodeEncodingType::MeatPack) |
| 170 | .value("MeatPackComments", core::EGCodeEncodingType::MeatPackComments); |
| 171 | py::enum_<core::EMetadataEncodingType>(m, "MetadataEncodingType") |
| 172 | .value("INI", core::EMetadataEncodingType::INI); |
| 173 | py::enum_<core::EChecksumType>(m, "ChecksumType") |
| 174 | .value("none", core::EChecksumType::None) |
| 175 | .value("CRC32", core::EChecksumType::CRC32); |
| 176 | py::enum_<core::EBlockType>(m, "EBlockType") |
| 177 | .value("FileMetadata", core::EBlockType::FileMetadata) |
| 178 | .value("GCode", core::EBlockType::GCode) |
| 179 | .value("SlicerMetadata", core::EBlockType::SlicerMetadata) |
| 180 | .value("PrinterMetadata", core::EBlockType::PrinterMetadata) |
| 181 | .value("PrintMetadata", core::EBlockType::PrintMetadata) |
| 182 | .value("Thumbnail", core::EBlockType::Thumbnail); |
| 183 | py::enum_<core::EThumbnailFormat>(m, "EThumbnailFormat") |
| 184 | .value("PNG", core::EThumbnailFormat::PNG) |
| 185 | .value("JPG", core::EThumbnailFormat::JPG) |
| 186 | .value("QOI", core::EThumbnailFormat::QOI); |
| 187 | |
| 188 | py::class_<core::FileHeader>(m, "FileHeader") |
| 189 | .def(py::init<>()) |
| 190 | .def_readonly("magic", &core::FileHeader::magic) |
| 191 | .def_readonly("version", &core::FileHeader::version) |
| 192 | .def_readonly("checksum_type", &core::FileHeader::checksum_type) |
| 193 | .def("read", [](core::FileHeader &self, FILEWrapper &file) { |
| 194 | return self.read(*file.fptr, nullptr); |
| 195 | }) |
| 196 | .def("write", [](core::FileHeader &self, FILEWrapper &file) { |
| 197 | return self.write(*file.fptr); |
| 198 | }); |
| 199 | |
| 200 | py::class_<core::BlockHeader>(m, "BlockHeader") |
| 201 | .def(py::init<>()) |
| 202 | .def_readonly("type", &core::BlockHeader::type) |
| 203 | .def_readonly("compression", &core::BlockHeader::compression) |
| 204 | .def_readonly("uncompressed_size", &core::BlockHeader::uncompressed_size) |
| 205 | .def_readonly("compressed_size", &core::BlockHeader::compressed_size) |
| 206 | .def("get_size", &core::BlockHeader::get_size) |
| 207 | .def("read", [](core::BlockHeader &self, FILEWrapper &file) { |
| 208 | return self.read(*file.fptr); |
| 209 | }) |
| 210 | .def("write", [](core::BlockHeader &self, FILEWrapper &file) { |
| 211 | return self.write(*file.fptr); |
| 212 | }); |
| 213 | |
| 214 | py::class_<core::ThumbnailParams>(m, "ThumbnailParams") |
| 215 | .def(py::init<>()) |
| 216 | .def_readonly("format", &core::ThumbnailParams::format) |
| 217 | .def_readonly("width", &core::ThumbnailParams::width) |
| 218 | .def_readonly("height", &core::ThumbnailParams::height) |
| 219 | .def("read", [](core::ThumbnailParams &self, FILEWrapper &file) { |
| 220 | return self.read(*file.fptr); |
| 221 | }) |
| 222 | .def("write", [](core::ThumbnailParams &self, FILEWrapper &file) { |
| 223 | return self.write(*file.fptr); |
| 224 | }); |
| 225 | |
| 226 | m.def("translate_result", |
| 227 | &core::translate_result, |
| 228 | R"pbdoc(Returns a string description of the given result)pbdoc", |
| 229 | py::arg("result") |
| 230 | ); |
| 231 | |
| 232 | m.def( |
| 233 | "is_valid_binary_gcode", |
| 234 | [](FILEWrapper& file, bool check_contents) { |
| 235 | std::vector<std::byte> cs_buffer; |
| 236 | |
| 237 | if (check_contents) |
| 238 | cs_buffer.resize(MaxBuffSz); |
| 239 | |
| 240 | size_t cs_buffer_size = cs_buffer.size(); |
| 241 | |
| 242 | return core::is_valid_binary_gcode(*file.fptr, check_contents, cs_buffer.data(), cs_buffer_size); |
| 243 | }, |
| 244 | R"pbdoc( |
| 245 | Returns EResult.Success if the given file is a valid binary gcode. |
| 246 | If check_contents is set to true, the order of the blocks is checked. |
| 247 | Does not modify the file position. |
| 248 | Caller is responsible for providing buffer for checksum calculation, if needed. |
| 249 | )pbdoc", |
| 250 | py::arg("file"), py::arg("check_contents") = false |
| 251 | ); |
| 252 | |
| 253 | m.def( |
| 254 | "read_header", |
| 255 | [](FILEWrapper& file, core::FileHeader& header) { |
| 256 | return core::read_header(*file.fptr, header, nullptr); |
| 257 | }, |
| 258 | R"pbdoc( |
| 259 | Reads the file header. |
| 260 | If max_version is not null, version is checked against the passed value. |
| 261 | If return == EResult.Success: |
| 262 | - header will contain the file header. |
| 263 | - file position will be set at the start of the 1st block header. |
| 264 | )pbdoc", |
| 265 | py::arg("file"), py::arg("header")); |
| 266 | |
| 267 | m.def("read_header", [](FILEWrapper& file, core::FileHeader& header, uint32_t max_version) { |
| 268 | return core::read_header(*file.fptr, header, &max_version); |
| 269 | }, py::arg("file"), py::arg("header"), py::arg("max_version")); |
| 270 | |
| 271 | m.def( |
| 272 | "read_next_block_header", |
| 273 | [](FILEWrapper &file, const core::FileHeader &file_header, core::BlockHeader &block_header) { |
| 274 | return core::read_next_block_header(*file.fptr, file_header, block_header, nullptr, 0); |
| 275 | }, |
| 276 | R"pbdoc( |
| 277 | Reads next block header from the current file position. |
| 278 | File position must be at the start of a block header. |
| 279 | If return == EResult.Success: |
| 280 | - block_header will contain the header of the block. |
| 281 | - file position will be set at the start of the block parameters data. |
| 282 | Caller is responsible for providing buffer for checksum calculation, if needed. |
| 283 | )pbdoc", |
| 284 | py::arg("file"), py::arg("file_header"), py::arg("block_header") |
| 285 | ); |
| 286 | |
| 287 | m.def( |
| 288 | "read_next_block_header", |
| 289 | [](FILEWrapper &file, |
| 290 | const core::FileHeader &file_header, core::BlockHeader &block_header, core::EBlockType block_type) { |
| 291 | return core::read_next_block_header(*file.fptr, file_header, block_header, |
| 292 | block_type, nullptr, 0); |
| 293 | }, |
| 294 | R"pbdoc( |
| 295 | Searches and reads next block header with the given type from the current file position. |
| 296 | File position must be at the start of a block header. |
| 297 | If return == EResult.Success: |
| 298 | - block_header will contain the header of the block with the required type. |
| 299 | - file position will be set at the start of the block parameters data. |
| 300 | otherwise: |
| 301 | - file position will keep the current value. |
| 302 | )pbdoc", |
| 303 | py::arg("file"), py::arg("file_header"), py::arg("block_header"), py::arg("block_type")); |
| 304 | |
| 305 | m.def( |
| 306 | "verify_block_checksum", |
| 307 | [](FILEWrapper& file, const core::FileHeader& file_header, const core::BlockHeader& block_header){ |
| 308 | std::array<std::byte, MaxBuffSz> buff; |
| 309 | return core::verify_block_checksum(*file.fptr, file_header, block_header, buff.data(), buff.size()); |
| 310 | }, |
| 311 | R"pbdoc( |
| 312 | Calculates block checksum and verify it against checksum stored in file. |
| 313 | If return == EResult.Success: |
| 314 | - file position will be set at the start of the next block header. |
| 315 | )pbdoc", |
| 316 | py::arg("file"), py::arg("file_header"), py::arg("block_header") |
| 317 | ); |
| 318 | |
| 319 | m.def( |
| 320 | "skip_block_content", |
| 321 | [](FILEWrapper &file, const core::FileHeader &file_header, const core::BlockHeader &block_header) { |
| 322 | return core::skip_block_content(*file.fptr, file_header, block_header); |
| 323 | }, |
| 324 | R"pbdoc( |
| 325 | Skips the content (parameters + data + checksum) of the block with the given block header. |
| 326 | File position must be at the start of the block parameters. |
| 327 | If return == EResult.Success: |
| 328 | - file position will be set at the start of the next block header. |
| 329 | )pbdoc", |
| 330 | py::arg("file"), py::arg("file_header"), py::arg("block_header") |
| 331 | ); |
| 332 | |
| 333 | m.def( |
| 334 | "skip_block", |
| 335 | [](FILEWrapper &file, const core::FileHeader &file_header, const core::BlockHeader& block_header) { |
| 336 | return core::skip_block(*file.fptr, file_header, block_header); |
| 337 | }, |
| 338 | R"pbdoc( |
| 339 | Skips the block with the given block header. |
| 340 | File position must be set by a previous call to BlockHeader::write() or BlockHeader::read(). |
| 341 | If return == EResult.Success: |
| 342 | - file position will be set at the start of the next block header. |
| 343 | )pbdoc", |
| 344 | py::arg("file"), py::arg("file_header"), py::arg("block_header") |
| 345 | ); |
| 346 | |
| 347 | m.def( |
| 348 | "block_parameters_size", |
| 349 | &core::block_parameters_size, |
| 350 | R"pbdoc( |
| 351 | Returns the size of the parameters of the given block type, in bytes. |
| 352 | )pbdoc", |
| 353 | py::arg("type") |
| 354 | ); |
| 355 | |
| 356 | m.def( |
| 357 | "block_payload_size", |
| 358 | &core::block_payload_size, |
| 359 | R"pbdoc( |
| 360 | Returns the size of the payload (parameters + data) of the block with the given header, in bytes. |
| 361 | )pbdoc", |
| 362 | py::arg("block_header") |
| 363 | ); |
| 364 | |
| 365 | m.def( |
| 366 | "checksum_size", |
| 367 | &core::checksum_size, |
| 368 | R"pbdoc( |
| 369 | Returns the size of the checksum of the given type, in bytes. |
| 370 | )pbdoc", |
| 371 | py::arg("type") |
| 372 | ); |
| 373 | |
| 374 | m.def( |
| 375 | "block_content_size", |
| 376 | &core::block_content_size, |
| 377 | R"pbdoc( |
| 378 | Returns the size of the content (parameters + data + checksum) of the block with the given header, in bytes. |
| 379 | )pbdoc", |
| 380 | py::arg("file_header"), py::arg("block_header") |
| 381 | ); |
| 382 | |
| 383 | m.def( |
| 384 | "bgcode_version", |
| 385 | &core::bgcode_version, |
| 386 | R"pbdoc( |
| 387 | Highest version of the binary format supported by this library instance |
| 388 | )pbdoc" |
| 389 | ); |
| 390 | |
| 391 | m.def( |
| 392 | "version", |
| 393 | &core::version, |
| 394 | R"pbdoc( |
| 395 | Version of the library. |
| 396 | )pbdoc" |
| 397 | ); |
| 398 | |
| 399 | // Binarizer API: |
| 400 | |
| 401 | py::class_<binarize::BaseMetadataBlock>(m, "BaseMetadataBlock") |
| 402 | .def(py::init<>()) |
| 403 | .def_readonly("encoding_type", &binarize::BaseMetadataBlock::encoding_type) |
| 404 | .def_readonly("raw_data", &binarize::BaseMetadataBlock::raw_data) |
| 405 | .def("read_data", [](binarize::BaseMetadataBlock &self, FILEWrapper &file, const core::BlockHeader& block_header) { |
| 406 | return self.read_data(*file.fptr, block_header); |
| 407 | }, R"pbdoc(read block data in encoded format)pbdoc", py::arg("file"), py::arg("block_header")); |
| 408 | |
| 409 | py::class_<binarize::FileMetadataBlock, binarize::BaseMetadataBlock>(m, "FileMetadataBlock") |
| 410 | .def(py::init<>()) |
| 411 | .def("write", [](binarize::FileMetadataBlock &self, FILEWrapper &file, core::ECompressionType compression_type, core::EChecksumType checksum_type){ |
| 412 | return self.write(*file.fptr, compression_type, checksum_type); |
| 413 | }, R"pbdoc(write block header and data)pbdoc", py::arg("file"), py::arg("compression_type"), py::arg("checksum_type")) |
| 414 | .def("read_data", [](binarize::FileMetadataBlock &self, FILEWrapper &file, const core::FileHeader &file_header, const core::BlockHeader& block_header) { |
| 415 | return self.read_data(*file.fptr, file_header, block_header); |
| 416 | }, R"pbdoc(read block data)pbdoc", py::arg("file"), py::arg("file_header"), py::arg("block_header")); |
| 417 | |
| 418 | py::class_<binarize::PrintMetadataBlock, binarize::BaseMetadataBlock>(m, "PrintMetadataBlock") |
| 419 | .def(py::init<>()) |
| 420 | .def("write", [](binarize::PrintMetadataBlock &self, FILEWrapper &file, core::ECompressionType compression_type, core::EChecksumType checksum_type){ |
| 421 | return self.write(*file.fptr, compression_type, checksum_type); |
| 422 | }, R"pbdoc(write block header and data)pbdoc", py::arg("file"), py::arg("compression_type"), py::arg("checksum_type")) |
| 423 | .def("read_data", [](binarize::PrintMetadataBlock &self, FILEWrapper &file, const core::FileHeader &file_header, const core::BlockHeader& block_header) { |
| 424 | return self.read_data(*file.fptr, file_header, block_header); |
| 425 | }, R"pbdoc(read block data)pbdoc", py::arg("file"), py::arg("file_header"), py::arg("block_header")); |
| 426 | |
| 427 | py::class_<binarize::PrinterMetadataBlock, binarize::BaseMetadataBlock>(m, "PrinterMetadataBlock") |
| 428 | .def(py::init<>()) |
| 429 | .def("write", [](binarize::PrinterMetadataBlock &self, FILEWrapper &file, core::ECompressionType compression_type, core::EChecksumType checksum_type){ |
| 430 | return self.write(*file.fptr, compression_type, checksum_type); |
| 431 | }, R"pbdoc(write block header and data)pbdoc", py::arg("file"), py::arg("compression_type"), py::arg("checksum_type")) |
| 432 | .def("read_data", [](binarize::PrinterMetadataBlock &self, FILEWrapper &file, const core::FileHeader &file_header, const core::BlockHeader& block_header) { |
| 433 | return self.read_data(*file.fptr, file_header, block_header); |
| 434 | }, R"pbdoc(read block data)pbdoc", py::arg("file"), py::arg("file_header"), py::arg("block_header")); |
| 435 | |
| 436 | py::class_<binarize::ThumbnailBlock>(m, "ThumbnailBlock") |
| 437 | .def(py::init<>()) |
| 438 | .def_readonly("params", &binarize::ThumbnailBlock::params) |
| 439 | .def("data", [](const binarize::ThumbnailBlock &self) { |
| 440 | return py::bytes(reinterpret_cast<const char*>(self.data.data()), self.data.size()); |
| 441 | }) |
| 442 | .def("write", [](binarize::ThumbnailBlock &self, FILEWrapper &file, core::EChecksumType checksum_type){ |
| 443 | return self.write(*file.fptr, checksum_type); |
| 444 | }, R"pbdoc(Write block header and data)pbdoc", py::arg("file"), py::arg("checksum_type")) |
| 445 | .def("read_data", [](binarize::ThumbnailBlock &self, FILEWrapper &file, const core::FileHeader& file_header, const core::BlockHeader& block_header) { |
| 446 | return self.read_data(*file.fptr, file_header, block_header); |
| 447 | }, R"pbdoc(Read block data)pbdoc", py::arg("file"), py::arg("file_header"), py::arg("block_header")); |
| 448 | |
| 449 | py::class_<binarize::GCodeBlock>(m, "GCodeBlock") |
| 450 | .def(py::init<>()) |
| 451 | .def_readonly("encoding_type", &binarize::GCodeBlock::encoding_type) |
| 452 | .def_readonly("raw_data", &binarize::GCodeBlock::raw_data) |
| 453 | .def("write", [](binarize::GCodeBlock &self, FILEWrapper &file, core::ECompressionType compression_type, core::EChecksumType checksum_type){ |
| 454 | return self.write(*file.fptr, compression_type, checksum_type); |
| 455 | }, R"pbdoc(write block header and data)pbdoc", py::arg("file"), py::arg("compression_type"), py::arg("checksum_type")) |
| 456 | .def("read_data", [](binarize::GCodeBlock &self, FILEWrapper &file, const core::FileHeader &file_header, const core::BlockHeader& block_header) { |
| 457 | return self.read_data(*file.fptr, file_header, block_header); |
| 458 | }, R"pbdoc(read block data)pbdoc", py::arg("file"), py::arg("file_header"), py::arg("block_header")); |
| 459 | |
| 460 | py::class_<binarize::SlicerMetadataBlock, binarize::BaseMetadataBlock>(m, "SlicerMetadataBlock") |
| 461 | .def(py::init<>()) |
| 462 | .def("write", [](binarize::SlicerMetadataBlock &self, FILEWrapper &file, core::ECompressionType compression_type, core::EChecksumType checksum_type){ |
| 463 | return self.write(*file.fptr, compression_type, checksum_type); |
| 464 | }, R"pbdoc(write block header and data)pbdoc", py::arg("file"), py::arg("compression_type"), py::arg("checksum_type")) |
| 465 | .def("read_data", [](binarize::SlicerMetadataBlock &self, FILEWrapper &file, const core::FileHeader &file_header, const core::BlockHeader& block_header) { |
| 466 | return self.read_data(*file.fptr, file_header, block_header); |
| 467 | }, R"pbdoc(read block data)pbdoc", py::arg("file"), py::arg("file_header"), py::arg("block_header")); |
| 468 | |
| 469 | py::class_<binarize::Slicer3MetadataBlock, binarize::BaseMetadataBlock>(m, "Slicer3MetadataBlock") |
| 470 | .def(py::init<>()) |
| 471 | .def("write", [](binarize::Slicer3MetadataBlock &self, FILEWrapper &file, core::ECompressionType compression_type, core::EChecksumType checksum_type){ |
| 472 | return self.write(*file.fptr, compression_type, checksum_type); |
| 473 | }, R"pbdoc(write block header and data)pbdoc", py::arg("file"), py::arg("compression_type"), py::arg("checksum_type")) |
| 474 | .def("read_data", [](binarize::Slicer3MetadataBlock &self, FILEWrapper &file, const core::FileHeader &file_header, const core::BlockHeader& block_header) { |
| 475 | return self.read_data(*file.fptr, file_header, block_header); |
| 476 | }, R"pbdoc(read block data)pbdoc", py::arg("file"), py::arg("file_header"), py::arg("block_header")); |
| 477 | |
| 478 | py::enum_<binarize::EPeekSlicerMetadataResult>(m, "EPeekSlicerMetadataResult") |
| 479 | .value("Slicer3MetadataFound", binarize::EPeekSlicerMetadataResult::Slicer3MetadataFound) |
| 480 | .value("SlicerMetadataFound", binarize::EPeekSlicerMetadataResult::SlicerMetadataFound) |
| 481 | .value("OtherBlockFound", binarize::EPeekSlicerMetadataResult::OtherBlockFound) |
| 482 | .value("ReadError", binarize::EPeekSlicerMetadataResult::ReadError) |
| 483 | ; |
| 484 | |
| 485 | m.def("peek_slicer_metadata_block", [](FILEWrapper& file, const core::BlockHeader& block_header) { |
| 486 | return binarize::peek_slicer_metadata_block(*file.fptr, block_header); |
| 487 | }); |
| 488 | |
| 489 | py::class_<binarize::BinarizerConfig::Compression>(m, "BinarizerCompression") |
| 490 | .def(py::init<>()) |
| 491 | .def_readwrite("file_metadata", &binarize::BinarizerConfig::Compression::file_metadata) |
| 492 | .def_readwrite("printer_metadata", &binarize::BinarizerConfig::Compression::printer_metadata) |
| 493 | .def_readwrite("print_metadata", &binarize::BinarizerConfig::Compression::print_metadata) |
| 494 | .def_readwrite("slicer_metadata", &binarize::BinarizerConfig::Compression::slicer_metadata) |
| 495 | .def_readwrite("gcode", &binarize::BinarizerConfig::Compression::gcode); |
| 496 | |
| 497 | py::class_<binarize::BinarizerConfig>(m, "BinarizerConfig") |
| 498 | .def(py::init<>()) |
| 499 | .def_readwrite("compression", &binarize::BinarizerConfig::compression) |
| 500 | .def_readwrite("gcode_encoding", &binarize::BinarizerConfig::gcode_encoding) |
| 501 | .def_readwrite("metadata_encoding", &binarize::BinarizerConfig::metadata_encoding) |
| 502 | .def_readwrite("checksum", &binarize::BinarizerConfig::checksum); |
| 503 | |
| 504 | py::class_<binarize::BinaryData>(m, "BinaryData") |
| 505 | .def(py::init<>()) |
| 506 | .def_readwrite("file_metadata", &binarize::BinaryData::file_metadata) |
| 507 | .def_readwrite("printer_metadata", &binarize::BinaryData::printer_metadata) |
| 508 | .def_readwrite("thumbnails", &binarize::BinaryData::thumbnails) |
| 509 | .def_readwrite("slicer_metadata", &binarize::BinaryData::slicer_metadata) |
| 510 | .def_readwrite("slicer3_metadata", &binarize::BinaryData::slicer3_metadata) |
| 511 | .def_readwrite("printer_metadata", &binarize::BinaryData::printer_metadata); |
| 512 | |
| 513 | py::class_<binarize::Binarizer>(m, "Binarizer") |
| 514 | .def(py::init<>()) |
| 515 | .def("is_enabled", &binarize::Binarizer::is_enabled) |
| 516 | .def("set_enabled", &binarize::Binarizer::set_enabled) |
| 517 | .def("get_binary_data", [](binarize::Binarizer &self) { |
| 518 | return self.binarize::Binarizer::get_binary_data(); |
| 519 | }) |
| 520 | .def("get_binary_data", [](const binarize::Binarizer &self) { |
| 521 | return self.binarize::Binarizer::get_binary_data(); |
| 522 | }) |
| 523 | .def("get_max_gcode_cache_size", &binarize::Binarizer::get_max_gcode_cache_size) |
| 524 | .def("set_max_gcode_cache_size", &binarize::Binarizer::set_max_gcode_cache_size) |
| 525 | .def("initialize", [](binarize::Binarizer &self, FILEWrapper &file, const binarize::BinarizerConfig &config){ |
| 526 | return self.initialize(*file.fptr, config); |
| 527 | }) |
| 528 | .def("append_gcode", &binarize::Binarizer::append_gcode) |
| 529 | .def("finalize", &binarize::Binarizer::finalize); |
| 530 | |
| 531 | // Convert API: |
| 532 | |
| 533 | m.def("get_config", &get_config, R"pbdoc(Create a default configuration for ascii to binary gcode conversion)pbdoc"); |
| 534 | |
| 535 | m.def("from_ascii_to_binary", [](FILEWrapper &infile, FILEWrapper &outfile, const binarize::BinarizerConfig &config) { |
| 536 | return convert::from_ascii_to_binary(*infile.fptr, *outfile.fptr, config); |
| 537 | }, |
| 538 | R"pbdoc(Convert ascii gcode to binary format)pbdoc", |
| 539 | py::arg("infile"), py::arg("outfile"), py::arg("config") = get_config() |
| 540 | ); |
| 541 | |
| 542 | m.def("from_binary_to_ascii", [] (FILEWrapper &infile, FILEWrapper &outfile, bool verify_checksum) { |
| 543 | return convert::from_binary_to_ascii(*infile.fptr, *outfile.fptr, verify_checksum); |
| 544 | }, |
| 545 | R"pbdoc(Convert binary gcode to textual format)pbdoc", |
| 546 | py::arg("infile"), py::arg("outfile"), py::arg("verify_checksum") = true |
| 547 | ); |
| 548 | |
| 549 | #ifdef VERSION_INFO |
| 550 | m.attr("__version__") = VERSION_INFO; |
| 551 | #else |
| 552 | m.attr("__version__") = "dev"; |
| 553 | #endif |
| 554 | } |