Application foundation utilities: startup configuration, user directory resolution, and single-instance control
Project description
kiarina-utils-app
English | 日本語
[!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:
- An explicit override from settings (
~is expanded to the home directory). - 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. - 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: ...
acquireattempts to take the lock, waiting up totimeoutseconds, and raisesAlreadyRunningErrorif another instance already holds it.releasereleases 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f67ed946bf8b9ec474f3820a1abd74219a33f787bad297c48c7b93ca9831516c
|
|
| MD5 |
2148bc104c146f607b40de50f5fab9c2
|
|
| BLAKE2b-256 |
cff765bc1ddb03b9b2aa418360aa2ee22cecce49c4a4dba84123ca7974a740ef
|
Provenance
The following attestation bundles were made for kiarina_utils_app-2.3.0.tar.gz:
Publisher:
release-pypi.yml on kiarina/kiarina-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kiarina_utils_app-2.3.0.tar.gz -
Subject digest:
f67ed946bf8b9ec474f3820a1abd74219a33f787bad297c48c7b93ca9831516c - Sigstore transparency entry: 2028457948
- Sigstore integration time:
-
Permalink:
kiarina/kiarina-python@bc73d15f91432d265fd669656a378c279862e7f9 -
Branch / Tag:
refs/tags/v2.3.0 - Owner: https://github.com/kiarina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-pypi.yml@bc73d15f91432d265fd669656a378c279862e7f9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file kiarina_utils_app-2.3.0-py3-none-any.whl.
File metadata
- Download URL: kiarina_utils_app-2.3.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbf0d8d54b62842d0b5de20c200eee114acba5e393461a2ace9436ea907da014
|
|
| MD5 |
51395f229a4947c51d1db52b5ae5954c
|
|
| BLAKE2b-256 |
595039dfa6a7e272ddd4b8c443e4076e07d0e3a7b2ffe7cea69c74c4f81fae82
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kiarina_utils_app-2.3.0-py3-none-any.whl -
Subject digest:
fbf0d8d54b62842d0b5de20c200eee114acba5e393461a2ace9436ea907da014 - Sigstore transparency entry: 2028458632
- Sigstore integration time:
-
Permalink:
kiarina/kiarina-python@bc73d15f91432d265fd669656a378c279862e7f9 -
Branch / Tag:
refs/tags/v2.3.0 - Owner: https://github.com/kiarina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-pypi.yml@bc73d15f91432d265fd669656a378c279862e7f9 -
Trigger Event:
push
-
Statement type: