File size: 1,443 Bytes
71fa0c7 60532a1 71fa0c7 604ef86 c20abe2 71fa0c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
from typing import Dict, Type, Optional
from pathlib import Path
from knowlang.parser.base.parser import LanguageParser
from knowlang.parser.languages.python.parser import PythonParser
from knowlang.configs.config import AppConfig
class CodeParserFactory():
"""Concrete implementation of parser factory"""
def __init__(self, config: AppConfig):
self.config = config
self._parsers: Dict[str, LanguageParser] = {}
self._parser_classes = self._register_parsers()
def _register_parsers(self) -> Dict[str, Type[LanguageParser]]:
"""Register available parser implementations"""
return {
"python": PythonParser,
# Add more languages here
}
def get_parser(self, file_path: Path) -> Optional[LanguageParser]:
"""Get appropriate parser for a file"""
extension = file_path.suffix
# Find parser class for this extension
for lang, parser_class in self._parser_classes.items():
if not self.config.parser.languages[lang].enabled:
continue
parser = self._parsers.get(lang)
if parser is None:
parser = parser_class(self.config)
parser.setup()
self._parsers[lang] = parser
if parser.supports_extension(extension):
return self._parsers[lang]
return None |