-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Add CLI and add validation commands
Adding a CLI that can validate Dragonfly schema JSONs and can be extended by estensions.
- Loading branch information
1 parent
1806c15
commit 60ec048
Showing
11 changed files
with
221 additions
and
12 deletions.
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 |
---|---|---|
|
@@ -12,3 +12,4 @@ tox.ini | |
/.vscode | ||
.eggs | ||
*.code-workspace | ||
dragonfly.log* |
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
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
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,6 @@ | ||
dragonfly command line interface | ||
=============================== | ||
|
||
.. click:: dragonfly.cli:main | ||
:prog: dragonfly | ||
:show-nested: |
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
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
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,4 @@ | ||
from dragonfly.cli import main | ||
|
||
if __name__ == '__main__': | ||
main() |
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,82 @@ | ||
""" | ||
Command Line Interface (CLI) entry point for dragonfly and dragonfly extensions. | ||
Use this file only to add commands related to dragonfly-core. For adding extra commands | ||
from each extention see below. | ||
Dragonfly is using click (https://click.palletsprojects.com/en/7.x/) for creating the CLI. | ||
You can extend the command line interface from inside each extention by following these | ||
steps: | ||
1. Create a ``cli`` folder in your extension. | ||
2. Import the ``main`` function from this ``dragonfly.cli``. | ||
3. Add your commands and command groups to main using add_command method. | ||
4. Add ``import [your-extention].cli`` to ``__init__.py`` file to the commands are added | ||
to the cli when the module is loaded. | ||
Good practice is to group all your extention commands in a command group named after | ||
the extension. This will make the commands organized under extension namespace. For | ||
instance commands for `dragonfly-energy` will be called like ``dragonfly energy [energy-command]``. | ||
.. code-block:: python | ||
import click | ||
from dragonfly.cli import main | ||
@click.group() | ||
def energy(): | ||
pass | ||
# add commands to energy group | ||
@energy.command('to-idf') | ||
# ... | ||
def to_idf(): | ||
pass | ||
# finally add the newly created commands to dragonfly cli | ||
main.add_command(energy) | ||
# do not forget to import this module in __init__.py otherwise it will not be added | ||
# to dragonfly commands. | ||
Note: | ||
For extension with several commands you can use a folder structure instead of a single | ||
file. | ||
""" | ||
|
||
try: | ||
import click | ||
except ImportError: | ||
raise ImportError( | ||
'click module is not installed. Try `pip install dragonfly-core[cli]` command.' | ||
) | ||
|
||
from dragonfly.cli.validate import validate | ||
|
||
import sys | ||
import os | ||
import logging | ||
import json | ||
|
||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
@click.group() | ||
@click.version_option() | ||
def main(): | ||
pass | ||
|
||
|
||
@main.command('viz') | ||
def viz(): | ||
"""Check if dragonfly is flying!""" | ||
click.echo('viiiiiiiiiiiiizzzzzzzzz!') | ||
|
||
|
||
main.add_command(validate) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,56 @@ | ||
"""dragonfly validation commands.""" | ||
|
||
try: | ||
import click | ||
except ImportError: | ||
raise ImportError( | ||
'click is not installed. Try `pip install . [cli]` command.' | ||
) | ||
|
||
from dragonfly.model import Model | ||
|
||
import sys | ||
import os | ||
import logging | ||
import json | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import dragonfly_schema.model as schema_model | ||
except ImportError: | ||
_logger.exception( | ||
'dragonfly_schema is not installed. Try `pip install . [cli]` command.' | ||
) | ||
|
||
|
||
@click.group(help='Commands for validating Dragonfly JSON files.') | ||
def validate(): | ||
pass | ||
|
||
@validate.command('model') | ||
@click.argument('model-json') | ||
def validate_model(model_json): | ||
"""Validate a Model JSON file against the Dragonfly schema. | ||
\b | ||
Args: | ||
model_json: Full path to a Model JSON file. | ||
""" | ||
try: | ||
assert os.path.isfile(model_json), 'No JSON file found at {}.'.format(model_json) | ||
|
||
# validate the Model JSON | ||
click.echo('Validating Model JSON ...') | ||
schema_model.Model.parse_file(model_json) | ||
click.echo('Pydantic validation passed.') | ||
with open(model_json) as json_file: | ||
data = json.load(json_file) | ||
parsed_model = Model.from_dict(data) | ||
parsed_model.check_missing_adjacencies(raise_exception=True) | ||
click.echo('Python re-serialization passed.') | ||
click.echo('Congratulations! Yout Model JSON is valid!') | ||
except Exception as e: | ||
_logger.exception('Model validation failed.\n{}'.format(e)) | ||
sys.exit(1) | ||
else: | ||
sys.exit(0) |
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
setuptools.setup( | ||
name="dragonfly-core", | ||
use_scm_version = True, | ||
use_scm_version=True, | ||
setup_requires=['setuptools_scm'], | ||
author="Ladybug Tools", | ||
author_email="[email protected]", | ||
|
@@ -20,6 +20,12 @@ | |
url="https://github.com/ladybug-tools/dragonfly-core", | ||
packages=setuptools.find_packages(exclude=["tests"]), | ||
install_requires=requirements, | ||
extra_requires={ | ||
'cli': ['click>=5.1', 'dragonfly-schema>=1.2.0'] | ||
}, | ||
entry_points={ | ||
"console_scripts": ["dragonfly = dragonfly.cli:main"] | ||
}, | ||
classifiers=[ | ||
"Programming Language :: Python :: 2.7", | ||
"Programming Language :: Python :: 3.6", | ||
|
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 @@ | ||
"""Test cli.""" | ||
|
||
from click.testing import CliRunner | ||
from dragonfly.cli import viz | ||
|
||
|
||
def test_viz(): | ||
runner = CliRunner() | ||
result = runner.invoke(viz) | ||
assert result.exit_code == 0 | ||
assert result.output.startswith('vi') | ||
assert result.output.endswith('z!\n') |