API Documentation: autosar_data¶
Example¶
from autosar_data import *
# alternatively: create a new data model from scratch
model = AutosarModel()
# create a file in the model
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", AutosarVersion.AUTOSAR_00051)
# initially the model only has its root element, <AUTOSAR>. Create some elements
el_elements = model.root_element \
.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("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("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
# check the cross reference
el_fibex_element_ref.character_data
# '/Pkg2/CanCluster'
el_fibex_element_ref.reference_target == el_can_cluster
# True
# get an attribute
el_fibex_element_ref.attribute_value("DEST")
# EnumItem.CanCluster
model.root_element.attribute_value("xmlns")
# 'http://autosar.org/schema/r4.0'
# set an attribute value
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("UUID", "1234567890abcdefg")
# get the current xml text of the model:
print(file1.serialize())
# this prints "<?xml version="1.0" encoding="utf-8"?>\n<AUTOSAR ..."
# write all the files in the model - this will create filename.arxml and filename2.arxml with identical content
model.write()
# get the autosar paths of all elements in the model
paths = model.identifiable_elements
# paths = ['/Pkg', '/Pkg/System', '/Pkg2', '/Pkg2/CanCluster']
# get an element by its path
el_ar_package1 = model.get_element_by_path("/Pkg")
el_ar_package2 = model.get_element_by_path("/Pkg2")
el_system = model.get_element_by_path("/Pkg/System")
# restrict the packages to only appear in one file each
el_ar_package1.remove_from_file(file2)
el_ar_package2.remove_from_file(file1)
# write all the files in the model - now the content is different
model.write()
API¶
autosar_data ¶
Provides functionality to read, modify and write Autosar arxml files, both separately and in projects consisting of multiple files.
Classes:
- ArxmlFile
- AutosarModel
- AutosarVersion
- Element
- ElementType
- ValidSubElementInfo
Variables:
- version
AttributeName
module-attribute
¶
AttributeName: TypeAlias = Literal[
"ACCESSKEY",
"ALIGN",
"ALLOW-BREAK",
"ALT",
"BASE",
"BGCOLOR",
"BINDING-TIME",
"BLUEPRINT-VALUE",
"BREAK",
"CLASS",
"COLNAME",
"COLNUM",
"COLOR",
"COLS",
"COLSEP",
"COLWIDTH",
"COORDS",
"DEST",
"EDIT-HEIGHT",
"EDIT-WIDTH",
"EDITFIT",
"EDITSCALE",
"ENUM-TABLE",
"FILENAME",
"FIT",
"FLOAT",
"FONT",
"FRAME",
"GENERATOR",
"GID",
"HEIGHT",
"HELP-ENTRY",
"HREF",
"HTML-FIT",
"HTML-HEIGHT",
"HTML-SCALE",
"HTML-WIDTH",
"INDEX",
"INTERVAL-TYPE",
"ITEM-LABEL-POS",
"KEEP-WITH-PREVIOUS",
"L",
"LEVEL",
"MIME-TYPE",
"MOREROWS",
"NAME",
"NAME-PATTERN",
"NAMEEND",
"NAMEST",
"NOHREF",
"NOTATION",
"NOTE-TYPE",
"ONBLUR",
"ONCLICK",
"ONDBLCLICK",
"ONFOCUS",
"ONKEYDOWN",
"ONKEYPRESS",
"ONKEYUP",
"ONMOUSEDOWN",
"ONMOUSEMOVE",
"ONMOUSEOUT",
"ONMOUSEOVER",
"ONMOUSEUP",
"ORIENT",
"PGWIDE",
"RESOLUTION-POLICY",
"ROTATE",
"ROWSEP",
"S",
"SCALE",
"SD",
"SHAPE",
"SHORT-LABEL",
"SHOW-CONTENT",
"SHOW-RESOURCE-ALIAS-NAME",
"SHOW-RESOURCE-CATEGORY",
"SHOW-RESOURCE-LONG-NAME",
"SHOW-RESOURCE-NUMBER",
"SHOW-RESOURCE-PAGE",
"SHOW-RESOURCE-SHORT-NAME",
"SHOW-RESOURCE-TYPE",
"SHOW-SEE",
"SI",
"SPANNAME",
"STYLE",
"T",
"TABINDEX",
"TABSTYLE",
"TEX-RENDER",
"TITLE",
"TYPE",
"UUID",
"VALIDITY",
"VALIGN",
"VIEW",
"WIDTH",
"xml:space",
"xmlns",
"xmlns:xsi",
"xsi:schemaLocation",
]
CharacterDataType
module-attribute
¶
CharacterDataType: TypeAlias = Union[
CharacterDataTypeEnum,
CharacterDataTypeFloat,
CharacterDataTypeRestrictedString,
CharacterDataTypeString,
CharacterDataTypeUnsignedInt,
]
IncompatibleItemError
module-attribute
¶
IncompatibleItemError: TypeAlias = Union[
IncompatibleAttributeError,
IncompatibleAttributeValueError,
IncompatibleElementError,
]
VersionSpecification
module-attribute
¶
VersionSpecification: TypeAlias = Union[
AutosarVersion, List[AutosarVersion]
]
ArxmlFile ¶
Represents a file that is part of an AutosarModel
elements_dfs
property
¶
elements_dfs: Iterator[Tuple[int, Element]]
dfs iterator over all elements in this file
xml_standalone
property
¶
xml_standalone: bool
get the "xml_standalone" attribute from the header of the ARXML file
check_version_compatibility
method descriptor
¶
check_version_compatibility() -> List[
IncompatibleItemError
]
Check if the data in the ARXML file is compatible with the given target version
elements_dfs_with_max_depth
method descriptor
¶
elements_dfs_with_max_depth(
max_depth: int,
) -> Iterator[Tuple[int, Element]]
dfs iterator over all elements in this file, with a maximum depth
Attribute ¶
An attribute on an element
content
class-attribute
¶
content: CharacterData = <member 'content' of 'autosar_data._autosar_data.Attribute' objects>
content of the attribute - this data can be free-form text, a pre-defined enum value (str), or very rarely a float or int
AttributeSpec ¶
AutosarDataError ¶
Bases: builtins.Exception
AutosarModel ¶
Autosar data model. It contains all elements.
elements_dfs
property
¶
elements_dfs: Iterator[Tuple[int, Element]]
depth first dearch iterator over all elements in the model, regardless of their association with a file
files
property
¶
files: List[ArxmlFile]
a list of ArxmlFile objects containing all files in the model
identifiable_elements
property
¶
identifiable_elements: Iterator[Tuple[str, Element]]
Iterate over pairs of (path, element) for all identifiable elements in the model
check_references
method descriptor
¶
check_references() -> List[Element]
check all references in the model and return a list of elements containing invalid references
create_file
method descriptor
¶
create_file(
filename: str, /, version: AutosarVersion = Ellipsis
) -> ArxmlFile
create a new file in the model
duplicate
method descriptor
¶
duplicate() -> AutosarModel
duplicate the model, creating a new independent copy
get_element_by_path
method descriptor
¶
get_element_by_path(path) -> Element
get an identifiable element in the model by its Autosar path
get_references_to
method descriptor
¶
get_references_to(target_path: str) -> List[Element]
get all reference elements which refer to the given Autosar path
load_buffer
method descriptor
¶
load_buffer(
buffer: str, filename: str, /, strict: bool = False
) -> Tuple[ArxmlFile, List[str]]
load a buffer (string) as arxml
load_file
method descriptor
¶
load_file(
filename: str, /, strict: bool = False
) -> Tuple[ArxmlFile, List[str]]
load a file as arxml
remove_file
method descriptor
¶
remove_file(file) -> None
remove a file from the model. Any elements belonging exclusively to that file will also be removed.
serialize_files
method descriptor
¶
serialize_files() -> Dict[str, str]
serialize all files individually, to generate a dict(filename, serialized content),
sort
method descriptor
¶
sort() -> None
sort the entire model in place. Takes all ordering constraints into account.
AutosarVersion ¶
CharacterDataTypeEnum ¶
The character data in an element or attribute is an enum value
CharacterDataTypeFloat ¶
The character data in an element or attribute is a float
CharacterDataTypeRestrictedString ¶
CharacterDataTypeString ¶
CharacterDataTypeUnsignedInt ¶
The character data in an element or attribute is an unsigned integer
ContentMode ¶
The content mode of an element type
Characters
class-attribute
¶
Characters: ContentMode = Characters
The content mode of an element type
ContentType ¶
The content type of an element
Element ¶
An element in the Autosar data model
character_data
property
¶
character_data: CharacterData
character content of this element, if any. For elements with ContentType=Element, or empty elements this is None
content
property
¶
content: Iterator[Union[Element, CharacterData]]
iterator over all content of this element
content_item_count
property
¶
content_item_count: int
number of content items (character data and/or sub elements)
content_type
property
¶
content_type: ContentType
content type of the element: character data (
element_name
property
¶
element_name: ElementName
ElementName of this element, e.g. AUTOSAR or AR-PACKAGE
element_type
property
¶
element_type: ElementType
Reference to the element type of the element in the specification
elements_dfs
property
¶
elements_dfs: Iterator[Tuple[int, Element]]
depth first search iterator for this element and all of its sub elements
file_membership
property
¶
file_membership: Tuple[bool, FrozenSet[ArxmlFile]]
file membership information: the tuple (is_local, set(ArxmlFile)) tells if there is a restriction to file membership attached to this element, and which files the element is part of
is_identifiable
property
¶
is_identifiable: bool
true if the element is identifiable, false otherwise
is_reference
property
¶
is_reference: bool
true if the element can contain a reference to another element
item_name
property
¶
item_name: str
item name of an identifiable element, or None for elements which are not identifiable.
Setting this value renames the element and updates all references to it.
min_version
property
¶
min_version: AutosarVersion
the autosar version of the file containing the element. If multiple files in a merged model contain the element, then this is the minimum of the file versions.
named_parent
property
¶
named_parent: Element
reference to the next named (grand-)parent of this element
reference_target
property
¶
reference_target: Element
Set the reference target of a reference element
This is only valid for elements with a reference content type.
sub_elements
property
¶
sub_elements: Iterator[Element]
an iterator over all sub elements in the content of this element. It skips character data content items
xml_path
property
¶
xml_path: str
a path listing all xml elements from the root of the model to the element. This is intended for display. e.g. in error messages
add_to_file
method descriptor
¶
add_to_file(file: ArxmlFile) -> None
add the element to a file. if necessary all parent elements of this element also become part of the file
attribute_value
method descriptor
¶
attribute_value(attrname_str) -> CharacterData
get the attribute value of a specific attribute. Returns None if that attribute is not set
create_copied_sub_element
method descriptor
¶
create_copied_sub_element() -> Element
Create a new sub-element by copying the given element and all its children
This creates a fully-independen copy. The function can copy elements between different models.
create_named_sub_element
method descriptor
¶
create_named_sub_element() -> Element
Create a new sub-element with the given element name and item name
create_sub_element
method descriptor
¶
create_sub_element() -> Element
Create a new sub-element with the given element name
elements_dfs_with_max_depth
method descriptor
¶
elements_dfs_with_max_depth(
max_depth: int,
) -> Iterator[Tuple[int, Element]]
depth first search iterator for this element and all of its sub elements, with a maximum depth
get_bsw_sub_element
method descriptor
¶
get_bsw_sub_element() -> Element
get the sub element with the given definition ref. It is possible to specify either the full definition ref, or only the last part after the final '/'
get_named_sub_element
method descriptor
¶
get_named_sub_element() -> Element
Get the sub-element with the given item name, if it exists
get_or_create_named_sub_element
method descriptor
¶
get_or_create_named_sub_element() -> Element
Get or create a sub-element with the given element name and item name
get_or_create_sub_element
method descriptor
¶
get_or_create_sub_element() -> Element
Get or create a sub-element with the given element name
This is used to ensure that a sub-element with the given name exists.
get_sub_element
method descriptor
¶
get_sub_element() -> Element
Get a sub-element with the given element name
If multiple sub-elements with the same name exist, only the first one is returned.
get_sub_element_at
method descriptor
¶
get_sub_element_at() -> Element
Get a sub-element at the given position
The position is 0-based, and must be less than the number of sub-elements.
insert_character_content_item
method descriptor
¶
insert_character_content_item() -> None
for elements with ElementType mixed, this allows character data to be inserted at any point in the content of this element
list_valid_sub_elements
method descriptor
¶
list_valid_sub_elements() -> List[ValidSubElementInfo]
provide information about valid sub elements as a list of ValidSubElementInfo
move_element_here
method descriptor
¶
move_element_here() -> Element
Move the given element to become a sub-element of this element
remove_attribute
method descriptor
¶
remove_attribute() -> None
remove an attribute from the element
remove_character_content_item
method descriptor
¶
remove_character_content_item() -> None
remove one character content item from the given position
remove_character_data
method descriptor
¶
remove_character_data() -> None
Remove the character data from the element
remove_from_file
method descriptor
¶
remove_from_file(file: ArxmlFile) -> None
remove this element from a file. Does not affect parent elements. When an element is no longer part of any file it is deleted.
remove_sub_element
method descriptor
¶
remove_sub_element() -> None
Remove the given sub-element from this element
Removing the element invalidates it, and causes all of the removed elements children to be removed as well.
remove_sub_element_kind
method descriptor
¶
remove_sub_element_kind() -> None
Remove a sub-element with the given element name
If multiple sub-elements with the same name exist, only the first one is removed.
set_attribute
method descriptor
¶
set_attribute(attrname_str, value) -> None
set the given attribute to the provided value. If the attribute is valid for this element it will be created or modified as needed.
set_reference_target_relative
method descriptor
¶
set_reference_target_relative() -> None
Set the reference target of a reference element using a relative reference
ElementType ¶
Type of an Element in the specification
attributes_spec
property
¶
attributes_spec: List[AttributeSpec]
a list of the specifications of all attributes allowed on elements of this type
chardata_spec
property
¶
chardata_spec: CharacterDataType
the specification of the character data content of elements of this type
content_mode
property
¶
content_mode: ContentMode
content mode of the element type: Sequence, Choice, Bag, Characters, Mixed
is_ordered
property
¶
is_ordered: bool
ordered elements may not be sorted, since the sub element order is semantically meaningful
is_ref
property
¶
is_ref: bool
elements of this type must contain an autosar path in their character data, and have a DEST attribute
splittable
property
¶
splittable: List[AutosarVersion]
a list of AutosarVersions in which this element is splittable
std_restriction
property
¶
std_restriction: str
a string indication if the element is restricted to ClassicPlatform, AdaptivePlatform or NotRestricted
sub_elements_spec
property
¶
sub_elements_spec: List[SubElementSpec]
a list of the specifications of all sub elements allowed on elements of this type
find_attribute_spec
method descriptor
¶
find_attribute_spec(attrname_str) -> AttributeSpec
find the specification for the given attribute name
find_sub_element
method descriptor
¶
find_sub_element(
target_name: ElementName, version_obj
) -> ElementType
find the ElementType of the named sub element in the specification of this ElementType
reference_dest_value
method descriptor
¶
reference_dest_value(target: ElementType) -> EnumItem
helper to determine the correct value for the DEST attribute when setting a reference
splittable_in
method descriptor
¶
splittable_in(version: AutosarVersion) -> bool
is this element splittable in a particular AutosarVersion
IncompatibleAttributeError ¶
Information about an attribute that is incompatible with a given target version
IncompatibleAttributeValueError ¶
Information about an attribute value that is incompatible with a given target version
allowed_versions
property
¶
allowed_versions: List[AutosarVersion]
list of versions in which the attribute value is permitted on this attribute
IncompatibleElementError ¶
SubElementSpec ¶
Specification of a sub element
ValidSubElementInfo ¶
Details about a particular sub element
check_buffer
builtin
¶
check_buffer() -> bool
Check if the given buffer contains valid Autosar data
The function returns true if the buffer starts with a valid arxml header (after skipping whitespace and comments). This function does not check anything after the header.
check_file
builtin
¶
check_file(filename: str) -> bool
Check if the file contains arxml data. Returns true if an arxml file header is found and does not parse anything after it.