usernook
usernook is a lightweight Python library that simplifies user data and settings management for your applications. It provides a consistent, reliable way to store configuration, user preferences, and application data without worrying about path handling, file operations, or settings persistence.
Features
- Zero Configuration: Automatically uses
.nookin the user's home directory if no path is specified - Simple Settings API: Store and retrieve settings with a clean, intuitive interface
- Nested Settings Support: Organize settings hierarchically with nested keys
- Path Safety: Built-in path validation to prevent common mistakes and security issues
- File Management: Easily search, find, and manage user data files
- Context Manager Support: Clean resource handling with Python's
withstatement - Cross-Platform: Works on Windows, macOS, and Linux
Why usernook?
Many applications need to store user preferences, cache data, or maintain configuration files. UserNook handles all the common challenges:
- Where to store the data (platform-specific user directories)
- How to organize settings (nested JSON with defaults)
- File operation safety (path validation and error handling)
- Directory structure management
Installation
pip install usernook
Quick Start
Basic Settings Usage
from usernook import Nook
# Create a nook with default location (~/.nook)
with Nook() as nook:
# Store a simple setting
nook.set_setting("username", "Alice")
# Retrieve the setting (with optional default)
username = nook.get_setting("username", default_value="Guest")
print(f"Hello, {username}!") # Output: Hello, Alice!
Resulting JSON file (~/.nook/default_settings.json):
{
"username": "Alice"
}
Nested Settings
from usernook import Nook
with Nook() as nook:
# Store nested settings
nook.set_setting(["app", "name"], "CodeEditor")
nook.set_setting(["app", "version"], "1.2.3")
nook.set_setting(["user", "preferences", "theme"], "dark")
# Retrieve nested settings
theme = nook.get_setting(["user", "preferences", "theme"], default_value="light")
print(f"Using {theme} theme") # Output: Using dark theme
Resulting JSON file (~/.nook/default_settings.json):
{
"app": {
"name": "CodeEditor",
"version": "1.2.3"
},
"user": {
"preferences": {
"theme": "dark"
}
}
}
Custom Settings Location
from usernook import Nook
# Store settings in a specific category
with Nook() as nook:
# Save high score in a game-specific file
nook.set_setting("high_score", 10000, path_parts=["games", "space_invaders"])
nook.set_setting("last_played", "2023-06-15", path_parts=["games", "space_invaders"])
# Later, retrieve that specific setting
high_score = nook.get_setting("high_score", default_value=0,
path_parts=["games", "space_invaders"])
print(f"High Score: {high_score}") # Output: High Score: 10000
Resulting JSON file (~/.nook/games/space_invaders.json):
{
"high_score": 10000,
"last_played": "2023-06-15"
}
Application Data Management
from usernook import Nook
import time
# Application startup example
with Nook("D:/MyApp/UserData") as nook:
# Check if this is first run
first_run = nook.get_setting("first_run", default_value=True)
if first_run:
print("First time running the app! Setting up...")
# Initialize default settings
nook.set_settings({
"first_run": False,
"install_date": time.time(),
"settings": {
"notifications": True,
"auto_update": True,
"language": "en"
}
})
else:
# Update last run time
nook.set_setting("last_run", time.time())
# Get current language setting
language = nook.get_setting(["settings", "language"], default_value="en")
print(f"App language: {language}")
Resulting JSON file (D:/MyApp/UserData/default_settings.json):
{
"first_run": false,
"install_date": 1623742800.4567,
"last_run": 1623829200.1234,
"settings": {
"notifications": true,
"auto_update": true,
"language": "en"
}
}
File Search and Management
from usernook import Nook
# Working with files in the nook
with Nook() as nook:
# Search for text in all files
results = nook.grep("important")
for match in results:
print(f"Found 'important' in {match['file']} at line {match['line_num']}")
# Find files by pattern
config_files = nook.find("config")
print(f"Found {len(config_files['files'])} config files")
# List all files in the nook
all_files = nook.get_all_files()
print(f"Total files in nook: {len(all_files)}")
API Reference
Core
Nook(path=None): Main class to manage user data, defaults to ~/.nook
Settings Management
get_setting(key, default_value=None, path_parts=None): Get a setting, with optional defaultset_setting(key, value, path_parts=None): Store a settingget_settings(path_parts=None): Get all settings from a fileset_settings(settings, path_parts=None): Replace all settings in a file
File Operations
ensure(path=None): Ensure a directory existsgrep(pattern): Search for text in filesfind(pattern): Find files and folders by nameget_all_files(),get_all_folders(),get_all_content(): List nook contents
License
Metadata
Release files for usernook 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| usernook-0.2.tar.gz | 10.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| usernook-0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.3 kB
Release files / usernook-0.2.tar.gz
| Download URL | usernook-0.2.tar.gz |
|---|---|
| Size | 10.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
043211da210cad519da471fa4bf52ce44e46fea132f5d7e96d7bb05c4e300c9e
|
|
BLAKE2b-256 checksum How to use checksums |
de627ec985c6b69182782a38f890af57ab3a8038a4150890315bb2a99ce09dc2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Release files / usernook-0.2-py3-none-any.whl
| Download URL | usernook-0.2-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ef364c90de7ee29550ef1cd0fa0ad4ab60fa1696ced327e36d996a0949959054
|
|
BLAKE2b-256 checksum How to use checksums |
f45934d1034653688fab10ff8f314a29ba98ab4fecbf4005533688c9014424e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|