Skip to content

Commit

Permalink
improve the logging of import locations when running in verbose mode (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fpgmaas committed Mar 23, 2024
1 parent e331e3d commit 3efc505
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions python/deptry/imports/extract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import json
import logging
from collections import defaultdict
from collections import OrderedDict, defaultdict
from typing import TYPE_CHECKING

from deptry.rust import get_imports_from_ipynb_files, get_imports_from_py_files
Expand All @@ -25,21 +26,29 @@ def get_imported_modules_from_list_of_files(list_of_files: list[Path]) -> dict[s
# Process all .py files in parallel using Rust
if py_files:
rust_result = get_imports_from_py_files(py_files)
for module, locations in convert_rust_locations_to_python_locations(rust_result).items():
for module, locations in _convert_rust_locations_to_python_locations(rust_result).items():
modules[module].extend(locations)

# Process all .ipynb files in parallel using Rust
if ipynb_files:
rust_result = get_imports_from_ipynb_files(ipynb_files)
for module, locations in convert_rust_locations_to_python_locations(rust_result).items():
for module, locations in _convert_rust_locations_to_python_locations(rust_result).items():
modules[module].extend(locations)

logging.debug("All imported modules: %s\n", modules)
sorted_modules = OrderedDict(sorted(modules.items()))
_log_modules_with_locations(sorted_modules)
return sorted_modules

return modules

def _log_modules_with_locations(modules: dict[str, list[Location]]) -> None:
modules_dict = {
module_name: [str(location) for location in locations] for module_name, locations in modules.items()
}
modules_json = json.dumps(modules_dict, indent=2)
logging.debug("All imported modules and their locations:\n%s", modules_json)

def convert_rust_locations_to_python_locations(

def _convert_rust_locations_to_python_locations(
imported_modules: dict[str, list[RustLocation]],
) -> dict[str, list[Location]]:
converted_modules: dict[str, list[Location]] = {}
Expand Down

0 comments on commit 3efc505

Please sign in to comment.