-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement import extraction for notebooks in Rust (#606)
* Move the functionality for extraction of imports from `ipynb` files to Rust. * Introduced the use of type aliases * Removed the functions `get_imports_from_py_file` since it was not used.
- Loading branch information
Showing
16 changed files
with
276 additions
and
234 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
58 changes: 0 additions & 58 deletions
58
python/deptry/imports/extractors/notebook_import_extractor.py
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
use crate::file_utils; | ||
use crate::location; | ||
|
||
use file_utils::read_file; | ||
use location::Location; | ||
use pyo3::exceptions::PySyntaxError; | ||
use pyo3::prelude::*; | ||
use pyo3::types::PyString; | ||
use rayon::prelude::*; | ||
use std::collections::HashMap; | ||
|
||
use super::shared; | ||
|
||
/// Processes multiple Python files in parallel to extract import statements and their locations. | ||
/// Accepts a list of file paths and returns a dictionary mapping module names to their import locations. | ||
#[pyfunction] | ||
pub fn get_imports_from_ipynb_files(py: Python, file_paths: Vec<&PyString>) -> PyResult<PyObject> { | ||
let rust_file_paths: Vec<String> = file_paths | ||
.iter() | ||
.map(|py_str| py_str.to_str().unwrap().to_owned()) | ||
.collect(); | ||
|
||
let results: Vec<_> = rust_file_paths | ||
.par_iter() | ||
.map(|path_str| { | ||
let result = _get_imports_from_ipynb_file(path_str); | ||
shared::ThreadResult { | ||
file: path_str.to_string(), | ||
result, | ||
} | ||
}) | ||
.collect(); | ||
|
||
let (all_imports, errors) = shared::merge_results_from_threads(results); | ||
shared::log_python_errors_as_warnings(&errors); | ||
|
||
shared::convert_to_python_dict(py, all_imports) | ||
} | ||
|
||
/// Core helper function that extracts import statements and their locations from a single .ipynb file. | ||
/// Ensures robust error handling and provides clearer, more detailed comments. | ||
fn _get_imports_from_ipynb_file(path_str: &str) -> PyResult<HashMap<String, Vec<Location>>> { | ||
let file_content = read_file(path_str)?; | ||
let notebook: serde_json::Value = | ||
serde_json::from_str(&file_content).map_err(|e| PySyntaxError::new_err(e.to_string()))?; | ||
let cells = notebook["cells"] | ||
.as_array() | ||
.ok_or_else(|| PySyntaxError::new_err("Expected 'cells' to be an array"))?; | ||
let python_code = _extract_code_from_notebook_cells(cells); | ||
|
||
let ast = shared::get_ast_from_file_content(&python_code, path_str)?; | ||
let imported_modules = shared::extract_imports_from_ast(ast); | ||
|
||
Ok(shared::convert_imports_with_textranges_to_location_objects( | ||
imported_modules, | ||
path_str, | ||
&python_code, | ||
)) | ||
} | ||
|
||
/// Extracts and concatenates code from notebook code cells. | ||
fn _extract_code_from_notebook_cells(cells: &[serde_json::Value]) -> String { | ||
let code_lines: Vec<String> = cells | ||
.iter() | ||
.filter(|cell| cell["cell_type"] == "code") | ||
.flat_map(|cell| cell["source"].as_array()) | ||
.flatten() | ||
.filter_map(|line| line.as_str()) | ||
.map(str::to_owned) | ||
.collect(); | ||
|
||
code_lines.join("\n") | ||
} |
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,3 @@ | ||
pub mod ipynb; | ||
pub mod py; | ||
pub mod shared; |
Oops, something went wrong.