anki/pylib/rsbridge/lib.rs

106 lines
3.3 KiB
Rust
Raw Normal View History

2020-02-04 11:07:28 +01:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use anki::backend::{init_backend, Backend as RustBackend, BackendMethod};
use pyo3::exceptions::PyException;
2019-12-23 05:42:14 +01:00
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::{create_exception, wrap_pyfunction};
use std::convert::TryFrom;
2019-12-23 05:42:14 +01:00
// Regular backend
//////////////////////////////////
2021-01-31 07:11:55 +01:00
#[pyclass(module = "rsbridge")]
2019-12-24 23:59:33 +01:00
struct Backend {
backend: RustBackend,
}
2019-12-23 05:42:14 +01:00
2021-01-31 07:11:55 +01:00
create_exception!(rsbridge, BackendError, PyException);
#[pyfunction]
fn buildhash() -> &'static str {
anki::version::buildhash()
}
#[pyfunction]
fn open_backend(init_msg: &PyBytes) -> PyResult<Backend> {
match init_backend(init_msg.as_bytes()) {
Ok(backend) => Ok(Backend { backend }),
Err(e) => Err(PyException::new_err(e)),
2019-12-23 05:42:14 +01:00
}
}
2019-12-23 05:42:14 +01:00
fn want_release_gil(method: u32) -> bool {
if let Ok(method) = BackendMethod::try_from(method) {
2020-11-02 06:02:30 +01:00
!matches!(
method,
2020-08-29 13:54:45 +02:00
BackendMethod::ExtractAVTags
2020-11-02 06:02:30 +01:00
| BackendMethod::ExtractLatex
| BackendMethod::RenderExistingCard
| BackendMethod::RenderUncommittedCard
| BackendMethod::StripAVTags
| BackendMethod::SchedTimingToday
| BackendMethod::AddOrUpdateDeckLegacy
| BackendMethod::NewDeckLegacy
| BackendMethod::NewDeckConfigLegacy
| BackendMethod::GetStockNotetypeLegacy
| BackendMethod::StudiedToday
| BackendMethod::TranslateString
| BackendMethod::FormatTimespan
| BackendMethod::LatestProgress
| BackendMethod::SetWantsAbort
| BackendMethod::I18nResources
| BackendMethod::JoinSearchNodes
| BackendMethod::ReplaceSearchNode
| BackendMethod::BuildSearchString
| BackendMethod::StateIsLeech
2020-11-02 06:02:30 +01:00
)
} else {
false
}
}
#[pymethods]
impl Backend {
fn command(&self, py: Python, method: u32, input: &PyBytes) -> PyResult<PyObject> {
let in_bytes = input.as_bytes();
if want_release_gil(method) {
py.allow_threads(|| self.backend.run_command_bytes(method, in_bytes))
} else {
2020-05-24 00:36:50 +02:00
self.backend.run_command_bytes(method, in_bytes)
}
.map(|out_bytes| {
let out_obj = PyBytes::new(py, &out_bytes);
out_obj.into()
})
.map_err(BackendError::new_err)
}
/// This takes and returns JSON, due to Python's slow protobuf
/// encoding/decoding.
fn db_command(&self, py: Python, input: &PyBytes) -> PyResult<PyObject> {
let in_bytes = input.as_bytes();
let out_res = py.allow_threads(|| {
2020-03-06 03:30:19 +01:00
self.backend
.run_db_command_bytes(in_bytes)
.map_err(BackendError::new_err)
2020-03-06 03:30:19 +01:00
});
2020-06-12 12:20:29 +02:00
let out_bytes = out_res?;
let out_obj = PyBytes::new(py, &out_bytes);
Ok(out_obj.into())
}
2019-12-23 05:42:14 +01:00
}
// Module definition
//////////////////////////////////
2019-12-23 05:42:14 +01:00
#[pymodule]
2021-01-31 07:11:55 +01:00
fn rsbridge(_py: Python, m: &PyModule) -> PyResult<()> {
2019-12-24 23:59:33 +01:00
m.add_class::<Backend>()?;
m.add_wrapped(wrap_pyfunction!(buildhash)).unwrap();
m.add_wrapped(wrap_pyfunction!(open_backend)).unwrap();
2019-12-23 05:42:14 +01:00
Ok(())
}