Skip to content

Commit e3765cb

Browse files
committed
Move scan_for_imports pyfunction into import_scanning module
1 parent 5821d44 commit e3765cb

2 files changed

Lines changed: 76 additions & 78 deletions

File tree

rust/src/import_scanning.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::errors::GrimpResult;
2-
use crate::filesystem::FileSystem;
1+
use crate::errors::{GrimpError, GrimpResult};
2+
use crate::filesystem::{FileSystem, get_file_system_boxed};
33
use crate::module_finding::{FoundPackage, Module};
44
use crate::{import_parsing, module_finding};
55
use itertools::Itertools;
@@ -350,3 +350,76 @@ pub fn imports_by_module_to_py(
350350
}
351351
imports_by_module_py
352352
}
353+
354+
/// Statically analyses the given module and returns a set of Modules that
355+
/// it imports.
356+
/// Python args:
357+
///
358+
/// - module_files The modules to scan.
359+
/// - found_packages: Set of FoundPackages containing all the modules
360+
/// for analysis.
361+
/// - include_external_packages: Whether to include imports of external modules (i.e.
362+
/// modules not contained in modules_by_package_directory)
363+
/// in the results.
364+
/// - exclude_type_checking_imports: If True, don't include imports behind TYPE_CHECKING guards.
365+
/// - file_system: The file system interface to use. (A BasicFileSystem.)
366+
///
367+
/// Returns dict[Module, set[DirectImport]].
368+
#[pyfunction]
369+
pub fn scan_for_imports<'py>(
370+
py: Python<'py>,
371+
module_files: Vec<Bound<'py, PyAny>>,
372+
found_packages: Bound<'py, PyAny>,
373+
include_external_packages: bool,
374+
exclude_type_checking_imports: bool,
375+
file_system: Bound<'py, PyAny>,
376+
) -> PyResult<Bound<'py, PyDict>> {
377+
let file_system_boxed = get_file_system_boxed(&file_system)?;
378+
let found_packages_rust = py_found_packages_to_rust(&found_packages);
379+
let modules_rust: HashSet<module_finding::Module> = module_files
380+
.iter()
381+
.map(|module_file| {
382+
module_file
383+
.getattr("module")
384+
.unwrap()
385+
.extract::<module_finding::Module>()
386+
.unwrap()
387+
})
388+
.collect();
389+
390+
let imports_by_module_result = py.detach(|| {
391+
scan_for_imports_no_py(
392+
&file_system_boxed,
393+
&found_packages_rust,
394+
include_external_packages,
395+
&modules_rust,
396+
exclude_type_checking_imports,
397+
)
398+
});
399+
400+
match imports_by_module_result {
401+
Err(GrimpError::ParseError {
402+
module_filename,
403+
line_number,
404+
text,
405+
..
406+
}) => {
407+
// TODO: define SourceSyntaxError using pyo3.
408+
let exceptions_pymodule = PyModule::import(py, "grimp.exceptions").unwrap();
409+
let py_exception_class = exceptions_pymodule.getattr("SourceSyntaxError").unwrap();
410+
let exception = py_exception_class
411+
.call1((module_filename, line_number, text))
412+
.unwrap();
413+
return Err(PyErr::from_value(exception));
414+
}
415+
Err(e) => {
416+
return Err(e.into());
417+
}
418+
_ => (),
419+
}
420+
let imports_by_module = imports_by_module_result.unwrap();
421+
422+
let imports_by_module_py = imports_by_module_to_py(py, imports_by_module);
423+
424+
Ok(imports_by_module_py)
425+
}

rust/src/lib.rs

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ mod module_finding;
1111
use crate::errors::{GrimpError, GrimpResult};
1212
use crate::graph::higher_order_queries::Level;
1313
use crate::graph::{Graph, Module, ModuleIterator, ModuleTokenIterator};
14-
use crate::import_scanning::{py_found_packages_to_rust, scan_for_imports_no_py};
1514
use crate::module_expressions::ModuleExpression;
1615
use derive_new::new;
17-
use filesystem::get_file_system_boxed;
1816
use itertools::Itertools;
1917
use pyo3::IntoPyObjectExt;
2018
use pyo3::exceptions::PyValueError;
@@ -27,7 +25,7 @@ use std::collections::HashSet;
2725
#[pymodule]
2826
mod _rustgrimp {
2927
#[pymodule_export]
30-
use super::scan_for_imports;
28+
use crate::import_scanning::scan_for_imports;
3129

3230
#[pymodule_export]
3331
use crate::caching::read_cache_data_map_file;
@@ -44,79 +42,6 @@ mod _rustgrimp {
4442
};
4543
}
4644

47-
/// Statically analyses the given module and returns a set of Modules that
48-
/// it imports.
49-
/// Python args:
50-
///
51-
/// - module_files The modules to scan.
52-
/// - found_packages: Set of FoundPackages containing all the modules
53-
/// for analysis.
54-
/// - include_external_packages: Whether to include imports of external modules (i.e.
55-
/// modules not contained in modules_by_package_directory)
56-
/// in the results.
57-
/// - exclude_type_checking_imports: If True, don't include imports behind TYPE_CHECKING guards.
58-
/// - file_system: The file system interface to use. (A BasicFileSystem.)
59-
///
60-
/// Returns dict[Module, set[DirectImport]].
61-
#[pyfunction]
62-
fn scan_for_imports<'py>(
63-
py: Python<'py>,
64-
module_files: Vec<Bound<'py, PyAny>>,
65-
found_packages: Bound<'py, PyAny>,
66-
include_external_packages: bool,
67-
exclude_type_checking_imports: bool,
68-
file_system: Bound<'py, PyAny>,
69-
) -> PyResult<Bound<'py, PyDict>> {
70-
let file_system_boxed = get_file_system_boxed(&file_system)?;
71-
let found_packages_rust = py_found_packages_to_rust(&found_packages);
72-
let modules_rust: HashSet<module_finding::Module> = module_files
73-
.iter()
74-
.map(|module_file| {
75-
module_file
76-
.getattr("module")
77-
.unwrap()
78-
.extract::<module_finding::Module>()
79-
.unwrap()
80-
})
81-
.collect();
82-
83-
let imports_by_module_result = py.detach(|| {
84-
scan_for_imports_no_py(
85-
&file_system_boxed,
86-
&found_packages_rust,
87-
include_external_packages,
88-
&modules_rust,
89-
exclude_type_checking_imports,
90-
)
91-
});
92-
93-
match imports_by_module_result {
94-
Err(GrimpError::ParseError {
95-
module_filename,
96-
line_number,
97-
text,
98-
..
99-
}) => {
100-
// TODO: define SourceSyntaxError using pyo3.
101-
let exceptions_pymodule = PyModule::import(py, "grimp.exceptions").unwrap();
102-
let py_exception_class = exceptions_pymodule.getattr("SourceSyntaxError").unwrap();
103-
let exception = py_exception_class
104-
.call1((module_filename, line_number, text))
105-
.unwrap();
106-
return Err(PyErr::from_value(exception));
107-
}
108-
Err(e) => {
109-
return Err(e.into());
110-
}
111-
_ => (),
112-
}
113-
let imports_by_module = imports_by_module_result.unwrap();
114-
115-
let imports_by_module_py = import_scanning::imports_by_module_to_py(py, imports_by_module);
116-
117-
Ok(imports_by_module_py)
118-
}
119-
12045
#[pyclass(name = "Graph")]
12146
#[derive(Clone)]
12247
struct GraphWrapper {

0 commit comments

Comments
 (0)