Skip to content

Commit e7a2d9b

Browse files
authored
Merge pull request #37 from justbuchanan/__file__
feat: set the __file__ variable for scripts loaded from files
2 parents a9e4b1a + 67573dd commit e7a2d9b

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed

src/cq_cli/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ def get_script_from_infile(infile, outfile, errfile):
123123
script_str = infile
124124
else:
125125
with open(infile, "r") as file:
126-
script_str = file.read()
126+
# prepend an assignment for the __file__ variable so the model
127+
# script knows its path and can potentially load resources relative
128+
# to that path.
129+
script_str = f"__file__ = '{os.path.abspath(infile)}'\n"
130+
script_str += file.read()
127131

128132
return script_str
129133

tests/test_cli.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,20 @@ def test_parameter_analysis():
291291

292292
# Grab the JSON output from cq-cli
293293
jsn = json.loads(out.decode())
294-
295-
# Check to make sure the first parameter was handled properly
296-
assert jsn[0]["type"] == "number"
297-
assert jsn[0]["name"] == "width"
298-
assert jsn[0]["initial"] == 1
299-
300-
# Check to make sure the second parameter was handled properly
301-
assert jsn[1]["type"] == "string"
302-
assert jsn[1]["name"] == "tag_name"
303-
assert jsn[1]["initial"] == "cube"
304-
305-
# Check to make sure the third parameter was handled properly
306-
assert jsn[2]["type"] == "boolean"
307-
assert jsn[2]["name"] == "centered"
308-
assert jsn[2]["initial"] == True
294+
params_by_name = helpers.params_list_to_dict(jsn)
295+
296+
# Check to make sure the parameters were handled properly
297+
assert params_by_name["width"] == {"name": "width", "type": "number", "initial": 1}
298+
assert params_by_name["tag_name"] == {
299+
"name": "tag_name",
300+
"type": "string",
301+
"initial": "cube",
302+
}
303+
assert params_by_name["centered"] == {
304+
"name": "centered",
305+
"type": "boolean",
306+
"initial": True,
307+
}
309308

310309

311310
def test_parameter_file_input_output():
@@ -348,10 +347,10 @@ def test_parameter_file_input_output():
348347
# Modify the parameters file
349348
with open(temp_file, "r") as file:
350349
json_str = file.read()
351-
json_dict = json.loads(json_str)
352-
json_dict[0]["initial"] = 10
350+
json_list = json.loads(json_str)
351+
json_list[1]["initial"] = 10
353352
with open(temp_file, "w") as file:
354-
file.writelines(json.dumps(json_dict))
353+
file.writelines(json.dumps(json_list))
355354

356355
# Run the command with the new parameters
357356
command3 = [
@@ -414,10 +413,11 @@ def test_params_stl_output():
414413
# Make sure that the customizer.json file exists and has what we expect in it
415414
with open(customizer_file_path, "r") as file2:
416415
json_str = file2.read()
417-
json_dict = json.loads(json_str)
418-
assert json_dict[0]["initial"] == 1
419-
assert json_dict[1]["initial"] == "cube"
420-
assert json_dict[2]["initial"] == True
416+
json_list = json.loads(json_str)
417+
params = helpers.params_list_to_dict(json_list)
418+
assert params["width"]["initial"] == 1
419+
assert params["tag_name"]["initial"] == "cube"
420+
assert params["centered"]["initial"] == True
421421

422422
# Write an STL using the default parameters so that we can compare it to what was generated with customized parameters
423423
command = [
@@ -544,3 +544,27 @@ def test_multiple_outfiles():
544544
]
545545
out, err, exitcode = helpers.cli_call(command)
546546
assert exitcode == 0
547+
548+
549+
def test_file_variable_is_set():
550+
"""
551+
Tests that cq-cli sets the __file__ variable for the model script.
552+
"""
553+
test_file = helpers.get_test_file_location("file_var.py")
554+
555+
temp_dir = tempfile.gettempdir()
556+
out_path = os.path.join(temp_dir, "temp_test_file_variable.stl")
557+
558+
command = [
559+
"python",
560+
"src/cq_cli/main.py",
561+
"--codec",
562+
"stl",
563+
"--infile",
564+
test_file,
565+
"--outfile",
566+
out_path,
567+
]
568+
out, err, exitcode = helpers.cli_call(command)
569+
assert exitcode == 0
570+
assert "__file__=" in out.decode()

tests/test_helpers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ def cli_call(command):
1515
"""
1616
Makes the operating system process calls to test the CLI properly.
1717
"""
18-
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
18+
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1919
out, err = proc.communicate()
2020
return out, err, proc.returncode
21+
22+
23+
def params_list_to_dict(param_list):
24+
"""
25+
Converts a list of params into a dictionary of those params keyed by name.
26+
"""
27+
d = {}
28+
for entry in param_list:
29+
d[entry["name"]] = entry
30+
return d

tests/testdata/file_var.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cadquery as cq
2+
3+
# print info about the __file__ variable to stdout so the test can check it
4+
if "__file__" in locals():
5+
print(f"__file__={__file__}")
6+
else:
7+
print("__FILE__ not set")
8+
9+
# render a simple shape so the cq-cli invocation succeeds
10+
b = cq.Workplane("XY").rect(10, 10).extrude(10)
11+
show_object(b)

0 commit comments

Comments
 (0)