Autosar-Data Python Library

0.15.0 · active · verified Thu Apr 16

The `autosar-data` library provides tools to read, write, and modify AUTOSAR ARXML data using Python. It offers an object-oriented API for interacting with AUTOSAR workspaces, packages, and elements, simplifying development with ARXML files. The current version is 0.15.0, and it follows an infrequent but impactful release cadence, often introducing breaking changes related to API and Python version support between minor versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an empty AUTOSAR workspace, add a package structure, create container and text elements, and then print the resulting ARXML content to a string. It also includes an example of how to load an ARXML file and access its content, highlighting the return value change for loading methods.

from autosar_data import Workspace
from autosar_data.element import create_container
import os

# Create an empty workspace
workspace = Workspace.create_empty()

# Add a package structure
packages = workspace.find_or_create_package('/MyProject/Packages')

# Add a container element to the package
container = create_container('ECU_CONFIGURATION', parent=packages)
container.create_text_element('SHORT-NAME', 'MyEcuConfig')
container.create_text_element('TYPE', 'ECU_EXTRACT')

# Add a sub-element to the container
sub_element = create_container('PARAMETERS', parent=container)
sub_element.create_text_element('VALUE', '123')

# Print the ARXML content to console
arxml_string = workspace.to_string()
print("Generated ARXML content:")
print(arxml_string)

# Example of loading from a file (requires a dummy file for execution)
# Create a dummy ARXML file for demonstration
dummy_arxml_path = 'dummy_example.arxml'
with open(dummy_arxml_path, 'w') as f:
    f.write(arxml_string)

try:
    # Load the workspace from the dummy file
    # Note: Workspace.from_file returns (workspace, errors) since v0.11.0
    loaded_workspace, errors = Workspace.from_file(dummy_arxml_path)

    if errors:
        print(f"\nErrors found during loading: {errors}")
    else:
        print(f"\nSuccessfully loaded workspace from {dummy_arxml_path}.")
        # Access a value from the loaded workspace
        loaded_value_element = loaded_workspace.find('/MyProject/Packages/ECU_CONFIGURATION/PARAMETERS/VALUE')
        if loaded_value_element:
            print(f"Loaded value: {loaded_value_element.text_content}")

except FileNotFoundError:
    print(f"Error: File not found at {dummy_arxml_path}")
finally:
    # Clean up the dummy file
    if os.path.exists(dummy_arxml_path):
        os.remove(dummy_arxml_path)

view raw JSON →