Skip to content

Commit cd6b1e6

Browse files
committed
commit avant la catastrophe
1 parent 2273fcf commit cd6b1e6

File tree

5 files changed

+128
-50
lines changed

5 files changed

+128
-50
lines changed

src/gitingest/ingestion.py

Lines changed: 20 additions & 9 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
from gitingest.utils.logging_config import get_logger
1414

@@ -96,6 +96,7 @@ def ingest_query(query: IngestionQuery) -> Context:
9696
"file_size": file_node.size,
9797
},
9898
)
99+
return Context([file_node], StupidFormatter(), query)
99100

100101
# root_node = FileSystemNode(
101102
# name=path.name,
@@ -124,7 +125,7 @@ def ingest_query(query: IngestionQuery) -> Context:
124125
},
125126
)
126127

127-
return Context([root_node], DefaultFormatter(), query)
128+
return Context([root_node], StupidFormatter(), query)
128129

129130

130131
def _process_node(node: FileSystemNode, query: IngestionQuery, stats: FileSystemStats) -> None:
@@ -263,12 +264,22 @@ def _process_file(path: Path, parent_node: FileSystemDirectory, stats: FileSyste
263264
stats.total_files += 1
264265
stats.total_size += file_size
265266

266-
child = FileSystemFile(
267-
name=path.name,
268-
path_str=str(path.relative_to(local_path)),
269-
path=path,
270-
depth=parent_node.depth + 1,
271-
)
267+
# if file is a .txt file, create a FileSystemTextFile
268+
if path.suffix == ".txt":
269+
child = FileSystemTextFile(
270+
name=path.name,
271+
path_str=str(path.relative_to(local_path)),
272+
path=path,
273+
depth=parent_node.depth + 1,
274+
)
275+
else:
276+
277+
child = FileSystemFile(
278+
name=path.name,
279+
path_str=str(path.relative_to(local_path)),
280+
path=path,
281+
depth=parent_node.depth + 1,
282+
)
272283

273284
parent_node.children.append(child)
274285
parent_node.size += file_size

src/gitingest/output_formatter.py

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,36 @@
1111
from gitingest.schemas import FileSystemNode
1212
from gitingest.utils.compat_func import readlink
1313
from functools import singledispatchmethod
14-
from gitingest.schemas import Source, FileSystemFile, FileSystemDirectory, FileSystemSymlink
15-
from gitingest.schemas.filesystem import SEPARATOR
14+
from gitingest.schemas import Source, FileSystemFile, FileSystemDirectory, FileSystemSymlink, FileSystemTextFile
15+
from gitingest.schemas.filesystem import SEPARATOR, FileSystemNodeType
1616
from gitingest.utils.logging_config import get_logger
1717
from jinja2 import Environment, BaseLoader
1818

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

@@ -179,59 +204,90 @@ def _format_token_count(text: str) -> str | None:
179204

180205
class DefaultFormatter:
181206
def __init__(self):
207+
self.separator = SEPARATOR
182208
self.env = Environment(loader=BaseLoader())
183209

184-
@singledispatchmethod
185-
def format(self, node: Source, query):
186-
return f"{getattr(node, 'content', '')}"
210+
# Set up custom dispatchers
211+
def _default_format(node: Source, query):
212+
return f"{getattr(node, 'content', '')}"
213+
214+
def _default_summary(node: Source, query):
215+
return f"{getattr(node, 'name', '')}"
187216

188-
@singledispatchmethod
189-
def summary(self, node: Source, query):
190-
# Default summary: just the name
191-
return f"{getattr(node, 'name', '')}"
217+
self.format = OverridableDispatcher(_default_format)
218+
self.summary = OverridableDispatcher(_default_summary)
192219

193-
@format.register
194-
def _(self, node: FileSystemFile, query):
195-
template = \
220+
# Register the default implementations
221+
self._register_defaults()
222+
223+
def _register_defaults(self):
224+
@self.format.register(FileSystemFile)
225+
def _(node: FileSystemFile, query):
226+
template = \
196227
"""
197228
{{ SEPARATOR }}
198229
{{ node.name }}
199230
{{ SEPARATOR }}
200231
201232
{{ node.content }}
202233
"""
203-
file_template = self.env.from_string(template)
204-
return file_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
234+
file_template = self.env.from_string(template)
235+
return file_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
205236

206-
@format.register
207-
def _(self, node: FileSystemDirectory, query):
208-
template = \
237+
@self.format.register(FileSystemDirectory)
238+
def _(node: FileSystemDirectory, query):
239+
template = \
209240
"""
210241
{% for child in node.children %}
211242
{{ formatter.format(child, query) }}
212243
{% endfor %}
213244
"""
214-
dir_template = self.env.from_string(template)
215-
return dir_template.render(node=node, query=query, formatter=self)
245+
dir_template = self.env.from_string(template)
246+
return dir_template.render(node=node, query=query, formatter=self)
216247

217-
@summary.register
218-
def _(self, node: FileSystemDirectory, query):
219-
template = \
248+
@self.summary.register(FileSystemDirectory)
249+
def _(node: FileSystemDirectory, query):
250+
template = \
220251
"""
221252
Directory structure:
222253
{{ node.tree }}
223254
"""
224-
summary_template = self.env.from_string(template)
225-
return summary_template.render(node=node, query=query, formatter=self)
226-
255+
summary_template = self.env.from_string(template)
256+
return summary_template.render(node=node, query=query, formatter=self)
227257

228-
@format.register
229-
def _(self, node: FileSystemSymlink, query):
230-
template = \
258+
@self.format.register(FileSystemSymlink)
259+
def _(node: FileSystemSymlink, query):
260+
template = \
231261
"""
232262
{{ SEPARATOR }}
233263
{{ node.name }}{% if node.target %} -> {{ node.target }}{% endif %}
234264
{{ SEPARATOR }}
235265
"""
236-
symlink_template = self.env.from_string(template)
237-
return symlink_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
266+
symlink_template = self.env.from_string(template)
267+
return symlink_template.render(SEPARATOR=SEPARATOR, node=node, query=query, formatter=self)
268+
269+
class StupidFormatter(DefaultFormatter):
270+
def __init__(self):
271+
super().__init__()
272+
273+
@self.summary.register(FileSystemTextFile)
274+
def _(node: FileSystemTextFile, query):
275+
template = \
276+
"""
277+
{{ SEPARATOR }}
278+
{{ node.name }}
279+
{{ SEPARATOR }}
280+
FileSystemTextFile
281+
"""
282+
283+
@self.format.register(FileSystemFile)
284+
def _(node: FileSystemFile, query):
285+
template = \
286+
"""
287+
{{ SEPARATOR }}
288+
{{ node.name }}
289+
{{ SEPARATOR }}
290+
FileSystemFile
291+
"""
292+
file_template = self.env.from_string(template)
293+
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
@@ -322,6 +322,7 @@ async def process_query(
322322
else:
323323
# Store locally
324324
local_txt_file = Path(clone_config.local_path).with_suffix(".txt")
325+
print(f"Writing to {local_txt_file}")
325326
with local_txt_file.open("w", encoding="utf-8") as f:
326327
f.write(digest)
327328

@@ -337,13 +338,13 @@ async def process_query(
337338
"download full ingest to see more)\n" + digest[:MAX_DISPLAY_SIZE]
338339
)
339340

340-
_print_success(
341-
url=query.url,
342-
max_file_size=max_file_size,
343-
pattern_type=pattern_type,
344-
pattern=pattern,
345-
summary=digest,
346-
)
341+
# _print_success(
342+
# url=query.url,
343+
# max_file_size=max_file_size,
344+
# pattern_type=pattern_type,
345+
# pattern=pattern,
346+
# summary=digest,
347+
# )
347348

348349
digest_url = _generate_digest_url(query)
349350

0 commit comments

Comments
 (0)