When generating README documentation, you may want to exclude certain files or directories from the output. To achieve this, you can create a .readmeai_ignore file in your repository to specify patterns for files and directories that should be ignored during README generation.
"""Handling logic for .readmeai_ignore files."""importpathlibfromtypingimportList,SetimportfnmatchclassReadmeIgnoreHandler:"""Handler for .readmeai_ignore file processing."""def__init__(self,repo_path:pathlib.Path)->None:"""Initialize the ignore handler with repository path."""self.repo_path=repo_pathself.ignore_patterns:Set[str]=set()self._load_ignore_file()def_load_ignore_file(self)->None:"""Load patterns from .readmeai_ignore file if it exists."""ignore_file=self.repo_path/".readmeai_ignore"ifnotignore_file.exists():returnwithopen(ignore_file,'r',encoding='utf-8')asf:forlineinf:# Remove comments and whitespaceline=line.split('#')[0].strip()ifline:self.ignore_patterns.add(line)defshould_ignore(self,file_path:pathlib.Path)->bool:""" Check if a file should be ignored based on .readmeai_ignore patterns. Args: file_path: Path object relative to repository root Returns: bool: True if file should be ignored, False otherwise """# Convert path to string for pattern matchingpath_str=str(file_path.relative_to(self.repo_path))# Check each ignore patternforpatterninself.ignore_patterns:iffnmatch.fnmatch(path_str,pattern):returnTruereturnFalsedefget_ignore_patterns(self)->List[str]:"""Return list of current ignore patterns."""returnsorted(self.ignore_patterns)defadd_pattern(self,pattern:str)->None:"""Add a new ignore pattern."""self.ignore_patterns.add(pattern.strip())defremove_pattern(self,pattern:str)->bool:""" Remove an ignore pattern. Returns: bool: True if pattern was removed, False if not found """try:self.ignore_patterns.remove(pattern.strip())returnTrueexceptKeyError:returnFalse