load_ignore_specs too meny local variables fix

This commit is contained in:
Kirill Markin 2024-12-17 17:06:38 +01:00
parent 2a08a70cf4
commit ecfbed98ac
No known key found for this signature in database
GPG key ID: 03AB9530E15B9C1C

View file

@ -134,49 +134,53 @@ def load_ignore_specs(
path: str = '.', path: str = '.',
cli_ignore_patterns: Optional[List[str]] = None cli_ignore_patterns: Optional[List[str]] = None
) -> Tuple[Optional[PathSpec], Optional[PathSpec], PathSpec]: ) -> Tuple[Optional[PathSpec], Optional[PathSpec], PathSpec]:
"""Load ignore specifications from various sources. """Load ignore specifications from various sources."""
Args:
path: Base directory path
cli_ignore_patterns: List of patterns from command line
Returns:
Tuple[Optional[PathSpec], Optional[PathSpec], PathSpec]: Tuple of gitignore_spec,
content_ignore_spec, and tree_and_content_ignore_spec
"""
gitignore_spec = None gitignore_spec = None
content_ignore_spec = None content_ignore_spec = None
tree_and_content_ignore_list: List[str] = [] tree_and_content_ignore_list: List[str] = []
use_gitignore = True use_gitignore = True
repo_settings_path = os.path.join(path, '.repo-to-text-settings.yaml') settings = load_settings_from_file(path)
if os.path.exists(repo_settings_path): if settings:
logging.debug('Loading .repo-to-text-settings.yaml from path: %s', repo_settings_path)
with open(repo_settings_path, 'r', encoding='utf-8') as f:
settings: Dict[str, Any] = yaml.safe_load(f)
use_gitignore = settings.get('gitignore-import-and-ignore', True) use_gitignore = settings.get('gitignore-import-and-ignore', True)
if 'ignore-content' in settings: content_ignore_spec = create_content_ignore_spec(settings)
content_ignore_spec: Optional[PathSpec] = pathspec.PathSpec.from_lines(
'gitwildmatch', settings['ignore-content']
)
if 'ignore-tree-and-content' in settings:
tree_and_content_ignore_list.extend(settings.get('ignore-tree-and-content', [])) tree_and_content_ignore_list.extend(settings.get('ignore-tree-and-content', []))
if cli_ignore_patterns: if cli_ignore_patterns:
tree_and_content_ignore_list.extend(cli_ignore_patterns) tree_and_content_ignore_list.extend(cli_ignore_patterns)
if use_gitignore: if use_gitignore:
gitignore_path = os.path.join(path, '.gitignore') gitignore_spec = load_gitignore_spec(path)
if os.path.exists(gitignore_path):
logging.debug('Loading .gitignore from path: %s', gitignore_path)
with open(gitignore_path, 'r', encoding='utf-8') as f:
gitignore_spec = pathspec.PathSpec.from_lines('gitwildmatch', f)
tree_and_content_ignore_spec = pathspec.PathSpec.from_lines( tree_and_content_ignore_spec = pathspec.PathSpec.from_lines(
'gitwildmatch', tree_and_content_ignore_list 'gitwildmatch', tree_and_content_ignore_list
) )
return gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec return gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec
def load_settings_from_file(path: str) -> Optional[Dict[str, Any]]:
"""Load settings from the .repo-to-text-settings.yaml file."""
repo_settings_path = os.path.join(path, '.repo-to-text-settings.yaml')
if os.path.exists(repo_settings_path):
logging.debug('Loading .repo-to-text-settings.yaml from path: %s', repo_settings_path)
with open(repo_settings_path, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
return None
def create_content_ignore_spec(settings: Dict[str, Any]) -> Optional[PathSpec]:
"""Create content ignore spec from settings."""
if 'ignore-content' in settings:
return pathspec.PathSpec.from_lines('gitwildmatch', settings['ignore-content'])
return None
def load_gitignore_spec(path: str) -> Optional[PathSpec]:
"""Load gitignore spec from the .gitignore file."""
gitignore_path = os.path.join(path, '.gitignore')
if os.path.exists(gitignore_path):
logging.debug('Loading .gitignore from path: %s', gitignore_path)
with open(gitignore_path, 'r', encoding='utf-8') as f:
return pathspec.PathSpec.from_lines('gitwildmatch', f)
return None
def should_ignore_file( def should_ignore_file(
file_path: str, file_path: str,
relative_path: str, relative_path: str,