mirror of
https://github.com/kirill-markin/repo-to-text.git
synced 2025-12-06 03:22:23 -08:00
remove_empty_dirs new logic and linter cleanup
This commit is contained in:
parent
5f283feefd
commit
a364328e60
4 changed files with 111 additions and 85 deletions
|
|
@ -1,3 +1,5 @@
|
|||
from .utils import setup_logging, check_tree_command, is_ignored_path, remove_empty_dirs
|
||||
"""This module contains utility functions for the repo_to_text package."""
|
||||
|
||||
__all__ = ['setup_logging', 'check_tree_command', 'is_ignored_path', 'remove_empty_dirs']
|
||||
from .utils import setup_logging, check_tree_command, is_ignored_path
|
||||
|
||||
__all__ = ['setup_logging', 'check_tree_command', 'is_ignored_path']
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
"""This module contains utility functions for the repo_to_text package."""
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import logging
|
||||
from typing import List, Set
|
||||
from typing import List
|
||||
|
||||
def setup_logging(debug: bool = False) -> None:
|
||||
"""Set up logging configuration.
|
||||
|
|
@ -47,36 +46,3 @@ def is_ignored_path(file_path: str) -> bool:
|
|||
if result:
|
||||
logging.debug('Path ignored: %s', file_path)
|
||||
return result
|
||||
|
||||
def remove_empty_dirs(tree_output: str) -> str:
|
||||
"""Remove empty directories from tree output."""
|
||||
logging.debug('Removing empty directories from tree output')
|
||||
lines = tree_output.splitlines()
|
||||
filtered_lines: List[str] = []
|
||||
|
||||
# Track directories that have files or subdirectories
|
||||
non_empty_dirs: Set[str] = set()
|
||||
|
||||
# First pass: identify non-empty directories
|
||||
for line in reversed(lines):
|
||||
stripped_line = line.strip()
|
||||
if not stripped_line.endswith('/'):
|
||||
# This is a file, mark its parent directory as non-empty
|
||||
parent_dir: str = os.path.dirname(stripped_line)
|
||||
while parent_dir:
|
||||
non_empty_dirs.add(parent_dir)
|
||||
parent_dir = os.path.dirname(parent_dir)
|
||||
|
||||
# Second pass: filter out empty directories
|
||||
for line in lines:
|
||||
stripped_line = line.strip()
|
||||
if stripped_line.endswith('/'):
|
||||
# This is a directory
|
||||
dir_path = stripped_line[:-1] # Remove trailing slash
|
||||
if dir_path not in non_empty_dirs:
|
||||
logging.debug('Directory is empty and will be removed: %s', dir_path)
|
||||
continue
|
||||
filtered_lines.append(line)
|
||||
|
||||
logging.debug('Empty directory removal complete')
|
||||
return '\n'.join(filtered_lines)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue