Skip to content

Commit d3a4fd8

Browse files
authored
Raw dog parse for python bindings (#6970)
* raw dog parse python Signed-off-by: Jess Frazelle <[email protected]> * add tests Signed-off-by: Jess Frazelle <[email protected]> --------- Signed-off-by: Jess Frazelle <[email protected]>
1 parent 2be7107 commit d3a4fd8

File tree

13 files changed

+77
-23
lines changed

13 files changed

+77
-23
lines changed

rust/Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/kcl-bumper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
[package]
33
name = "kcl-bumper"
4-
version = "0.1.70"
4+
version = "0.1.71"
55
edition = "2021"
66
repository = "https://github.com/KittyCAD/modeling-api"
77
rust-version = "1.76"

rust/kcl-derive-docs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "kcl-derive-docs"
33
description = "A tool for generating documentation from Rust derive macros"
4-
version = "0.1.70"
4+
version = "0.1.71"
55
edition = "2021"
66
license = "MIT"
77
repository = "https://github.com/KittyCAD/modeling-app"

rust/kcl-directory-test-macro/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "kcl-directory-test-macro"
33
description = "A tool for generating tests from a directory of kcl files"
4-
version = "0.1.70"
4+
version = "0.1.71"
55
edition = "2021"
66
license = "MIT"
77
repository = "https://github.com/KittyCAD/modeling-app"

rust/kcl-language-server-release/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "kcl-language-server-release"
3-
version = "0.1.70"
3+
version = "0.1.71"
44
edition = "2021"
55
authors = ["KittyCAD Inc <[email protected]>"]
66
publish = false

rust/kcl-language-server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "kcl-language-server"
33
description = "A language server for KCL."
44
authors = ["KittyCAD Inc <[email protected]>"]
5-
version = "0.2.70"
5+
version = "0.2.71"
66
edition = "2021"
77
license = "MIT"
88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

rust/kcl-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "kcl-lib"
33
description = "KittyCAD Language implementation and tools"
4-
version = "0.2.70"
4+
version = "0.2.71"
55
edition = "2021"
66
license = "MIT"
77
repository = "https://github.com/KittyCAD/modeling-app"

rust/kcl-python-bindings/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "kcl-python-bindings"
3-
version = "0.3.70"
3+
version = "0.3.71"
44
edition = "2021"
55
repository = "https://github.com/kittycad/modeling-app"
66
exclude = ["tests/*", "files/*", "venv/*"]

rust/kcl-python-bindings/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,31 @@ async fn new_context_state(current_file: Option<std::path::PathBuf>) -> Result<(
227227
Ok((ctx, state))
228228
}
229229

230+
/// Parse the kcl code from a file path.
231+
#[pyfunction]
232+
async fn parse(path: String) -> PyResult<()> {
233+
tokio()
234+
.spawn(async move {
235+
let (code, path) = get_code_and_file_path(&path)
236+
.await
237+
.map_err(|err| pyo3::exceptions::PyException::new_err(err.to_string()))?;
238+
let _program = kcl_lib::Program::parse_no_errs(&code)
239+
.map_err(|err| into_miette_for_parse(&path.display().to_string(), &code, err))?;
240+
241+
Ok(())
242+
})
243+
.await
244+
.map_err(|err| pyo3::exceptions::PyException::new_err(err.to_string()))?
245+
}
246+
247+
/// Parse the kcl code.
248+
#[pyfunction]
249+
fn parse_code(code: String) -> PyResult<()> {
250+
let _program = kcl_lib::Program::parse_no_errs(&code).map_err(|err| into_miette_for_parse("", &code, err))?;
251+
252+
Ok(())
253+
}
254+
230255
/// Execute the kcl code from a file path.
231256
#[pyfunction]
232257
async fn execute(path: String) -> PyResult<()> {
@@ -534,6 +559,8 @@ fn kcl(m: &Bound<'_, PyModule>) -> PyResult<()> {
534559
m.add_class::<Discovered>()?;
535560

536561
// Add our functions to the module.
562+
m.add_function(wrap_pyfunction!(parse, m)?)?;
563+
m.add_function(wrap_pyfunction!(parse_code, m)?)?;
537564
m.add_function(wrap_pyfunction!(execute, m)?)?;
538565
m.add_function(wrap_pyfunction!(execute_code, m)?)?;
539566
m.add_function(wrap_pyfunction!(execute_and_snapshot, m)?)?;

rust/kcl-python-bindings/tests/tests.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ async def test_kcl_execute():
3939
await kcl.execute(lego_file)
4040

4141

42+
@pytest.mark.asyncio
43+
async def test_kcl_parse_with_exception():
44+
# Read from a file.
45+
try:
46+
await kcl.parse(os.path.join(files_dir, "parse_file_error"))
47+
except Exception as e:
48+
assert e is not None
49+
assert len(str(e)) > 0
50+
assert "lksjndflsskjfnak;jfna##" in str(e)
51+
52+
53+
@pytest.mark.asyncio
54+
async def test_kcl_parse():
55+
# Read from a file.
56+
await kcl.parse(lego_file)
57+
58+
59+
@pytest.mark.asyncio
60+
async def test_kcl_parse_code():
61+
# Read from a file.
62+
with open(lego_file, "r") as f:
63+
code = str(f.read())
64+
assert code is not None
65+
assert len(code) > 0
66+
kcl.parse_code(code)
67+
68+
4269
@pytest.mark.asyncio
4370
async def test_kcl_execute_code():
4471
# Read from a file.
@@ -97,9 +124,7 @@ async def test_kcl_execute_and_snapshot():
97124
@pytest.mark.asyncio
98125
async def test_kcl_execute_and_snapshot_dir():
99126
# Read from a file.
100-
image_bytes = await kcl.execute_and_snapshot(
101-
car_wheel_dir, kcl.ImageFormat.Jpeg
102-
)
127+
image_bytes = await kcl.execute_and_snapshot(car_wheel_dir, kcl.ImageFormat.Jpeg)
103128
assert image_bytes is not None
104129
assert len(image_bytes) > 0
105130

@@ -129,10 +154,12 @@ def test_kcl_format():
129154
assert formatted_code is not None
130155
assert len(formatted_code) > 0
131156

157+
132158
@pytest.mark.asyncio
133159
async def test_kcl_format_dir():
134160
await kcl.format_dir(car_wheel_dir)
135161

162+
136163
def test_kcl_lint():
137164
# Read from a file.
138165
with open(os.path.join(files_dir, "box_with_linter_errors.kcl"), "r") as f:

rust/kcl-test-server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "kcl-test-server"
33
description = "A test server for KCL"
4-
version = "0.1.70"
4+
version = "0.1.71"
55
edition = "2021"
66
license = "MIT"
77

rust/kcl-to-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "kcl-to-core"
33
description = "Utility methods to convert kcl to engine core executable tests"
4-
version = "0.1.70"
4+
version = "0.1.71"
55
edition = "2021"
66
license = "MIT"
77
repository = "https://github.com/KittyCAD/modeling-app"

rust/kcl-wasm-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "kcl-wasm-lib"
3-
version = "0.1.70"
3+
version = "0.1.71"
44
edition = "2021"
55
repository = "https://github.com/KittyCAD/modeling-app"
66
rust-version = "1.83"

0 commit comments

Comments
 (0)