Skip to content

Commit

Permalink
Use strings instead of enums for ElementName, AttributeName, etc
Browse files Browse the repository at this point in the history
Tt is much more obvious to directly use the names from the arxml files.
It also allows autosar-data-specification to drop its dependency on pyo3.
  • Loading branch information
DanielT committed Aug 23, 2023
1 parent 15b436e commit b54b05c
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 219 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ crate-type = ["cdylib"]

[dependencies]
autosar-data = {version = "0.7"}
autosar-data-specification = {version = "0.7", features = ["pylib"]}
autosar-data-specification = {version = "0.7"}
pyo3 = "0.19.2"
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@ from autosar_data import *
model = AutosarModel()

# create a file in the model
file1 = model.create_file("filename.arxml", specification.AutosarVersion.Autosar_4_3_0)
file1 = model.create_file("filename.arxml", AutosarVersion.Autosar_4_3_0)
# a model can consist of multiple files - elements appear in all of them by default, unless restrictions are set
file2 = model.create_file("filename2.arxml", specification.AutosarVersion.Autosar_00051)
file2 = model.create_file("filename2.arxml", AutosarVersion.Autosar_00051)

# initially the model only has its root element, <AUTOSAR>. Create some elements
el_elements = model.root_element \
.create_sub_element(specification.ElementName.ArPackages) \
.create_named_sub_element(specification.ElementName.ArPackage, "Pkg") \
.create_sub_element(specification.ElementName.Elements)
.create_sub_element("AR-PACKAGES") \
.create_named_sub_element("AR-PACKAGE", "Pkg") \
.create_sub_element("ELEMENTS")

# create some more elements
el_fibex_element_ref = el_elements \
.create_named_sub_element(specification.ElementName.System, "System") \
.create_sub_element(specification.ElementName.FibexElements) \
.create_sub_element(specification.ElementName.FibexElementRefConditional) \
.create_sub_element(specification.ElementName.FibexElementRef)
.create_named_sub_element("SYSTEM", "System") \
.create_sub_element("FIBEX-ELEMENTS") \
.create_sub_element("FIBEX-ELEMENT-REF-CONDITIONAL") \
.create_sub_element("FIBEX-ELEMENT-REF")
el_can_cluster = model.root_element \
.get_sub_element(specification.ElementName.ArPackages) \
.create_named_sub_element(specification.ElementName.ArPackage, "Pkg2") \
.create_sub_element(specification.ElementName.Elements) \
.create_named_sub_element(specification.ElementName.CanCluster, "CanCluster")
.get_sub_element("AR-PACKAGES") \
.create_named_sub_element("AR-PACKAGE", "Pkg2") \
.create_sub_element("ELEMENTS") \
.create_named_sub_element("CAN-CLUSTER", "CanCluster")

# set a cross reference
el_fibex_element_ref.reference_target = el_can_cluster
Expand All @@ -81,17 +81,17 @@ el_fibex_element_ref.reference_target == el_can_cluster
# True

# get an attribute
el_fibex_element_ref.attribute_value(specification.AttributeName.Dest)
el_fibex_element_ref.attribute_value("DEST")
# EnumItem.CanCluster
model.root_element.attribute_value(specification.AttributeName.xmlns)
model.root_element.attribute_value("xmlns")
# 'http://autosar.org/schema/r4.0'

# set an attribute value
el_fibex_element_ref.set_attribute(specification.AttributeName.Dest, specification.EnumItem.ISignal)
el_fibex_element_ref.set_attribute("DEST", "I-SIGNAL")
# setting the DEST of the reference to an invalid value has invalidated the
# reference, so accessing el_fibex_element_ref.reference_target will now cause an exception

el_can_cluster.set_attribute(specification.AttributeName.Uuid, "1234567890abcdefg")
el_can_cluster.set_attribute("UUID", "1234567890abcdefg")

# get the current xml text of the model:
print(file1.serialize())
Expand Down Expand Up @@ -127,8 +127,8 @@ model = AutosarModel()

# display all the triggered PDUs in the file
for (depth, element) in model.elements_dfs:
if element.element_name == specification.ElementName.PduTriggering:
pdu = element.get_sub_element(specification.ElementName.IPduRef).reference_target
if element.element_name == "PDU-TRIGGERING":
pdu = element.get_sub_element("I-PDU-REF").reference_target
print(str.format("PDU: <{}> = {}", pdu.element_name, pdu.item_name))

```
Expand Down
16 changes: 8 additions & 8 deletions src/arxmlfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ impl ArxmlFile {
}

#[getter]
fn version(&self) -> autosar_data_rs::AutosarVersion {
self.0.version()
fn version(&self) -> AutosarVersion {
self.0.version().into()
}

#[setter]
fn set_version(&self, version: autosar_data_rs::AutosarVersion) -> PyResult<()> {
fn set_version(&self, version: AutosarVersion) -> PyResult<()> {
self.0
.set_version(version)
.set_version(version.into())
.map_err(|error| AutosarDataError::new_err(error.to_string()))
}

fn check_version_compatibility(
&self,
target_version: autosar_data_rs::AutosarVersion,
target_version: AutosarVersion,
) -> Vec<PyObject> {
Python::with_gil(|py| {
self.0
.check_version_compatibility(target_version)
.check_version_compatibility(target_version.into())
.0
.iter()
.map(|cerr| -> PyObject {
Expand All @@ -76,7 +76,7 @@ impl ArxmlFile {
py,
IncompatibleAttributeError {
element: Element(element.to_owned()),
attribute: *attribute,
attribute: attribute.to_string(),
version_mask: *version_mask,
target_version,
},
Expand All @@ -92,7 +92,7 @@ impl ArxmlFile {
py,
IncompatibleAttributeValueError {
element: Element(element.to_owned()),
attribute: *attribute,
attribute: attribute.to_string(),
attribute_value: attribute_value.to_owned(),
version_mask: *version_mask,
target_version,
Expand Down
88 changes: 52 additions & 36 deletions src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl Element {
}

#[getter]
fn element_name(&self) -> autosar_data_rs::ElementName {
self.0.element_name()
fn element_name(&self) -> String {
self.0.element_name().to_string()
}

#[getter]
Expand Down Expand Up @@ -105,29 +105,24 @@ impl Element {
}
}

fn create_sub_element(&self, element_name: autosar_data_rs::ElementName) -> PyResult<Element> {
fn create_sub_element(&self, name_str: String) -> PyResult<Element> {
let element_name = get_element_name(name_str)?;
match self.0.create_sub_element(element_name) {
Ok(element) => Ok(Element(element)),
Err(error) => Err(AutosarDataError::new_err(error.to_string())),
}
}

fn create_sub_element_at(
&self,
element_name: autosar_data_rs::ElementName,
position: usize,
) -> PyResult<Element> {
fn create_sub_element_at(&self, name_str: String, position: usize) -> PyResult<Element> {
let element_name = get_element_name(name_str)?;
match self.0.create_sub_element_at(element_name, position) {
Ok(element) => Ok(Element(element)),
Err(error) => Err(AutosarDataError::new_err(error.to_string())),
}
}

fn create_named_sub_element(
&self,
element_name: autosar_data_rs::ElementName,
item_name: &str,
) -> PyResult<Element> {
fn create_named_sub_element(&self, name_str: String, item_name: &str) -> PyResult<Element> {
let element_name = get_element_name(name_str)?;
match self.0.create_named_sub_element(element_name, item_name) {
Ok(element) => Ok(Element(element)),
Err(error) => Err(AutosarDataError::new_err(error.to_string())),
Expand All @@ -136,10 +131,11 @@ impl Element {

fn create_named_sub_element_at(
&self,
element_name: autosar_data_rs::ElementName,
name_str: String,
item_name: &str,
position: usize,
) -> PyResult<Element> {
let element_name = get_element_name(name_str)?;
match self
.0
.create_named_sub_element_at(element_name, item_name, position)
Expand Down Expand Up @@ -198,8 +194,9 @@ impl Element {
}
}

fn get_sub_element(&self, name: autosar_data_rs::ElementName) -> Option<Element> {
self.0.get_sub_element(name).map(Element)
fn get_sub_element(&self, name_str: String) -> PyResult<Option<Element>> {
let element_name = get_element_name(name_str)?;
Ok(self.0.get_sub_element(element_name).map(Element))
}

#[getter]
Expand All @@ -214,7 +211,14 @@ impl Element {

#[setter]
fn set_character_data(&self, chardata: PyObject) -> PyResult<()> {
let cdata = extract_character_data(chardata)?;
let spec = self
.0
.element_type()
.chardata_spec()
.ok_or(AutosarDataError::new_err(
autosar_data_rs::AutosarDataError::IncorrectContentType.to_string(),
))?;
let cdata = extract_character_data(spec, chardata)?;
self.0
.set_character_data(cdata)
.map_err(|error| AutosarDataError::new_err(error.to_string()))
Expand Down Expand Up @@ -256,41 +260,53 @@ impl Element {
AttributeIterator(self.0.attributes())
}

fn attribute_value(&self, attrname: autosar_data_rs::AttributeName) -> Option<PyObject> {
Some(character_data_to_object(&self.0.attribute_value(attrname)?))
}

fn set_attribute(
&self,
attrname: autosar_data_rs::AttributeName,
value: PyObject,
) -> PyResult<()> {
let cdata = extract_character_data(value)?;
fn attribute_value(&self, attrname_str: String) -> PyResult<Option<PyObject>> {
let attrname = get_attribute_name(attrname_str)?;
Ok(self
.0
.attribute_value(attrname)
.map(|cdata| character_data_to_object(&cdata)))
}

fn set_attribute(&self, attrname_str: String, value: PyObject) -> PyResult<()> {
let attrname = get_attribute_name(attrname_str)?;
let attrspec = self.0.element_type().find_attribute_spec(attrname).ok_or(
AutosarDataError::new_err(
autosar_data_rs::AutosarDataError::IncorrectContentType.to_string(),
),
)?;
let cdata = extract_character_data(attrspec.spec, value)?;
self.0
.set_attribute(attrname, cdata)
.map_err(|error| AutosarDataError::new_err(error.to_string()))
}

fn set_attribute_string(
&self,
attrname: autosar_data_rs::AttributeName,
text: &str,
) -> PyResult<()> {
fn set_attribute_string(&self, attrname_str: String, text: &str) -> PyResult<()> {
let attrname = get_attribute_name(attrname_str)?;
self.0
.set_attribute_string(attrname, text)
.map_err(|error| AutosarDataError::new_err(error.to_string()))
}

fn remove_attribute(&self, attrname: autosar_data_rs::AttributeName) -> bool {
self.0.remove_attribute(attrname)
fn remove_attribute(&self, attrname_str: String) -> PyResult<bool> {
let attrname = get_attribute_name(attrname_str)?;
Ok(self.0.remove_attribute(attrname))
}

fn sort(&self) {
self.0.sort()
}

fn list_valid_sub_elements(&self) -> Vec<(autosar_data_rs::ElementName, bool, bool)> {
self.0.list_valid_sub_elements()
fn list_valid_sub_elements(&self) -> Vec<ValidSubElementInfo> {
self.0
.list_valid_sub_elements()
.iter()
.map(|(name, is_named, is_allowed)| ValidSubElementInfo {
element_name: name.to_string(),
is_named: *is_named,
is_allowed: *is_allowed,
})
.collect()
}

#[getter]
Expand Down
Loading

0 comments on commit b54b05c

Please sign in to comment.