Skip to main content

Application foundation utilities: startup configuration, user directory resolution, and single-instance control

Project description

kiarina-utils-app

English | 日本語

PyPI version Python License: MIT

[!NOTE] What is this? Foundation utilities for applications (especially CLI tools): configuring the application identity at startup, resolving user directories, and controlling single-instance execution.

Dependencies

Package Version License
platformdirs >=4.10.0 MIT
filelock >=3.19.1 MIT
Pydantic >=2.11.7 MIT
pydantic-settings >=2.10.1 MIT
pydantic-settings-manager >=3.2.0 MIT

Installation

pip install kiarina-utils-app

Features

  • Configuring the application identity Set the application name and author once at startup, used as the namespace for directories and locks.
  • Resolving user directories Resolve user-specific cache, config, and data directories, honoring XDG_* environment variables and platform defaults.
  • Controlling single instance Prevent duplicate application instances using an OS-level advisory file lock.

Configuring the application identity

Call configure() once when the application starts. The name and author can each be set only once; reconfiguring raises AppAlreadyConfiguredError, and accessing them before configuration raises AppNotConfiguredError (use reset() to clear them in tests).

from kiarina.utils.app import configure

configure(
    app_name="kiapi",
    app_author="kiarina",
)

Resolving user directories

The user_directory service returns user-specific directories as Path objects.

from kiarina.utils.app import user_directory

cache_dir = user_directory.get_user_cache_dir()
config_dir = user_directory.get_user_config_dir()
data_dir = user_directory.get_user_data_dir()

The resolution order is:

  1. An explicit override from settings (~ is expanded to the home directory).
  2. The XDG_* environment variable (XDG_CACHE_HOME / XDG_CONFIG_HOME / XDG_DATA_HOME), joined with the app name, when set. XDG takes priority on all platforms, including macOS.
  3. The platform default for the running user (via platformdirs).

The settings can be overridden with environment variables:

Environment variable Description
KIARINA_UTILS_APP_USER_CACHE_DIR Override the user cache directory.
KIARINA_UTILS_APP_USER_CONFIG_DIR Override the user config directory.
KIARINA_UTILS_APP_USER_DATA_DIR Override the user data directory.

Controlling single instance

The single_instance service prevents duplicate instances using a lock file under the user cache directory. The lock is an OS-level advisory lock, so it is released automatically when the process exits.

from kiarina.utils.app import single_instance
from kiarina.utils.app import AlreadyRunningError

try:
    single_instance.acquire()
except AlreadyRunningError:
    raise SystemExit("Another instance is already running.")

try:
    ...  # run the application
finally:
    single_instance.release()

API Reference

kiarina.utils.app

from kiarina.utils.app import (
    configure,
    reset,
    single_instance,
    user_directory,
    AppSettings,
    settings_manager,
    AlreadyRunningError,
    AppAlreadyConfiguredError,
    AppNotConfiguredError,
)

configure

def configure(app_name: str, app_author: str) -> None: ...

Set the application identity once at startup. The name and author are used as the namespace for user directories and lock files.

Parameters

  • app_name (str): Application name.
  • app_author (str): Application author.

Raises

  • AppAlreadyConfiguredError: The name or author has already been set.

reset

def reset() -> None: ...

Clear the configured application name and author. Intended for use in tests.

single_instance

The single_instance service module prevents duplicate instances using an OS-level advisory lock file placed under the user cache directory.

def acquire(*, timeout: float = 10.0) -> None: ...

def release() -> None: ...
  • acquire attempts to take the lock, waiting up to timeout seconds, and raises AlreadyRunningError if another instance already holds it.
  • release releases the lock if it is currently held.

Parameters

  • timeout (float): Maximum seconds to wait for the lock (default: 10.0).

Raises

  • AlreadyRunningError: Another instance already holds the lock.

user_directory

The user_directory service module resolves user-specific directories as Path objects, honoring settings overrides, XDG_* environment variables, and platform defaults in that order.

def get_user_cache_dir() -> Path: ...

def get_user_config_dir() -> Path: ...

def get_user_data_dir() -> Path: ...

Returns

  • Path: The resolved user cache, config, or data directory.

Raises

  • AppNotConfiguredError: The application name or author has not been configured (when falling back to platform defaults).

AppSettings

class AppSettings(BaseSettings):
    user_cache_dir: str | None = None
    user_config_dir: str | None = None
    user_data_dir: str | None = None

Pydantic settings model for directory overrides. Reads environment variables with the prefix KIARINA_UTILS_APP_.

Fields

  • user_cache_dir (str | None): Override for the user cache directory (default: None).
  • user_config_dir (str | None): Override for the user config directory (default: None).
  • user_data_dir (str | None): Override for the user data directory (default: None).

settings_manager

settings_manager: SettingsManager[AppSettings]

Global settings manager instance for AppSettings, provided by pydantic-settings-manager. Access the active settings via settings_manager.settings.

from kiarina.utils.app import settings_manager

settings_manager.user_config = {
    "user_cache_dir": "~/.cache/kiapi",
}

Exceptions

class AppNotConfiguredError(RuntimeError): ...
class AppAlreadyConfiguredError(RuntimeError): ...
class AlreadyRunningError(RuntimeError): ...
Exception Raised when
AppNotConfiguredError The application name or author is accessed before being configured.
AppAlreadyConfiguredError configure() is called after the name or author has already been set.
AlreadyRunningError single_instance.acquire() fails because another instance holds the lock.

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

kiarina_utils_app-2.3.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

kiarina_utils_app-2.3.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_utils_app-2.3.0.tar.gz.

File metadata

  • Download URL: kiarina_utils_app-2.3.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kiarina_utils_app-2.3.0.tar.gz
Algorithm Hash digest
SHA256 f67ed946bf8b9ec474f3820a1abd74219a33f787bad297c48c7b93ca9831516c
MD5 2148bc104c146f607b40de50f5fab9c2
BLAKE2b-256 cff765bc1ddb03b9b2aa418360aa2ee22cecce49c4a4dba84123ca7974a740ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for kiarina_utils_app-2.3.0.tar.gz:

Publisher: release-pypi.yml on kiarina/kiarina-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kiarina_utils_app-2.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_utils_app-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbf0d8d54b62842d0b5de20c200eee114acba5e393461a2ace9436ea907da014
MD5 51395f229a4947c51d1db52b5ae5954c
BLAKE2b-256 595039dfa6a7e272ddd4b8c443e4076e07d0e3a7b2ffe7cea69c74c4f81fae82

See more details on using hashes here.

Provenance

The following attestation bundles were made for kiarina_utils_app-2.3.0-py3-none-any.whl:

Publisher: release-pypi.yml on kiarina/kiarina-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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