Skip to content

Commit 32d24d8

Browse files
committed
Start adding binding for new IFileTree glob and walk.
1 parent 00a6d59 commit 32d24d8

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/mobase/pybind11_all.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pybind11_qt/pybind11_qt.h"
1414

1515
#include "pybind11_utils/functional.h"
16+
#include "pybind11_utils/generator.h"
1617
#include "pybind11_utils/shared_cpp_owner.h"
1718
#include "pybind11_utils/smart_variant_wrapper.h"
1819

src/mobase/wrappers/pyfiletree.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ namespace mo2::python {
8484
void add_ifiletree_bindings(pybind11::module_& m)
8585
{
8686

87+
// register generator for walk and glob
88+
register_generator_type<std::shared_ptr<const FileTreeEntry>>(m);
89+
8790
// FileTreeEntry class:
8891
auto fileTreeEntryClass =
8992
py::class_<FileTreeEntry, std::shared_ptr<FileTreeEntry>>(m,
@@ -164,6 +167,11 @@ namespace mo2::python {
164167
.value("SKIP", IFileTree::WalkReturn::SKIP)
165168
.export_values();
166169

170+
py::enum_<IFileTree::GlobPatternType>(iFileTreeClass, "GlobPatternType")
171+
.value("GLOB", IFileTree::GlobPatternType::GLOB)
172+
.value("REGEX", IFileTree::GlobPatternType::REGEX)
173+
.export_values();
174+
167175
// Non-mutable operations:
168176
iFileTreeClass.def("exists",
169177
py::overload_cast<QString, IFileTree::FileTypes>(
@@ -175,10 +183,18 @@ namespace mo2::python {
175183
iFileTreeClass.def("pathTo", &IFileTree::pathTo, py::arg("entry"),
176184
py::arg("sep") = "\\");
177185

178-
// Note: walk() would probably be better as a generator in python, but
179-
// it is likely impossible to construct from the C++ walk() method.
180-
iFileTreeClass.def("walk", &IFileTree::walk, py::arg("callback"),
181-
py::arg("sep") = "\\");
186+
iFileTreeClass.def(
187+
"walk",
188+
py::overload_cast<
189+
std::function<IFileTree::WalkReturn(
190+
QString const&, std::shared_ptr<const FileTreeEntry>)>,
191+
QString>(&IFileTree::walk, py::const_),
192+
py::arg("callback"), py::arg("sep") = "\\");
193+
194+
iFileTreeClass.def("walk", py::overload_cast<>(&IFileTree::walk, py::const_));
195+
196+
iFileTreeClass.def("glob", &IFileTree::glob, py::arg("pattern"),
197+
py::arg("type") = IFileTree::GlobPatternType::GLOB);
182198

183199
// Kind-of-static operations:
184200
iFileTreeClass.def("createOrphanTree", &IFileTree::createOrphanTree,

src/pybind11-utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.16)
22

33
add_library(pybind11-utils STATIC
44
./include/pybind11_utils/functional.h
5+
./include/pybind11_utils/generator.h
56
./include/pybind11_utils/shared_cpp_owner.h
67
./include/pybind11_utils/smart_variant_wrapper.h
78
./include/pybind11_utils/smart_variant.h
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef PYTHON_PYBIND11_GENERATOR_H
2+
#define PYTHON_PYBIND11_GENERATOR_H
3+
4+
#include <generator>
5+
6+
#include <pybind11/pybind11.h>
7+
8+
namespace mo2::python {
9+
10+
// create a Python generator from a C++ generator
11+
//
12+
template <typename T>
13+
auto register_generator_type(pybind11::handle scope,
14+
const char* name = "_mo2_generator")
15+
{
16+
namespace py = pybind11;
17+
18+
return py::class_<std::generator<T>>(scope, name, py::module_local())
19+
.def("__iter__",
20+
[](std::generator<T>& gen) -> std::generator<T>& {
21+
return gen;
22+
})
23+
.def("__next__", [](std::generator<T>& gen) {
24+
auto it = gen.begin();
25+
if (it != gen.end()) {
26+
return *it;
27+
}
28+
else {
29+
throw py::stop_iteration();
30+
}
31+
});
32+
}
33+
34+
} // namespace mo2::python
35+
36+
#endif

0 commit comments

Comments
 (0)