Skip to content

Commit

Permalink
enable access to more information from the specification
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielT committed Aug 31, 2023
1 parent 01cdafe commit 6ed16fd
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 55 deletions.
45 changes: 45 additions & 0 deletions autosar_data.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ EnumItem: TypeAlias = str # ~2500 variants is too many to list here
CharacterData: TypeAlias = Union[EnumItem, str, int, float]
ElementContent: TypeAlias = Union[Element, CharacterData]
VersionSpecification: TypeAlias = Union[AutosarVersion, List[AutosarVersion]]
CharacterDataType: TypeAlias = Union[CharacterDataTypeEnum, CharacterDataTypeFloat, CharacterDataTypeRestrictedString, CharacterDataTypeString, CharacterDataTypeUnsignedInt]

class ArxmlFile:
"""
Expand Down Expand Up @@ -303,6 +304,13 @@ class ElementType:
def find_sub_element(self, target_name: ElementName, version: VersionSpecification) -> ElementType:
"""find the ElementType of the named sub element in the specification of this ElementType"""
...
chardata_spec: CharacterDataType
"""the specification of the character data content of elements of this type"""
attributes_spec: List[AttributeSpec]
"""a list of the specifications of all attributes allowed on elements of this type"""
def find_attribute_spec(self, attribute_name: AttributeName) -> AttributeSpec:
"""find the specification for the given attribute name"""
...

class ElementsDfsIterator:
"""
Expand Down Expand Up @@ -368,6 +376,43 @@ class ValidSubElementInfo:
is_allowed: bool
"""is the sub element currently allowed, given the existing content of the element. Note that some sub elements are mutually exclusive"""

class AttributeSpec:
"""The specification of an attribute"""
attribute_name: str
"""name of the attribute"""
value_spec: CharacterDataType
"""specification of the attribute value"""
required: bool
"""is the attribute required or optional"""

class CharacterDataTypeEnum:
"""Character data type: enum"""
values: List[str]
"""List of valid enum values"""

class CharacterDataTypeFloat:
"""Character data type: float"""
def __repr__(self) -> str: ...
def __str__(self) -> str: ...

class CharacterDataTypeRestrictedString:
"""Character data type: restricted string"""
def __repr__(self) -> str: ...
def __str__(self) -> str: ...
regex: str
"""to be valid, a string must match this regex"""

class CharacterDataTypeString:
"""Character data type: string"""
def __repr__(self) -> str: ...
def __str__(self) -> str: ...

class CharacterDataTypeUnsignedInt:
"""Character data type: unsigned int"""
def __repr__(self) -> str: ...
def __str__(self) -> str: ...


__version__: str
"""
Version of the running autosar_data module.
Expand Down
119 changes: 65 additions & 54 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod arxmlfile;
mod element;
mod model;
mod version;
mod specification;

use version::*;

Expand Down Expand Up @@ -122,6 +123,63 @@ enum ContentType {
Mixed,
}

#[pyclass]
#[derive(Debug)]
/// Specification of an attribute
struct AttributeSpec {
#[pyo3(get)]
/// name of the attribute
attribute_name: String,
/// specification of the attribute value
value_spec: &'static CharacterDataSpec,
#[pyo3(get)]
/// is the attribute required or optional
required: bool,
}

#[pyclass]
#[derive(Debug)]
/// The character data in an element or attribute is an enum value
struct CharacterDataTypeEnum {
#[pyo3(get)]
/// list of permitted enum values
values: Vec<String>,
}

#[pyclass]
#[derive(Debug)]
/// The character data in an element or attribute is a string that must match a regex
struct CharacterDataTypeRestrictedString {
#[pyo3(get)]
/// validation regex
regex: String,
#[pyo3(get)]
/// max length (if any)
max_length: Option<usize>,
}

#[pyclass]
#[derive(Debug)]
/// The character data in an element or attribute is a string
struct CharacterDataTypeString {
#[pyo3(get)]
/// does this element preserve whitespace in its character data
preserve_whitespace: bool,
#[pyo3(get)]
/// max length (if any)
max_length: Option<usize>,
}

#[pyclass]
#[derive(Debug)]
/// The character data in an element or attribute is an unsigned integer
struct CharacterDataTypeUnsignedInt(());

#[pyclass]
#[derive(Debug)]
/// The character data in an element or attribute is a float
struct CharacterDataTypeFloat(());

#[pymethods]
impl IncompatibleAttributeError {
fn __repr__(&self) -> String {
Expand Down Expand Up @@ -194,60 +252,6 @@ impl IncompatibleElementError {
}
}

#[pymethods]
impl ElementType {
fn __repr__(&self) -> String {
format!("{:#?}", self.0)
}

#[getter]
fn is_named(&self) -> bool {
self.0.is_named()
}

#[getter]
fn is_ref(&self) -> bool {
self.0.is_ref()
}

#[getter]
fn is_ordered(&self) -> bool {
self.0.is_ordered()
}

#[getter]
fn splittable(&self) -> Vec<AutosarVersion> {
let versions = expand_version_mask(self.0.splittable());
versions
.iter()
.map(|&ver| AutosarVersion::from(ver))
.collect()
}

fn splittable_in(&self, version: AutosarVersion) -> bool {
self.0.splittable_in(version.into())
}

fn reference_dest_value(&self, target: &ElementType) -> Option<String> {
self.0
.reference_dest_value(&target.0)
.map(|enumitem| enumitem.to_string())
}

fn find_sub_element(
&self,
target_name: String,
version_obj: PyObject,
) -> PyResult<Option<ElementType>> {
let version = version_mask_from_any(version_obj)?;
let elem_name = get_element_name(target_name)?;
Ok(self
.0
.find_sub_element(elem_name, version)
.map(|(etype, _)| ElementType(etype)))
}
}

#[pymethods]
impl ContentType {
fn __repr__(&self) -> String {
Expand Down Expand Up @@ -367,6 +371,7 @@ impl ValidSubElementInfo {
}
}


/// Provides functionality to read, modify and write Autosar arxml files,
/// both separately and in projects consisting of multiple files.
///
Expand Down Expand Up @@ -399,7 +404,13 @@ fn autosar_data(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<ElementsIterator>()?;
m.add_class::<AttributeIterator>()?;
m.add_class::<Attribute>()?;
m.add_class::<AttributeSpec>()?;
m.add_class::<ValidSubElementInfo>()?;
m.add_class::<CharacterDataTypeEnum>()?;
m.add_class::<CharacterDataTypeFloat>()?;
m.add_class::<CharacterDataTypeRestrictedString>()?;
m.add_class::<CharacterDataTypeString>()?;
m.add_class::<CharacterDataTypeUnsignedInt>()?;
m.add("AutosarDataError", py.get_type::<AutosarDataError>())?;
m.add("__version__", intern!(m.py(), env!("CARGO_PKG_VERSION")))?;
Ok(())
Expand Down
Loading

0 comments on commit 6ed16fd

Please sign in to comment.