Add package manager parsers (npm, pip, go, cargo)
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
72
src/depcheck/parsers/__init__.py
Normal file
72
src/depcheck/parsers/__init__.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
"""Base parser class and parser registry."""
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from depcheck.models import Dependency, PackageManager
|
||||||
|
|
||||||
|
|
||||||
|
class Parser(ABC):
|
||||||
|
"""Base class for package dependency parsers."""
|
||||||
|
|
||||||
|
package_manager: PackageManager = PackageManager.PIP
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def parse(self, file_path: Path) -> list[Dependency]:
|
||||||
|
"""Parse a dependency file and return list of dependencies."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def supports_file(self, file_path: Path) -> bool:
|
||||||
|
"""Check if this parser supports the given file."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_file_patterns(self) -> list[str]:
|
||||||
|
"""Get file patterns this parser handles."""
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
class ParserRegistry:
|
||||||
|
"""Registry of available parsers."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._parsers: list[Parser] = []
|
||||||
|
|
||||||
|
def register(self, parser: Parser) -> None:
|
||||||
|
"""Register a parser."""
|
||||||
|
self._parsers.append(parser)
|
||||||
|
|
||||||
|
def get_parser(self, file_path: Path) -> Optional[Parser]:
|
||||||
|
"""Get parser for a specific file."""
|
||||||
|
for parser in self._parsers:
|
||||||
|
if parser.supports_file(file_path):
|
||||||
|
return parser
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_all_parsers(self) -> list[Parser]:
|
||||||
|
"""Get all registered parsers."""
|
||||||
|
return self._parsers
|
||||||
|
|
||||||
|
def detect_package_manager(self, directory: Path) -> Optional[PackageManager]:
|
||||||
|
"""Detect package manager based on file existence."""
|
||||||
|
for parser in self._parsers:
|
||||||
|
for pattern in parser.get_file_patterns():
|
||||||
|
if (directory / pattern).exists():
|
||||||
|
return parser.package_manager
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def create_parser_registry() -> ParserRegistry:
|
||||||
|
"""Create and register all available parsers."""
|
||||||
|
from depcheck.parsers.npm import NpmParser
|
||||||
|
from depcheck.parsers.pip import PipParser
|
||||||
|
from depcheck.parsers.go import GoParser
|
||||||
|
from depcheck.parsers.cargo import CargoParser
|
||||||
|
|
||||||
|
registry = ParserRegistry()
|
||||||
|
registry.register(NpmParser())
|
||||||
|
registry.register(PipParser())
|
||||||
|
registry.register(GoParser())
|
||||||
|
registry.register(CargoParser())
|
||||||
|
return registry
|
||||||
Reference in New Issue
Block a user