Source code for pip_rating.req_files.base

from pathlib import Path
from typing import Union, List, Optional

from pkg_resources import Requirement


[docs]class ReqFileBase(list): """Base class for requirement files.""" def __init__(self, path: Union[str, Path]): """Initialize the requirement file.""" if isinstance(path, str): path = Path(path) self.path = path if not self.path.exists(): raise IOError(f"File {self.path} does not exist") super().__init__(self.get_dependencies())
[docs] @classmethod def find_in_directory(cls, directory: Union[str, Path]) -> Optional["ReqFileBase"]: """Find requirement file in the given directory.""" raise NotImplementedError
[docs] @classmethod def is_valid(cls, path: Union[str, Path]) -> bool: """Check if the given path is a valid requirement file.""" raise NotImplementedError
[docs] def get_dependencies(self) -> List[str]: """Get the dependencies from the file.""" raise NotImplementedError
def __contains__(self, item: str) -> bool: req = Requirement(item) for package in self: package = Requirement(package) if ( not req.specifier and package.name.lower() == req.name.lower() ) or package == req: return True return False def __str__(self) -> str: return self.path.name def __repr__(self) -> str: return f"<ReqFile {self.path}>"