Skip to content

Commit

Permalink
Merge pull request #346 from korpling/feature/sleep
Browse files Browse the repository at this point in the history
added sleep module
  • Loading branch information
MartinKl authored Jan 13, 2025
2 parents 27a0e86 + c8a770e commit 697972d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `map` allows to delete annotation via query index
- `enumerate` via its attribute `by` allows to restart counting when the value of a node specified in the query changes.
- `sleep` lets annatto sleep

## [0.20.2] - 2024-12-18

Expand Down
2 changes: 1 addition & 1 deletion src/importer/saltxml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl From<&str> for SaltObject {
} else if let Some(value) = value.strip_prefix("U::") {
SaltObject::Url(value.to_string())
} else if let Some(value) = value.strip_prefix("B::") {
let value = value.to_ascii_lowercase() == "true";
let value = value.eq_ignore_ascii_case("true");
SaltObject::Boolean(value)
} else if let Some(value) = value.strip_prefix("N::") {
let value = value.parse::<i64>().unwrap_or_default();
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use importer::{
};
use manipulator::{
check::Check, chunker::Chunk, collapse::Collapse, enumerate::EnumerateMatches,
filter::FilterNodes, link::LinkNodes, map::MapAnnos, no_op::NoOp, re::Revise,
filter::FilterNodes, link::LinkNodes, map::MapAnnos, no_op::NoOp, re::Revise, sleep::Sleep,
split::SplitValues, time::Filltime, visualize::Visualize, Manipulator,
};
use serde_derive::Deserialize;
Expand Down Expand Up @@ -324,7 +324,8 @@ pub enum GraphOp {
Time(#[serde(default)] Filltime),
Chunk(#[serde(default)] Chunk),
Split(#[serde(default)] SplitValues), // default does nothing
None(#[serde(default)] NoOp), // has no attributes
Sleep(#[serde(default)] Sleep),
None(#[serde(default)] NoOp), // has no attributes
}

impl Default for GraphOp {
Expand All @@ -349,6 +350,7 @@ impl GraphOp {
GraphOp::Split(m) => m,
GraphOp::Filter(m) => m,
GraphOp::Time(m) => m,
GraphOp::Sleep(m) => m,
}
}
}
Expand All @@ -368,6 +370,7 @@ impl GraphOpDiscriminants {
GraphOpDiscriminants::Split => SplitValues::DOCS,
GraphOpDiscriminants::Filter => FilterNodes::DOCS,
GraphOpDiscriminants::Time => Filltime::DOCS,
GraphOpDiscriminants::Sleep => Sleep::DOCS,
}
}

Expand Down Expand Up @@ -397,6 +400,7 @@ impl GraphOpDiscriminants {
(FilterNodes::FIELD_NAMES_AS_SLICE, FilterNodes::FIELD_DOCS)
}
GraphOpDiscriminants::Time => (Filltime::FIELD_NAMES_AS_SLICE, Filltime::FIELD_DOCS),
GraphOpDiscriminants::Sleep => (Sleep::FIELD_NAMES_AS_SLICE, Sleep::FIELD_DOCS),
};
for (idx, n) in field_names.iter().enumerate() {
if idx < field_docs.len() {
Expand Down
1 change: 1 addition & 0 deletions src/manipulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod link;
pub mod map;
pub mod no_op;
pub mod re;
pub mod sleep;
pub mod split;
pub mod time;
pub mod visualize;
Expand Down
39 changes: 39 additions & 0 deletions src/manipulator/sleep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use documented::{Documented, DocumentedFields};
use serde::Deserialize;
use struct_field_names_as_array::FieldNamesAsSlice;

use super::Manipulator;

/// This operation pauses the conversion process. As a regular user, you usually do not need to use this feature.
#[derive(Deserialize, Documented, DocumentedFields, FieldNamesAsSlice)]
#[serde(deny_unknown_fields)]
pub struct Sleep {
/// Time to sleep in seconds.
#[serde(default)]
seconds: u64,
}

impl Manipulator for Sleep {
fn manipulate_corpus(
&self,
graph: &mut graphannis::AnnotationGraph,
_workflow_directory: &std::path::Path,
_step_id: crate::StepID,
_tx: Option<crate::workflow::StatusSender>,
) -> Result<(), Box<dyn std::error::Error>> {
std::thread::sleep(std::time::Duration::from_secs(self.seconds));
graph.ensure_loaded_all()?;
Ok(())
}
}

#[cfg(test)]
mod tests {
#[test]
fn deserialize() {
let toml_str = "seconds = 10";
let r: Result<super::Sleep, _> = toml::from_str(toml_str);
assert!(r.is_ok());
assert_eq!(r.unwrap().seconds, 10);
}
}
4 changes: 3 additions & 1 deletion tests/snapshots/cli__list_modules.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ expression: output
|------------------|-----------------------------------------------------------------------------------------------------------------------------|
| Import formats | conllu, exmaralda, graphml, meta, none, opus, path, ptb, relannis, saltxml, table, textgrid, toolbox, treetagger, xlsx, xml |
| Export formats | conllu, exmaralda, graphml, meta, saltxml, sequence, table, textgrid, xlsx |
| Graph operations | check, collapse, filter, visualize, enumerate, link, map, revise, time, chunk, split, none |
| Graph operations | check, collapse, filter, visualize, enumerate, link, map, revise, time, chunk, split, sleep, none |

Use `annatto info <name>` to get more information about one of the formats or graph operations.

0 comments on commit 697972d

Please sign in to comment.