Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions prelude_parser/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
]
3 changes: 3 additions & 0 deletions prelude_parser/_prelude_parser.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -244,6 +247,14 @@ fn parse_site_native_file(_py: Python, xml_file: PathBuf) -> PyResult<SiteNative
}
}

#[pyfunction]
fn parse_site_native_string(_py: Python, xml_str: &str) -> PyResult<SiteNative> {
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<SubjectNative> {
match parse_subject_native_file_rs(&xml_file) {
Expand All @@ -255,6 +266,14 @@ fn parse_subject_native_file(_py: Python, xml_file: PathBuf) -> PyResult<Subject
}
}

#[pyfunction]
fn parse_subject_native_string(_py: Python, xml_str: &str) -> PyResult<SubjectNative> {
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<UserNative> {
match parse_user_native_file_rs(&xml_file) {
Expand All @@ -266,6 +285,14 @@ fn parse_user_native_file(_py: Python, xml_file: PathBuf) -> PyResult<UserNative
}
}

#[pyfunction]
fn parse_user_native_string(_py: Python, xml_str: &str) -> PyResult<UserNative> {
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::<SiteNative>()?;
Expand All @@ -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::<FileNotFoundError>(),
Expand Down
33 changes: 30 additions & 3 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading