A CLI tool and library to create directory trees from tree strings.
Project description
creatree
A Python package and CLI tool for creating directory structures from a tree-like string.
Overview
creatree is a simple yet powerful tool that allows you to define directory structures using a human-readable text format and then create those structures automatically in your filesystem. This is particularly useful for quickly setting up project templates, scaffolding applications, or sharing directory structures in documentation.
Motivation
This project was born out of a major pain point I faced repeatedly—manually creating project structures whenever I started a new project. Since I mostly adhere to the same structure unless there’s a specific need to modify it, I wanted a simple and efficient way to automate this process. Unfortunately, I couldn't find any good tools that fit my workflow.
Additionally, I often write proof-of-concept (POC) functionalities in a Jupyter notebook and later ask ChatGPT to break them down into a structured project format, including a tree with comments. creatree bridges this gap by allowing me to quickly generate well-organized project directories without unnecessary manual effort.
Installation
Using uv
To install creatree as a uv tool, run:
uv tool install creatree
Using pip
Alternatively, install creatree via pip:
pip install creatree
Usage
As a Python Library
You can use creatree programmatically to generate directory structures.
Example Usage
from creatree import creatree, tree_to_dict
# Define a directory tree as a string
tree = '''
example_project/ # Project root
├── main.py # Main entry point
├── config.yaml # Configuration file
├── src/ # Source code directory (Not empty)
│ ├── app.py # Application logic
│ └── utils.py # Utility functions
├── empty_directory/ # Empty directory (This will be created as it ends with /)
└── xyz_file # This will be created as a file as it doesn't end with /
'''
# Convert the tree string to a dictionary
tree_dict = tree_to_dict(tree, include_comments=False)
print(tree_dict)
# Create the directory structure in the current directory
# For each file that has a comment, it will be added to the file as a comment (Think of it like a TODO, except it's not explicit)
creatree(tree, where_to_create=".")
As a CLI Tool
Create a directory tree from a file
If you have a text file (for example, tree.txt) containing the directory structure, you can create it using:
creatree tree.txt -w .
Use pipe to create directory trees
You can also define a directory tree inline using pipes:
echo "root1/
├── a # This comment will not be present in the a directory :(
│ ├── b.py # This comment will be added to b.py as a comment :)
│ └── c.py
├── d/
└── e.py" | creatree -w .
Generate a directory tree from an existing structure
You can capture an existing directory structure and replicate it elsewhere using tree and creatree:
tree /path/to/existing/directory | creatree -w /path/to/new/location
Comparison with Other Tools
While there are other tools available for scaffolding projects, creatree offers a unique approach by allowing users to define directory structures in a simple, tree-like string format. This method provides a clear and concise way to visualize and create complex directory hierarchies without the need for extensive configuration files or templates.
Features
- Convert a tree string to a structured dictionary.
- Generate directory structures automatically.
- Supports both Python API and CLI usage.
- Works with standard input (
stdin) for flexible scripting. - Useful for setting up project templates and scaffolding.
- Supports nested directory structures with file placeholders.
- Lightweight and easy to use.
How to Contribute
We welcome contributions from the community! Here's how you can get involved:
-
Fork the Repository: Click on the 'Fork' button at the top right of the repository page to create a copy of the repository on your GitHub account.
-
Clone the Forked Repository: On your local machine, clone the forked repository using:
git clone https://github.com/[yourusername]/creatree.git cd creatree
-
Create a New Branch: Create a new branch for your feature or bug fix:
git checkout -b feature-name
-
Make Your Changes: Implement your feature or fix the bug in your branch.
-
Commit Your Changes: Commit your changes with a descriptive commit message:
git commit -m "Description of the feature or fix"
-
Push to GitHub: Push your changes to your forked repository:
git push origin feature-name
-
Create a Pull Request: Go to the original repository and create a pull request from your forked repository. Provide a clear description of your changes and the motivation behind them.
Before contributing, please ensure that your code adheres to the project's coding standards and that all tests pass. If you're adding a new feature, consider including tests to cover the new functionality.
License
creatree is released under the MIT License, allowing free use, modification, and distribution.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file creatree-0.1.2.tar.gz.
File metadata
- Download URL: creatree-0.1.2.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bb509c17b8a299a6ef9e1438c36421798894fdf89ad8ebfc59d99ec2997b369
|
|
| MD5 |
c8e8b2acb73c7193966899f24a2a8792
|
|
| BLAKE2b-256 |
d559ab795a3fbeb6e45cb3db64c43c481db49717831977f58bf7a5ecee667335
|
File details
Details for the file creatree-0.1.2-py3-none-any.whl.
File metadata
- Download URL: creatree-0.1.2-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2db5d9087fa78f00d2f92b8bfb532756ed7f7891006193a7e4f0c7804953a703
|
|
| MD5 |
d146d497f3334552b5a9cfc8be00f363
|
|
| BLAKE2b-256 |
d02d387ca41160d37bffe44e705b0ada54ce178dc064f46d9868dd372ca77c64
|