Skip to content

Commit cb7b3cb

Browse files
committed
feature: streamson script added
1 parent fe38763 commit cb7b3cb

13 files changed

Lines changed: 402 additions & 160 deletions

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ keywords = ["json", "python", "splitter"]
1010
repository = "https://github.com/shenek/python-streamson"
1111
categories = ["parsing"]
1212

13+
[package.metadata.maturin.scripts]
14+
streamson = "streamson:__main__.main"
15+
1316
[package.metadata.maturin]
1417
classifier = [
1518
"Development Status :: 4 - Beta",

src/convert.rs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,7 @@
1-
use super::{RustMatcher, StreamsonError};
2-
use pyo3::{prelude::*, types::PyBytes};
1+
use super::{PythonConverter, RustMatcher, StreamsonError};
2+
use pyo3::prelude::*;
33
use std::sync::{Arc, Mutex};
4-
use streamson_lib::{error, handler, path::Path, strategy};
5-
6-
/// TODO
7-
#[pyclass]
8-
#[derive(Clone)]
9-
pub struct PythonConverter {
10-
callable: PyObject,
11-
use_path: bool,
12-
}
13-
14-
#[pymethods]
15-
impl PythonConverter {
16-
/// Create instance of PythonConverter
17-
///
18-
/// # Arguments
19-
/// * `callable` - python callable (3 arguments)
20-
#[new]
21-
pub fn new(callable: PyObject, use_path: bool) -> Self {
22-
Self { callable, use_path }
23-
}
24-
}
25-
26-
impl handler::Handler for PythonConverter {
27-
fn handle(
28-
&mut self,
29-
path: &Path,
30-
matcher_idx: usize,
31-
data: Option<&[u8]>,
32-
) -> Result<Option<Vec<u8>>, error::Handler> {
33-
let gil = Python::acquire_gil();
34-
let res = self
35-
.callable
36-
.call1(
37-
gil.python(),
38-
(
39-
if self.use_path {
40-
Some(path.to_string())
41-
} else {
42-
None
43-
},
44-
matcher_idx,
45-
data.unwrap().to_vec(),
46-
),
47-
)
48-
.map_err(|_| error::Handler::new("Failed to call handler function"))?;
49-
let bytes = res.cast_as::<PyBytes>(gil.python()).unwrap();
50-
Ok(FromPyObject::extract(bytes)
51-
.map_err(|_| error::Handler::new("Function does not return bytes."))?)
52-
}
53-
}
4+
use streamson_lib::{handler, strategy};
545

556
/// Low level Python wrapper for Convert strategy
567
#[pyclass]

src/handler.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
use pyo3::{prelude::*, types::PyBytes};
2+
use streamson_lib::{error, handler, path::Path};
3+
4+
/// Streamson handler which performs data conversion
5+
#[pyclass]
6+
#[derive(Clone)]
7+
pub struct PythonConverter {
8+
callable: PyObject,
9+
use_path: bool,
10+
}
11+
12+
#[pymethods]
13+
impl PythonConverter {
14+
/// Create instance of PythonConverter
15+
///
16+
/// # Arguments
17+
/// * `callable` - python callable (3 arguments)
18+
#[new]
19+
pub fn new(callable: PyObject, use_path: bool) -> Self {
20+
Self { callable, use_path }
21+
}
22+
}
23+
24+
impl handler::Handler for PythonConverter {
25+
fn handle(
26+
&mut self,
27+
path: &Path,
28+
matcher_idx: usize,
29+
data: Option<&[u8]>,
30+
) -> Result<Option<Vec<u8>>, error::Handler> {
31+
let gil = Python::acquire_gil();
32+
let res = self
33+
.callable
34+
.call1(
35+
gil.python(),
36+
(
37+
if self.use_path {
38+
Some(path.to_string())
39+
} else {
40+
None
41+
},
42+
matcher_idx,
43+
data.unwrap().to_vec(),
44+
),
45+
)
46+
.map_err(|_| error::Handler::new("Failed to call handler function"))?;
47+
let bytes = res.cast_as::<PyBytes>(gil.python()).unwrap();
48+
Ok(FromPyObject::extract(bytes)
49+
.map_err(|_| error::Handler::new("Function does not return bytes."))?)
50+
}
51+
}
52+
53+
/// Streamson handler which calls python callable
54+
#[pyclass]
55+
#[derive(Clone)]
56+
pub struct PythonHandler {
57+
callable: PyObject,
58+
require_path: bool,
59+
}
60+
61+
#[pymethods]
62+
impl PythonHandler {
63+
/// Create instance of PythonHandler
64+
///
65+
/// # Arguments
66+
/// * `callable` - python callable (3 arguments)
67+
/// * `require_path` - should path be passed to handler
68+
#[new]
69+
pub fn new(callable: PyObject, require_path: bool) -> Self {
70+
Self {
71+
callable,
72+
require_path,
73+
}
74+
}
75+
}
76+
77+
impl handler::Handler for PythonHandler {
78+
/// Call python function as a part of rust handler
79+
///
80+
/// # Arguments
81+
/// * `path` - matched path
82+
/// * `matcher_idx` - index of triggered matcher
83+
/// * `data` - matched data
84+
fn handle(
85+
&mut self,
86+
path: &Path,
87+
matcher_idx: usize,
88+
data: Option<&[u8]>,
89+
) -> Result<Option<Vec<u8>>, error::Handler> {
90+
let gil = Python::acquire_gil();
91+
self.callable
92+
.call1(
93+
gil.python(),
94+
(
95+
if self.require_path {
96+
Some(path.to_string())
97+
} else {
98+
None
99+
},
100+
matcher_idx,
101+
data,
102+
),
103+
)
104+
.map_err(|_| error::Handler::new("Calling python failed"))?;
105+
Ok(None)
106+
}
107+
}

src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
pub mod convert;
22
pub mod extract;
33
pub mod filter;
4+
pub mod handler;
45
pub mod trigger;
56

6-
pub use convert::{Convert, PythonConverter};
7+
pub use convert::Convert;
78
pub use extract::Extract;
89
pub use filter::Filter;
9-
pub use trigger::{PythonHandler, Trigger};
10+
pub use handler::{PythonConverter, PythonHandler};
11+
pub use trigger::Trigger;
1012

1113
use pyo3::{class::PyNumberProtocol, create_exception, exceptions, prelude::*};
12-
14+
use std::str::FromStr;
1315
use streamson_lib::{error, matcher};
1416

1517
create_exception!(streamson, StreamsonError, exceptions::ValueError);
@@ -45,20 +47,21 @@ impl RustMatcher {
4547
pub fn simple(path: String) -> PyResult<Self> {
4648
Ok(Self {
4749
inner: matcher::Combinator::new(
48-
matcher::Simple::new(&path).map_err(StreamsonError::from)?,
50+
matcher::Simple::from_str(&path).map_err(StreamsonError::from)?,
4951
),
5052
})
5153
}
5254

5355
/// Create a new instance of depth matcher
5456
///
5557
/// # Arguments
56-
/// * `min_depth` - min depth
57-
/// * `max_depth` - max depth (Optional)
58+
/// * `depth_str` - string which can be parsed to depth matcher
5859
#[staticmethod]
59-
pub fn depth(min_depth: usize, max_depth: Option<usize>) -> PyResult<Self> {
60+
pub fn depth(depth_str: String) -> PyResult<Self> {
6061
Ok(Self {
61-
inner: matcher::Combinator::new(matcher::Depth::new(min_depth, max_depth)),
62+
inner: matcher::Combinator::new(
63+
matcher::Depth::from_str(&depth_str).map_err(StreamsonError::from)?,
64+
),
6265
})
6366
}
6467
}

src/trigger.rs

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,7 @@
1-
use super::{RustMatcher, StreamsonError};
1+
use super::{PythonHandler, RustMatcher, StreamsonError};
22
use pyo3::prelude::*;
33
use std::sync::{Arc, Mutex};
4-
use streamson_lib::{error, handler, path::Path, strategy};
5-
6-
/// Streamson handler which calls python callable
7-
#[pyclass]
8-
#[derive(Clone)]
9-
pub struct PythonHandler {
10-
callable: PyObject,
11-
require_path: bool,
12-
}
13-
14-
#[pymethods]
15-
impl PythonHandler {
16-
/// Create instance of PythonHandler
17-
///
18-
/// # Arguments
19-
/// * `callable` - python callable (3 arguments)
20-
/// * `require_path` - should path be passed to handler
21-
#[new]
22-
pub fn new(callable: PyObject, require_path: bool) -> Self {
23-
Self {
24-
callable,
25-
require_path,
26-
}
27-
}
28-
}
29-
30-
impl handler::Handler for PythonHandler {
31-
/// Call python function as a part of rust handler
32-
///
33-
/// # Arguments
34-
/// * `path` - matched path
35-
/// * `matcher_idx` - index of triggered matcher
36-
/// * `data` - matched data
37-
fn handle(
38-
&mut self,
39-
path: &Path,
40-
matcher_idx: usize,
41-
data: Option<&[u8]>,
42-
) -> Result<Option<Vec<u8>>, error::Handler> {
43-
let gil = Python::acquire_gil();
44-
self.callable
45-
.call1(
46-
gil.python(),
47-
(
48-
if self.require_path {
49-
Some(path.to_string())
50-
} else {
51-
None
52-
},
53-
matcher_idx,
54-
data,
55-
),
56-
)
57-
.map_err(|_| error::Handler::new("Calling python failed"))?;
58-
Ok(None)
59-
}
60-
}
4+
use streamson_lib::{handler, strategy};
615

626
/// Low level Python wrapper for Trigger strategy
637
#[pyclass]

0 commit comments

Comments
 (0)