Skip to content

Commit 4fc6090

Browse files
committed
Call scan_for_imports in Rust
This removes all mention of ImportScanner from Python.
1 parent 7310faf commit 4fc6090

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

rust/src/import_scanning.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl ImportScanner {
3838
#[allow(unused_variables)]
3939
#[new]
4040
#[pyo3(signature = (file_system, found_packages, include_external_packages=false))]
41-
fn new(
41+
pub fn new(
4242
py: Python,
4343
file_system: Bound<'_, PyAny>,
4444
found_packages: Bound<'_, PyAny>,
@@ -59,7 +59,7 @@ impl ImportScanner {
5959
/// Statically analyses the given module and returns a set of Modules that
6060
/// it imports.
6161
#[pyo3(signature = (module, exclude_type_checking_imports=false))]
62-
fn scan_for_imports<'a>(
62+
pub fn scan_for_imports<'a>(
6363
&self,
6464
py: Python<'a>,
6565
module: Bound<'_, PyAny>,

rust/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::collections::HashSet;
2727
#[pymodule]
2828
fn _rustgrimp(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
2929
m.add_wrapped(wrap_pyfunction!(parse_imported_objects_from_code))?;
30+
m.add_wrapped(wrap_pyfunction!(scan_for_imports))?;
3031
m.add_class::<GraphWrapper>()?;
3132
m.add_class::<PyRealBasicFileSystem>()?;
3233
m.add_class::<PyFakeBasicFileSystem>()?;
@@ -41,6 +42,35 @@ fn _rustgrimp(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
4142
Ok(())
4243
}
4344

45+
#[pyfunction]
46+
fn scan_for_imports<'py,>(
47+
py: Python<'py>,
48+
module_files: Vec<Bound<'py, PyAny>>,
49+
found_packages: Bound<'py, PyAny>,
50+
include_external_packages: bool,
51+
exclude_type_checking_imports: bool,
52+
file_system: Bound<'py, PyAny>,
53+
) -> PyResult<Bound<'py, PyDict>>{
54+
let scanner = ImportScanner::new(
55+
py,
56+
file_system,
57+
found_packages,
58+
include_external_packages,
59+
)?;
60+
61+
let dict = PyDict::new(py);
62+
for module_file in module_files {
63+
let py_module_instance = module_file.getattr("module").unwrap();
64+
let imports = scanner.scan_for_imports(
65+
py,
66+
py_module_instance,
67+
exclude_type_checking_imports,
68+
)?;
69+
dict.set_item(module_file, imports).unwrap();
70+
}
71+
Ok(dict)
72+
}
73+
4474
#[pyfunction]
4575
fn parse_imported_objects_from_code<'py>(
4676
py: Python<'py>,

src/grimp/application/ports/filesystem.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ def convert_to_basic(self) -> BasicFileSystem:
9393

9494
class BasicFileSystem(Protocol):
9595
"""
96-
A more limited file system, used by the ImportScanner.
96+
A more limited file system, used by the Rust-based scan_for_imports function.
9797
9898
Having two different file system APIs is an interim approach, allowing us to
99-
implement BasicFileSystem in Rust, for use with the ImportScanner, without needing
100-
to implement the full range of methods used by other parts of Grimp.
99+
implement BasicFileSystem in Rust without needing to implement the full range
100+
of methods used by other parts of Grimp.
101101
102102
Eventually we'll move the full implementation to Rust, and have a single
103103
FileSystem interface.

src/grimp/application/scanning.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from grimp.application.ports.filesystem import AbstractFileSystem
1111
from grimp.application.ports.modulefinder import ModuleFile, FoundPackage
1212

13-
ImportScanner = rust.ImportScanner
1413

1514
# Calling code can set this environment variable if it wants to tune when to switch to
1615
# multiprocessing, or set it to a large number to disable it altogether.
@@ -72,19 +71,15 @@ def _scan_chunk(
7271
) -> Dict[ModuleFile, Set[DirectImport]]:
7372
file_system: AbstractFileSystem = settings.FILE_SYSTEM
7473
basic_file_system = file_system.convert_to_basic()
75-
import_scanner = ImportScanner(
76-
file_system=basic_file_system,
74+
return rust.scan_for_imports(
75+
module_files=chunk,
7776
found_packages=found_packages,
7877
# Ensure that the passed exclude_type_checking_imports is definitely a boolean,
7978
# otherwise the Rust class will error.
8079
include_external_packages=bool(include_external_packages),
80+
exclude_type_checking_imports=exclude_type_checking_imports,
81+
file_system=basic_file_system,
8182
)
82-
return {
83-
module_file: import_scanner.scan_for_imports(
84-
module_file.module, exclude_type_checking_imports=exclude_type_checking_imports
85-
)
86-
for module_file in chunk
87-
}
8883

8984

9085
def _scan_chunks(

0 commit comments

Comments
 (0)