How to use paths

paths provides project-aware path handling, streamlining how you handle paths and ensuring cross-platform compatibility.

Use it to work with paths relative to your project root, regardless of where your code is executed from.

Skpath

Enhanced path object that detects your project root. Cross-platform compatible.

autopath

Decorator for automatic path type conversion. Smack it on all of your functions that work with paths, and no more type mismatches will ever happen again.

There are a lot of other random annoying things you might come across when working with paths. Many of them are packed in here.

Importing

from suitkaise import paths
from suitkaise.paths import Skpath, autopath, AnyPath, PathDetectionError, NotAFileError, CustomRoot, set_custom_root, get_custom_root, clear_custom_root, get_project_root, get_caller_path, get_current_dir, get_cwd, get_module_path, get_id, get_project_paths, get_project_structure, get_formatted_project_tree, is_valid_filename, streamline_path, streamline_path_quick

Skpath

Enhanced path object with automatic project root detection.

All paths use normalized separators (/) for cross-platform consistency.

from suitkaise.paths import Skpath

# create from caller's file path
path = Skpath()

# create from string
path = Skpath("myproject/feature/file.txt")

# create from Path object
from pathlib import Path
path = Path("myproject/feature/file.txt")
path = Skpath(path)

# create from encoded ID
path = Skpath("bXlwcm9qZWN0L2ZlYXR1cmUvZmlsZS50eHQ")

Constructor

Arguments

path: Path to wrap.

Core Properties

ap: Absolute path with normalized separators (/).

rp: Relative path to project root.

id: Reversible base64url encoded ID.

root: Project root as Skpath object.

root_str: Project root as string with normalized separators.

root_path: Project root as pathlib.Path object.

path = Skpath("src/main.py")

path.ap      # "/Users/me/myproject/src/main.py"
path.rp      # "src/main.py"
path.id      # "c3JjL21haW4ucHk"
path.root    # Skpath('/Users/me/myproject')
Properties

Path Joining

Use the / operator to join paths:

root = Skpath()
data_file = root / "data" / "file.txt"
Methods

os.fspath Compatibility

Skpath works with open(), os.path, and other functions that accept paths:

path = Skpath("data/file.txt")

# works directly with open()
with open(path, 'r') as f:
    content = f.read()

# works with os.path functions
import os
os.path.exists(path)

autopath Decorator

Automatically converts path parameters based on type annotations.

from suitkaise.paths import autopath, AnyPath, Skpath

@autopath()
def process(path: AnyPath):
    # path is guaranteed to be Skpath
    return path.id

# works with any input type
process("src/main.py")
process(Path("src/main.py"))
process(Skpath("src/main.py"))

Arguments

use_caller: Use caller's file path for missing parameters.

debug: Print conversion messages.

only: Only convert specific parameters.

Type Annotations

The decorator converts parameters based on their type annotations.

Supported types:

Supported iterables:

@autopath()
def process(
    path: Skpath,
    files: list[Path],
    names: set[str],
):
    ...

use_caller Option

Fill missing path parameters with the caller's file path.

@autopath(use_caller=True)
def log_from(path: Skpath = None):
    print(f"Logging from: {path.rp}")

# called without argument - uses caller's file
log_from()  # prints the file that called log_from()

only Option

Restrict conversion to specific parameters (faster for large lists).

@autopath(only="file_path")
def process(file_path: str, names: list[str], ids: list[str]):
    # only file_path is normalized
    # names and ids are left unchanged
    return file_path

debug Option

Print conversion messages.

@autopath(debug=True)
def process(path: Skpath):
    return path

process("src/main.py")
# @autopath: Converted path: str → Skpath
Project Root
Caller Path Functions
Project Path Functions
Path ID Functions
Path Validation Functions