Compare commits

...

6 Commits

11 changed files with 690 additions and 6 deletions

438
Cargo.lock generated
View File

@@ -80,6 +80,8 @@ version = "1.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be714c154be609ec7f5dad223a33bf1482fff90472de28f7362806e6d4832b8c"
dependencies = [
"jobserver",
"libc",
"shlex",
]
@@ -124,6 +126,26 @@ dependencies = [
"phf_codegen",
]
[[package]]
name = "const_format"
version = "0.2.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "126f97965c8ad46d6d9163268ff28432e8f6a1196a55578867832e3049df63dd"
dependencies = [
"const_format_proc_macros",
]
[[package]]
name = "const_format_proc_macros"
version = "0.2.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d57c2eccfb16dbac1f4e61e206105db5820c9d26c3c472bc17c774259ef7744"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
@@ -188,12 +210,32 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "displaydoc"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "either"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "form_urlencoded"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
dependencies = [
"percent-encoding",
]
[[package]]
name = "futures"
version = "0.3.31"
@@ -301,6 +343,19 @@ version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "git2"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fda788993cc341f69012feba8bf45c0ba4f3291fcc08e214b4d5a7332d88aff"
dependencies = [
"bitflags",
"libc",
"libgit2-sys",
"log",
"url",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
@@ -336,6 +391,145 @@ dependencies = [
"cc",
]
[[package]]
name = "icu_collections"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526"
dependencies = [
"displaydoc",
"yoke",
"zerofrom",
"zerovec",
]
[[package]]
name = "icu_locid"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637"
dependencies = [
"displaydoc",
"litemap",
"tinystr",
"writeable",
"zerovec",
]
[[package]]
name = "icu_locid_transform"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e"
dependencies = [
"displaydoc",
"icu_locid",
"icu_locid_transform_data",
"icu_provider",
"tinystr",
"zerovec",
]
[[package]]
name = "icu_locid_transform_data"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e"
[[package]]
name = "icu_normalizer"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f"
dependencies = [
"displaydoc",
"icu_collections",
"icu_normalizer_data",
"icu_properties",
"icu_provider",
"smallvec",
"utf16_iter",
"utf8_iter",
"write16",
"zerovec",
]
[[package]]
name = "icu_normalizer_data"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516"
[[package]]
name = "icu_properties"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93d6020766cfc6302c15dbbc9c8778c37e62c14427cb7f6e601d849e092aeef5"
dependencies = [
"displaydoc",
"icu_collections",
"icu_locid_transform",
"icu_properties_data",
"icu_provider",
"tinystr",
"zerovec",
]
[[package]]
name = "icu_properties_data"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569"
[[package]]
name = "icu_provider"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9"
dependencies = [
"displaydoc",
"icu_locid",
"icu_provider_macros",
"stable_deref_trait",
"tinystr",
"writeable",
"yoke",
"zerofrom",
"zerovec",
]
[[package]]
name = "icu_provider_macros"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "idna"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "686f825264d630750a544639377bae737628043f20d38bbc029e8f29ea968a7e"
dependencies = [
"idna_adapter",
"smallvec",
"utf8_iter",
]
[[package]]
name = "idna_adapter"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daca1df1c957320b2cf139ac61e7bd64fed304c5040df000a745aa1de3b4ef71"
dependencies = [
"icu_normalizer",
"icu_properties",
]
[[package]]
name = "ijson"
version = "0.1.4"
@@ -354,6 +548,12 @@ version = "2.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd"
[[package]]
name = "is_debug"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fe266d2e243c931d8190177f20bf7f24eed45e96f39e87dc49a27b32d12d407"
[[package]]
name = "itertools"
version = "0.14.0"
@@ -369,6 +569,15 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]]
name = "jobserver"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0"
dependencies = [
"libc",
]
[[package]]
name = "js-sys"
version = "0.3.77"
@@ -391,6 +600,36 @@ version = "0.2.171"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
[[package]]
name = "libgit2-sys"
version = "0.18.0+1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1a117465e7e1597e8febea8bb0c410f1c7fb93b1e1cddf34363f8390367ffec"
dependencies = [
"cc",
"libc",
"libz-sys",
"pkg-config",
]
[[package]]
name = "libz-sys"
version = "1.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b70e7a7df205e92a1a4cd9aaae7898dac0aa555503cc0a649494d0d60e7651d"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "litemap"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23fb14cb19457329c82206317a5663005a4d404783dc74f4252769b0d5f42856"
[[package]]
name = "lock_api"
version = "0.4.12"
@@ -456,6 +695,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_threads"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "object"
version = "0.36.7"
@@ -499,6 +747,12 @@ dependencies = [
"regex",
]
[[package]]
name = "percent-encoding"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "phf"
version = "0.11.3"
@@ -549,6 +803,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "portable-atomic"
version = "1.11.0"
@@ -587,6 +847,7 @@ checksum = "7778bffd85cf38175ac1f545509665d0b9b92a198ca7941f131f85f7a4f9a872"
dependencies = [
"cfg-if",
"chrono",
"chrono-tz",
"indoc",
"libc",
"memoffset",
@@ -804,6 +1065,19 @@ dependencies = [
"serde",
]
[[package]]
name = "shadow-rs"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3672eb035a31ac62bf171765d04e3f1f01659920847384d08ac979ca6bb56763"
dependencies = [
"const_format",
"git2",
"is_debug",
"time",
"tzdb",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
@@ -852,6 +1126,7 @@ dependencies = [
"pyo3",
"pyo3-async-runtimes",
"serde_json",
"shadow-rs",
"snafu",
"strum",
"tokio",
@@ -882,6 +1157,12 @@ dependencies = [
"syn",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "strum"
version = "0.27.1"
@@ -915,6 +1196,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "target-lexicon"
version = "0.12.16"
@@ -959,7 +1251,9 @@ checksum = "dad298b01a40a23aac4580b67e3dbedb7cc8402f3592d7f49469de2ea4aecdd8"
dependencies = [
"deranged",
"itoa",
"libc",
"num-conv",
"num_threads",
"powerfmt",
"serde",
"time-core",
@@ -982,6 +1276,16 @@ dependencies = [
"time-core",
]
[[package]]
name = "tinystr"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f"
dependencies = [
"displaydoc",
"zerovec",
]
[[package]]
name = "tokio"
version = "1.44.1"
@@ -1061,6 +1365,32 @@ dependencies = [
"tracing-log",
]
[[package]]
name = "tz-rs"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1450bf2b99397e72070e7935c89facaa80092ac812502200375f1f7d33c71a1"
[[package]]
name = "tzdb"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0be2ea5956f295449f47c0b825c5e109022ff1a6a53bb4f77682a87c2341fbf5"
dependencies = [
"iana-time-zone",
"tz-rs",
"tzdb_data",
]
[[package]]
name = "tzdb_data"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0604b35c1f390a774fdb138cac75a99981078895d24bcab175987440bbff803b"
dependencies = [
"tz-rs",
]
[[package]]
name = "ulid"
version = "1.2.0"
@@ -1089,12 +1419,41 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
[[package]]
name = "url"
version = "2.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
]
[[package]]
name = "utf16_iter"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246"
[[package]]
name = "utf8_iter"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "valuable"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "wasi"
version = "0.13.3+wasi-0.2.2"
@@ -1282,6 +1641,42 @@ dependencies = [
"bitflags",
]
[[package]]
name = "write16"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936"
[[package]]
name = "writeable"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51"
[[package]]
name = "yoke"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40"
dependencies = [
"serde",
"stable_deref_trait",
"yoke-derive",
"zerofrom",
]
[[package]]
name = "yoke-derive"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zerocopy"
version = "0.8.23"
@@ -1301,3 +1696,46 @@ dependencies = [
"quote",
"syn",
]
[[package]]
name = "zerofrom"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5"
dependencies = [
"zerofrom-derive",
]
[[package]]
name = "zerofrom-derive"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zerovec"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa2b893d79df23bfb12d5461018d408ea19dfafe76c2c7ef6d4eba614f8ff079"
dependencies = [
"yoke",
"zerofrom",
"zerovec-derive",
]
[[package]]
name = "zerovec-derive"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@@ -20,9 +20,10 @@ derive_more = { version = "2.0.1", features = [
] }
ijson = "0.1.4"
itertools = "0.14.0"
pyo3 = { version = "0.23.0", features = ["chrono"] }
pyo3 = { version = "0.23.0", features = ["chrono", "chrono-tz"] }
pyo3-async-runtimes = { version = "0.23.0", features = ["tokio-runtime"] }
serde_json = "1.0.140"
shadow-rs = { version = "1.0.1", default-features = false }
snafu = "0.8.5"
strum = { version = "0.27.1", features = ["derive"] }
tokio = { version = "1.32.0", features = ["rt", "rt-multi-thread", "time"] }
@@ -30,3 +31,6 @@ tracing = "0.1.37"
tracing-appender = "0.2.3"
tracing-subscriber = "0.3.17"
ulid = "1.2.0"
[build-dependencies]
shadow-rs = "1.0.1"

8
build.rs Normal file
View File

@@ -0,0 +1,8 @@
use shadow_rs::{BuildPattern, ShadowBuilder};
fn main() {
ShadowBuilder::builder()
.build_pattern(BuildPattern::RealTime)
.build()
.unwrap();
}

View File

@@ -4,6 +4,7 @@ use ijson::{IArray, INumber, IObject, IString, IValue};
use pyo3::{
exceptions::{PyTypeError, PyValueError},
prelude::*,
types::{PyList, PyNone},
};
use snafu::Snafu;
@@ -89,3 +90,24 @@ impl<'py> FromPyObject<'py> for Arbitrary {
}
}
}
impl<'py> IntoPyObject<'py> for Arbitrary {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self {
Arbitrary::Null => Ok(PyNone::get(py).to_owned().into_any()),
Arbitrary::Bool(b) => Ok(b.into_pyobject(py)?.to_owned().into_any()),
Arbitrary::Integer(i) => Ok(i.into_pyobject(py)?.into_any()),
Arbitrary::Float(finite_f64) => Ok(finite_f64.into_pyobject(py)?.into_any()),
Arbitrary::String(s) => Ok(s.into_pyobject(py)?.into_any()),
Arbitrary::Array(vec) => Ok(PyList::new(py, vec)?.into_any()),
Arbitrary::Map(map) => Ok(map.into_pyobject(py)?.into_any()),
Arbitrary::DateTime(date_time) => Ok(date_time.into_pyobject(py)?.into_any()),
}
}
}

View File

@@ -1,6 +1,7 @@
use pyo3::IntoPyObject;
use snafu::Snafu;
#[derive(Debug, Clone, derive_more::Into)]
#[derive(Debug, Clone, derive_more::Into, IntoPyObject)]
pub struct FiniteF64(f64);
#[derive(Debug, Snafu)]

View File

@@ -4,12 +4,12 @@ use pyo3::prelude::*;
use super::{arbitrary::Arbitrary, map_key::MapKey};
#[derive(Debug, Clone, derive_more::From, derive_more::Into)]
#[derive(Debug, Clone, Default, derive_more::From, derive_more::Into, IntoPyObject)]
pub struct Map(pub BTreeMap<MapKey, Arbitrary>);
impl<'py> FromPyObject<'py> for Map {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let inner: BTreeMap<MapKey, Arbitrary> = ob.extract()?;
let inner = ob.extract()?;
Ok(Self(inner))
}

View File

@@ -4,7 +4,11 @@ use chrono::DateTime;
use chrono_tz::Tz;
use ijson::IString;
use itertools::Itertools;
use pyo3::{exceptions::PyTypeError, prelude::*, types::PyNone};
use pyo3::{
exceptions::PyTypeError,
prelude::*,
types::{PyNone, PyTuple},
};
use super::arbitrary::{Arbitrary, MapKeyFromArbitraryError};
@@ -58,6 +62,25 @@ impl<'py> FromPyObject<'py> for MapKey {
}
}
impl<'py> IntoPyObject<'py> for MapKey {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self {
MapKey::Null => Ok(PyNone::get(py).to_owned().into_any()),
MapKey::Bool(b) => Ok(b.into_pyobject(py)?.to_owned().into_any()),
MapKey::Integer(i) => Ok(i.into_pyobject(py)?.into_any()),
MapKey::String(s) => Ok(s.into_pyobject(py)?.into_any()),
MapKey::Tuple(vec) => Ok(PyTuple::new(py, vec)?.into_any()),
MapKey::DateTime(date_time) => Ok(date_time.into_pyobject(py)?.into_any()),
}
}
}
impl From<MapKey> for IString {
fn from(map_key: MapKey) -> Self {
Self::from(map_key.to_string())

View File

@@ -0,0 +1,180 @@
use pyo3::{prelude::*, types::PyTuple};
use crate::{
arbitrary::{arbitrary::Arbitrary, map::Map},
python_utils::{detach, validate_type_by_name},
};
#[derive(Debug)]
pub struct HassLogger(Py<PyAny>);
impl<'source> FromPyObject<'source> for HassLogger {
fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult<Self> {
// region: Validation
validate_type_by_name(ob, "HassLogger")?;
// endregion: Validation
Ok(Self(detach(ob)))
}
}
#[derive(Debug, Clone, IntoPyObject)]
pub struct LogData<ExcInfo> {
/// If exc_info does not evaluate as false, it causes exception information to be added to the logging message.
/// If an exception tuple (in the format returned by sys.exc_info()) or an exception instance is provided, it is used;
/// otherwise, sys.exc_info() is called to get the exception information.
exc_info: Option<ExcInfo>,
/// If true, stack information is added to the logging message, including the actual logging call.
/// Note that this is not the same stack information as that displayed through specifying exc_info:
/// The former is stack frames from the bottom of the stack up to the logging call in the current thread,
/// whereas the latter is information about stack frames which have been unwound,
/// following an exception, while searching for exception handlers.
///
/// You can specify stack_info independently of exc_info,
/// e.g. to just show how you got to a certain point in your code, even when no exceptions were raised.
/// The stack frames are printed following a header line which says:
///
/// Stack (most recent call last):
///
/// This mimics the `Traceback (most recent call last):` which is used when displaying exception frames.
stack_info: bool,
/// If greater than 1, the corresponding number of stack frames are skipped
/// when computing the line number and function name set in the LogRecord created for the logging event.
/// This can be used in logging helpers so that the function name, filename and line number recorded
/// are not the information for the helper function/method, but rather its caller.
stacklevel: u16,
/// This can be used to pass a dictionary which is used to populate the __dict__ of the LogRecord
/// created for the logging event with user-defined attributes.
/// These custom attributes can then be used as you like.
/// For example, they could be incorporated into logged messages.
extra: Map,
}
impl HassLogger {
pub fn new(py: Python<'_>, name: &str) -> PyResult<Self> {
let logging = py.import("logging")?;
let logger = logging.call_method1("getLogger", (name,))?;
Ok(logger.extract()?)
}
pub fn debug<'py, ExcInfo: IntoPyObject<'py>>(
&self,
py: Python<'py>,
msg: &str,
args: Vec<Arbitrary>,
log_data: Option<LogData<ExcInfo>>,
) -> PyResult<()> {
let mut all_args = vec![msg.into_pyobject(py)?.into_any()];
for arg in args {
let arg = arg.into_pyobject(py)?;
all_args.push(arg);
}
let all_args = PyTuple::new(py, all_args)?;
let kwargs = log_data
.map(|log_data| log_data.into_pyobject(py))
.transpose()?;
self.0.call_method(py, "debug", all_args, kwargs.as_ref())?;
Ok(())
}
pub fn info<'py, ExcInfo: IntoPyObject<'py>>(
&self,
py: Python<'py>,
msg: &str,
args: Vec<Arbitrary>,
log_data: Option<LogData<ExcInfo>>,
) -> PyResult<()> {
let mut all_args = vec![msg.into_pyobject(py)?.into_any()];
for arg in args {
let arg = arg.into_pyobject(py)?;
all_args.push(arg);
}
let all_args = PyTuple::new(py, all_args)?;
let kwargs = log_data
.map(|log_data| log_data.into_pyobject(py))
.transpose()?;
self.0.call_method(py, "info", all_args, kwargs.as_ref())?;
Ok(())
}
pub fn warning<'py, ExcInfo: IntoPyObject<'py>>(
&self,
py: Python<'py>,
msg: &str,
args: Vec<Arbitrary>,
log_data: Option<LogData<ExcInfo>>,
) -> PyResult<()> {
let mut all_args = vec![msg.into_pyobject(py)?.into_any()];
for arg in args {
let arg = arg.into_pyobject(py)?;
all_args.push(arg);
}
let all_args = PyTuple::new(py, all_args)?;
let kwargs = log_data
.map(|log_data| log_data.into_pyobject(py))
.transpose()?;
self.0
.call_method(py, "warning", all_args, kwargs.as_ref())?;
Ok(())
}
pub fn error<'py, ExcInfo: IntoPyObject<'py>>(
&self,
py: Python<'py>,
msg: &str,
args: Vec<Arbitrary>,
log_data: Option<LogData<ExcInfo>>,
) -> PyResult<()> {
let mut all_args = vec![msg.into_pyobject(py)?.into_any()];
for arg in args {
let arg = arg.into_pyobject(py)?;
all_args.push(arg);
}
let all_args = PyTuple::new(py, all_args)?;
let kwargs = log_data
.map(|log_data| log_data.into_pyobject(py))
.transpose()?;
self.0.call_method(py, "error", all_args, kwargs.as_ref())?;
Ok(())
}
pub fn critical<'py, ExcInfo: IntoPyObject<'py>>(
&self,
py: Python<'py>,
msg: &str,
args: Vec<Arbitrary>,
log_data: Option<LogData<ExcInfo>>,
) -> PyResult<()> {
let mut all_args = vec![msg.into_pyobject(py)?.into_any()];
for arg in args {
let arg = arg.into_pyobject(py)?;
all_args.push(arg);
}
let all_args = PyTuple::new(py, all_args)?;
let kwargs = log_data
.map(|log_data| log_data.into_pyobject(py))
.transpose()?;
self.0
.call_method(py, "critical", all_args, kwargs.as_ref())?;
Ok(())
}
}

View File

@@ -2,6 +2,7 @@ pub mod domain;
pub mod entity_id;
pub mod event;
pub mod home_assistant;
pub mod logger;
pub mod object_id;
pub mod state;
pub mod state_machine;

View File

@@ -7,7 +7,7 @@ use snafu::Snafu;
pub struct ObjectId(Arc<str>);
#[derive(Debug, Clone, Snafu)]
#[snafu(display("expected a lowercase ASCII alphabetical character (i.e. a through z) or an underscore (i.e. _) but encountered {encountered}"))]
#[snafu(display("expected a lowercase ASCII alphabetical character (i.e. a through z) or a digit (i.e. 0 through 9) or an underscore (i.e. _) but encountered {encountered}"))]
pub struct ObjectIdParsingError {
encountered: char,
}
@@ -19,6 +19,7 @@ impl FromStr for ObjectId {
for c in s.chars() {
match c {
'a'..='z' => {}
'0'..='9' => {}
'_' => {}
_ => return Err(ObjectIdParsingError { encountered: c }),
}

View File

@@ -2,6 +2,7 @@ use std::time::Duration;
use home_assistant::home_assistant::HomeAssistant;
use pyo3::prelude::*;
use shadow_rs::shadow;
use tokio::time::interval;
use tracing::Level;
use tracing_subscriber::fmt::format::FmtSpan;
@@ -10,6 +11,8 @@ mod arbitrary;
mod home_assistant;
mod python_utils;
shadow!(build_info);
async fn real_main(home_assistant: HomeAssistant) -> ! {
tracing_subscriber::fmt()
.with_max_level(Level::TRACE)
@@ -17,6 +20,9 @@ async fn real_main(home_assistant: HomeAssistant) -> ! {
.pretty()
.init();
let built_at = build_info::BUILD_TIME;
tracing::info!(built_at);
let duration = Duration::from_millis(5900);
let mut interval = interval(duration);