Skip to content

Commit 0a0d812

Browse files
committed
substores: implemented associate() method to associate items with substores
1 parent 21fecf6 commit 0a0d812

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/substore.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ use std::borrow::Cow;
66
use std::ops::FnOnce;
77
use std::sync::{Arc, RwLock};
88

9-
use crate::annotation::PyAnnotations;
9+
use crate::annotation::PyAnnotation;
10+
use crate::annotationdataset::PyAnnotationDataSet;
11+
use crate::annotationstore::MapStore;
1012
use crate::error::PyStamError;
13+
use crate::resources::PyTextResource;
1114
use stam::*;
1215

1316
#[pyclass(dict, module = "stam", name = "AnnotationSubStore")]
@@ -79,6 +82,35 @@ impl PyAnnotationSubStore {
7982
fn __hash__(&self) -> usize {
8083
self.handle.as_usize()
8184
}
85+
86+
fn associate(&mut self, item: &PyAny) -> PyResult<()> {
87+
if item.is_instance_of::<PyAnnotation>() {
88+
let item: PyRef<PyAnnotation> = item.extract()?;
89+
let substore_handle = self.handle;
90+
self.map_store_mut(|store| store.associate_substore(item.handle, substore_handle))
91+
} else if item.is_instance_of::<PyTextResource>() {
92+
let item: PyRef<PyTextResource> = item.extract()?;
93+
let substore_handle = self.handle;
94+
self.map_store_mut(|store| store.associate_substore(item.handle, substore_handle))
95+
} else if item.is_instance_of::<PyAnnotationDataSet>() {
96+
let item: PyRef<PyAnnotationDataSet> = item.extract()?;
97+
let substore_handle = self.handle;
98+
self.map_store_mut(|store| store.associate_substore(item.handle, substore_handle))
99+
} else {
100+
Err(PyValueError::new_err(
101+
"Invalid type for item, expected Annotation, TextResource or AnnotationDataSet",
102+
))
103+
}
104+
}
105+
}
106+
107+
impl MapStore for PyAnnotationSubStore {
108+
fn get_store(&self) -> &Arc<RwLock<AnnotationStore>> {
109+
&self.store
110+
}
111+
fn get_store_mut(&mut self) -> &mut Arc<RwLock<AnnotationStore>> {
112+
&mut self.store
113+
}
82114
}
83115

84116
impl PyAnnotationSubStore {

stam.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,9 @@ class AnnotationSubStore:
21912191
def has_filename(self, filename: str) -> bool:
21922192
"""Tests the filename for the stand-off file specified using @include (if any)."""
21932193

2194+
def associate(self, item: Union[Annotation,TextResource,AnnotationDataSet]):
2195+
"""Associates an annotation, text resource or annotation dataset with this substore"""
2196+
21942197

21952198
class StamError(Exception):
21962199
"""STAM Error"""

0 commit comments

Comments
 (0)