Skip to content

Commit 7659962

Browse files
Refactor ingest_from_query to run_ingest_query, add process_notebook function, and add tests for notebook processing
1 parent f4f7853 commit 7659962

File tree

6 files changed

+76
-8
lines changed

6 files changed

+76
-8
lines changed

src/gitingest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from gitingest.clone import clone_repo
44
from gitingest.ingest import ingest
5-
from gitingest.ingest_from_query import ingest_from_query
5+
from gitingest.ingest_from_query import run_ingest_query
66
from gitingest.parse_query import parse_query
77

8-
__all__ = ["ingest_from_query", "clone_repo", "parse_query", "ingest"]
8+
__all__ = ["run_ingest_query", "clone_repo", "parse_query", "ingest"]

src/gitingest/ingest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from config import TMP_BASE_PATH
88
from gitingest.clone import CloneConfig, clone_repo
9-
from gitingest.ingest_from_query import ingest_from_query
9+
from gitingest.ingest_from_query import run_ingest_query
1010
from gitingest.parse_query import parse_query
1111

1212

@@ -75,7 +75,7 @@ def ingest(
7575
else:
7676
raise TypeError("clone_repo did not return a coroutine as expected.")
7777

78-
summary, tree, content = ingest_from_query(query)
78+
summary, tree, content = run_ingest_query(query)
7979

8080
if output is not None:
8181
with open(output, "w", encoding="utf-8") as f:

src/gitingest/ingest_from_query.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import tiktoken
88

99
from gitingest.exceptions import AlreadyVisitedError, MaxFileSizeReachedError, MaxFilesReachedError
10+
from gitingest.notebook_utils import process_notebook
1011

1112
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10 MB
1213
MAX_DIRECTORY_DEPTH = 20 # Maximum depth of directory traversal
@@ -158,7 +159,10 @@ def _read_file_content(file_path: Path) -> str:
158159
The content of the file, or an error message if the file could not be read.
159160
"""
160161
try:
161-
with file_path.open(encoding="utf-8", errors="ignore") as f:
162+
if file_path.suffix == ".ipynb":
163+
return process_notebook(file_path)
164+
165+
with open(file_path, encoding="utf-8", errors="ignore") as f:
162166
return f.read()
163167
except OSError as e:
164168
return f"Error reading file: {e}"
@@ -819,7 +823,7 @@ def _ingest_directory(path: Path, query: dict[str, Any]) -> tuple[str, str, str]
819823
return summary, tree, files_content
820824

821825

822-
def ingest_from_query(query: dict[str, Any]) -> tuple[str, str, str]:
826+
def run_ingest_query(query: dict[str, Any]) -> tuple[str, str, str]:
823827
"""
824828
Main entry point for analyzing a codebase directory or single file.
825829

src/gitingest/notebook_utils.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
""" Utilities for processing Jupyter notebooks. """
2+
3+
import json
4+
from pathlib import Path
5+
6+
7+
def process_notebook(file: Path) -> str:
8+
"""
9+
Process a Jupyter notebook file and return an executable Python script as a string.
10+
11+
Parameters
12+
----------
13+
file : Path
14+
The path to the Jupyter notebook file.
15+
16+
Returns
17+
-------
18+
str
19+
The executable Python script as a string.
20+
21+
Raises
22+
------
23+
ValueError
24+
If an unexpected cell type is encountered.
25+
"""
26+
with file.open(encoding="utf-8") as f:
27+
notebook = json.load(f)
28+
29+
result = []
30+
31+
for cell in notebook["cells"]:
32+
cell_type = cell.get("cell_type")
33+
34+
# Validate cell type and handle unexpected types
35+
if cell_type not in ("markdown", "code", "raw"):
36+
raise ValueError(f"Unknown cell type: {cell_type}")
37+
38+
str_ = "".join(cell.get("source", []))
39+
if not str_:
40+
continue
41+
42+
# Convert Markdown and raw cells to multi-line comments
43+
if cell_type in ("markdown", "raw"):
44+
str_ = f'"""\n{str_}\n"""'
45+
46+
result.append(str_)
47+
48+
return "\n\n".join(result)

src/process_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from config import EXAMPLE_REPOS, MAX_DISPLAY_SIZE
1010
from gitingest.clone import CloneConfig, clone_repo
11-
from gitingest.ingest_from_query import ingest_from_query
11+
from gitingest.ingest_from_query import run_ingest_query
1212
from gitingest.parse_query import parse_query
1313
from server_utils import Colors, log_slider_to_size
1414

@@ -91,7 +91,7 @@ async def process_query(
9191
branch=query.get("branch"),
9292
)
9393
await clone_repo(clone_config)
94-
summary, tree, content = ingest_from_query(query)
94+
summary, tree, content = run_ingest_query(query)
9595
with open(f"{clone_config.local_path}.txt", "w", encoding="utf-8") as f:
9696
f.write(tree + "\n" + content)
9797
except Exception as e:

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" This module contains fixtures for the tests. """
22

3+
import json
34
from pathlib import Path
45
from typing import Any
56

@@ -72,3 +73,18 @@ def temp_directory(tmp_path: Path) -> Path:
7273
(dir2 / "file_dir2.txt").write_text("Hello from dir2")
7374

7475
return test_dir
76+
77+
78+
@pytest.fixture
79+
def write_notebook(tmp_path: Path):
80+
"""
81+
A helper fixture that returns a function for writing arbitrary notebook content to a temporary .ipynb file.
82+
"""
83+
84+
def _write_notebook(name: str, content: dict) -> Path:
85+
notebook_path = tmp_path / name
86+
with notebook_path.open("w", encoding="utf-8") as f:
87+
json.dump(content, f)
88+
return notebook_path
89+
90+
return _write_notebook

0 commit comments

Comments
 (0)