Skip to content

Commit

Permalink
feat(cli): Add a cli in that can be used from different interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey committed Aug 9, 2024
1 parent c4863c6 commit fdf75eb
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r cli-requirements.txt
pip install -r dev-requirements.txt
- name: run tests
run: python -m pytest --cov=. tests/
Expand Down Expand Up @@ -52,6 +53,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r cli-requirements.txt
pip install -r dev-requirements.txt
- name: install semantic-release
run:
Expand Down Expand Up @@ -95,6 +97,7 @@ jobs:
- name: install dependencies
run: |
pip install -U .
pip install -r cli-requirements.txt
pip install -r dev-requirements.txt
sphinx-apidoc -f -e -d 4 -o ./docs ./ladybug_geometry_polyskel
sphinx-build -b html ./docs ./docs/_build/docs
Expand Down
1 change: 1 addition & 0 deletions cli-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
click>=7.1.2
4 changes: 4 additions & 0 deletions ladybug_geometry_polyskel/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ladybug_geometry_polyskel.cli import geometry

if __name__ == '__main__':
geometry()
83 changes: 83 additions & 0 deletions ladybug_geometry_polyskel/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""ladybug-geometry-polyskel commands."""
import click
import sys
import logging
import json

from ladybug_geometry.geometry3d import Face3D
from ladybug_geometry_polyskel.polysplit import perimeter_core_subfaces as \
lgp_perimeter_core_subfaces

_logger = logging.getLogger(__name__)


# command group for all geometry extension commands.
@click.group(help='ladybug geometry polyskel commands.')
@click.version_option()
def main():
pass


@main.command('perimeter-core-subfaces')
@click.argument('face3d-file', type=click.Path(
exists=True, file_okay=True, dir_okay=False, resolve_path=True))
@click.argument('perimeter-offset', type=float)
@click.option(
'--tolerance', '-t', help='The maximum difference between x, y, and z '
'values at which vertices are considered equivalent.',
type=float, default=1e-5)
@click.option(
'--output-file', help='Optional file to output the he string of the visualization '
'file contents. By default, it will be printed out to stdout',
type=click.File('w'), default='-', show_default=True)
def perimeter_core_subfaces_cli(face3d_file, perimeter_offset, tolerance, output_file):
"""Compute perimeter and core Face3Ds using the straight skeleton of input Face3Ds.
\b
Args:
face3d_file: Full path to a JSON file containing an array of Face3D
objects for which core/perimeter Face3Ds will be computed.
perimeter_offset: Distance to offset perimeter sub-faces.
"""
try:
perimeter_core_subfaces(face3d_file, perimeter_offset, tolerance, output_file)
except Exception as e:
_logger.exception('Failed to compute core/perimeter Face3Ds.\n{}'.format(e))
sys.exit(1)
else:
sys.exit(0)


def perimeter_core_subfaces(
face3d_file, perimeter_offset, tolerance=1e-5, output_file=None
):
"""Compute perimeter and core Face3Ds using the straight skeleton of input Face3Ds.
Args:
face3d_file: Full path to a JSON file containing an array of Face3D
objects for which core/perimeter Face3Ds will be computed.
perimeter_offset: Distance to offset perimeter sub-faces.
tolerance: The maximum difference between x, y, and z values at which
vertices are considered equivalent.
output_file: Optional file to output the string of the visualization
file contents. If None, the string will simply be returned from
this method.
"""
# load the Face3D objects
with open(face3d_file) as inf:
data = json.load(inf)
face_3ds = [Face3D.from_dict(geo) for geo in data]

# compute core/perimeter Face3Ds
core_per_geos = []
for geo_obj in face_3ds:
perimeter_sub_faces, core_sub_faces = \
lgp_perimeter_core_subfaces(geo_obj, perimeter_offset, tolerance)
core_per_geos.extend([f.to_dict() for f in perimeter_sub_faces])
core_per_geos.extend([f.to_dict() for f in core_sub_faces])

# write the resulting geometries into the output
if output_file is None:
return json.dumps(core_per_geos)
else:
output_file.write(json.dumps(core_per_geos))
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
with open('requirements.txt') as f:
requirements = f.read().splitlines()

with open('cli-requirements.txt') as f:
cli_requirements = f.read().splitlines()

setuptools.setup(
name="ladybug-geometry-polyskel",
use_scm_version=True,
Expand All @@ -18,6 +21,10 @@
url="https://github.com/ladybug-tools/ladybug-geometry-polyskel",
packages=setuptools.find_packages(exclude=["tests"]),
install_requires=requirements,
extras_require={'cli': cli_requirements},
entry_points={
"console_scripts": ["ladybug-geometry-polyskel = ladybug_geometry_polyskel.cli:main"]
},
classifiers=[
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.6",
Expand Down
1 change: 1 addition & 0 deletions tests/assets/sample_geometries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"type": "Face3D", "plane": {"o": [-3.1066183692618212, 19.759722547974839, 0.0], "n": [0.0, 0.0, -1.0], "type": "Plane", "x": [1.0, 0.0, 0.0]}, "boundary": [[-3.1066183692618212, 19.759722547974839, 0.0], [-7.5979797625430354, 19.759722547974839, 0.0], [-7.5979797625430354, 21.392381536601498, 0.0], [-6.5966265078992699, 21.392381536601498, 0.0], [-6.5966265078992699, 21.824013947431094, 0.0], [-4.1144432017329446, 21.824013947431094, 0.0], [-4.1144432017329446, 21.392381536601498, 0.0], [-3.1066183692618239, 21.392381536601498, 0.0]]}, {"type": "Face3D", "plane": {"o": [-3.6999621911860219, 15.90298770546752, 0.0], "n": [0.0, 0.0, -1.0], "type": "Plane", "x": [1.0, 0.0, 0.0]}, "boundary": [[-3.6999621911860219, 15.90298770546752, 0.0], [-8.1913235844672361, 15.90298770546752, 0.0], [-8.1913235844672361, 17.535646694094179, 0.0], [-7.1899703298234705, 17.535646694094179, 0.0], [-7.1899703298234705, 17.967279104923776, 0.0], [-4.7077870236571453, 17.967279104923776, 0.0], [-4.7077870236571453, 17.082447849590533, 0.0], [-3.6999621911860245, 17.082447849590533, 0.0]]}, {"type": "Face3D", "plane": {"o": [20.911599710612037, -12.469149222792852, 0.0], "n": [0.0, 0.0, 1.0], "type": "Plane", "x": [1.0, 0.0, 0.0]}, "boundary": [[20.911599710612037, -12.469149222792852, 0.0], [27.292069203710405, -12.469149222792852, 0.0], [27.292069203710405, -12.05468237864326, 0.0], [29.593621187576694, -12.05468237864326, 0.0], [29.593621187576694, -9.5928062416062296, 0.0], [29.241924596571408, -9.5928062416062296, 0.0], [29.241924596571408, -6.8797182538511352, 0.0], [26.126897647667406, -6.8797182538511352, 0.0], [26.126897647667406, -4.3843472033850315, 0.0], [23.832496077775755, -4.3843472033850315, 0.0], [23.832496077775755, -8.2865046178722963, 0.0], [21.990275839176618, -8.2865046178722963, 0.0], [21.990275839176618, -11.887207811497882, 0.0], [20.911599710612037, -11.887207811497882, 0.0]]}]
18 changes: 18 additions & 0 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Test cli."""
from click.testing import CliRunner
import json

from ladybug_geometry.geometry3d import Face3D
from ladybug_geometry_polyskel.cli import perimeter_core_subfaces_cli


def test_perimeter_core_subfaces():
input_file = './tests/assets/sample_geometries.json'
runner = CliRunner()
result = runner.invoke(
perimeter_core_subfaces_cli, [input_file, '1.0', '--tolerance', '0.01'])
assert result.exit_code == 0

result_dict = json.loads(result.output)
new_faces = [Face3D.from_dict(f) for f in result_dict]
assert len(new_faces) == 33

0 comments on commit fdf75eb

Please sign in to comment.