scaffold_kit.tree
Generates a hierarchical tree representation of a directory.
This module scans a specified directory, builds a hierarchical structure of its
contents, and renders a text-based tree diagram. It uses an IgnoreParser to
exclude files and directories based on patterns found in a specified ignore
file (e.g., ‘.gitignore’). The generated tree is written to a file and also
printed to the console.
Usage
To run this script, navigate to your project’s root directory and execute it as a module:
Generate full project tree:#
$ uv run python -m scaffold_kit.tree
Generate partial tree from subdirectory:#
$ uv run python -m scaffold_kit.tree my_project/data
build_tree_structure(paths, ignore_matches)
#
Builds a hierarchical tree structure from a list of file paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list[str]
|
A list of file/directory paths. |
required |
ignore_matches
|
Callable
|
A function to check if a path should be ignored. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A nested dictionary representing the directory structure. |
Source code in src/scaffold_kit/tree.py
generate_tree(root_dir='.', ignore_file='.gitignore', output_file='directory-tree.txt', output_dir=None)
#
Generates a directory tree of files in the specified directory.
The function scans a directory, applies ignore patterns, and creates a text-based tree representation that is saved to a file and printed to the console.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_dir
|
str
|
The root directory to scan (default: current directory). |
'.'
|
ignore_file
|
str
|
The name of the file containing ignore patterns. |
'.gitignore'
|
output_file
|
str
|
The name of the output file for the tree. |
'directory-tree.txt'
|
output_dir
|
Optional[str]
|
The directory where the output file will be saved. (default: current directory). |
None
|
Raises:
| Type | Description |
|---|---|
SystemExit
|
If the specified |
Source code in src/scaffold_kit/tree.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
main()
#
Main entry point to run the directory tree generation process.
Parses command-line arguments and runs the tree generation process.
Source code in src/scaffold_kit/tree.py
render_tree(tree, prefix='', current_path='')
#
Recursively renders the tree structure with proper tree characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
dict
|
The nested dictionary representing the directory structure. |
required |
prefix
|
str
|
The current line prefix for indentation. |
''
|
current_path
|
str
|
The full path to the current directory being processed. |
''
|
Returns:
| Type | Description |
|---|---|
list
|
A list of formatted lines representing the tree structure. |
Source code in src/scaffold_kit/tree.py
sort_tree_items(items, current_path='')
#
Sorts items with directories first, then files, both alphabetically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
A list of |
required |
current_path
|
str
|
The current path for checking if items are directories. |
''
|
Returns:
| Type | Description |
|---|---|
list
|
A sorted list of |