Skip to content

Commit

Permalink
Merge pull request #26 from justbuchanan/entrypoint
Browse files Browse the repository at this point in the history
Add an --expression flag to specify what cq-cli should render
  • Loading branch information
jmwright authored Nov 26, 2023
2 parents 6dc5f23 + de4fbdf commit 583da30
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/cq_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
from cq_cli.cqcodecs import loader


def build_and_parse(script_str, params, errfile):
def build_and_parse(script_str, params, errfile, expression):
"""
Uses CQGI to parse and build a script, substituting in parameters if any were supplied.
"""
# We need to do a broad try/catch to let the user know if something higher-level fails
try:
# Do the CQGI handling of the script here and, if successful, pass the build result to the codec
if expression != None:
script_str += "\nshow_object({expr})".format(expr=expression)
cqModel = cqgi.parse(script_str)
build_result = cqModel.build(params)

Expand Down Expand Up @@ -181,6 +183,10 @@ def main():
"--validate",
help="Setting to true forces the CLI to only parse and validate the script and not produce converted output.",
)
parser.add_argument(
"--expression",
help="A python expression (such as `my_shape(x=5)`) to evaluate and render. This allows rendering different models/parts from the same python file.",
)

args = parser.parse_args()

Expand Down Expand Up @@ -223,7 +229,7 @@ def main():
# Set the PYTHONPATH variable to the current directory to allow module loading
set_pythonpath_for_infile(args.infile)

build_result = build_and_parse(script_str, params, errfile)
build_result = build_and_parse(script_str, params, errfile, args.expression)

# Double-check that the build was a success
if build_result != None and build_result.success:
Expand Down Expand Up @@ -424,7 +430,7 @@ def main():
#
build_result = None
try:
build_result = build_and_parse(script_str, params, errfile)
build_result = build_and_parse(script_str, params, errfile, args.expression)

# If None was returned, it means the build failed and the exception has already been reported
if build_result == None:
Expand Down
49 changes: 49 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,52 @@ def test_exit_codes():

# Make sure that we got exit code 100 for a failed model build
assert exitcode == 100


def test_expression_argument():
"""
Tests the CLI with the the expression argument.
"""
test_file = helpers.get_test_file_location("no_toplevel_objects.py")

# Get a temporary output file location
temp_dir = tempfile.gettempdir()
temp_file = os.path.join(temp_dir, "temp_test_10.step")

# Run cq-cli with --expression "cube()"
command = [
"python",
"src/cq_cli/main.py",
"--codec",
"step",
"--infile",
test_file,
"--outfile",
temp_file,
"--expression",
"cube()",
]
out, err, exitcode = helpers.cli_call(command)

# Read the STEP output back from the outfile
with open(temp_file, "r") as file:
step_str = file.read()

assert step_str.startswith("ISO-10303-21;")

# Run cq-cli on the same model file, but don't specify an --expression. This
# should fail because the file contains no top-level show_object() calls.
command = [
"python",
"src/cq_cli/main.py",
"--codec",
"step",
"--infile",
test_file,
"--outfile",
temp_file,
]
out, err, exitcode = helpers.cli_call(command)

# cq-cli invocation should fail
assert exitcode == 200
8 changes: 8 additions & 0 deletions tests/testdata/no_toplevel_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import cadquery as cq

# This test file contains a method for creating a shape, but does not contain
# any top-level `show_object()` calls.


def cube():
return cq.Workplane().box(1, 1, 1)

0 comments on commit 583da30

Please sign in to comment.