|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from repo_context.ignore import EXTENSIONS, FILES, PATTERNS |
| 5 | +from repo_context.utils import should_ignore |
| 6 | + |
| 7 | +logger = logging.getLogger("repo_context.structure") |
| 8 | + |
| 9 | + |
| 10 | +class RepoStructure: |
| 11 | + def __init__(self, ignore_patterns: list[str] | None = None) -> None: |
| 12 | + self.ignore_patterns = ignore_patterns or [] |
| 13 | + self.ignore_patterns += FILES + EXTENSIONS + PATTERNS |
| 14 | + |
| 15 | + def generate_tree( |
| 16 | + self, |
| 17 | + directory: Path, |
| 18 | + prefix: str = "", |
| 19 | + is_last: bool = True, |
| 20 | + ignore_patterns: list[str] | None = None, |
| 21 | + ) -> list[str]: |
| 22 | + """ |
| 23 | + Recursively generate a tree structure of the directory. |
| 24 | +
|
| 25 | + Args: |
| 26 | + directory: Path object pointing to the directory |
| 27 | + prefix: Prefix for the current line (used for recursion) |
| 28 | + is_last: Boolean indicating if this is the last item in current directory |
| 29 | + ignore_patterns: List of patterns to ignore |
| 30 | +
|
| 31 | + Returns: |
| 32 | + List[str]: Lines of the tree structure |
| 33 | + """ |
| 34 | + if ignore_patterns is None: |
| 35 | + ignore_patterns = [] |
| 36 | + |
| 37 | + if not directory.is_dir(): |
| 38 | + logger.error(f"'{directory}' is not a valid directory") |
| 39 | + return [] |
| 40 | + |
| 41 | + tree_lines = [] |
| 42 | + items = [ |
| 43 | + item |
| 44 | + for item in sorted(directory.iterdir()) |
| 45 | + if not should_ignore(item.name, ignore_patterns) |
| 46 | + ] |
| 47 | + |
| 48 | + for i, item in enumerate(items): |
| 49 | + is_last_item = i == len(items) - 1 |
| 50 | + connector = "??? " if is_last_item else "??? " |
| 51 | + |
| 52 | + tree_lines.append(f"{prefix}{connector}{item.name}") |
| 53 | + |
| 54 | + if item.is_dir(): |
| 55 | + extension = " " if is_last_item else "? " |
| 56 | + tree_lines.extend( |
| 57 | + self.generate_tree( |
| 58 | + item, |
| 59 | + prefix + extension, |
| 60 | + is_last_item, |
| 61 | + ignore_patterns, |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + return tree_lines |
| 66 | + |
| 67 | + def create_tree_structure( |
| 68 | + self, |
| 69 | + path: str, |
| 70 | + output_file: str | None = None, |
| 71 | + ignore_patterns: list[str] | None = None, |
| 72 | + ) -> None: |
| 73 | + """ |
| 74 | + Create and display/save a tree structure of the specified directory. |
| 75 | +
|
| 76 | + Args: |
| 77 | + path: Path to the directory |
| 78 | + output_file: Optional file path to save the tree structure |
| 79 | + ignore_patterns: List of patterns to ignore |
| 80 | + """ |
| 81 | + directory = Path(path) |
| 82 | + if not directory.exists(): |
| 83 | + logger.error(f"Directory '{path}' does not exist") |
| 84 | + return |
| 85 | + |
| 86 | + logger.info(f"Generating tree structure for: {directory.absolute()}") |
| 87 | + |
| 88 | + tree_lines = ["Directory Structure:", directory.name] |
| 89 | + tree_lines.extend( |
| 90 | + self.generate_tree(directory, ignore_patterns=ignore_patterns or []) |
| 91 | + ) |
| 92 | + |
| 93 | + # Join lines with newlines |
| 94 | + tree_structure = "\n".join(tree_lines) |
| 95 | + |
| 96 | + # Print to console |
| 97 | + logger.info(tree_structure) |
| 98 | + |
| 99 | + # Save to file if specified |
| 100 | + if output_file: |
| 101 | + output_path = Path(output_file) |
| 102 | + try: |
| 103 | + output_path.write_text(tree_structure) |
| 104 | + logger.info(f"Tree structure saved to: {output_path.absolute()}") |
| 105 | + except Exception as e: |
| 106 | + logger.error(f"Failed to save tree structure: {e}") |
0 commit comments