diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e667984 --- /dev/null +++ b/.gitignore @@ -0,0 +1,177 @@ +# Mac OS +.DS_Store + +# VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/README.md b/README.md index b547a24..2918c33 100644 --- a/README.md +++ b/README.md @@ -6,16 +6,34 @@ | Git | [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org) | | Formatting | [![Black](https://img.shields.io/badge/Code%20Style-black-000000)](https://github.com/psf/black) [![docformatter](https://img.shields.io/badge/Docstring%20Formatter-docformatter-fedcba.svg)](https://github.com/PyCQA/docformatter) [![numpy](https://img.shields.io/badge/Docstring%20Style-numpy-459db9.svg)](https://numpydoc.readthedocs.io/en/latest/format.html) [![Imports: isort](https://img.shields.io/badge/%20Imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/) | -Repo to store universal communication specification and parser. +DAIDE parser using [parsimonious](https://github.com/erikrose/parsimonious). Parsimonious is a python package that uses "a simplified sort of EBNF notation" to define a grammar. The parser currently supports all 130 levels of DAIDE. - The original DAIDE specification is [here](daide-syntax.pdf) - The working markdown document that will included DAIDE enhancements is [here](daide-specification.md) -- The machine-parsable grammar can be found in [this `.py` file](./daidepp/parser/grammar.py) +- The machine-parsable grammar can be found in [this `.py` file](./src/daidepp/grammar.py) + +## Use + +Using the grammar in grammar.py, you can create a parse tree from a DAIDE press message or reply. The nodes of the parse tree can be visited to return something more useful. The visiting rules for each of the nodes of the parse tree are defined in node_visitor.py. + +Example: + +```python3 +>>> from daidepp import create_daide_grammar, daide_visitor +>>> grammar = create_daide_grammar(level=130) +>>> message = 'PRP (AND (SLO (ENG)) (SLO (GER)) (SLO (RUS)) (AND (SLO (ENG)) (SLO (GER)) (SLO (RUS))))' +>>> parse_tree = grammar.parse(message) +>>> output = daide_visitor.visit(parse_tree) +>>> print(output) +('PRP', ('AND', [('SLO', 'ENG'), ('SLO', 'GER'), ('SLO', 'RUS'), ('AND', [('SLO', 'ENG'), ('SLO', 'GER'), ('SLO', 'RUS')])])) +``` + +If the DAIDE token is not in the grammar or if the message is malformed, the parser will just thrown an exception. We're currently working on returning a list of unrecognized tokens instead of just erroring out. ## Pull Requests Three files should be updated whenever making a PR: -- [`grammar.py`](./daidepp/parser/grammar.py): the machine-readable grammar -- [`node_visitor.py`](./daidepp/parser/node_visitor.py): the visitor object to parse a message +- [`grammar.py`](./src/daidepp/grammar.py): the machine-readable grammar +- [`node_visitor.py`](./src/daidepp/node_visitor.py): the visitor object to parse a message - [The daide markdown specification](./daide-specification.md): the human-readable specification diff --git a/daidepp/README.md b/daidepp/README.md deleted file mode 100644 index 9b5df47..0000000 --- a/daidepp/README.md +++ /dev/null @@ -1,20 +0,0 @@ -# daidepp -DAIDE parser using [parsimonious](https://github.com/erikrose/parsimonious). Parsimonious is a python package that uses "a simplified sort of EBNF notation" to define a grammar. The parser currently supports all 130 levels of DAIDE. - -## How to use -Using the grammar in grammar.py, you can create a parse tree from a DAIDE press message or reply. The nodes of the parse tree can be visited to return something more useful. The visiting rules for each of the nodes of the parse tree are defined in node_visitor.py. - -Example: -```python3 ->>> parse_tree = grammar.parse('PRP (AND (SLO (ENG)) (SLO (GER)) (SLO (RUS)) (AND (SLO (ENG)) (SLO (GER)) (SLO (RUS))))') ->>> dv = DaideVisitor() ->>> output = dv.visit(parse_tree) ->>> print(output) -('PRP', - ('AND', - [('SLO', 'ENG'), - ('SLO', 'GER'), - ('SLO', 'RUS'), - ('AND', [('SLO', 'ENG'), ('SLO', 'GER'), ('SLO', 'RUS')])])) -``` -If the DAIDE token is not in the grammar or if the message is malformed, the parser will just thrown an exception. We're currently working on returning a list of unrecognized tokens instead of just erroring out. \ No newline at end of file diff --git a/daidepp/requirements.txt b/daidepp/requirements.txt deleted file mode 100644 index 76b5283..0000000 --- a/daidepp/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -parsimonious==0.9.0 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..6dae23a --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools>=65"] +build-backend = "setuptools.build_meta" diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..13f006f --- /dev/null +++ b/setup.cfg @@ -0,0 +1,64 @@ +[metadata] +name = daidepp +version = 0.1.0 +author = Byung Oh +author_email = byung.oh@cynnovative.com +description = "DAIDE Parser" +long_description = file: README.md +long_description_content_type = text/markdown + +[options] +package_dir = + =src +packages = find: +python_requires = >=3.7 +install_requires = + parsimonious==0.9.0 + +[options.packages.find] +where = src + +[options.extras_require] +dev = + black>=22.8.0 + docformatter==1.4 + isort>=5.10.1 + pydocstyle==6.1.1 + pylint==2.12.2 + pytest==7.0.0 + pytest-cov==3.0.0 + pytest-dependency==0.5.1 + pytest-mypy==0.9.1 + pytest-timeout==2.1.0 + +[isort] +multi_line_output = 3 +include_trailing_comma = True +force_grid_wrap = 0 +use_parentheses = True +line_length = 88 +known_first_party = daidepp + +[mypy] +ignore_missing_imports = True +disallow_untyped_calls = False +disallow_untyped_defs = True +disallow_incomplete_defs = True +files = src/ +exclude = tests/ +follow_imports = silent + +[tool:pytest] +testpaths = tests +addopts = + --cov=daidepp + --cov-report html +timeout = 10 + +[pylint.MESSAGES CONTROL] +extension-pkg-whitelist = + pydantic + +[pydocstyle] +convention = numpy +add_ignore = D2,D105,D101,D100,D104,D106 \ No newline at end of file diff --git a/src/daidepp/__init__.py b/src/daidepp/__init__.py new file mode 100644 index 0000000..734318c --- /dev/null +++ b/src/daidepp/__init__.py @@ -0,0 +1,2 @@ +from daidepp.grammar import create_daide_grammar +from daidepp.node_visitor import daide_visitor diff --git a/daidepp/parser/grammar.py b/src/daidepp/grammar.py similarity index 100% rename from daidepp/parser/grammar.py rename to src/daidepp/grammar.py diff --git a/daidepp/parser/node_visitor.py b/src/daidepp/node_visitor.py similarity index 100% rename from daidepp/parser/node_visitor.py rename to src/daidepp/node_visitor.py