Skip to main content

Static definition of directory and file structure to avoid hard-coding file paths

Project description

PathStruct

A Python library for statically defining and managing file path structures. Define file and directory structures using type annotations, and visualize them as trees or manage paths.

Features

  • 📁 Type-based structure definition: Define file/directory structures using class annotations
  • 🌳 Tree visualization: Visualize defined structures as trees
  • Existence checking: Display file/directory existence with colors
  • 🔗 Path management: Convenient path access through pathlib.Path objects

Why PathStruct?

When working with complex file structures, hardcoding paths throughout your codebase leads to maintenance nightmares. PathStruct solves this by allowing you to define your file structure once, statically, and reuse it everywhere.

Benefits

  • 🛡️ Type Safety: Get autocomplete and type checking for all your paths. No more typos or runtime errors from incorrect path strings.
  • 📝 Single Source of Truth: Define your file structure once in a class definition, and reference it consistently across your entire project.
  • 🔍 Better Developer Experience: IDE autocomplete works perfectly with your path structure, making development faster and less error-prone.
  • 🧹 Cleaner Code: Replace scattered string literals like "data/config/settings.yaml" with structured access like root.data.config.settings_yaml.path.

Example: Before vs After

Before (hardcoded paths):

config_path = os.path.join(base_dir, "config", "settings.yaml")
log_path = os.path.join(base_dir, "logs", "app.log")
# Easy to make mistakes, no autocomplete, hard to refactor

After (with PathStruct):

class Project(Root):
    config: Config
    logs: Dir

root = Project(name=base_dir)
config_path = root.config.settings_yaml.path  # Autocomplete works!
log_path = root.logs.app_log.path  # Type-safe and refactor-friendly

Installation

Development mode installation

pip install -e .

Build distribution package

pip install build
python -m build

Deploy to PyPI

pip install twine
python -m build
twine upload dist/*

Usage

Basic usage example

from pathstruct import Root, Dir, File

# Define structure
class Config(Dir):
    """Configuration directory."""
    config_yaml: File

class Dataset(Dir):
    """Dataset directory."""
    log_json: File
    images: Dir

class Resource(Root):
    """Resource root directory."""
    config: Config
    dataset: Dataset
    readme_txt: File

# Create instance
root = Resource(name="/path/to/resource")

# Access paths
print(root.config.config_yaml.path)
# Output: /path/to/resource/config/config.yaml

# Print tree (with existence check)
root.print_tree(check_exist=True)

# Print tree (without colors)
root.print_tree(check_exist=False)

File extension naming convention

File fields must be named in the filename_extension format:

  • config_yamlconfig.yaml
  • readme_txtreadme.txt
  • log_jsonlog.json
  • config (warning will be issued)

Tree output example

/path/to/resource/
├── config/
│   └── config.yaml
├── dataset/
│   ├── log.json
│   └── images/
└── readme.txt

Existing files/directories are displayed in green, non-existing ones in red.

API Documentation

Classes

Element

Base class for all file/directory elements.

Dir

Class representing a directory.

Root

Class representing a root directory. Inherits from Dir.

File

Class representing a file. Includes extension support.

Key Methods

Element.print_tree(prefix="", is_last=True, check_exist=True)

Print directory/file structure in tree format.

Parameters:

  • prefix: Prefix for tree output (for indentation)
  • is_last: Whether this is the last child node
  • check_exist: Whether to check file/directory existence and display with colors

Element.path

Returns the full path of the element as a pathlib.Path object.

Running Examples

python -m pathstruct.examples.example

Requirements

  • Python 3.8 or higher
  • Uses only standard library (no external dependencies)

License

MIT License

Contributing

Issues and pull requests are welcome!

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

pathstruct-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pathstruct-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pathstruct-0.1.0.tar.gz.

File metadata

  • Download URL: pathstruct-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pathstruct-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eb8ee03aa0e783e4d1316ed4fc37ac512448ee9ec686e44aa34c818fda5edbff
MD5 e2f36c22d9abb79942459f9ce196950e
BLAKE2b-256 81c766dda761a841d0132440bf41f9cc66455b0fb9204e53da9f6694e50c5168

See more details on using hashes here.

File details

Details for the file pathstruct-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pathstruct-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pathstruct-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0177b20f76f4ed7bf977d4f2edab35b0d1763a57017ee5ae772ba7da626e66f
MD5 f0ab9b51cd7a5de84423d5f9bc5cc197
BLAKE2b-256 47509eb94371e6fe887cc5b54a0a7848fab82b10b10e890f37ffee020befcfb4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page