Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone parser with to json and back converters #4

Merged
merged 9 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[flake8]
max-line-length = 88
max-complexity = 16
# B = bugbear
# B9 = bugbear opinionated (incl line length)
select = C,E,F,W,B,B9
# E203: whitespace before ':' (black behaviour)
# E501: flake8 line length (covered by bugbear B950)
# W503: line break before binary operator (black behaviour)
ignore = E203,E501,W503
classmethod-decorators =
classmethod
validator
exclude = pytopas/lark_standalone.py
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"

- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "monthly"
54 changes: 54 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
# yamllint disable rule:line-length
name: Lint Code Base

on: # yamllint disable-line rule:truthy rule:comments
# allow this workflow to be called from other workflows
workflow_call:

jobs:
lint:
name: Lint Code Base
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8"]

steps:
- name: Checkout repository
uses: actions/checkout@v3
if: ${{ !env.ACT }} # skip during local actions testing
with:
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: pyproject.toml

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[lint,test]

- name: Autoflake
run: autoflake -c -r pytopas tests examples

- name: Black
run: black --check --diff .

- name: isort
run: isort -c --diff .

- name: flake8
run: flake8

- name: pylint
run: pylint .

- name: pyupgrade
run: |
find -name "*.py" -and -not -name "lark_standalone.py" -print0 |\
xargs -0 -n1 pyupgrade --py38-plus
39 changes: 39 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
# yamllint disable rule:line-length
name: PR action

on:
pull_request:
types: [opened, reopened, synchronize]

jobs:
lint:
uses: ./.github/workflows/linter.yml

test:
uses: ./.github/workflows/test.yml

build:
name: Build
runs-on: ubuntu-latest
needs: test

steps:
- name: Checkout repository
uses: actions/checkout@v3
if: ${{ !env.ACT }} # skip during local actions testing

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.11
cache: pip
cache-dependency-path: pyproject.toml

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[dev,release]

- name: Build
run: make dist
75 changes: 75 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
# yamllint disable rule:line-length
name: Push action

on:
push:
branches:
- master

jobs:
test:
uses: ./.github/workflows/test.yml

release:
name: Bump version and create draft release
runs-on: ubuntu-latest
needs: test
permissions:
contents: write
concurrency:
group: release

steps:
- name: Checkout repository
uses: actions/checkout@v3
if: ${{ !env.ACT }} # skip during local actions testing
with:
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.11
cache: pip
cache-dependency-path: pyproject.toml

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[release]

- name: Create bump and changelog
id: cz
if: github.repository == 'metis-science/pytopas'
uses: commitizen-tools/commitizen-action@4498afe6f2f1c5993fb29f22db7b597538c00aae
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
changelog_increment_filename: .CHANGELOG-CURRENT.md
push: true
commit: true

- name: Print new version
if: github.repository == 'metis-science/pytopas'
run: echo "Bumped to version ${{ steps.cz.outputs.version }}"

- name: Build
run: make dist

- name: Stop if no bump
id: no-bump
continue-on-error: true
# will fail if not on exact tag
run: git describe --tags --exact-match

- name: Create Release Draft
uses: softprops/action-gh-release@c9b46fe7aad9f02afd89b12450b780f52dacfb2d
if: steps.no-bump.outcome == 'success'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
draft: true
tag_name: v${{ steps.cz.outputs.version }}
body_path: .CHANGELOG-CURRENT.md
fail_on_unmatched_files: true
files: dist/*
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
# yamllint disable rule:line-length
name: Publish release to pypi

on:
release:
types: [published]

workflow_dispatch:

jobs:
pypi:
runs-on: ubuntu-latest
concurrency:
group: release

steps:
- name: Checkout repository
uses: actions/checkout@v3
if: ${{ !env.ACT }} # skip during local actions testing
with:
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.11
cache: pip
cache-dependency-path: pyproject.toml

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[release]

- name: Build
run: make dist

- name: Publish package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
2 changes: 0 additions & 2 deletions .github/workflows/reviewer.yml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this project seems to be down 😞

This file was deleted.

49 changes: 49 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
# yamllint disable rule:line-length
name: Run tests

on: # yamllint disable-line rule:truthy rule:comments
# allow this workflow to be called from other workflows
workflow_call:

workflow_dispatch:

jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- name: Checkout repository
uses: actions/checkout@v3
if: ${{ !env.ACT }} # skip during local actions testing
with:
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: pyproject.toml

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[test]

- name: Run tests
run: |
pytest --junitxml=junit/test-results-${{ matrix.python-version }}.xml

- name: Upload pytest test results
uses: actions/upload-artifact@v3
# Use always() to always run this step to publish test results
# when there are test failures
if: always()
with:
name: pytest-results-${{ matrix.python-version }}
path: junit/test-results-${{ matrix.python-version }}.xml
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.log
.CHANGELOG-CURRENT.md
.DS_Store
.coverage
.idea
dist/
private_examples/
tmp/
10 changes: 10 additions & 0 deletions Makefile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knopki wow, this is so C++ 😆

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DEFAULT_GOAL := parser
.PHONY: parser

pytopas/lark_standalone.py: pytopas/grammar.lark
PYTHONHASHSEED=0 python -m lark.tools.standalone -s topas $< > $@

parser: pytopas/lark_standalone.py

dist: pytopas/* pyproject.toml
flit build
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# pytopas

Bruker's TOPAS macro language parser.

## Usage

Parse TOPAS source code and convert it to json:

```python
from pytopas import TOPASParser

src = "a = a + 1 ; 0"

parser = TOPASParser()
tree = parser.parse(src)
print(tree.to_json(compact=True))
```

Convert JSON encoded TOPAS code back into TOPAS source code:

```python
from pytopas import TOPASParseTree

input_json = '["topas", ["a = a + 1 ; 0"]]'

tree = TOPASParseTree.from_json(input_json)
print(tree.to_topas())

```

## Command line utilities

After installing the package, two command line utilities will be available.

```
usage: topas2json [-h] [-c] file

Parse TOPAS input and output JSON

positional arguments:
file Path to TOPAS file or '-' for stdin input

options:
-h, --help show this help message and exit
-c, --compact Use compact output
```

```
usage: json2topas [-h] file

Parse JSON input and output TOPAS

positional arguments:
file Path to JSON file or '-' for stdin input

options:
-h, --help show this help message and exit

```

## Development

Install package with optional dependencies: `pip install -e .[dev,lint,test,release]`

Regenerate standalone `lark` parser after `grammar.lark` change: `make parser`
Loading