56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
"""Dependency parsers for the Auto README Generator."""
|
|
|
|
from abc import ABC, abstractmethod
|
|
from pathlib import Path
|
|
from typing import Optional, Protocol
|
|
|
|
from ..models import Dependency
|
|
|
|
|
|
class DependencyParser(Protocol):
|
|
"""Protocol for dependency parsers."""
|
|
|
|
def can_parse(self, path: Path) -> bool: ...
|
|
def parse(self, path: Path) -> list[Dependency]: ...
|
|
|
|
|
|
class BaseParser(ABC):
|
|
"""Abstract base class for dependency parsers."""
|
|
|
|
@abstractmethod
|
|
def can_parse(self, path: Path) -> bool:
|
|
"""Check if this parser can handle the given file."""
|
|
pass
|
|
|
|
@abstractmethod
|
|
def parse(self, path: Path) -> list[Dependency]:
|
|
"""Parse dependencies from the file."""
|
|
pass
|
|
|
|
def _normalize_version(self, version: Optional[str]) -> Optional[str]:
|
|
"""Normalize version string."""
|
|
if version is None:
|
|
return None
|
|
version = version.strip()
|
|
if version.startswith("^") or version.startswith("~"):
|
|
version = version[1:]
|
|
if version.startswith(">="):
|
|
version = version[2:]
|
|
return version if version else None
|
|
|
|
|
|
from .python_parser import PythonDependencyParser
|
|
from .javascript_parser import JavaScriptDependencyParser
|
|
from .go_parser import GoDependencyParser
|
|
from .rust_parser import RustDependencyParser
|
|
from .parser_factory import DependencyParserFactory
|
|
|
|
__all__ = [
|
|
"DependencyParser",
|
|
"BaseParser",
|
|
"PythonDependencyParser",
|
|
"JavaScriptDependencyParser",
|
|
"GoDependencyParser",
|
|
"RustDependencyParser",
|
|
"DependencyParserFactory",
|
|
] |