Skip to content

Commit

Permalink
Create enumecg executable
Browse files Browse the repository at this point in the history
Currently the executable is a stub, and this commit merely sets up the
necessary modules, tests and documentation. The purpose is to allow
most of the functionality of the library (config options etc.) to from
command line.
  • Loading branch information
jasujm committed May 6, 2020
1 parent 770d1d4 commit 4103f6f
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 4 deletions.
30 changes: 28 additions & 2 deletions docs/enumecglib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ values of the enum members are obvious in this style.

.. _enumecg-definition-from-dict:

Creating C++ enum from a dict
.............................
Creating C++ enum from a mapping
................................

This is a convenient method if the enum definitions are loaded from a
file using general purpose serialization format like JSON or YAML.
Expand Down Expand Up @@ -371,6 +371,32 @@ enhanced enum type. The doxygen documentation of
:ref:`enumecg-primary-enum` also includes the possible docstring of
the Python enum definition.

.. _enumecg-cli:

Command line interface
----------------------

The :mod:`enumecg` module can be invoked as a command. Given a YAML
file ``status.yaml``:

.. literalinclude:: examples/status.yaml
:language: yaml

The command line interface can use this file as an input to print the
generated code to stdout.

.. code-block:: console
$ enumecg status.yaml
... # C++ definition printed to stdout
The input file is a single YAML document containing an enum
definition. See :ref:`enumecg-definition-from-dict` for the details of
the schema.

Invoking ``enumecg --help`` will list the supported options and
arguments.

.. _enumecg-high-level-api:

High level API
Expand Down
8 changes: 8 additions & 0 deletions docs/examples/status.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
typename: Status
members:
- name: INITIALIZING
value: initializing
- name: WAITING_FOR_INPUT
value: waitingForInput
- name: BUSY
value: busy
2 changes: 2 additions & 0 deletions python/enumecg/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .cli import cli
cli()
30 changes: 30 additions & 0 deletions python/enumecg/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
EnumECG command line interface
..............................
Contains the command line interface to EnumECG. The entry point is
:func:`cli()`.
"""

import click
import yaml

from . import generate

@click.command()
@click.argument("file", type=click.File())
def cli(file):
"""Generate C++ boilerplate for an Enhanced Enum definition
This executable is a part of the Enhanced Enum library. It is used
to generate the necessary C++ boilerplate to make an enumeration
type work with the library.
For a full discussion of the purpose of the library, and a
detailed description of the code generation process, see:
https://enhanced-enum.readthedocs.io/en/latest/
"""
enum = yaml.safe_load(file)
click.echo(generate(enum))
12 changes: 11 additions & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,24 @@ def get_package_dunder(name):
license="MIT",
url="https://github.com/jasujm/enhanced-enum",
packages=["enumecg"],
entry_points={
"console_scripts": ["enumecg=enumecg.__main__:main"],
},
package_data={"enumecg": ["templates/*.in", "templates/doxygen/*.in"]},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.7",
install_requires=["Jinja2>=2.10", "regex", "inflect>=3.0", "docstring-parser>=0.6"],
install_requires=[
"Jinja2>=2.10",
"regex",
"inflect>=3.0",
"docstring-parser>=0.6",
"PyYAML>=5.3",
"click>=7.1",
],
)

# This is to bootstrap the build in case it is made with Enhanced Enum
Expand Down
24 changes: 24 additions & 0 deletions python/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import yaml
import pytest

from click.testing import CliRunner

from enumecg import generate
from enumecg.cli import cli

@pytest.fixture
def cli_runner():
"""Return CliRunner() to invoke enumecg CLI"""
return CliRunner()

@pytest.fixture
def enum_file(tmpdir, status_definition_dict):
"""Return path to an YAML file containing serialized :const:`conftest.STATUS_DEFINITION_DICT`"""
p = tmpdir.join("enum.yaml")
with open(p, "w") as f:
yaml.dump(status_definition_dict, f)
return p

def test_cli_should_generate_enum_definition_from_file(cli_runner, enum_file, status_definition):
result = cli_runner.invoke(cli, [str(enum_file)])
assert result.output == generate(status_definition) + "\n"
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ breathe==4.18.0 # via -r requirements-dev.in
certifi==2020.4.5.1 # via requests
cffi==1.14.0 # via cryptography
chardet==3.0.4 # via requests
click==7.1.2 # via black
click==7.1.2 # via -c requirements.txt, black
cryptography==2.9.2 # via secretstorage
distlib==0.3.0 # via virtualenv
docutils==0.16 # via breathe, readme-renderer, sphinx
Expand Down
2 changes: 2 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ jinja2>=2.10
regex
inflect>=3.0
docstring-parser>=0.6
PyYAML>=5.3
click>=7.1
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#
# pip-compile
#
click==7.1.2 # via -r requirements.in
docstring-parser==0.7.1 # via -r requirements.in
importlib-metadata==1.6.0 # via inflect
inflect==4.1.0 # via -r requirements.in
jinja2==2.11.2 # via -r requirements.in
markupsafe==1.1.1 # via jinja2
pyyaml==5.3.1 # via -r requirements.in
regex==2020.4.4 # via -r requirements.in
zipp==3.1.0 # via importlib-metadata

0 comments on commit 4103f6f

Please sign in to comment.