Skip to content

typerdrive Files modules

Listing files

Use FilesManager.list_items to list files from directories below the configured files directory:

from typerdrive.files.manager import FilesManager

manager = FilesManager()
manager.list_items("documents")
manager.list_items("documents", recursive=True)
manager.list_items("documents", recursive=True, missing_ok=True)
manager.list_items("documents", recursive=True, pattern="*.md")
manager.list_items("documents", recursive=True, pattern="**/*.md")

Listing is nonrecursive by default and returns immediate file names. Recursive listing returns storage-root-relative keys, including files nested below the requested directory. A single optional glob pattern filters files relative to the requested directory. Patterns support *, ?, and bracket expressions within path components; / separates path components, and ** as a path component matches zero or more directories. Thus, *.md.j2 matches immediate files only, while **/*.md.j2 also matches nested files. recursive=False remains nonrecursive regardless of the pattern. Wildcards can match dot-prefixed names. missing_ok=True returns an empty list only when the requested directory is missing. Non-directories, unreadable directories, and explicit paths through symlinks raise FilesError. Listings omit symlink entries and never follow symlinked directories. The configured files root cannot be listed, including through equivalent paths such as . or subdirectory/...

typerdrive.files.attach

Provide a decorator that attaches the typerdrive files to a typer command function.

Attributes

Classes

Functions:

attach_files

attach_files(
    show: bool = False,
) -> Callable[
    [ContextFunction[P, T]], ContextFunction[P, T]
]

Attach the typerdrive files to the decorated typer command function.

Parameters:

Name Type Description Default
show bool

If set, show the files directory after the function runs.

False
Source code in src/typerdrive/files/attach.py
def attach_files(show: bool = False) -> Callable[[ContextFunction[P, T]], ContextFunction[P, T]]:
    """
    Attach the `typerdrive` files to the decorated `typer` command function.

    Parameters:
        show: If set, show the files directory after the function runs.
    """

    def _decorate(func: ContextFunction[P, T]) -> ContextFunction[P, T]:
        manager_param_key: str | None = None
        rewriter = SignatureRewriter(func)
        for key, hint in rewriter.hints.items():
            if hint is FilesManager:
                rewriter.cloak(key, Annotated[FilesManager | None, CloakingDevice])
                manager_param_key = key

        def wrapper(ctx: typer.Context, *args: P.args, **kwargs: P.kwargs) -> T:
            manager: FilesManager = FilesManager()
            to_context(ctx, "files_manager", manager)

            if manager_param_key:
                kwargs_dict = cast(dict[str, Any], kwargs)
                kwargs_dict[manager_param_key] = manager

            ret_val = func(ctx, *args, **kwargs)

            if show:
                show_directory(manager.files_dir, subject="Current files")

            return ret_val

        rewriter.apply(wrapper)
        return wrapper

    return _decorate

get_files_manager

get_files_manager(ctx: Context) -> FilesManager

Retrieve the FilesManager from the TyperdriveContext.

Source code in src/typerdrive/files/attach.py
def get_files_manager(ctx: typer.Context) -> FilesManager:
    """
    Retrieve the `FilesManager` from the `TyperdriveContext`.
    """
    with FilesError.handle_errors("Files is not bound to the context. Use the @attach_files() decorator"):
        mgr: Any = from_context(ctx, "files_manager")
    return FilesError.ensure_type(
        mgr,
        FilesManager,
        "Item in user context at `files_manager` was not a FilesManager",
    )

typerdrive.files.commands

Provide commands that can be added to a typer app to interact with files.

Classes

Functions:

add_files_subcommand

add_files_subcommand(cli: Typer)

Add all files subcommands to the given app.

Source code in src/typerdrive/files/commands.py
def add_files_subcommand(cli: typer.Typer):
    """
    Add all `files` subcommands to the given app.
    """
    files_cli = typer.Typer(help="Manage files for the app", invoke_without_command=True, no_args_is_help=True)

    add_show(files_cli)

    cli.add_typer(files_cli, name="files")

add_show

add_show(cli: Typer)

Add the show command to the given typer app.

Source code in src/typerdrive/files/commands.py
def add_show(cli: typer.Typer):
    """
    Add the `show` command to the given `typer` app.
    """
    cli.command()(show)

show

show(ctx: Context)

Show the current files directory.

Source code in src/typerdrive/files/commands.py
@handle_errors("Failed to show files", handle_exc_class=FilesError)
@attach_files(show=True)
def show(ctx: typer.Context):
    """
    Show the current files directory.
    """
    pass

typerdrive.files.exceptions

Provide exceptions specific to the files feature of typerdrive.

Classes

FilesClearError

Bases: FilesError

Indicate that there was a problem deleting a file.

Source code in src/typerdrive/files/exceptions.py
class FilesClearError(FilesError):
    """
    Indicate that there was a problem deleting a file.
    """

FilesError

Bases: TyperdriveError

The base exception for files errors.

Source code in src/typerdrive/files/exceptions.py
class FilesError(TyperdriveError):
    """
    The base exception for files errors.
    """

    exit_code: ExitCode = ExitCode.GENERAL_ERROR
Attributes
exit_code class-attribute instance-attribute
exit_code: ExitCode = ExitCode.GENERAL_ERROR

FilesInitError

Bases: FilesError

Indicate that there was a problem initializing the files storage.

Source code in src/typerdrive/files/exceptions.py
class FilesInitError(FilesError):
    """
    Indicate that there was a problem initializing the files storage.
    """

FilesLoadError

Bases: FilesError

Indicate that there was a problem loading a file.

Source code in src/typerdrive/files/exceptions.py
class FilesLoadError(FilesError):
    """
    Indicate that there was a problem loading a file.
    """

FilesStoreError

Bases: FilesError

Indicate that there was a problem storing a file.

Source code in src/typerdrive/files/exceptions.py
class FilesStoreError(FilesError):
    """
    Indicate that there was a problem storing a file.
    """

typerdrive.files.manager

Provide a class for managing the typerdrive files feature.

Classes

FilesManager

Manage the typerdrive files feature.

This manager provides a simple key-value file storage system where files are stored in a directory structure. Files can be stored as bytes, text, or JSON and retrieved the same way.

Source code in src/typerdrive/files/manager.py
class FilesManager:
    """
    Manage the `typerdrive` files feature.

    This manager provides a simple key-value file storage system where files are stored
    in a directory structure. Files can be stored as bytes, text, or JSON and retrieved
    the same way.
    """

    files_dir: Path
    """ The directory where files are stored. """

    def __init__(self):
        config: TyperdriveConfig = get_typerdrive_config()

        self.files_dir = config.files_dir

        with FilesInitError.handle_errors("Failed to initialize files storage"):
            self.files_dir.mkdir(parents=True, exist_ok=True)

    @staticmethod
    def matches_glob(path: str, pattern: str) -> bool:
        """
        Checks if a given path matches a supplied glob pattern.

        Note:
            Replace with `glob.translate` once Python 3.12 support is dropped; it is unavailable in Python 3.12.
        """
        path_parts = path.split("/")
        pattern_parts = pattern.split("/")

        @cache
        def matches(path_index: int, pattern_index: int) -> bool:
            if pattern_index == len(pattern_parts):
                return path_index == len(path_parts)
            if pattern_parts[pattern_index] == "**":
                return matches(path_index, pattern_index + 1) or (
                    path_index < len(path_parts) and matches(path_index + 1, pattern_index)
                )
            return (
                path_index < len(path_parts)
                and fnmatchcase(path_parts[path_index], pattern_parts[pattern_index])
                and matches(path_index + 1, pattern_index + 1)
            )

        return matches(0, 0)

    def resolve_path(self, path: Path | str, mkdir: bool = False) -> Path:
        """
        Resolve a given file key path to an absolute path within the files directory.

        If the resolved path is outside the files directory, an exception will be raised.
        If the resolved path is the same as the files directory, an exception will be raised.

        Parameters:
            path:  The file key
            mkdir: If set, create the directory for the file key if it does not exist.
        """
        if isinstance(path, str):
            path = Path(path)
        full_path = self.files_dir / path
        resolved_files_dir = self.files_dir.resolve()
        try:
            relative_path = full_path.relative_to(self.files_dir)
            component_root = self.files_dir
        except ValueError:
            try:
                relative_path = full_path.relative_to(resolved_files_dir)
                component_root = resolved_files_dir
            except ValueError:
                relative_path = None
                component_root = self.files_dir
        if relative_path is not None:
            component_path = component_root
            for part in relative_path.parts:
                if part in ("", "."):
                    continue
                component_path /= part
                FilesError.require_condition(
                    not component_path.is_symlink(),
                    f"File path {str(path)} contains a symlink component at {str(component_path)}",
                )
        full_path = full_path.resolve()
        FilesError.require_condition(
            is_child(full_path, resolved_files_dir),
            f"Resolved file path {str(full_path)} is not within files directory {str(self.files_dir)}",
        )
        FilesError.require_condition(
            full_path != resolved_files_dir,
            f"Resolved file path {str(full_path)} must not be the same as files directory {str(self.files_dir)}",
        )
        if mkdir:
            full_path.parent.mkdir(parents=True, exist_ok=True)
        return full_path

    def list_items(
        self,
        path: Path | str,
        recursive: bool = False,
        missing_ok: bool = False,
        pattern: str | None = None,
    ) -> list[str]:
        """
        List files in a directory below the configured files directory.

        Nonrecursive listing returns immediate file names. Recursive listing returns keys relative to the configured files
        directory, including files nested below the requested directory. Symlink entries are omitted and never traversed.
        The configured files directory itself cannot be listed.

        A glob pattern filters files relative to the requested directory. Patterns support `*`, `?`, and bracket
        expressions within path components, with `/` as the path separator. A `**` path component matches zero or more
        directories, so `*.md.j2` matches only immediate files while `**/*.md.j2` also matches nested files. Wildcards
        can match dot-prefixed names. Only one pattern is supported.

        Parameters:
            path: The directory key to list
            recursive: If set, include files from nested directories
            missing_ok: If set, return an empty list when the requested directory is missing
            pattern: If set, include only files matching this glob pattern

        Raises:
            FilesError: If the requested directory is missing and missing_ok is false, is not a directory, is unreadable,
                is the configured root, or contains a symlink component
        """
        full_path = self.resolve_path(path)
        if not full_path.exists():
            if missing_ok:
                return []
            FilesError.require_condition(
                False,
                f"Resolved file path {str(full_path)} does not exist",
            )
        FilesError.require_condition(
            full_path.is_dir(),
            f"Resolved file path {str(full_path)} is not a directory",
        )
        items: list[str] = []

        def collect(directory: Path):
            with FilesError.handle_errors(f"Failed to list directory {str(directory)}"):
                entries = list(directory.iterdir())
            for entry in entries:
                if entry.is_symlink():
                    continue
                if entry.is_dir():
                    if recursive:
                        collect(entry)
                else:
                    relative_path = entry.relative_to(full_path).as_posix()
                    if pattern is not None and not self.matches_glob(relative_path, pattern):
                        continue
                    if recursive:
                        items.append(entry.relative_to(self.files_dir.resolve()).as_posix())
                    else:
                        items.append(entry.name)

        collect(full_path)
        return items

    def store_bytes(self, data: bytes, path: Path | str, mode: int | None = None):
        """
        Store binary data as a file at the given key.

        Parameters:
            data: The binary data to store
            path: The file key where the data should be stored
            mode: The file mode to use when creating the file
        """
        full_path = self.resolve_path(path, mkdir=True)

        logger.debug(f"Storing file at {full_path}")

        with FilesStoreError.handle_errors(f"Failed to store file at {str(path)}"):
            full_path.write_bytes(data)
        if mode:
            with FilesStoreError.handle_errors(f"Failed to set mode for file at {str(path)} to {mode=}"):
                full_path.chmod(mode)

    def store_text(self, text: str, path: Path | str, mode: int | None = None):
        """
        Store text as a file at the given key.

        Parameters:
            text: The text to store
            path: The file key where the text should be stored
            mode: The file mode to use when creating the file
        """
        self.store_bytes(text.encode("utf-8"), path, mode=mode)

    def store_json(self, data: dict[str, Any], path: Path | str, mode: int | None = None):
        """
        Store a dictionary as a JSON file at the given key.

        The dictionary must be json serializable.

        Parameters:
            data: The dict to store as JSON
            path: The file key where the JSON should be stored
            mode: The file mode to use when creating the file
        """
        self.store_bytes(json.dumps(data, indent=2).encode("utf-8"), path, mode=mode)

    def load_bytes(self, path: Path | str) -> bytes:
        """
        Load binary data from a file at the given key.

        If there is no file at the given key, an exception will be raised.
        If there is an error reading the file, an exception will be raised.

        Parameters:
            path: The file key where the data should be loaded from
        """
        full_path = self.resolve_path(path, mkdir=False)

        logger.debug(f"Loading file from {full_path}")

        FilesLoadError.require_condition(full_path.exists(), f"File at {str(path)} does not exist")
        with FilesLoadError.handle_errors(f"Failed to load file from {str(path)}"):
            return full_path.read_bytes()

    def load_text(self, path: Path | str) -> str:
        """
        Load text from a file at the given key.

        Parameters:
            path: The file key where the text should be loaded from
        """
        return self.load_bytes(path).decode("utf-8")

    def load_json(self, path: Path | str) -> dict[str, Any]:
        """
        Load a dictionary from a JSON file at the given key.

        The file at the given key must be valid JSON.

        Parameters:
            path: The file key where the JSON should be loaded from
        """
        text = self.load_bytes(path).decode("utf-8")
        with FilesLoadError.handle_errors(f"Failed to parse JSON from file at {str(path)}"):
            return json.loads(text)

    def delete(self, path: Path | str) -> Path:
        """
        Delete a file at the given key.

        Parameters:
            path: The file key where the file should be deleted
        """
        full_path = self.resolve_path(path)

        logger.debug(f"Deleting file at {full_path}")

        with FilesClearError.handle_errors(f"Failed to delete file at {str(path)}"):
            full_path.unlink()

        # Clean up empty parent directories
        if len([p for p in full_path.parent.iterdir()]) == 0:
            with FilesClearError.handle_errors(f"Failed to remove empty directory {str(full_path.parent)}"):
                full_path.parent.rmdir()
        return full_path
Attributes
files_dir instance-attribute
files_dir: Path = config.files_dir

The directory where files are stored.

Methods:
__init__
__init__()
Source code in src/typerdrive/files/manager.py
def __init__(self):
    config: TyperdriveConfig = get_typerdrive_config()

    self.files_dir = config.files_dir

    with FilesInitError.handle_errors("Failed to initialize files storage"):
        self.files_dir.mkdir(parents=True, exist_ok=True)
delete
delete(path: Path | str) -> Path

Delete a file at the given key.

Parameters:

Name Type Description Default
path Path | str

The file key where the file should be deleted

required
Source code in src/typerdrive/files/manager.py
def delete(self, path: Path | str) -> Path:
    """
    Delete a file at the given key.

    Parameters:
        path: The file key where the file should be deleted
    """
    full_path = self.resolve_path(path)

    logger.debug(f"Deleting file at {full_path}")

    with FilesClearError.handle_errors(f"Failed to delete file at {str(path)}"):
        full_path.unlink()

    # Clean up empty parent directories
    if len([p for p in full_path.parent.iterdir()]) == 0:
        with FilesClearError.handle_errors(f"Failed to remove empty directory {str(full_path.parent)}"):
            full_path.parent.rmdir()
    return full_path
list_items
list_items(
    path: Path | str,
    recursive: bool = False,
    missing_ok: bool = False,
    pattern: str | None = None,
) -> list[str]

List files in a directory below the configured files directory.

Nonrecursive listing returns immediate file names. Recursive listing returns keys relative to the configured files directory, including files nested below the requested directory. Symlink entries are omitted and never traversed. The configured files directory itself cannot be listed.

A glob pattern filters files relative to the requested directory. Patterns support *, ?, and bracket expressions within path components, with / as the path separator. A ** path component matches zero or more directories, so *.md.j2 matches only immediate files while **/*.md.j2 also matches nested files. Wildcards can match dot-prefixed names. Only one pattern is supported.

Parameters:

Name Type Description Default
path Path | str

The directory key to list

required
recursive bool

If set, include files from nested directories

False
missing_ok bool

If set, return an empty list when the requested directory is missing

False
pattern str | None

If set, include only files matching this glob pattern

None

Raises:

Type Description
FilesError

If the requested directory is missing and missing_ok is false, is not a directory, is unreadable, is the configured root, or contains a symlink component

Source code in src/typerdrive/files/manager.py
def list_items(
    self,
    path: Path | str,
    recursive: bool = False,
    missing_ok: bool = False,
    pattern: str | None = None,
) -> list[str]:
    """
    List files in a directory below the configured files directory.

    Nonrecursive listing returns immediate file names. Recursive listing returns keys relative to the configured files
    directory, including files nested below the requested directory. Symlink entries are omitted and never traversed.
    The configured files directory itself cannot be listed.

    A glob pattern filters files relative to the requested directory. Patterns support `*`, `?`, and bracket
    expressions within path components, with `/` as the path separator. A `**` path component matches zero or more
    directories, so `*.md.j2` matches only immediate files while `**/*.md.j2` also matches nested files. Wildcards
    can match dot-prefixed names. Only one pattern is supported.

    Parameters:
        path: The directory key to list
        recursive: If set, include files from nested directories
        missing_ok: If set, return an empty list when the requested directory is missing
        pattern: If set, include only files matching this glob pattern

    Raises:
        FilesError: If the requested directory is missing and missing_ok is false, is not a directory, is unreadable,
            is the configured root, or contains a symlink component
    """
    full_path = self.resolve_path(path)
    if not full_path.exists():
        if missing_ok:
            return []
        FilesError.require_condition(
            False,
            f"Resolved file path {str(full_path)} does not exist",
        )
    FilesError.require_condition(
        full_path.is_dir(),
        f"Resolved file path {str(full_path)} is not a directory",
    )
    items: list[str] = []

    def collect(directory: Path):
        with FilesError.handle_errors(f"Failed to list directory {str(directory)}"):
            entries = list(directory.iterdir())
        for entry in entries:
            if entry.is_symlink():
                continue
            if entry.is_dir():
                if recursive:
                    collect(entry)
            else:
                relative_path = entry.relative_to(full_path).as_posix()
                if pattern is not None and not self.matches_glob(relative_path, pattern):
                    continue
                if recursive:
                    items.append(entry.relative_to(self.files_dir.resolve()).as_posix())
                else:
                    items.append(entry.name)

    collect(full_path)
    return items
load_bytes
load_bytes(path: Path | str) -> bytes

Load binary data from a file at the given key.

If there is no file at the given key, an exception will be raised. If there is an error reading the file, an exception will be raised.

Parameters:

Name Type Description Default
path Path | str

The file key where the data should be loaded from

required
Source code in src/typerdrive/files/manager.py
def load_bytes(self, path: Path | str) -> bytes:
    """
    Load binary data from a file at the given key.

    If there is no file at the given key, an exception will be raised.
    If there is an error reading the file, an exception will be raised.

    Parameters:
        path: The file key where the data should be loaded from
    """
    full_path = self.resolve_path(path, mkdir=False)

    logger.debug(f"Loading file from {full_path}")

    FilesLoadError.require_condition(full_path.exists(), f"File at {str(path)} does not exist")
    with FilesLoadError.handle_errors(f"Failed to load file from {str(path)}"):
        return full_path.read_bytes()
load_json
load_json(path: Path | str) -> dict[str, Any]

Load a dictionary from a JSON file at the given key.

The file at the given key must be valid JSON.

Parameters:

Name Type Description Default
path Path | str

The file key where the JSON should be loaded from

required
Source code in src/typerdrive/files/manager.py
def load_json(self, path: Path | str) -> dict[str, Any]:
    """
    Load a dictionary from a JSON file at the given key.

    The file at the given key must be valid JSON.

    Parameters:
        path: The file key where the JSON should be loaded from
    """
    text = self.load_bytes(path).decode("utf-8")
    with FilesLoadError.handle_errors(f"Failed to parse JSON from file at {str(path)}"):
        return json.loads(text)
load_text
load_text(path: Path | str) -> str

Load text from a file at the given key.

Parameters:

Name Type Description Default
path Path | str

The file key where the text should be loaded from

required
Source code in src/typerdrive/files/manager.py
def load_text(self, path: Path | str) -> str:
    """
    Load text from a file at the given key.

    Parameters:
        path: The file key where the text should be loaded from
    """
    return self.load_bytes(path).decode("utf-8")
matches_glob staticmethod
matches_glob(path: str, pattern: str) -> bool

Checks if a given path matches a supplied glob pattern.

Note

Replace with glob.translate once Python 3.12 support is dropped; it is unavailable in Python 3.12.

Source code in src/typerdrive/files/manager.py
@staticmethod
def matches_glob(path: str, pattern: str) -> bool:
    """
    Checks if a given path matches a supplied glob pattern.

    Note:
        Replace with `glob.translate` once Python 3.12 support is dropped; it is unavailable in Python 3.12.
    """
    path_parts = path.split("/")
    pattern_parts = pattern.split("/")

    @cache
    def matches(path_index: int, pattern_index: int) -> bool:
        if pattern_index == len(pattern_parts):
            return path_index == len(path_parts)
        if pattern_parts[pattern_index] == "**":
            return matches(path_index, pattern_index + 1) or (
                path_index < len(path_parts) and matches(path_index + 1, pattern_index)
            )
        return (
            path_index < len(path_parts)
            and fnmatchcase(path_parts[path_index], pattern_parts[pattern_index])
            and matches(path_index + 1, pattern_index + 1)
        )

    return matches(0, 0)
resolve_path
resolve_path(path: Path | str, mkdir: bool = False) -> Path

Resolve a given file key path to an absolute path within the files directory.

If the resolved path is outside the files directory, an exception will be raised. If the resolved path is the same as the files directory, an exception will be raised.

Parameters:

Name Type Description Default
path Path | str

The file key

required
mkdir bool

If set, create the directory for the file key if it does not exist.

False
Source code in src/typerdrive/files/manager.py
def resolve_path(self, path: Path | str, mkdir: bool = False) -> Path:
    """
    Resolve a given file key path to an absolute path within the files directory.

    If the resolved path is outside the files directory, an exception will be raised.
    If the resolved path is the same as the files directory, an exception will be raised.

    Parameters:
        path:  The file key
        mkdir: If set, create the directory for the file key if it does not exist.
    """
    if isinstance(path, str):
        path = Path(path)
    full_path = self.files_dir / path
    resolved_files_dir = self.files_dir.resolve()
    try:
        relative_path = full_path.relative_to(self.files_dir)
        component_root = self.files_dir
    except ValueError:
        try:
            relative_path = full_path.relative_to(resolved_files_dir)
            component_root = resolved_files_dir
        except ValueError:
            relative_path = None
            component_root = self.files_dir
    if relative_path is not None:
        component_path = component_root
        for part in relative_path.parts:
            if part in ("", "."):
                continue
            component_path /= part
            FilesError.require_condition(
                not component_path.is_symlink(),
                f"File path {str(path)} contains a symlink component at {str(component_path)}",
            )
    full_path = full_path.resolve()
    FilesError.require_condition(
        is_child(full_path, resolved_files_dir),
        f"Resolved file path {str(full_path)} is not within files directory {str(self.files_dir)}",
    )
    FilesError.require_condition(
        full_path != resolved_files_dir,
        f"Resolved file path {str(full_path)} must not be the same as files directory {str(self.files_dir)}",
    )
    if mkdir:
        full_path.parent.mkdir(parents=True, exist_ok=True)
    return full_path
store_bytes
store_bytes(
    data: bytes, path: Path | str, mode: int | None = None
)

Store binary data as a file at the given key.

Parameters:

Name Type Description Default
data bytes

The binary data to store

required
path Path | str

The file key where the data should be stored

required
mode int | None

The file mode to use when creating the file

None
Source code in src/typerdrive/files/manager.py
def store_bytes(self, data: bytes, path: Path | str, mode: int | None = None):
    """
    Store binary data as a file at the given key.

    Parameters:
        data: The binary data to store
        path: The file key where the data should be stored
        mode: The file mode to use when creating the file
    """
    full_path = self.resolve_path(path, mkdir=True)

    logger.debug(f"Storing file at {full_path}")

    with FilesStoreError.handle_errors(f"Failed to store file at {str(path)}"):
        full_path.write_bytes(data)
    if mode:
        with FilesStoreError.handle_errors(f"Failed to set mode for file at {str(path)} to {mode=}"):
            full_path.chmod(mode)
store_json
store_json(
    data: dict[str, Any],
    path: Path | str,
    mode: int | None = None,
)

Store a dictionary as a JSON file at the given key.

The dictionary must be json serializable.

Parameters:

Name Type Description Default
data dict[str, Any]

The dict to store as JSON

required
path Path | str

The file key where the JSON should be stored

required
mode int | None

The file mode to use when creating the file

None
Source code in src/typerdrive/files/manager.py
def store_json(self, data: dict[str, Any], path: Path | str, mode: int | None = None):
    """
    Store a dictionary as a JSON file at the given key.

    The dictionary must be json serializable.

    Parameters:
        data: The dict to store as JSON
        path: The file key where the JSON should be stored
        mode: The file mode to use when creating the file
    """
    self.store_bytes(json.dumps(data, indent=2).encode("utf-8"), path, mode=mode)
store_text
store_text(
    text: str, path: Path | str, mode: int | None = None
)

Store text as a file at the given key.

Parameters:

Name Type Description Default
text str

The text to store

required
path Path | str

The file key where the text should be stored

required
mode int | None

The file mode to use when creating the file

None
Source code in src/typerdrive/files/manager.py
def store_text(self, text: str, path: Path | str, mode: int | None = None):
    """
    Store text as a file at the given key.

    Parameters:
        text: The text to store
        path: The file key where the text should be stored
        mode: The file mode to use when creating the file
    """
    self.store_bytes(text.encode("utf-8"), path, mode=mode)

Functions: