diff --git a/prelude_parser/__init__.py b/prelude_parser/__init__.py index 804ce93..6a6667a 100644 --- a/prelude_parser/__init__.py +++ b/prelude_parser/__init__.py @@ -1,14 +1,20 @@ from prelude_parser._prelude_parser import ( parse_site_native_file, + parse_site_native_string, parse_subject_native_file, + parse_subject_native_string, parse_user_native_file, + parse_user_native_string, ) from prelude_parser.parser import parse_to_classes, parse_to_dict __all__ = [ "parse_site_native_file", + "parse_site_native_string", "parse_subject_native_file", + "parse_subject_native_string", "parse_user_native_file", + "parse_user_native_string", "parse_to_classes", "parse_to_dict", ] diff --git a/prelude_parser/_prelude_parser.pyi b/prelude_parser/_prelude_parser.pyi index 1071e9a..6e7be3f 100644 --- a/prelude_parser/_prelude_parser.pyi +++ b/prelude_parser/_prelude_parser.pyi @@ -16,8 +16,11 @@ def _parse_flat_file_to_pandas_dict( xml_file: str | Path, short_names: bool ) -> dict[str, FlatFormInfo]: ... def parse_site_native_file(xml_file: str | Path) -> SiteNative: ... +def parse_site_native_string(xml_str: str) -> SiteNative: ... def parse_subject_native_file(xml_file: str | Path) -> SubjectNative: ... +def parse_subject_native_string(xml_str: str) -> SubjectNative: ... def parse_user_native_file(xml_file: str | Path) -> UserNative: ... +def parse_user_native_string(xml_str: str) -> UserNative: ... class FileNotFoundError(Exception): pass diff --git a/src/lib.rs b/src/lib.rs index 7de84c2..b4a8516 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,8 +10,11 @@ pub use prelude_xml_parser::native::{ site_native::SiteNative, subject_native::SubjectNative, user_native::UserNative, }; use prelude_xml_parser::parse_site_native_file as parse_site_native_file_rs; +use prelude_xml_parser::parse_site_native_string as parse_site_native_string_rs; use prelude_xml_parser::parse_subject_native_file as parse_subject_native_file_rs; +use prelude_xml_parser::parse_subject_native_string as parse_subject_native_string_rs; use prelude_xml_parser::parse_user_native_file as parse_user_native_file_rs; +use prelude_xml_parser::parse_user_native_string as parse_user_native_string_rs; use pyo3::prelude::*; use pyo3::types::{IntoPyDict, PyDict, PyList}; use roxmltree::Document; @@ -244,6 +247,14 @@ fn parse_site_native_file(_py: Python, xml_file: PathBuf) -> PyResult PyResult { + match parse_site_native_string_rs(xml_str) { + Ok(native) => Ok(native), + Err(e) => Err(ParsingError::new_err(format!("Error parsing xml: {:?}", e))), + } +} + #[pyfunction] fn parse_subject_native_file(_py: Python, xml_file: PathBuf) -> PyResult { match parse_subject_native_file_rs(&xml_file) { @@ -255,6 +266,14 @@ fn parse_subject_native_file(_py: Python, xml_file: PathBuf) -> PyResult PyResult { + match parse_subject_native_string_rs(xml_str) { + Ok(native) => Ok(native), + Err(e) => Err(ParsingError::new_err(format!("Error parsing xml: {:?}", e))), + } +} + #[pyfunction] fn parse_user_native_file(_py: Python, xml_file: PathBuf) -> PyResult { match parse_user_native_file_rs(&xml_file) { @@ -266,6 +285,14 @@ fn parse_user_native_file(_py: Python, xml_file: PathBuf) -> PyResult PyResult { + match parse_user_native_string_rs(xml_str) { + Ok(native) => Ok(native), + Err(e) => Err(ParsingError::new_err(format!("Error parsing xml: {:?}", e))), + } +} + #[pymodule] fn _prelude_parser(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; @@ -274,8 +301,11 @@ fn _prelude_parser(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_function(wrap_pyfunction!(_parse_flat_file_to_dict, m)?)?; m.add_function(wrap_pyfunction!(_parse_flat_file_to_pandas_dict, m)?)?; m.add_function(wrap_pyfunction!(parse_site_native_file, m)?)?; + m.add_function(wrap_pyfunction!(parse_site_native_string, m)?)?; m.add_function(wrap_pyfunction!(parse_subject_native_file, m)?)?; + m.add_function(wrap_pyfunction!(parse_subject_native_string, m)?)?; m.add_function(wrap_pyfunction!(parse_user_native_file, m)?)?; + m.add_function(wrap_pyfunction!(parse_user_native_string, m)?)?; m.add( "FileNotFoundError", py.get_type_bound::(), diff --git a/tests/test_parser.py b/tests/test_parser.py index 920d5fd..042ed28 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -4,32 +4,59 @@ from prelude_parser import ( parse_site_native_file, + parse_site_native_string, parse_subject_native_file, + parse_subject_native_string, parse_to_classes, parse_to_dict, parse_user_native_file, + parse_user_native_string, ) from prelude_parser._prelude_parser import FileNotFoundError, InvalidFileTypeError, ParsingError -def test_parse_site_native(site_native_xml): +def test_parse_site_native_file(site_native_xml): result = parse_site_native_file(site_native_xml) assert result.sites[0].name == "Some Site" -def test_parse_subject_native(subject_native_xml): +def test_parse_site_native_string(site_native_xml): + with open(site_native_xml) as f: + xml = f.read() + result = parse_site_native_string(xml) + + assert result.sites[0].name == "Some Site" + + +def test_parse_subject_native_file(subject_native_xml): result = parse_subject_native_file(subject_native_xml) assert result.patients[0].patient_id == "ABC-001" -def test_parse_user_native(user_native_xml): +def test_parse_subject_native_string(subject_native_xml): + with open(subject_native_xml) as f: + xml = f.read() + result = parse_subject_native_string(xml) + + assert result.patients[0].patient_id == "ABC-001" + + +def test_parse_user_native_file(user_native_xml): result = parse_user_native_file(user_native_xml) assert result.users[0].unique_id == "1691421275437" +def test_parse_user_native_string(user_native_xml): + with open(user_native_xml) as f: + xml = f.read() + result = parse_user_native_string(xml) + + assert result.users[0].unique_id == "1691421275437" + + def test_parse_to_classes(test_file_1): result = parse_to_classes(test_file_1) assert len(result) == 2