Skip to content

Commit

Permalink
exception handling....
Browse files Browse the repository at this point in the history
  • Loading branch information
jessekrubin committed Nov 7, 2023
1 parent 3f4861a commit 435c28c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
36 changes: 24 additions & 12 deletions crates/utiles-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ pub struct Cli {

// debug flag
#[arg(
long,
short,
global = true,
default_value = "false",
help = "debug mode"
long,
short,
global = true,
default_value = "false",
help = "debug mode"
)]
debug: bool,
// #[command(flatten , help="verbosity level (-v, -vv, -vvv, -vvvv)" )]
Expand All @@ -100,7 +100,7 @@ pub enum Commands {
// #[arg(required = true)]
// quadkey: String,
// },
#[command(name = "quadkey", visible_alias= "qk", about = "convert xyz <-> quadkey", long_about = None)]
#[command(name = "quadkey", visible_alias = "qk", about = "convert xyz <-> quadkey", long_about = None)]
Quadkey(QuadkeyArgs),

/// tiles
Expand Down Expand Up @@ -174,7 +174,7 @@ impl std::fmt::Display for ColorWhen {
}
}

pub fn cli_main(argv: Option<Vec<String>>) {
pub fn cli_main(argv: Option<Vec<String>>, loop_fn: Option<&dyn Fn() -> ()>) {
// print args
let argv = match argv {
Some(argv) => argv,
Expand Down Expand Up @@ -209,15 +209,16 @@ pub fn cli_main(argv: Option<Vec<String>>) {
println!("Line from stdin: `{}`", line.unwrap());
}
}
Commands::Tiles { zoom, input , seq} => {
let thingy = StdInterator::new(input).unwrap();
println!("zoom: {}", zoom);
for line in thingy
Commands::Tiles { zoom, input, seq } => {
let input_lines = StdInterator::new(input).unwrap();
// println!("zoom: {}", zoom);
let mut niter = 0;
for line in input_lines
.filter(|l| !l.is_err())
.filter(|l| !l.as_ref().unwrap().is_empty())
{
let lstr = line.unwrap();
println!("Line from stdin: `{}`", lstr);
// println!("Line from stdin: `{}`", lstr);
// let json: serde_json::Value = serde_json::from_str(the_file)l;
let thingy = BBox::from(lstr);

Expand All @@ -226,6 +227,17 @@ pub fn cli_main(argv: Option<Vec<String>>) {
ZoomOrZooms::Zoom(zoom),
) {
println!("{}", tile.json_arr());

// call loop_fn if it's defined
niter += 1;

// call fn every 1000 iterations
if niter % 1000 == 0{

if let Some(f) = loop_fn {
f();
}
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/utiles/src/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::bbox::BBox;
use serde_json::Value;

// pub fn parse_bbox(s: &str) -> serde_json::Result<BBox> {
pub fn parse_bbox(s: &str) -> serde_json::Result<BBox> {
let v: Value = serde_json::from_str(s)?;

Expand Down Expand Up @@ -41,4 +42,11 @@ mod tests {
let bbox = bbox_result.unwrap();
assert_eq!(bbox, BBox::new(-180.0, -85.0, -180.0, -85.0));
}

#[test]
fn parse_bbox_bad() {
let string = r#"[-180.0,]"#;
let bbox_result = parse_bbox(string);
assert!(bbox_result.is_err());
}
}
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ testpaths = [
# "python"
]
addopts = [
"--doctest-modules"
"--doctest-modules",
"-v"
]
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
Expand Down
16 changes: 16 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use pyo3::{pyfunction, PyResult, Python};
use utiles_cli::cli::cli_main;

#[pyfunction]
pub fn utcli(py: Python, args: Option<Vec<String>>) {
let argv = match args {
Some(args) => args,
None => std::env::args().collect(),
};
cli_main(
Option::Some(argv),
Option::Some(&|| {
py.check_signals().unwrap();
}),
)
}
16 changes: 2 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use pyo3::types::{PyDict, PyTuple};
use utiles::bbox::BBox;
use utiles::libtiletype;
use utiles::zoom::ZoomOrZooms;
use utiles_cli::cli::cli_main;

use pyutiles::pybbox::PyBbox;
use pyutiles::pyiters::CoordinateIterator;
use pyutiles::pylnglat::PyLngLat;
use pyutiles::pylnglatbbox::PyLngLatBbox;
use pyutiles::pytile::PyTile;

mod pyutiles;
mod cli;
// mod pyutilesqlite;
// mod utiles;

Expand Down Expand Up @@ -757,17 +756,6 @@ fn feature(
Ok(f)
}

#[pyfunction]
fn utcli(py: Python, args: Option<Vec<String>>) {
let argv = match args {
Some(args) => args,
None => std::env::args().collect(),
};
cli_main(
Option::Some(argv)
)
}

fn lib_constants(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add("__version_lib__", env!("CARGO_PKG_VERSION"))?;
m.add("__build_profile__", env!("PROFILE"))?;
Expand Down Expand Up @@ -839,7 +827,7 @@ fn libutiles(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
// m.add_function(wrap_pyfunction!(query_db, m)?)?;

// rust-cli
m.add_function(wrap_pyfunction!(utcli, m)?)?;
m.add_function(wrap_pyfunction!(cli::utcli, m)?)?;

Ok(())
}

0 comments on commit 435c28c

Please sign in to comment.