Storage

Filesystem storage with mount system.

LocalStorage - Filesystem-only storage with genro-storage compatible API.

This module provides a minimal storage implementation that uses the same API as genro-storage, but only supports local filesystem. When genro-storage becomes available, simply change the import:

# Before (local only) from genro_asgi.storage import LocalStorage

# After (full genro-storage) from genro_storage import StorageManager as LocalStorage

class genro_asgi.storage.LocalStorage(base_dir=None)[source]

Bases: object

Storage manager filesystem-only. API compatible with genro_storage.StorageManager.

Mount resolution order (see _resolve_mount): 1. Method mount_{prefix}() → dynamic, overridable via subclass 2. Dict _mounts → configured from config.yaml 3. ValueError if not found

__init__(base_dir=None)[source]

Create storage manager without configured mounts.

Parameters:

base_dir (str | Path | None) – Base directory for resolving relative paths. Defaults to cwd.

add_mount(config)[source]

Add a single mount point.

Parameters:

config (dict[str, Any]) – {‘name’: str, ‘type’: ‘local’, ‘path’: str}

Raises:
Return type:

None

configure(source)[source]

Configure mount points from list of dicts.

Parameters:

source (str | list[dict[str, Any]]) – List of mount configurations

Return type:

None

Format:

[{‘name’: ‘site’, ‘type’: ‘local’, ‘path’: ‘/path/to/dir’}]

Note

Only type=’local’ is supported. Other types raise ValueError.

delete_mount(name)[source]

Remove a mount point.

Return type:

None

get_mount_names()[source]

List configured mount names.

Return type:

list[str]

has_mount(name)[source]

True if mount exists (predefined method or configured).

Return type:

bool

mount_site()[source]

Predefined mount: server base directory.

Return type:

Path

node(mount_or_path=None, *path_parts)[source]

Create a storage node.

Parameters:
  • mount_or_path (str | None) – “mount:path” or just “mount”

  • *path_parts (str) – Additional path parts

Return type:

LocalStorageNode

Returns:

LocalStorageNode for the specified path

Examples

storage.node(‘site:resources/logo.png’) storage.node(‘site’, ‘resources’, ‘logo.png’) storage.node(‘site:resources’, ‘images’, ‘logo.png’)

Raises:

ValueError – if mount doesn’t exist

class genro_asgi.storage.LocalStorageNode(storage, mount, path)[source]

Bases: object

Storage node for local filesystem. API compatible with genro_storage.StorageNode.

property basename: str

Filename with extension.

child(*parts)[source]

Return a child node.

Return type:

LocalStorageNode

children()[source]

List children if it’s a directory.

Return type:

list[LocalStorageNode]

property exists: bool

True if file/directory exists.

property ext: str

Extension without dot.

property fullpath: str

path” complete.

Type:

Return “mount

property isdir: bool

True if it’s a directory.

property isfile: bool

True if it’s a file.

property mimetype: str

MIME type based on extension.

property parent: LocalStorageNode

Return parent directory node.

property path: str

Return path without mount.

read(mode='r', encoding='utf-8')[source]

Read content. mode=’r’ for text, mode=’rb’ for binary.

Return type:

str | bytes

read_bytes()[source]

Read content as bytes.

Return type:

bytes

read_text(encoding='utf-8')[source]

Read content as text.

Return type:

str

property size: int

Size in bytes. 0 if doesn’t exist.

property suffix: str

Extension with dot.

write(data, mode='w', encoding='utf-8')[source]

Write content. mode=’w’ for text, mode=’wb’ for binary.

Return type:

bool

write_bytes(data)[source]

Write bytes. Returns True if written.

Return type:

bool

write_text(text, encoding='utf-8')[source]

Write text. Returns True if written.

Return type:

bool

class genro_asgi.storage.StorageNode(*args, **kwargs)[source]

Bases: Protocol

Abstract interface for storage nodes.

Any storage backend (local, S3, HTTP) must implement this protocol. LocalStorageNode is the filesystem implementation.

property basename: str

Filename with extension.

child(*parts)[source]

Return a child node.

Return type:

StorageNode

children()[source]

List children if it’s a directory.

Return type:

list[StorageNode]

property exists: bool

True if file/directory exists.

property fullpath: str

path” complete.

Type:

Return “mount

property isdir: bool

True if it’s a directory.

property isfile: bool

True if it’s a file.

property mimetype: str

MIME type based on extension.

property path: str

Return path without mount.

read_bytes()[source]

Read content as bytes.

Return type:

bytes

read_text(encoding='utf-8')[source]

Read content as text.

Return type:

str