Code Monkey home page Code Monkey logo

a2lparser's Introduction

Python A2L Parser

Main Workflow PyPI Workflow Build Workflow Flake8 Workflow codecov License: GPL v3

Overview

The Python A2L Parser is a tool designed to parse A2L files compliant with the ASAM MCD-2 MC Data Model for ECU Measurement and Calibration. Implemented in Python using PLY, it constructs an Abstract Syntax Tree (AST) from A2L files, enabling structured data access and utility functions such as searching. All resources used in development are sourced from publicly available information, including the ASAM Wiki.

The parser supports ASAM MCD-2 MC Version 1.7.1 and is focused on parsing A2L grammar without providing mapping capabilities. Additionally, the module includes functionality for converting parsed A2L files into simpler formats like XML, JSON, and YAML.

This repository can be used for interpreting or validating A2L files, extending its functionality, or for educational purposes.

Note: Released under the GPL license with no warranty, this project is recommended for educational use. For professional solutions, consider specialized tools such as the MATLAB Vehicle Network Toolbox or the Vector ASAP2 Toolset.

Installation

To install the A2L Parser, run:

Note: Until I fix some more minor issues and create a release version, a TestPyPi version is uploaded which you can use:

pip install -i https://test.pypi.org/simple/ a2lparser --extra-index-url https://pypi.org/simple/

Usage as Module

from a2lparser.a2lparser import A2LParser
from a2lparser.a2lparser_exception import A2LParserException

try:
    # Create a parser and parse files.
    # Allows multiple files to be passed with wildcards.
    # Will only print errors, no information like progressbar.
    # Returns a dictionary.
    ast_dict = A2LParser(log_level="INFO").parse_file("./testfiles/test_*.a2l")

    # The dictionary holds the AbstractSyntaxTree object under the file name key.
    ast = ast_dict["test_1.a2l"]

    # Dictionary access on the abstract syntax tree.
    # Returns a Python dictionary.
    project = ast["PROJECT"]
    module = project["MODULE"]
    print(f"Project {project['Name']} with module: {module['Name']}"

    # Searches for all MEASUREMENT sections.
    # find_section returns an AbstractSyntaxTree
    measurements = ast.find_sections("MEASUREMENT")

    # All found MEASUREMENT sections are under the "MEASUREMENT" key
    measurements_list = measurements["MEASUREMENT"]

    print(f"Found {len(measurements_list)} MEASUREMENT sections.")

except A2LParserException as ex:
    # Catching A2LParserException:
    # Generally occurs when a fatal error in parsing is encountered,
    # or if the generated AST is empty (i.e., no data could be parsed).
    print(ex)

Usage from CLI

a2lparser --help
usage: a2lparser [-h] [-x] [-j] [-y] [--output-dir [PATH]] [--prompt] [--quiet] [--no-optimize] [--no-validation]
                 [--gen-ast [CONFIG]] [--log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [--version]
                 [file]

positional arguments:
  file                  A2L files to parse

options:
  -h, --help            show this help message and exit
  -x, --xml             Converts an A2L file to a XML output file
  -j, --json            Converts an A2L file to a JSON output file
  -y, --yaml            Converts an A2L file to a YAML output file
  --output-dir [PATH]   Output directory for converted files
  --prompt              Enables CLI prompt after parsing
  --quiet               Disables console output
  --no-optimize         Disables optimization mode
  --no-validation       Disables possible A2L validation warnings
  --gen-ast [CONFIG]    Generates python file containing AST node classes
  --log-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
  --version             show program's version number and exit

a2lparser's People

Contributors

mrom1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

a2lparser's Issues

Pip packaging

Hi! I was wondering if we could make this a package and host it on pip?

I could help do this by making the necessary changes. Just wanted to get feedback from the main contributors if that's a direction we would like to go for.

A2L combine with S19

Hello Michael,
Pretty appreciate you implement these code for a2l parser.
I just wonder if you have a plan to combine a2l with s19.
Because a2lparse is just a explanation but not a mapping tool.
Theoretically, for an S19 file, we can use A2L to clarify the value corresponding to a variable. I don't see such a project in github at the moment, if you see such a project, please point it out, thank you~

Chenrui

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.