|
10 | 10 |
|
11 | 11 | #include <osmium/io/any_input.hpp> |
12 | 12 | #include <osmium/io/any_output.hpp> |
| 13 | +#include <osmium/thread/pool.hpp> |
13 | 14 |
|
14 | 15 | #include <filesystem> |
15 | 16 |
|
| 17 | +#include "io.h" |
| 18 | + |
16 | 19 | namespace py = pybind11; |
17 | 20 |
|
18 | 21 | namespace { |
@@ -78,34 +81,65 @@ PYBIND11_MODULE(io, m) |
78 | 81 | py::return_value_policy::reference_internal) |
79 | 82 | ; |
80 | 83 |
|
81 | | - py::class_<osmium::io::Reader>(m, "Reader") |
82 | | - .def(py::init<std::string>()) |
83 | | - .def(py::init<std::string, osmium::osm_entity_bits::type>()) |
84 | | - .def(py::init<>([] (std::filesystem::path const &file) { |
85 | | - return new osmium::io::Reader(file.string()); |
86 | | - })) |
87 | | - .def(py::init<>([] (std::filesystem::path const &file, osmium::osm_entity_bits::type etype) { |
88 | | - return new osmium::io::Reader(file.string(), etype); |
89 | | - })) |
90 | | - .def(py::init<osmium::io::File>(), |
91 | | - py::keep_alive<1, 2>()) |
92 | | - .def(py::init<osmium::io::File, osmium::osm_entity_bits::type>(), |
93 | | - py::keep_alive<1, 2>()) |
94 | | - .def("eof", &osmium::io::Reader::eof) |
95 | | - .def("close", &osmium::io::Reader::close) |
96 | | - .def("header", &osmium::io::Reader::header) |
| 84 | + py::class_<pyosmium::PyReader>(m, "Reader") |
| 85 | + .def(py::init<osmium::io::File, osmium::osm_entity_bits::type const *, |
| 86 | + osmium::thread::Pool *>(), |
| 87 | + py::keep_alive<1, 2>(), py::keep_alive<1, 4>(), |
| 88 | + py::arg("file"), py::arg("types") = nullptr, py::arg("thread_pool") = nullptr |
| 89 | + ) |
| 90 | + .def(py::init<>([] (std::string file, |
| 91 | + osmium::osm_entity_bits::type const *types, |
| 92 | + osmium::thread::Pool *pool) { |
| 93 | + return new pyosmium::PyReader(osmium::io::File(std::move(file)), |
| 94 | + types, pool); }), |
| 95 | + py::keep_alive<1, 2>(), py::keep_alive<1, 4>(), |
| 96 | + py::arg("file"), py::arg("types") = nullptr, py::arg("thread_pool") = nullptr |
| 97 | + ) |
| 98 | + .def(py::init<>([] (std::filesystem::path const &file, |
| 99 | + osmium::osm_entity_bits::type const *types, |
| 100 | + osmium::thread::Pool *pool) { |
| 101 | + return new pyosmium::PyReader(osmium::io::File(file.string()), |
| 102 | + types, pool); }), |
| 103 | + py::keep_alive<1, 2>(), py::keep_alive<1, 4>(), |
| 104 | + py::arg("file"), py::arg("types") = nullptr, py::arg("thread_pool") = nullptr |
| 105 | + ) |
| 106 | + .def("eof", [](pyosmium::PyReader const &self) { return self.get()->eof(); }) |
| 107 | + .def("close", [](pyosmium::PyReader &self) { self.get()->close(); }) |
| 108 | + .def("header", [](pyosmium::PyReader &self) { return self.get()->header(); }) |
97 | 109 | .def("__enter__", [](py::object const &self) { return self; }) |
98 | | - .def("__exit__", [](osmium::io::Reader &self, py::args args) { self.close(); }) |
| 110 | + .def("__exit__", [](pyosmium::PyReader &self, py::args args) { self.get()->close(); }) |
99 | 111 | ; |
100 | 112 |
|
101 | | - py::class_<osmium::io::Writer>(m, "Writer") |
102 | | - .def(py::init<std::string>()) |
103 | | - .def(py::init<>([] (std::filesystem::path const &file) { |
104 | | - return new osmium::io::Writer(file.string()); |
105 | | - })) |
106 | | - .def(py::init<osmium::io::File>()) |
107 | | - .def(py::init<std::string, osmium::io::Header>()) |
108 | | - .def(py::init<osmium::io::File, osmium::io::Header>()) |
109 | | - .def("close", &osmium::io::Writer::close) |
| 113 | + py::class_<pyosmium::PyWriter>(m, "Writer") |
| 114 | + .def(py::init<osmium::io::File, osmium::io::Header const *, bool, osmium::thread::Pool *>(), |
| 115 | + py::keep_alive<1, 5>(), |
| 116 | + py::arg("file"), py::arg("header") = nullptr, |
| 117 | + py::arg("overwrite") = false, py::arg("thread_pool") = nullptr |
| 118 | + ) |
| 119 | + .def(py::init<>([] (std::filesystem::path const &file, osmium::io::Header const *header, |
| 120 | + bool overwrite, osmium::thread::Pool *pool) { |
| 121 | + return new pyosmium::PyWriter(osmium::io::File(file.string()), |
| 122 | + header, overwrite, pool); }), |
| 123 | + py::keep_alive<1, 5>(), |
| 124 | + py::arg("file"), py::arg("header") = nullptr, |
| 125 | + py::arg("overwrite") = false, py::arg("thread_pool") = nullptr |
| 126 | + ) |
| 127 | + .def(py::init<>([] (std::string filename, osmium::io::Header const *header, |
| 128 | + bool overwrite, osmium::thread::Pool *pool) { |
| 129 | + return new pyosmium::PyWriter(osmium::io::File(std::move(filename)), |
| 130 | + header, overwrite, pool); }), |
| 131 | + py::keep_alive<1, 5>(), |
| 132 | + py::arg("file"), py::arg("header") = nullptr, |
| 133 | + py::arg("overwrite") = false, py::arg("thread_pool") = nullptr |
| 134 | + ) |
| 135 | + .def("close", [](pyosmium::PyWriter &self) { self.get()->close(); }) |
110 | 136 | ; |
| 137 | + |
| 138 | + py::class_<osmium::thread::Pool>(m, "ThreadPool") |
| 139 | + .def(py::init<int, std::size_t>(), |
| 140 | + py::arg("num_threads")=0, py::arg("max_queue_size")=0U) |
| 141 | + .def_property_readonly("num_threads", &osmium::thread::Pool::num_threads) |
| 142 | + .def("queue_size", &osmium::thread::Pool::queue_size) |
| 143 | + .def("queue_empty", &osmium::thread::Pool::queue_empty) |
| 144 | + ; |
111 | 145 | } |
0 commit comments