A Python library for parsing directory structures from text files and implementing them in target folders
Project description
TreeDir - Directory Structure Parser and Manager
A Python library for parsing directory structures from text files and implementing them in target folders with various operation modes.
Features
- Multiple Input Formats: Support for tree format, dictionary format, and simple path format
- Flexible Operations: Additive and strict enforcement modes
- Visualization: Tree-style visualization of directory structures
- Sandbox Mode: Preview changes before applying them
- Search Functions: Find files and folders with absolute or relative paths
- Backup Creation: Automatic backups before destructive operations
- Cross-Platform: Works on Windows, macOS, and Linux
Installation
pip install treedir-py
Quick Start
import treedir
# Create directory structure from file (additive mode)
treedir.run('structure.txt', 'my_project')
# Strictly enforce structure (removes extra files)
treedir.urun('structure.txt', 'my_project')
# Visualize current directory
print(treedir.vis('my_project'))
# Find a file
path = treedir.find('main.py', 'my_project')
# Preview changes before applying
preview = treedir.sandbox(treedir.run, 'structure.txt', 'my_project')
print(preview)
Supported Input Formats
Tree Format
project/
├── src/
│ ├── main.py
│ └── utils.py
├── tests/
│ └── test_main.py
└── README.md
Dictionary Format
{
"src": {
"main.py": null,
"utils.py": null
},
"tests": {
"test_main.py": null
},
"README.md": null
}
Path Format
src/main.py
src/utils.py
tests/test_main.py
README.md
API Reference
Core Functions
run(structure_file, target="current")
Additive file system execution. Only adds new files/directories, preserves existing ones.
Parameters:
structure_file(str): Path to structure definition filetarget(str): Target directory path or "current" for current directory
Returns: bool - True if successful
urun(structure_file, target="current")
Unconditional run - strictly enforce structure. Keeps common files intact but removes files not in the structure.
Parameters:
structure_file(str): Path to structure definition filetarget(str): Target directory path or "current" for current directory
Returns: bool - True if successful
reset(target="current")
Reset target folder (remove all contents).
Parameters:
target(str): Target directory path or "current" for current directory
Returns: bool - True if successful
vis(target="current")
Visualize directory structure in tree format.
Parameters:
target(str): Target directory path or "current" for current directory
Returns: str - Tree representation of directory structure
find(filename, target="current")
Find file/folder and return absolute path.
Parameters:
filename(str): Name of file/folder to findtarget(str): Target directory path or "current" for current directory
Returns: str or None - Absolute path if found, None otherwise
findr(filename, target="current")
Find file/folder and return relative path.
Parameters:
filename(str): Name of file/folder to findtarget(str): Target directory path or "current" for current directory
Returns: str or None - Relative path if found, None otherwise
sandbox(operation_func, *args, **kwargs)
Visualize how directory will look after changes without actually executing them.
Parameters:
operation_func: Function to simulate (run, urun, etc.)*args: Arguments for the operation function**kwargs: Keyword arguments for the operation function
Returns: str - Preview of resulting directory tree
Advanced Usage
Using Classes Directly
from treedir import TreeDir, TreeParser, TreeVisualizer
# Initialize components
td = TreeDir()
parser = TreeParser()
visualizer = TreeVisualizer()
# Parse structure file
structure = parser.parse_file('my_structure.txt')
# Create structure
td._create_structure(structure, '/path/to/target', mode='additive')
# Compare two directories
comparison = visualizer.compare_structures('dir1', 'dir2')
print(comparison)
Generating Structure Files
from treedir import TreeVisualizer
tv = TreeVisualizer()
# Generate tree format from existing directory
tree_content = tv.generate_structure_file('my_project', 'tree')
# Generate dictionary format
dict_content = tv.generate_structure_file('my_project', 'dict')
# Generate path format
path_content = tv.generate_structure_file('my_project', 'path')
Error Handling
import treedir
try:
result = treedir.run('structure.txt', 'target_folder')
if result:
print("Structure created successfully!")
else:
print("Failed to create structure")
except FileNotFoundError:
print("Structure file not found")
except ValueError as e:
print(f"Invalid structure format: {e}")
Examples
Example 1: Basic Project Setup
Create a project_structure.txt:
my_app/
├── src/
│ ├── __init__.py
│ ├── main.py
│ └── config.py
├── tests/
│ ├── __init__.py
│ └── test_main.py
├── docs/
│ └── README.md
├── requirements.txt
└── setup.py
Run:
import treedir
# Create the structure
treedir.run('project_structure.txt', 'new_project')
# Visualize result
print(treedir.vis('new_project'))
Example 2: Using Sandbox Mode
import treedir
# Preview what will happen
preview = treedir.sandbox(treedir.run, 'structure.txt', 'test_folder')
print("Preview:")
print(preview)
# If satisfied, apply changes
treedir.run('structure.txt', 'test_folder')
Example 3: Finding Files
import treedir
# Find a specific file
main_py_path = treedir.find('main.py', 'my_project')
if main_py_path:
print(f"Found main.py at: {main_py_path}")
# Get relative path
rel_path = treedir.findr('main.py', 'my_project')
print(f"Relative path: {rel_path}")
Safety Features
- Automatic Backups:
urun()andreset()automatically create backups - Sandbox Mode: Preview changes before applying them
- Path Validation: Validates file and directory names
- Error Handling: Comprehensive error handling and reporting
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
Version 1.0.0
- Initial release
- Support for tree, dictionary, and path formats
- Core functionality: run, urun, reset, vis, find, findr, sandbox
- Automatic backup creation
- Cross-platform compatibility
Project details
Release history Release notifications | RSS feed
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 treedir_py-0.1.2.tar.gz.
File metadata
- Download URL: treedir_py-0.1.2.tar.gz
- Upload date:
- Size: 19.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 |
69f08b8cbcfc951df3b9e342f2881a568d8b34d33e0020a89aea189c4cd3f530
|
|
| MD5 |
4912cd73b102bc512f3cff8f017f6c96
|
|
| BLAKE2b-256 |
900dc3f1f3a6a5fd9094fb915af8bbe28fa6b26fae31d4c2fa714ee9859dffaa
|
File details
Details for the file treedir_py-0.1.2-py3-none-any.whl.
File metadata
- Download URL: treedir_py-0.1.2-py3-none-any.whl
- Upload date:
- Size: 16.1 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 |
e9c70a7d113fc3a8ab990a0cc0911fad2b9f9353709e598f353ac8c5a91fcd08
|
|
| MD5 |
55401be530097f8ec92258454b1be6fd
|
|
| BLAKE2b-256 |
d81c747f607a6a7bc2fa071e0d887fe0f8aecb5e16ab1253dc2e0979e24bb1ab
|