-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbind_core.cpp
More file actions
205 lines (194 loc) · 8.6 KB
/
Copy pathbind_core.cpp
File metadata and controls
205 lines (194 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "bindings.hpp"
#include <odr/exceptions.hpp>
#include <odr/file.hpp>
#include <odr/global_params.hpp>
#include <odr/logger.hpp>
#include <odr/odr.hpp>
#include <pybind11/stl.h>
#include <string>
#include <vector>
namespace py = pybind11;
void odr_python::bind_core(py::module_ &m) {
m.def("version", &odr::version, "Version of the underlying odrcore library.");
m.def("commit_hash", &odr::commit_hash,
"Git commit hash of the underlying odrcore library.");
m.def("is_dirty", &odr::is_dirty);
m.def("is_debug", &odr::is_debug);
m.def("identify", &odr::identify,
"Identification string of the underlying odrcore library.");
py::class_<odr::GlobalParams>(m, "GlobalParams",
"Global resource paths of the library.")
.def_static("odr_core_data_path", &odr::GlobalParams::odr_core_data_path)
// the two libmagic paths are deprecated and inert: libmagic is gone, so
// nothing reads back what they store
.def_static("libmagic_database_path",
&odr::GlobalParams::libmagic_database_path)
.def_static("set_odr_core_data_path",
&odr::GlobalParams::set_odr_core_data_path, py::arg("path"))
.def_static("set_libmagic_database_path",
&odr::GlobalParams::set_libmagic_database_path,
py::arg("path"));
// Mirrors odr::Exception, so `except odr.Error` catches the whole library.
// `register_exception`, not `py::exception`: the latter has no translator.
// Registered first so it is tried last - pybind11 reverses that order.
const py::exception<odr::Exception> &error =
py::register_exception<odr::Exception>(m, "Error", PyExc_RuntimeError);
py::register_exception<odr::UnsupportedOperation>(m, "UnsupportedOperation",
error);
// The one deliberate exception: mapping onto Python's own FileNotFoundError
// is worth more here than sitting under `Error`.
py::register_exception<odr::FileNotFound>(m, "FileNotFound",
PyExc_FileNotFoundError);
py::register_exception<odr::UnknownFileType>(m, "UnknownFileTypeError",
error);
py::register_exception<odr::UnsupportedFileType>(
m, "UnsupportedFileTypeError", error);
py::register_exception<odr::FileReadError>(m, "FileReadError", error);
py::register_exception<odr::FileWriteError>(m, "FileWriteError", error);
py::register_exception<odr::NoDocumentFile>(m, "NoDocumentFileError", error);
py::register_exception<odr::UnknownDocumentType>(
m, "UnknownDocumentTypeError", error);
py::register_exception<odr::UnsupportedCryptoAlgorithm>(
m, "UnsupportedCryptoAlgorithmError", error);
py::register_exception<odr::WrongPasswordError>(m, "WrongPasswordError",
error);
py::register_exception<odr::DecryptionFailed>(m, "DecryptionFailedError",
error);
py::register_exception<odr::NotEncryptedError>(m, "NotEncryptedError", error);
py::register_exception<odr::FileEncryptedError>(m, "FileEncryptedError",
error);
py::register_exception<odr::DocumentCopyProtectedException>(
m, "DocumentCopyProtectedError", error);
}
void odr_python::bind_functions(py::module_ &m) {
m.def("all_file_types", &odr::all_file_types,
"Every file type this library knows about.");
m.def("file_type_by_file_extension", &odr::file_type_by_file_extension,
py::arg("extension"));
m.def(
"file_extensions_by_file_type",
[](const odr::FileType type) {
const auto extensions = odr::file_extensions_by_file_type(type);
return std::vector<std::string>(extensions.begin(), extensions.end());
},
py::arg("type"), "Every file extension accepted for the file type.");
m.def(
"file_extension_by_file_type",
[](const odr::FileType type) {
return std::string(odr::file_extension_by_file_type(type));
},
py::arg("type"));
m.def("file_category_by_file_type", &odr::file_category_by_file_type,
py::arg("type"));
m.def("document_type_by_file_type", &odr::document_type_by_file_type,
py::arg("type"));
m.def("file_type_to_string", &odr::file_type_to_string, py::arg("type"));
m.def("file_category_to_string", &odr::file_category_to_string,
py::arg("category"));
m.def("document_type_to_string", &odr::document_type_to_string,
py::arg("type"));
m.def(
"file_type_by_mimetype",
[](const std::string &mimetype) {
return odr::file_type_by_mimetype(mimetype);
},
py::arg("mimetype"));
m.def(
"mimetype_by_file_type",
[](const odr::FileType type) {
return std::string(odr::mimetype_by_file_type(type));
},
py::arg("type"));
m.def(
"mimetypes_by_file_type",
[](const odr::FileType type) {
const auto mimetypes = odr::mimetypes_by_file_type(type);
return std::vector<std::string>(mimetypes.begin(), mimetypes.end());
},
py::arg("type"), "Every MIME type accepted for the file type.");
m.def("capabilities_by_file_type", &odr::capabilities_by_file_type,
py::arg("type"), "What this library can do with the file type.");
// Each of these is bound for `File` as well as for a path: a caller holding
// bytes (`File.from_memory`) must be able to reach everything a caller
// holding a path can.
m.def(
"list_file_types",
[](const odr::File &file, const odr::Logger &logger) {
return odr::list_file_types(file, logger);
},
py::arg("file"), py::arg("logger") = odr::Logger::null(),
"Determine the possible file types of a file.");
m.def(
"list_file_types",
[](const std::string &path, const odr::Logger &logger) {
return odr::list_file_types(path, logger);
},
py::arg("path"), py::arg("logger") = odr::Logger::null(),
"Determine the possible file types of a file.");
m.def(
"mimetype",
[](const odr::File &file, const odr::Logger &logger) {
return std::string(odr::mimetype(file, logger));
},
py::arg("file"), py::arg("logger") = odr::Logger::null(),
"Determine the MIME type of a file.");
m.def(
"mimetype",
[](const std::string &path, const odr::Logger &logger) {
return std::string(odr::mimetype(path, logger));
},
py::arg("path"), py::arg("logger") = odr::Logger::null(),
"Determine the MIME type of a file.");
// Decoding is long-running, so it must not hold the GIL. Re-entry is safe: a
// Python `ILogger` re-acquires it in the trampoline.
m.def(
"open",
[](const odr::File &file, const odr::Logger &logger) {
return odr::open(file, logger);
},
py::arg("file"), py::arg("logger") = odr::Logger::null(),
py::call_guard<py::gil_scoped_release>(), "Decode a file.");
m.def(
"open",
[](const odr::File &file, const odr::FileType as,
const odr::Logger &logger) { return odr::open(file, as, logger); },
py::arg("file"), py::arg("as_type"),
py::arg("logger") = odr::Logger::null(),
py::call_guard<py::gil_scoped_release>(),
"Decode a file as a specific file type.");
m.def(
"open",
[](const odr::File &file, const odr::DecodePreference &preference,
const odr::Logger &logger) {
return odr::open(file, preference, logger);
},
py::arg("file"), py::arg("preference"),
py::arg("logger") = odr::Logger::null(),
py::call_guard<py::gil_scoped_release>(),
"Decode a file with a decode preference.");
m.def(
"open",
[](const std::string &path, const odr::Logger &logger) {
return odr::open(path, logger);
},
py::arg("path"), py::arg("logger") = odr::Logger::null(),
py::call_guard<py::gil_scoped_release>(), "Open and decode a file.");
m.def(
"open",
[](const std::string &path, const odr::FileType as,
const odr::Logger &logger) { return odr::open(path, as, logger); },
py::arg("path"), py::arg("as_type"),
py::arg("logger") = odr::Logger::null(),
py::call_guard<py::gil_scoped_release>(),
"Open and decode a file as a specific file type.");
m.def(
"open",
[](const std::string &path, const odr::DecodePreference &preference,
const odr::Logger &logger) {
return odr::open(path, preference, logger);
},
py::arg("path"), py::arg("preference"),
py::arg("logger") = odr::Logger::null(),
py::call_guard<py::gil_scoped_release>(),
"Open and decode a file with a decode preference.");
}