gabykim commited on
Commit
604ef86
·
1 Parent(s): 16bbb61

know_lang_bot __main__.py

Browse files
.vscode/launch.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "version": "0.2.0",
3
+ "configurations": [
4
+ {
5
+ "name": "Know Lang Bot",
6
+ "type": "debugpy",
7
+ "request": "launch",
8
+ "module": "know_lang_bot",
9
+ "args": [
10
+ "--v"
11
+ ]
12
+ }
13
+ ]
14
+ }
src/know_lang_bot/__main__.py CHANGED
@@ -1,18 +1,139 @@
1
- from know_lang_bot.code_parser.parser import CodeParser
 
 
 
 
 
 
 
2
  from know_lang_bot.config import AppConfig
3
- from know_lang_bot.code_parser.summarizer import CodeSummarizer
4
- import asyncio
 
 
5
 
6
- # Usage example
7
- if __name__ == "__main__":
8
- async def main():
9
- config = AppConfig() # Will load from .env file if available
10
- summarizer = CodeSummarizer(config)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Example usage with your parser
13
- parser = CodeParser(".")
14
- chunks = parser.parse_repository()
15
 
16
- await summarizer.process_chunks(chunks)
17
-
18
- asyncio.run(main())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import sys
3
+ from pathlib import Path
4
+ from typing import List, Optional
5
+ from rich.console import Console
6
+ from rich.table import Table
7
+
8
+ from know_lang_bot.code_parser.parser import CodeChunk
9
  from know_lang_bot.config import AppConfig
10
+ from know_lang_bot.parser.factory import CodeParserFactory
11
+ from know_lang_bot.parser.providers.git import GitProvider
12
+ from know_lang_bot.parser.providers.filesystem import FilesystemProvider
13
+ from know_lang_bot.utils.fancy_log import FancyLogger
14
 
15
+ LOG = FancyLogger(__name__)
16
+ console = Console()
17
+
18
+ def parse_args():
19
+ parser = argparse.ArgumentParser(
20
+ description="Know Lang Bot - Code Analysis Tool",
21
+ formatter_class=argparse.RawDescriptionHelpFormatter
22
+ )
23
+
24
+ parser.add_argument(
25
+ "--source_path",
26
+ type=str,
27
+ default=".",
28
+ help="Path to the source code (git repository or directory)"
29
+ )
30
+
31
+ parser.add_argument(
32
+ "--config",
33
+ type=str,
34
+ help="Path to custom configuration file",
35
+ default=None
36
+ )
37
+
38
+ parser.add_argument(
39
+ "--verbose", "-v",
40
+ action="store_true",
41
+ help="Enable verbose output"
42
+ )
43
+
44
+ parser.add_argument(
45
+ "--output",
46
+ type=str,
47
+ choices=["table", "json"],
48
+ default="table",
49
+ help="Output format (default: table)"
50
+ )
51
+
52
+ return parser.parse_args()
53
+
54
+ def create_config(config_path: Optional[str] = None) -> AppConfig:
55
+ """Create configuration, optionally from a file"""
56
+ if config_path:
57
+ with open(config_path, 'r') as file:
58
+ config_data = file.read()
59
+ return AppConfig.model_validate_json(config_data)
60
+ return AppConfig()
61
+
62
+ def display_results_table(chunks : List[CodeChunk]):
63
+ """Display parsed chunks in a rich table"""
64
+ table = Table(show_header=True, header_style="bold magenta")
65
+ table.add_column("Type")
66
+ table.add_column("Name")
67
+ table.add_column("File")
68
+ table.add_column("Lines")
69
+ table.add_column("Parent")
70
+
71
+ for chunk in chunks:
72
+ table.add_row(
73
+ chunk.type.value,
74
+ chunk.name or "N/A",
75
+ str(chunk.file_path),
76
+ f"{chunk.start_line}-{chunk.end_line}",
77
+ chunk.parent_name or "N/A"
78
+ )
79
+
80
+ console.print(table)
81
+
82
+ def display_results_json(chunks: List[CodeChunk]):
83
+ """Display parsed chunks as JSON"""
84
+ import json
85
+ print(json.dumps([chunk.model_dump() for chunk in chunks], indent=2))
86
+
87
+ def main():
88
+ args = parse_args()
89
+
90
+ # Setup logging
91
+ if args.verbose:
92
+ LOG.setLevel("DEBUG")
93
+
94
+ try:
95
+ # Load configuration
96
+ config = create_config(args.config)
97
+ source_path = Path(args.source_path)
98
 
99
+ # Create parser factory
100
+ factory = CodeParserFactory(config)
 
101
 
102
+ # Determine provider
103
+ if (source_path / '.git').exists():
104
+ LOG.info(f"Detected Git repository at {source_path}")
105
+ provider = GitProvider(source_path, config)
106
+ else:
107
+ LOG.info(f"Using filesystem provider for {source_path}")
108
+ provider = FilesystemProvider(source_path, config)
109
+
110
+ # Process files
111
+ total_chunks = []
112
+ with console.status("[bold green]Parsing files...") as status:
113
+ for file_path in provider.get_files():
114
+ status.update(f"[bold green]Processing {file_path}...")
115
+
116
+ parser = factory.get_parser(file_path)
117
+ if parser:
118
+ chunks = parser.parse_file(file_path)
119
+ total_chunks.extend(chunks)
120
+
121
+ # Display results
122
+ if total_chunks:
123
+ LOG.info(f"\nFound {len(total_chunks)} code chunks")
124
+ if args.output == "table":
125
+ display_results_table(total_chunks)
126
+ else:
127
+ display_results_json(total_chunks)
128
+ else:
129
+ LOG.warning("No code chunks found")
130
+
131
+ except Exception as e:
132
+ LOG.error(f"Error: {str(e)}")
133
+ if args.verbose:
134
+ import traceback
135
+ traceback.print_exc()
136
+ sys.exit(1)
137
+
138
+ if __name__ == "__main__":
139
+ main()
src/know_lang_bot/parser/base/parser.py CHANGED
@@ -8,7 +8,6 @@ from tree_sitter import Language, Parser
8
  class LanguageParser(ABC):
9
  """Abstract base class for language-specific parsers"""
10
 
11
- @abstractmethod
12
  def __init__(self, config: AppConfig):
13
  self.config : AppConfig = config
14
  self.language : Language = None
 
8
  class LanguageParser(ABC):
9
  """Abstract base class for language-specific parsers"""
10
 
 
11
  def __init__(self, config: AppConfig):
12
  self.config : AppConfig = config
13
  self.language : Language = None
src/know_lang_bot/parser/factory.py CHANGED
@@ -29,11 +29,13 @@ class CodeParserFactory():
29
  if not self.config.parser.languages[lang].enabled:
30
  continue
31
 
32
- parser = parser_class(self.config)
 
 
 
 
 
33
  if parser.supports_extension(extension):
34
- if lang not in self._parsers:
35
- parser.setup()
36
- self._parsers[lang] = parser
37
  return self._parsers[lang]
38
 
39
  return None
 
29
  if not self.config.parser.languages[lang].enabled:
30
  continue
31
 
32
+ parser = self._parsers.get(lang)
33
+ if parser is None:
34
+ parser = parser_class(self.config)
35
+ parser.setup()
36
+ self._parsers[lang] = parser
37
+
38
  if parser.supports_extension(extension):
 
 
 
39
  return self._parsers[lang]
40
 
41
  return None