Skip to content

Commit 3874412

Browse files
committed
Add functions to parse native xml strings
1 parent bded900 commit 3874412

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

prelude_parser/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
from prelude_parser._prelude_parser import (
22
parse_site_native_file,
3+
parse_site_native_string,
34
parse_subject_native_file,
5+
parse_subject_native_string,
46
parse_user_native_file,
7+
parse_user_native_string,
58
)
69
from prelude_parser.parser import parse_to_classes, parse_to_dict
710

811
__all__ = [
912
"parse_site_native_file",
13+
"parse_site_native_string",
1014
"parse_subject_native_file",
15+
"parse_subject_native_string",
1116
"parse_user_native_file",
17+
"parse_user_native_string",
1218
"parse_to_classes",
1319
"parse_to_dict",
1420
]

prelude_parser/_prelude_parser.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ def _parse_flat_file_to_pandas_dict(
1616
xml_file: str | Path, short_names: bool
1717
) -> dict[str, FlatFormInfo]: ...
1818
def parse_site_native_file(xml_file: str | Path) -> SiteNative: ...
19+
def parse_site_native_string(xml_str: str) -> SiteNative: ...
1920
def parse_subject_native_file(xml_file: str | Path) -> SubjectNative: ...
21+
def parse_subject_native_string(xml_str: str) -> SubjectNative: ...
2022
def parse_user_native_file(xml_file: str | Path) -> UserNative: ...
23+
def parse_user_native_string(xml_str: str) -> UserNative: ...
2124

2225
class FileNotFoundError(Exception):
2326
pass

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ pub use prelude_xml_parser::native::{
1010
site_native::SiteNative, subject_native::SubjectNative, user_native::UserNative,
1111
};
1212
use prelude_xml_parser::parse_site_native_file as parse_site_native_file_rs;
13+
use prelude_xml_parser::parse_site_native_string as parse_site_native_string_rs;
1314
use prelude_xml_parser::parse_subject_native_file as parse_subject_native_file_rs;
15+
use prelude_xml_parser::parse_subject_native_string as parse_subject_native_string_rs;
1416
use prelude_xml_parser::parse_user_native_file as parse_user_native_file_rs;
17+
use prelude_xml_parser::parse_user_native_string as parse_user_native_string_rs;
1518
use pyo3::prelude::*;
1619
use pyo3::types::{IntoPyDict, PyDict, PyList};
1720
use roxmltree::Document;
@@ -244,6 +247,14 @@ fn parse_site_native_file(_py: Python, xml_file: PathBuf) -> PyResult<SiteNative
244247
}
245248
}
246249

250+
#[pyfunction]
251+
fn parse_site_native_string(_py: Python, xml_str: &str) -> PyResult<SiteNative> {
252+
match parse_site_native_string_rs(xml_str) {
253+
Ok(native) => Ok(native),
254+
Err(e) => Err(ParsingError::new_err(format!("Error parsing xml: {:?}", e))),
255+
}
256+
}
257+
247258
#[pyfunction]
248259
fn parse_subject_native_file(_py: Python, xml_file: PathBuf) -> PyResult<SubjectNative> {
249260
match parse_subject_native_file_rs(&xml_file) {
@@ -255,6 +266,14 @@ fn parse_subject_native_file(_py: Python, xml_file: PathBuf) -> PyResult<Subject
255266
}
256267
}
257268

269+
#[pyfunction]
270+
fn parse_subject_native_string(_py: Python, xml_str: &str) -> PyResult<SubjectNative> {
271+
match parse_subject_native_string_rs(xml_str) {
272+
Ok(native) => Ok(native),
273+
Err(e) => Err(ParsingError::new_err(format!("Error parsing xml: {:?}", e))),
274+
}
275+
}
276+
258277
#[pyfunction]
259278
fn parse_user_native_file(_py: Python, xml_file: PathBuf) -> PyResult<UserNative> {
260279
match parse_user_native_file_rs(&xml_file) {
@@ -266,6 +285,14 @@ fn parse_user_native_file(_py: Python, xml_file: PathBuf) -> PyResult<UserNative
266285
}
267286
}
268287

288+
#[pyfunction]
289+
fn parse_user_native_string(_py: Python, xml_str: &str) -> PyResult<UserNative> {
290+
match parse_user_native_string_rs(xml_str) {
291+
Ok(native) => Ok(native),
292+
Err(e) => Err(ParsingError::new_err(format!("Error parsing xml: {:?}", e))),
293+
}
294+
}
295+
269296
#[pymodule]
270297
fn _prelude_parser(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
271298
m.add_class::<SiteNative>()?;
@@ -274,8 +301,11 @@ fn _prelude_parser(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
274301
m.add_function(wrap_pyfunction!(_parse_flat_file_to_dict, m)?)?;
275302
m.add_function(wrap_pyfunction!(_parse_flat_file_to_pandas_dict, m)?)?;
276303
m.add_function(wrap_pyfunction!(parse_site_native_file, m)?)?;
304+
m.add_function(wrap_pyfunction!(parse_site_native_string, m)?)?;
277305
m.add_function(wrap_pyfunction!(parse_subject_native_file, m)?)?;
306+
m.add_function(wrap_pyfunction!(parse_subject_native_string, m)?)?;
278307
m.add_function(wrap_pyfunction!(parse_user_native_file, m)?)?;
308+
m.add_function(wrap_pyfunction!(parse_user_native_string, m)?)?;
279309
m.add(
280310
"FileNotFoundError",
281311
py.get_type_bound::<FileNotFoundError>(),

tests/test_parser.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,59 @@
44

55
from prelude_parser import (
66
parse_site_native_file,
7+
parse_site_native_string,
78
parse_subject_native_file,
9+
parse_subject_native_string,
810
parse_to_classes,
911
parse_to_dict,
1012
parse_user_native_file,
13+
parse_user_native_string,
1114
)
1215
from prelude_parser._prelude_parser import FileNotFoundError, InvalidFileTypeError, ParsingError
1316

1417

15-
def test_parse_site_native(site_native_xml):
18+
def test_parse_site_native_file(site_native_xml):
1619
result = parse_site_native_file(site_native_xml)
1720

1821
assert result.sites[0].name == "Some Site"
1922

2023

21-
def test_parse_subject_native(subject_native_xml):
24+
def test_parse_site_native_string(site_native_xml):
25+
with open(site_native_xml) as f:
26+
xml = f.read()
27+
result = parse_site_native_string(xml)
28+
29+
assert result.sites[0].name == "Some Site"
30+
31+
32+
def test_parse_subject_native_file(subject_native_xml):
2233
result = parse_subject_native_file(subject_native_xml)
2334

2435
assert result.patients[0].patient_id == "ABC-001"
2536

2637

27-
def test_parse_user_native(user_native_xml):
38+
def test_parse_subject_native_string(subject_native_xml):
39+
with open(subject_native_xml) as f:
40+
xml = f.read()
41+
result = parse_subject_native_string(xml)
42+
43+
assert result.patients[0].patient_id == "ABC-001"
44+
45+
46+
def test_parse_user_native_file(user_native_xml):
2847
result = parse_user_native_file(user_native_xml)
2948

3049
assert result.users[0].unique_id == "1691421275437"
3150

3251

52+
def test_parse_user_native_string(user_native_xml):
53+
with open(user_native_xml) as f:
54+
xml = f.read()
55+
result = parse_user_native_string(xml)
56+
57+
assert result.users[0].unique_id == "1691421275437"
58+
59+
3360
def test_parse_to_classes(test_file_1):
3461
result = parse_to_classes(test_file_1)
3562
assert len(result) == 2

0 commit comments

Comments
 (0)