-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4563ff3
feat(parser): create standalone parser
knopki be9e311
style(gitignore): create gitignore
knopki c07017a
build(linters): setup
knopki cc3143f
ci(actions): create
knopki a0cd57b
feat: topas to json and back
knopki 03e02cd
feat: tests, lint, docs
knopki 8f87202
fix: uncompress parser
knopki 30d1c44
fix(typings): tree generic
knopki bc32dc2
refactor(token): backport to 3.8
knopki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @knopki wow, this is so C++ 😆 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😞