adapt python lib to new API
parent
01f508188d
commit
9823fc4476
@ -0,0 +1,69 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- master
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
linux:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: messense/maturin-action@v1
|
||||
with:
|
||||
manylinux: auto
|
||||
command: build
|
||||
args: --release --sdist -o dist --find-interpreter
|
||||
- name: Upload wheels
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: wheels
|
||||
path: dist
|
||||
|
||||
windows:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: messense/maturin-action@v1
|
||||
with:
|
||||
command: build
|
||||
args: --release -o dist --find-interpreter
|
||||
- name: Upload wheels
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: wheels
|
||||
path: dist
|
||||
|
||||
macos:
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: messense/maturin-action@v1
|
||||
with:
|
||||
command: build
|
||||
args: --release -o dist --universal2 --find-interpreter
|
||||
- name: Upload wheels
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: wheels
|
||||
path: dist
|
||||
|
||||
release:
|
||||
name: Release
|
||||
runs-on: ubuntu-latest
|
||||
if: "startsWith(github.ref, 'refs/tags/')"
|
||||
needs: [ macos, windows, linux ]
|
||||
steps:
|
||||
- uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: wheels
|
||||
- name: Publish to PyPI
|
||||
uses: messense/maturin-action@v1
|
||||
env:
|
||||
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
||||
with:
|
||||
command: upload
|
||||
args: --skip-existing *
|
@ -0,0 +1,73 @@
|
||||
/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
|
||||
_test_db/
|
@ -0,0 +1,43 @@
|
||||
[package]
|
||||
name = "cozo_py_module"
|
||||
version = "0.1.7"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
name = "cozo_embedded"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[features]
|
||||
#! # Features
|
||||
|
||||
## Enables the `minimal`, `requests` and `graph-algo` features
|
||||
compact = ["minimal", "requests", "graph-algo", "rayon"]
|
||||
## Enables the `storage-sqlite` and `graph-algo` features
|
||||
mobile = ["storage-sqlite", "graph-algo", "rayon"]
|
||||
## Enables the `minimal`, `requests` and `graph-algo` features in single threaded mode
|
||||
compact-single-threaded = ["minimal", "requests", "graph-algo"]
|
||||
## Enables the `storage-sqlite` feature
|
||||
minimal = ["storage-sqlite"]
|
||||
## Enables the [Sqlite](https://www.sqlite.org/index.html) backend, also allows backup and restore with Sqlite data files.
|
||||
storage-sqlite = ["cozo/storage-sqlite"]
|
||||
## Enables the [RocksDB](http://rocksdb.org/) backend
|
||||
storage-rocksdb = ["cozo/storage-rocksdb"]
|
||||
## Enables the graph algorithms
|
||||
graph-algo = ["cozo/graph-algo"]
|
||||
## Allows the utilities to make web requests to fetch data
|
||||
requests = ["cozo/requests"]
|
||||
## Uses jemalloc as the global allocator, can make a difference in performance
|
||||
jemalloc = ["cozo/jemalloc"]
|
||||
## Enables io-uring option for the RocksDB storage
|
||||
io-uring = ["cozo/io-uring"]
|
||||
## Allows threading and enables the use of the `rayon` library for parallelizing algorithms
|
||||
rayon = ["cozo/rayon"]
|
||||
## Disallows the use of threads
|
||||
nothread = ["cozo/nothread"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
pyo3 = { version = "0.17.1", features = ["extension-module", "abi3", "abi3-py37"] }
|
||||
cozo = { version = "0.1.7", path = "../cozo-core", default-features = false }
|
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Ziyang Hu
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -0,0 +1,14 @@
|
||||
# cozo-lib-python
|
||||
|
||||
[![pypi](https://img.shields.io/pypi/v/cozo_embedded)](https://pypi.org/project/cozo_embedded/)
|
||||
|
||||
Native bindings for embedding [CozoDB](https://github.com/cozodb/cozo) in Python, providing the
|
||||
`cozo_embedded` package.
|
||||
|
||||
You are not supposed to be using this package directly in your code. Use [PyCozo](https://github.com/cozodb/pycozo),
|
||||
which depends on this package.
|
||||
|
||||
To build this package, you need to install the Rust toolchain
|
||||
as well as the [maturin](https://github.com/PyO3/maturin) python package.
|
||||
Refer maturin's docs for how to [develop](https://www.maturin.rs/develop.html)
|
||||
and [build](https://www.maturin.rs/distribution.html) this package.
|
@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
PYO3_NO_PYTHON=1 maturin build --release --strip
|
@ -0,0 +1,15 @@
|
||||
[build-system]
|
||||
requires = ["maturin>=0.13,<0.14"]
|
||||
build-backend = "maturin"
|
||||
|
||||
[project]
|
||||
name = "cozo_embedded"
|
||||
requires-python = ">=3.7"
|
||||
classifiers = [
|
||||
"Topic :: Database",
|
||||
"Programming Language :: Rust",
|
||||
"Programming Language :: Python :: Implementation :: CPython",
|
||||
"Programming Language :: Python :: Implementation :: PyPy",
|
||||
]
|
||||
|
||||
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2022, The Cozo Project Authors. Licensed under MIT/Apache-2.0/BSD-3-Clause.
|
||||
*/
|
||||
|
||||
use pyo3::exceptions::PyException;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use cozo::*;
|
||||
|
||||
#[pyclass]
|
||||
struct CozoDbPy {
|
||||
db: Option<DbInstance>,
|
||||
}
|
||||
|
||||
const DB_CLOSED_MSG: &str = r##"{"ok":false,"message":"database closed"}"##;
|
||||
|
||||
#[pymethods]
|
||||
impl CozoDbPy {
|
||||
#[new]
|
||||
fn new(kind: &str, path: &str) -> PyResult<Self> {
|
||||
match DbInstance::new(kind, path, Default::default()) {
|
||||
Ok(db) => Ok(Self { db: Some(db) }),
|
||||
Err(err) => Err(PyException::new_err(format!("{:?}", err))),
|
||||
}
|
||||
}
|
||||
pub fn run_query(&self, py: Python<'_>, query: &str, params: &str) -> String {
|
||||
if let Some(db) = &self.db {
|
||||
py.allow_threads(|| db.run_script_str(query, params))
|
||||
} else {
|
||||
DB_CLOSED_MSG.to_string()
|
||||
}
|
||||
}
|
||||
pub fn export_relations(&self, py: Python<'_>, rels: &str) -> String {
|
||||
if let Some(db) = &self.db {
|
||||
py.allow_threads(|| db.export_relations_str(rels))
|
||||
} else {
|
||||
DB_CLOSED_MSG.to_string()
|
||||
}
|
||||
}
|
||||
pub fn import_relation(&self, py: Python<'_>, data: &str) -> String {
|
||||
if let Some(db) = &self.db {
|
||||
py.allow_threads(|| db.import_relation_str(data))
|
||||
} else {
|
||||
DB_CLOSED_MSG.to_string()
|
||||
}
|
||||
}
|
||||
pub fn backup(&self, py: Python<'_>, path: &str) -> String {
|
||||
if let Some(db) = &self.db {
|
||||
py.allow_threads(|| db.backup_db_str(path))
|
||||
} else {
|
||||
DB_CLOSED_MSG.to_string()
|
||||
}
|
||||
}
|
||||
pub fn restore(&self, py: Python<'_>, path: &str) -> String {
|
||||
if let Some(db) = &self.db {
|
||||
py.allow_threads(|| db.restore_backup_str(path))
|
||||
} else {
|
||||
DB_CLOSED_MSG.to_string()
|
||||
}
|
||||
}
|
||||
pub fn close(&mut self) -> bool {
|
||||
self.db.take().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
fn cozo_embedded(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<CozoDbPy>()?;
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue