https://github.com/sauci/pya2l
https://github.com/sauci/pya2l
a2l asam-mcd-2mc automotive python3
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sauci/pya2l
- Owner: Sauci
- License: bsd-3-clause
- Created: 2018-03-21T16:56:36.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-03-12T08:14:11.000Z (7 months ago)
- Last Synced: 2025-03-29T22:04:25.260Z (6 months ago)
- Topics: a2l, asam-mcd-2mc, automotive, python3
- Language: Python
- Homepage:
- Size: 2.13 MB
- Stars: 37
- Watchers: 5
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
| branch | build | coverage |
|:------------------------------------------------------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------:|
| [master](https://github.com/Sauci/pya2l/tree/master) | [](https://github.com/Sauci/pya2l/actions/workflows/build.yml) | [](https://codecov.io/gh/Sauci/pya2l?branch=master) |
| [develop](https://github.com/Sauci/pya2l/tree/develop) | [](https://github.com/Sauci/pya2l/actions/workflows/build.yml) | [](https://codecov.io/gh/Sauci/pya2l?branch=develop) |[](https://raw.githubusercontent.com/Sauci/pya2l/master/LICENSE.md) [](https://gitter.im/pya2l/Lobby)
## Package description
the purpose of this package is to provide an easy way to access and navigate
in [a2l](https://www.asam.net/standards/detail/mcd-2-mc/) formatted file.
once the file has been loaded, a tree of Python objects is generated, allowing the user to access nodes.## Installation
### Using *pip*
Install the last released version of the package by running the following command:
`pip install pya2l`or install the most recent version of the package (master branch) by running the following command:
`pip install git+https://github.com/Sauci/pya2l.git@master`## Example of usage
### Command line tool
Once the package installed, the `pya2l` command will be available. It provides several different commands:
- Convert an A2L file to JSON with `pya2l -v .a2l to_json -o -i 2`
- Convert an A2L file to A2L with `pya2l -v to_a2l -o -i 2`
- Convert a JSON-formatted A2L file to JSON with `pya2l -v .json to_json -o -i 2`
- Convert a JSON-formatted A2L file to A2L with `pya2l -v .json to_a2l -o -i 2`### Python API
the bellow code snippet shows how properties of a node in an a2l string can be retrieved using this package.
```python
from pya2l.parser import A2lParser as Parsera2l_string = """/begin PROJECT project_name "example project"
/begin MODULE first_module "first module long identifier"
/begin CHARACTERISTIC
example_of_characteristic
"first characteristic long identifier"
VALUE
0
DAMOS_SST
0
first_characteristic_conversion
-4.5
12.0
/end CHARACTERISTIC
/end MODULE
/end PROJECT
"""with Parser() as p:
# get the AST.
ast = p.tree_from_a2l(a2l_string.encode())# get a list of available properties for a specific node.
assert set(ast.PROJECT.properties) == {'Name', 'LongIdentifier', 'HEADER', 'MODULE'}# access nodes explicitly.
assert ast.PROJECT.MODULE[0].CHARACTERISTIC[0].Name.Value == 'example_of_characteristic'
assert ast.PROJECT.MODULE[0].CHARACTERISTIC[0].LowerLimit.Value == -4.5
assert ast.PROJECT.MODULE[0].CHARACTERISTIC[0].UpperLimit.Value == 12.0a2l_string = """/begin PROJECT project_name "example project"
/begin MODULE first_module "first module long identifier"
/end MODULE
/end PROJECT
"""with Parser() as p:
# get the AST.
ast = p.tree_from_a2l(a2l_string.encode())# convert node to json-formatted string.
assert p.json_from_tree(ast, indent=2).decode() == """{
"PROJECT": {
"Name": {
"Value": "project_name"
},
"LongIdentifier": {
"Value": "example project"
},
"MODULE": [
{
"Name": {
"Value": "first_module"
},
"LongIdentifier": {
"Value": "first module long identifier"
}
}
]
}
}"""
```