-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathpython-module.rs
More file actions
84 lines (66 loc) · 2.54 KB
/
Copy pathpython-module.rs
File metadata and controls
84 lines (66 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright © 2021 Adrian <adrian.eddy at gmail>
use pyo3::prelude::*;
use std::collections::BTreeMap;
use pythonize::pythonize;
use std::sync::{ Arc, atomic::AtomicBool };
use ::telemetry_parser::*;
#[pyclass]
struct Parser {
#[pyo3(get, set)]
camera: Option<String>,
#[pyo3(get, set)]
model: Option<String>,
input: Input
}
#[pymethods]
impl Parser {
#[new]
fn new(path: &str) -> PyResult<Self> {
let mut stream = std::fs::File::open(&path)?;
let filesize = stream.metadata()?.len() as usize;
let input = Input::from_stream(&mut stream, filesize, &path, |_|(), Arc::new(AtomicBool::new(false)))?;
Ok(Self {
camera: Some(input.camera_type()),
model: input.camera_model().map(String::clone),
input: input,
})
}
fn telemetry(&self, human_readable: Option<bool>) -> PyResult<Py<PyAny>> {
if self.input.samples.is_none() { return Err(pyo3::exceptions::PyValueError::new_err("No metadata")); }
let samples = self.input.samples.as_ref().unwrap();
let mut output = Vec::with_capacity(samples.len());
for info in samples {
if info.tag_map.is_none() { continue; }
let mut groups = BTreeMap::new();
let groups_map = info.tag_map.as_ref().unwrap();
for (group, map) in groups_map {
let group_map = groups.entry(group).or_insert_with(BTreeMap::new);
for (tagid, info) in map {
let value = if human_readable.unwrap_or(false) {
serde_json::to_value(info.value.to_string())
} else {
serde_json::to_value(info.value.clone())
}.unwrap();
group_map.insert(tagid, value);
}
}
output.push(groups);
}
Python::with_gil(|py| {
Ok(pythonize(py, &output)?)
})
}
fn normalized_imu(&self, orientation: Option<String>) -> PyResult<Py<PyAny>> {
if self.input.samples.is_none() { return Err(pyo3::exceptions::PyValueError::new_err("No metadata")); }
let imu_data = util::normalized_imu(&self.input, orientation)?;
Python::with_gil(|py| {
Ok(pythonize(py, &imu_data)?)
})
}
}
#[pymodule]
fn telemetry_parser(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Parser>()?;
Ok(())
}