python module
parent
e4b9df3f78
commit
d46e1e3141
@ -0,0 +1,72 @@
|
|||||||
|
/target
|
||||||
|
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
.pytest_cache/
|
||||||
|
*.py[cod]
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
.venv/
|
||||||
|
env/
|
||||||
|
bin/
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
include/
|
||||||
|
man/
|
||||||
|
venv/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
pip-selfcheck.json
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.coverage
|
||||||
|
.cache
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
|
||||||
|
# Mr Developer
|
||||||
|
.mr.developer.cfg
|
||||||
|
.project
|
||||||
|
.pydevproject
|
||||||
|
|
||||||
|
# Rope
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
*.log
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyCharm
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# VSCode
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
# Pyenv
|
||||||
|
.python-version
|
@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "cozo_py_module"
|
||||||
|
version = "0.1.1"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
[lib]
|
||||||
|
name = "cozo_py_module"
|
||||||
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
pyo3 = { version = "0.17.1", features = ["extension-module"] }
|
||||||
|
cozo = { version = "0.1.1", path = ".." }
|
||||||
|
miette = { version = "=5.3.0", features = ["fancy"] }
|
||||||
|
serde_json = "1.0.81"
|
@ -0,0 +1,14 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["maturin>=0.13,<0.14"]
|
||||||
|
build-backend = "maturin"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "python"
|
||||||
|
requires-python = ">=3.7"
|
||||||
|
classifiers = [
|
||||||
|
"Programming Language :: Rust",
|
||||||
|
"Programming Language :: Python :: Implementation :: CPython",
|
||||||
|
"Programming Language :: Python :: Implementation :: PyPy",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022, The Cozo Project Authors. Licensed under MIT/Apache-2.0/BSD-3-Clause.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use miette::miette;
|
||||||
|
use pyo3::exceptions::PyException;
|
||||||
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
|
use cozo::Db;
|
||||||
|
|
||||||
|
#[pyclass(extends=PyException)]
|
||||||
|
struct ErrorBridge(cozo::Error);
|
||||||
|
trait PyResultExt<T> {
|
||||||
|
fn into_py_res(self) -> PyResult<T>;
|
||||||
|
}
|
||||||
|
impl<T> PyResultExt<T> for miette::Result<T> {
|
||||||
|
fn into_py_res(self) -> PyResult<T> {
|
||||||
|
match self {
|
||||||
|
Ok(t) => Ok(t),
|
||||||
|
Err(e) => Err(PyException::new_err(format!("{:?}", e))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[pyclass]
|
||||||
|
struct CozoDbPy {
|
||||||
|
db: Db,
|
||||||
|
}
|
||||||
|
#[pymethods]
|
||||||
|
impl CozoDbPy {
|
||||||
|
#[new]
|
||||||
|
#[args(create_if_missing = true, destroy_on_exit = false)]
|
||||||
|
fn new(path: &str) -> PyResult<Self> {
|
||||||
|
let db = Db::new(path).into_py_res()?;
|
||||||
|
Ok(Self { db })
|
||||||
|
}
|
||||||
|
pub fn run_query(&self, py: Python<'_>, query: &str, params: &str) -> PyResult<String> {
|
||||||
|
let params_map: serde_json::Value = serde_json::from_str(params)
|
||||||
|
.map_err(|_| miette!("the given params is not valid JSON"))
|
||||||
|
.into_py_res()?;
|
||||||
|
let params_arg: BTreeMap<_, _> = match params_map {
|
||||||
|
serde_json::Value::Object(m) => m.into_iter().collect(),
|
||||||
|
_ => Err(miette!("the given params is not a map")).into_py_res()?,
|
||||||
|
};
|
||||||
|
let ret = py.allow_threads(|| self.db.run_script(query, ¶ms_arg).into_py_res())?;
|
||||||
|
Ok(ret.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[pymodule]
|
||||||
|
fn cozo_py_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||||
|
m.add_class::<CozoDbPy>()?;
|
||||||
|
m.add_class::<ErrorBridge>()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue