Skip to content

Commit

Permalink
update to autosar-data 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielT committed Sep 17, 2023
1 parent ec78fb4 commit 014f3a3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ name = "autosar_data"
crate-type = ["cdylib"]

[dependencies]
autosar-data = {version = "0.10"}
autosar-data-specification = {version = "0.10"}
autosar-data = {version = "0.11"}
autosar-data-specification = {version = "0.11"}
pyo3 = "0.19.2"
7 changes: 7 additions & 0 deletions autosar_data.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ class CharacterDataTypeUnsignedInt:
def __repr__(self) -> str: ...
def __str__(self) -> str: ...

def check_file(filename: str):
"""Check if the file contains arxml data. Returns true if an arxml file header is found and does not parse anything after it."""
...

def check_buffer(filename: bytes):
"""Check if the buffer contains arxml data. Returns true if an arxml file header is found and does not parse anything after it."""
...

__version__: str
"""
Expand Down
46 changes: 38 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use autosar_data_rs::CharacterData;
use autosar_data_specification::expand_version_mask;
use autosar_data_specification::CharacterDataSpec;
use pyo3::create_exception;
use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::intern;
use pyo3::prelude::*;
use pyo3::types::*;
Expand Down Expand Up @@ -371,6 +372,28 @@ impl ValidSubElementInfo {
}
}

#[pyfunction]
fn check_file(filename: &str) -> bool {
autosar_data_rs::check_file(filename)
}

#[pyfunction]
fn check_buffer(object: PyObject) -> PyResult<bool> {
Python::with_gil(|py| {
if let Ok(bytebuffer) = object.extract::<&[u8]>(py) {
Ok(autosar_data_rs::check_buffer(bytebuffer))
} else if let Ok(stringbuffer) = object.extract::<&str>(py) {
Ok(autosar_data_rs::check_buffer(stringbuffer.as_bytes()))
} else {
let any = object.as_ref(py);
Err(PyTypeError::new_err(format!(
"'{}' cannot be converted to 'bytes'",
any.get_type()
)))
}
})
}

/// Provides functionality to read, modify and write Autosar arxml files,
/// both separately and in projects consisting of multiple files.
///
Expand All @@ -383,6 +406,11 @@ impl ValidSubElementInfo {
/// - ElementType
/// - ValidSubElementInfo
///
/// Functions:
///
/// - check_file
/// - check_buffwe
///
/// Variables:
///
/// - __version__
Expand Down Expand Up @@ -410,6 +438,8 @@ fn autosar_data(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<CharacterDataTypeRestrictedString>()?;
m.add_class::<CharacterDataTypeString>()?;
m.add_class::<CharacterDataTypeUnsignedInt>()?;
m.add_function(wrap_pyfunction!(check_file, m)?)?;
m.add_function(wrap_pyfunction!(check_buffer, m)?)?;
m.add("AutosarDataError", py.get_type::<AutosarDataError>())?;
m.add("__version__", intern!(m.py(), env!("CARGO_PKG_VERSION")))?;
Ok(())
Expand All @@ -427,12 +457,12 @@ fn extract_character_data(
if let Ok(enumitem) = autosar_data_rs::EnumItem::from_str(&strval) {
Ok(CharacterData::Enum(enumitem))
} else {
Err(pyo3::exceptions::PyValueError::new_err(format!(
Err(PyValueError::new_err(format!(
"string value '{strval}' cannot be converted to 'EnumItem'"
)))
}
} else {
Err(pyo3::exceptions::PyTypeError::new_err(format!(
Err(PyTypeError::new_err(format!(
"'{}' cannot be converted to 'EnumItem'",
any.get_type()
)))
Expand All @@ -446,7 +476,7 @@ fn extract_character_data(
} else if let Ok(floatval) = any.extract::<f64>() {
Ok(CharacterData::String(floatval.to_string()))
} else {
Err(pyo3::exceptions::PyTypeError::new_err(format!(
Err(PyTypeError::new_err(format!(
"'{}' cannot be converted to 'str'",
any.get_type()
)))
Expand All @@ -457,14 +487,14 @@ fn extract_character_data(
if let Ok(intval) = strval.parse() {
Ok(CharacterData::UnsignedInteger(intval))
} else {
Err(pyo3::exceptions::PyValueError::new_err(format!(
Err(PyValueError::new_err(format!(
"invalid literal '{strval}' for conversion to int"
)))
}
} else if let Ok(intval) = any.extract::<u64>() {
Ok(CharacterData::UnsignedInteger(intval))
} else {
Err(pyo3::exceptions::PyTypeError::new_err(format!(
Err(PyTypeError::new_err(format!(
"'{}' cannot be converted to 'int'",
any.get_type()
)))
Expand All @@ -475,7 +505,7 @@ fn extract_character_data(
if let Ok(floatval) = strval.parse() {
Ok(CharacterData::Double(floatval))
} else {
Err(pyo3::exceptions::PyValueError::new_err(format!(
Err(PyValueError::new_err(format!(
"invalid literal '{strval}' for conversion to float"
)))
}
Expand All @@ -484,7 +514,7 @@ fn extract_character_data(
} else if let Ok(floatval) = any.extract::<f64>() {
Ok(CharacterData::Double(floatval))
} else {
Err(pyo3::exceptions::PyTypeError::new_err(format!(
Err(PyTypeError::new_err(format!(
"'{}' cannot be converted to 'float'",
any.get_type()
)))
Expand Down Expand Up @@ -537,7 +567,7 @@ fn version_mask_from_any(version_obj: PyObject) -> PyResult<u32> {
Ok(ver as u32)
} else {
let any = version_obj.as_ref(py);
Err(pyo3::exceptions::PyTypeError::new_err(format!(
Err(PyTypeError::new_err(format!(
"'{}' cannot be converted to 'VersionSpecification'",
any.get_type()
)))
Expand Down
11 changes: 11 additions & 0 deletions test/element_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ def test_character_data_5() -> None:
el_ar_packages.remove_character_content_item(0)


def test_character_data_6() -> None:
# reading and writing character data on an element with content_type == Mixed
# this should work as long as there are 0 or 1 content items in the mixed content
model = AutosarModel()
model.create_file("file")
el_l2 = model.root_element.create_sub_element("AR-PACKAGES") \
.create_named_sub_element("AR-PACKAGE", "Pkg1") \
.create_sub_element("DESC") \
.create_sub_element("L-2")
el_l2.character_data = "text"
assert el_l2.character_data == "text"

def test_element_creation() -> None:
model = AutosarModel()
Expand Down
16 changes: 16 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from autosar_data import *
import pytest
import os

def test_others() -> None:
model = AutosarModel()
Expand Down Expand Up @@ -41,3 +42,18 @@ def test_others() -> None:

with pytest.raises(AutosarDataError):
model.root_element.set_attribute("bla", 0)


def test_check_arxml(tmp_path: str) -> None:
model = AutosarModel()
filename1 = os.path.join(tmp_path, "test.arxml")
file = model.create_file(filename1)
model.write()
assert check_file(filename1) == True
assert check_file("no_such_file") == False

text = file.serialize()
assert check_buffer(text.encode('utf-8')) == True
assert check_buffer(b'abcdef') == False
with pytest.raises(TypeError):
check_buffer(file)

0 comments on commit 014f3a3

Please sign in to comment.