Skip to content

Commit 61d2ed8

Browse files
committed
commit avant la catastrophe
1 parent ea047c6 commit 61d2ed8

File tree

5 files changed

+128
-51
lines changed

5 files changed

+128
-51
lines changed

src/gitingest/ingestion.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from typing import TYPE_CHECKING
77

88
from gitingest.config import MAX_DIRECTORY_DEPTH, MAX_FILES, MAX_TOTAL_SIZE_BYTES
9-
from gitingest.output_formatter import DefaultFormatter
9+
from gitingest.output_formatter import DefaultFormatter, StupidFormatter
1010
from gitingest.schemas import FileSystemNode, FileSystemStats, Context
11-
from gitingest.schemas.filesystem import FileSystemDirectory, FileSystemFile, FileSystemSymlink
11+
from gitingest.schemas.filesystem import FileSystemDirectory, FileSystemFile, FileSystemSymlink, FileSystemTextFile
1212
from gitingest.utils.ingestion_utils import _should_exclude, _should_include
1313

1414
if TYPE_CHECKING:
@@ -70,7 +70,7 @@ def ingest_query(query: IngestionQuery) -> Context:
7070
msg = f"File {file_node.name} has no content"
7171
raise ValueError(msg)
7272

73-
return Context([file_node], DefaultFormatter(), query)
73+
return Context([file_node], StupidFormatter(), query)
7474

7575
# root_node = FileSystemNode(
7676
# name=path.name,
@@ -88,7 +88,7 @@ def ingest_query(query: IngestionQuery) -> Context:
8888

8989
_process_node(node=root_node, query=query, stats=stats)
9090

91-
return Context([root_node], DefaultFormatter(), query)
91+
return Context([root_node], StupidFormatter(), query)
9292

9393

9494
def _process_node(node: FileSystemNode, query: IngestionQuery, stats: FileSystemStats) -> None:
@@ -205,12 +205,22 @@ def _process_file(path: Path, parent_node: FileSystemDirectory, stats: FileSyste
205205
stats.total_files += 1
206206
stats.total_size += file_size
207207

208-
child = FileSystemFile(
209-
name=path.name,
210-
path_str=str(path.relative_to(local_path)),
211-
path=path,
212-
depth=parent_node.depth + 1,
213-
)
208+
# if file is a .txt file, create a FileSystemTextFile
209+
if path.suffix == ".txt":
210+
child = FileSystemTextFile(
211+
name=path.name,
212+
path_str=str(path.relative_to(local_path)),
213+
path=path,
214+
depth=parent_node.depth + 1,
215+
)
216+
else:
217+
218+
child = FileSystemFile(
219+
name=path.name,
220+
path_str=str(path.relative_to(local_path)),
221+
path=path,
222+
depth=parent_node.depth + 1,
223+
)
214224

215225
parent_node.children.append(child)
216226
parent_node.size += file_size

src/gitingest/output_formatter.py

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,35 @@
99
from gitingest.schemas import FileSystemNode
1010
from gitingest.utils.compat_func import readlink
1111
from functools import singledispatchmethod
12-
from gitingest.schemas import Source, FileSystemFile, FileSystemDirectory, FileSystemSymlink
13-
from gitingest.schemas.filesystem import SEPARATOR
12+
from gitingest.schemas import Source, FileSystemFile, FileSystemDirectory, FileSystemSymlink, FileSystemTextFile
13+
from gitingest.schemas.filesystem import SEPARATOR, FileSystemNodeType
1414
from jinja2 import Environment, BaseLoader
1515

16+
17+
class OverridableDispatcher:
18+
"""Custom dispatcher that allows later registrations to override earlier ones, even for parent types."""
19+
20+
def __init__(self, default_func):
21+
self.default_func = default_func
22+
self.registry = [] # List of (type, func) in registration order
23+
24+
def register(self, type_):
25+
def decorator(func):
26+
# Remove any existing registration for this exact type
27+
self.registry = [(t, f) for t, f in self.registry if t != type_]
28+
# Add new registration at the end (highest priority)
29+
self.registry.append((type_, func))
30+
return func
31+
return decorator
32+
33+
def __call__(self, instance, *args, **kwargs):
34+
# Check registrations in reverse order (most recent first)
35+
for type_, func in reversed(self.registry):
36+
if isinstance(instance, type_):
37+
return func(instance, *args, **kwargs)
38+
# Fall back to default
39+
return self.default_func(instance, *args, **kwargs)
40+
1641
if TYPE_CHECKING:
1742
from gitingest.schemas import IngestionQuery
1843

@@ -169,59 +194,90 @@ def _format_token_count(text: str) -> str | None:
169194

170195
class DefaultFormatter:
171196
def __init__(self):
197+
self.separator = SEPARATOR
172198
self.env = Environment(loader=BaseLoader())
173199

174-
@singledispatchmethod
175-
def format(self, node: Source, query):
176-
return f"{getattr(node, 'content', '')}"
200+
# Set up custom dispatchers
201+
def _default_format(node: Source, query):
202+
return f"{getattr(node, 'content', '')}"
203+
204+
def _default_summary(node: Source, query):
205+
return f"{getattr(node, 'name', '')}"
177206

178-
@singledispatchmethod
179-
def summary(self, node: Source, query):
180-
# Default summary: just the name
181-
return f"{getattr(node, 'name', '')}"
207+
self.format = OverridableDispatcher(_default_format)
208+
self.summary = OverridableDispatcher(_default_summary)
182209

183-
@format.register
184-
def _(self, node: FileSystemFile, query):
185-
template = \
210+
# Register the default implementations
211+
self._register_defaults()
212+
213+
def _register_defaults(self):
214+
@self.format.register(FileSystemFile)
215+
def _(node: FileSystemFile, query):
216+
template = \
186217
"""
187218
{{ SEPARATOR }}
188219
{{ node.name }}
189220
{{ SEPARATOR }}
190221
191222
{{ node.content }}
192223
"""
193-
file_template = self.env.from_string(template)
194-
return file_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
224+
file_template = self.env.from_string(template)
225+
return file_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
195226

196-
@format.register
197-
def _(self, node: FileSystemDirectory, query):
198-
template = \
227+
@self.format.register(FileSystemDirectory)
228+
def _(node: FileSystemDirectory, query):
229+
template = \
199230
"""
200231
{% for child in node.children %}
201232
{{ formatter.format(child, query) }}
202233
{% endfor %}
203234
"""
204-
dir_template = self.env.from_string(template)
205-
return dir_template.render(node=node, query=query, formatter=self)
235+
dir_template = self.env.from_string(template)
236+
return dir_template.render(node=node, query=query, formatter=self)
206237

207-
@summary.register
208-
def _(self, node: FileSystemDirectory, query):
209-
template = \
238+
@self.summary.register(FileSystemDirectory)
239+
def _(node: FileSystemDirectory, query):
240+
template = \
210241
"""
211242
Directory structure:
212243
{{ node.tree }}
213244
"""
214-
summary_template = self.env.from_string(template)
215-
return summary_template.render(node=node, query=query, formatter=self)
216-
245+
summary_template = self.env.from_string(template)
246+
return summary_template.render(node=node, query=query, formatter=self)
217247

218-
@format.register
219-
def _(self, node: FileSystemSymlink, query):
220-
template = \
248+
@self.format.register(FileSystemSymlink)
249+
def _(node: FileSystemSymlink, query):
250+
template = \
221251
"""
222252
{{ SEPARATOR }}
223253
{{ node.name }}{% if node.target %} -> {{ node.target }}{% endif %}
224254
{{ SEPARATOR }}
225255
"""
226-
symlink_template = self.env.from_string(template)
227-
return symlink_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
256+
symlink_template = self.env.from_string(template)
257+
return symlink_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
258+
259+
class StupidFormatter(DefaultFormatter):
260+
def __init__(self):
261+
super().__init__()
262+
263+
@self.summary.register(FileSystemTextFile)
264+
def _(node: FileSystemTextFile, query):
265+
template = \
266+
"""
267+
{{ SEPARATOR }}
268+
{{ node.name }}
269+
{{ SEPARATOR }}
270+
FileSystemTextFile
271+
"""
272+
273+
@self.format.register(FileSystemFile)
274+
def _(node: FileSystemFile, query):
275+
template = \
276+
"""
277+
{{ SEPARATOR }}
278+
{{ node.name }}
279+
{{ SEPARATOR }}
280+
FileSystemFile
281+
"""
282+
file_template = self.env.from_string(template)
283+
return file_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)

src/gitingest/schemas/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Module containing the schemas for the Gitingest package."""
22

33
from gitingest.schemas.cloning import CloneConfig
4-
from gitingest.schemas.filesystem import FileSystemNode, FileSystemFile, FileSystemDirectory, FileSystemSymlink, FileSystemStats, Context, Source
4+
from gitingest.schemas.filesystem import FileSystemNode, FileSystemFile, FileSystemDirectory, FileSystemSymlink, FileSystemTextFile, FileSystemStats, Context, Source
55
from gitingest.schemas.ingestion import IngestionQuery
66

7-
__all__ = ["CloneConfig", "FileSystemNode", "FileSystemFile", "FileSystemDirectory", "FileSystemSymlink", "FileSystemStats", "IngestionQuery", "Context"]
7+
__all__ = ["CloneConfig", "FileSystemNode", "FileSystemFile", "FileSystemDirectory", "FileSystemSymlink", "FileSystemTextFile", "FileSystemStats", "IngestionQuery", "Context"]

src/gitingest/schemas/filesystem.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ def _tree(self):
6868

6969
@dataclass
7070
class FileSystemFile(FileSystemNode):
71-
pass # Nothing for now
71+
@property
72+
def content(self):
73+
# read the file
74+
try:
75+
with open(self.path, "r") as f:
76+
return f.read()
77+
except Exception as e:
78+
return f"Error reading content of {self.name}: {e}"
79+
80+
@dataclass
81+
class FileSystemTextFile(FileSystemFile):
82+
pass
7283

7384
@FileSystemNode._tree.register
7485
def _(self: 'FileSystemFile'):
@@ -117,7 +128,6 @@ def _(self: 'FileSystemSymlink'):
117128
return f"{self.name} -> {self.target}" if self.target else self.name
118129

119130

120-
@dataclass
121131
class Context:
122132
"""Context for holding a list of Source objects and generating a digest on demand using a Formatter.
123133

src/server/query_processor.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ async def process_query(
103103
else:
104104
# Store locally
105105
local_txt_file = Path(clone_config.local_path).with_suffix(".txt")
106+
print(f"Writing to {local_txt_file}")
106107
with local_txt_file.open("w", encoding="utf-8") as f:
107108
f.write(digest)
108109

@@ -116,13 +117,13 @@ async def process_query(
116117
"download full ingest to see more)\n" + digest[:MAX_DISPLAY_SIZE]
117118
)
118119

119-
_print_success(
120-
url=query.url,
121-
max_file_size=max_file_size,
122-
pattern_type=pattern_type,
123-
pattern=pattern,
124-
summary=digest,
125-
)
120+
# _print_success(
121+
# url=query.url,
122+
# max_file_size=max_file_size,
123+
# pattern_type=pattern_type,
124+
# pattern=pattern,
125+
# summary=digest,
126+
# )
126127

127128
# Generate digest_url based on S3 configuration
128129
if is_s3_enabled():

0 commit comments

Comments
 (0)