diff --git a/src/auto_readme/analyzers/__init__.py b/src/auto_readme/analyzers/__init__.py new file mode 100644 index 0000000..bce3a13 --- /dev/null +++ b/src/auto_readme/analyzers/__init__.py @@ -0,0 +1,55 @@ +"""Code analyzers for extracting functions, classes, and imports from source code.""" + +from abc import ABC, abstractmethod +from pathlib import Path +from typing import Protocol + +from ..utils.path_utils import PathUtils + + +class CodeAnalyzer(Protocol): + """Protocol for code analyzers.""" + + def can_analyze(self, path: Path) -> bool: ... + def analyze(self, path: Path) -> dict: ... + + +class BaseAnalyzer(ABC): + """Abstract base class for code analyzers.""" + + @abstractmethod + def can_analyze(self, path: Path) -> bool: + """Check if this analyzer can handle the file.""" + pass + + @abstractmethod + def analyze(self, path: Path) -> dict: + """Analyze the file and return extracted information.""" + pass + + def _get_file_content(self, path: Path) -> str | None: + """Safely read file content.""" + try: + if PathUtils.get_file_size(path) > 1024 * 1024: + return None + with open(path, "r", encoding="utf-8", errors="ignore") as f: + return f.read() + except OSError: + return None + + +from .python_analyzer import PythonAnalyzer +from .javascript_analyzer import JavaScriptAnalyzer +from .go_analyzer import GoAnalyzer +from .rust_analyzer import RustAnalyzer +from .analyzer_factory import CodeAnalyzerFactory + +__all__ = [ + "CodeAnalyzer", + "BaseAnalyzer", + "PythonAnalyzer", + "JavaScriptAnalyzer", + "GoAnalyzer", + "RustAnalyzer", + "CodeAnalyzerFactory", +]