Skip to content

Commit

Permalink
Merge pull request #8 from knopki/pyparsing
Browse files Browse the repository at this point in the history
Long-awaited rewrite to pyparsing and AST
  • Loading branch information
blokhin committed Apr 15, 2024
2 parents 01f5564 + a23d3de commit e15670e
Show file tree
Hide file tree
Showing 230 changed files with 4,888 additions and 4,263 deletions.
5 changes: 3 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ max-complexity = 16
# B = bugbear
# B9 = bugbear opinionated (incl line length)
select = C,E,F,W,B,B9
# B905 `zip()` without an explicit `strict=` parameter
# 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
ignore = B905,B907,E203,E501,W503
classmethod-decorators =
classmethod
validator
exclude = pytopas/lark_standalone.py
per-file-ignores = __init__.py:F401
6 changes: 3 additions & 3 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8"]
python-version: ["3.11"]

steps:
- name: Checkout repository
Expand Down Expand Up @@ -50,5 +50,5 @@ jobs:

- name: pyupgrade
run: |
find -name "*.py" -and -not -name "lark_standalone.py" -print0 |\
xargs -0 -n1 pyupgrade --py38-plus
find -name "*.py" -print0 |\
xargs -0 -n1 pyupgrade --py38-plus --keep-runtime-typing
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@ This is the parser for Bruker's TOPAS macro language. We have compared two canon
Parse TOPAS input and convert it to JSON:

```python
import json
from pytopas import TOPASParser

src = "a = a + 1 ; 0"
src = "a(b,c)"

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

Convert parser's JSON-encoded TOPAS code back into the TOPAS input format:

```python
import json
from pytopas import TOPASParseTree

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

tree = TOPASParseTree.from_json(input_json)
print(tree.to_topas())
input_json = """
["topas",
["formula",
["func_call", "a",
["formula", ["p", {"n": ["parameter_name", "b"]}]],
["formula", ["p", {"n": ["parameter_name", "c"]}]]]]]
"""
serialized = json.loads(input_json)
src = TOPASParser.reconstruct(serialized)
print(src)

```

Expand All @@ -33,16 +40,16 @@ print(tree.to_topas())
After installing the package, two command line utilities will be available.

```
usage: topas2json [-h] [-c] file
usage: topas2json [-h] [--ignore-warnings] file
Parse TOPAS input and output JSON
positional arguments:
file Path to TOPAS file or '-' for stdin input
file Path to TOPAS file or '-' for stdin input
options:
-h, --help show this help message and exit
-c, --compact Use compact output
-h, --help show this help message and exit
--ignore-warnings Don't print parsing warnings
```

```
Expand All @@ -60,9 +67,7 @@ options:

## Development

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

Regenerate standalone `lark` parser after `grammar.lark` change: `make parser`
Install package with optional dependencies: `pip install -e .[lint,test,release]`

## License

Expand Down
20 changes: 6 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "pytopas"
version = "0.0.1"
version = "0.0.3"
description = """Bruker's TOPAS macro language parser"""
authors = [{name = "Sergei Korolev", email = "knopki@duck.com"}]
readme = "README.md"
Expand All @@ -30,12 +30,11 @@ classifiers = [
keywords = ["TOPAS", "XRPD", "Rietveld refinement", "pair distribution function", "stacking-faults", "charge flipping", "structure solution", "deconvolution"]
requires-python = ">=3.8"
dependencies = [
"pyparsing >= 3",
"typing-extensions >= 4.2.0; python_version < '3.11'",
]

[project.optional-dependencies]
dev = [
"lark >= 1.1.7",
]
lint = [
"autoflake",
"black >= 23.1",
Expand Down Expand Up @@ -71,10 +70,8 @@ ignore-init-module-imports = true
remove-all-unused-imports = true
remove-duplicate-keys = true
remove-unused-variables = true
exclude = "pytopas/lark_standalone.py"

[tool.black]
exclude = "pytopas/lark_standalone.py"
target-version = ['py38', 'py39', 'py310', 'py311']

[tool.commitizen]
Expand All @@ -83,27 +80,22 @@ tag_format = "v$version"
major_version_zero = true
version_files = [
"pyproject.toml:^version",
"pytopas/__init__.py:^__version__",
"pytopas/__init__.py:^__VERSION__",
]
version_provider = "pep621"
update_changelog_on_bump = true

[tool.coverage.run]
omit = ['pytopas/lark_standalone.py', 'tests/*']
omit = ['tests/*']

[tool.flit.module]
name = "pytopas"

[tool.flit.sdist]
include = ["pytopas/grammar.lark"]

[tool.isort]
profile = "black"
py_version = 38
skip = ["pytopas/lark_standalone.py"]

[tool.pylint.MASTER]
ignore = "lark_standalone.py"
load-plugins=[
"pylint_per_file_ignores",
]
Expand All @@ -115,7 +107,7 @@ recursive = true
suggestion-mode = true

[tool.pylint.basic]
good-names = ["id", "x", "xy", "y", "_", "__"]
good-names = ["id", "kv", "op", "x", "xy", "y", "_", "__"]

[tool.pylint.design]
max-args = 7
Expand Down
8 changes: 2 additions & 6 deletions pytopas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
"TOPAS parser"

from .lark_standalone import UnexpectedToken
from .parser import TOPASParser
from .tree import TOPASParseTree
from .parser import Parser as TOPASParser

__VERSION__ = "0.0.1"

__all__ = ["TOPASParser", "TOPASParseTree", "UnexpectedToken"]
__VERSION__ = "0.0.3"
Loading

0 comments on commit e15670e

Please sign in to comment.