perspective_python/
lib.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13#![doc = include_str!("../docs/lib.md")]
14#![warn(unstable_features)]
15
16mod client;
17pub(crate) mod py_async;
18mod py_err;
19mod server;
20
21pub use client::client_sync::{Client, Table, View};
22pub use client::proxy_session::ProxySession;
23use py_err::PyPerspectiveError;
24use pyo3::prelude::*;
25use tracing_subscriber::layer::SubscriberExt;
26use tracing_subscriber::util::SubscriberInitExt;
27use tracing_subscriber::{EnvFilter, fmt};
28
29/// Create a tracing filter which mimics the default behavior of reading from
30/// env, customized to exclude timestamp.
31/// [`tracing` filter docs](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html#per-layer-filtering)
32fn init_tracing() {
33    let fmt_layer = fmt::layer().without_time().with_target(true);
34    let filter_layer = EnvFilter::try_from_default_env()
35        .or_else(|_| EnvFilter::try_new("info"))
36        .unwrap();
37
38    tracing_subscriber::registry()
39        .with(filter_layer)
40        .with(fmt_layer)
41        .init();
42}
43
44#[pyfunction]
45fn num_cpus() -> i32 {
46    perspective_server::num_cpus()
47}
48
49#[pyfunction]
50fn set_num_cpus(num_cpus: i32) {
51    perspective_server::set_num_cpus(num_cpus)
52}
53
54/// Perspective Python main module.
55#[pymodule]
56fn perspective(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
57    init_tracing();
58    m.add_class::<client::client_sync::Client>()?;
59    m.add_class::<server::PyServer>()?;
60    m.add_class::<server::PyAsyncServer>()?;
61    m.add_class::<server::PySession>()?;
62    m.add_class::<server::PyAsyncSession>()?;
63    m.add_class::<client::client_sync::Table>()?;
64    m.add_class::<client::client_sync::View>()?;
65    m.add_class::<client::client_async::AsyncClient>()?;
66    m.add_class::<client::client_async::AsyncTable>()?;
67    m.add_class::<client::client_async::AsyncView>()?;
68    m.add_class::<client::proxy_session::ProxySession>()?;
69    m.add("PerspectiveError", py.get_type::<PyPerspectiveError>())?;
70    m.add_function(wrap_pyfunction!(num_cpus, m)?)?;
71    m.add_function(wrap_pyfunction!(set_num_cpus, m)?)?;
72    Ok(())
73}