Autosar-Data Python Library
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
-
TypeError: cannot unpack non-iterable Workspace object
cause Attempting to assign the result of `Workspace.from_file()` or `Workspace.from_string()` to a single variable (`workspace = ...`) after version 0.11.0, when these methods started returning a tuple `(workspace, errors)`.fixChange the assignment to unpack the tuple: `workspace, errors = Workspace.from_file('path/to/file.arxml')`. -
AttributeError: 'Workspace' object has no attribute 'parse_string'
cause Trying to call the `parse_string` method on an `AutosarData` (or `Workspace`) object after version 0.14.0, where it was renamed.fixReplace `parse_string` with `from_string`: `workspace, errors = AutosarData.from_string('<ARXML>...</ARXML>')`. -
FileNotFoundError: [Errno 2] No such file or directory: 'your_file.arxml'
cause The ARXML file specified in `Workspace.from_file()` does not exist at the provided path or the path is incorrect.fixVerify that the file path is correct and accessible. Ensure the file exists and that the current working directory or the absolute path is properly specified.
Warnings
- breaking Starting with version 0.15.0, `autosar-data` dropped support for Python 3.9 and 3.10. It now requires Python 3.11 or newer.
- breaking Since version 0.11.0, the methods `Workspace.from_file()` and `Workspace.from_string()` return a tuple `(workspace, errors)` instead of just the `workspace` object. Old code expecting only the workspace will raise a `TypeError`.
- breaking In version 0.14.0, the method `AutosarData.parse_string()` was renamed to `AutosarData.from_string()`. Using the old name will result in an `AttributeError`.
- gotcha The library can be strict with ARXML file structure and content according to the AUTOSAR standard. Malformed or non-compliant ARXML files may lead to parsing errors or unexpected behavior during loading.
Install
-
pip install autosar-data
Imports
- Workspace
from autosar_data import Workspace
- create_container
from autosar_data.element import create_container
- AutosarData
from autosar_data import AutosarData
Quickstart
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)