Qualitybase is a Python library that provides standardized services and tooling for managing code quality, development workflows, and project maintenance in Python projects.
pip install qualitybaseFor development:
pip install -e .
pip install -e ".[dev,lint,quality,security,test]"Qualitybase provides a unified entry point via the service.py script:
./service.py <service> <command>quality: Quality checks (lint, security, test, complexity, cleanup)dev: Development tools (venv, install, clean, build, etc.)django: Django-specific servicespublish: Package publishing and distributioncli: Command-line interface for packages
# Quality checks
./service.py quality lint
./service.py quality security
./service.py quality all
# Development tools
./service.py dev venv
./service.py dev install-dev
./service.py dev clean
# Help
./service.py quality help
./service.py dev helpQualitybase includes a flexible CLI system that allows packages to define custom commands through a commands directory.
The CLI system automatically discovers commands from:
commands/directory: A directory next tocli.pycontaining command modules.commands.jsonconfiguration file: A JSON file that can specify:packages: List of packages to discover commands fromdirectories: List of directories to scan for commandscommands: Direct command definitions
Commands can be created in two ways:
Create a file in the commands/ directory (e.g., commands/mycommand.py):
from .base import Command
def _mycommand_command(args: list[str]) -> bool:
"""Description of what this command does."""
# Command implementation
print("Hello from mycommand!")
return True
mycommand_command = Command(_mycommand_command, "Description of what this command does")The command will be automatically discovered and available as:
./service.py cli mycommanddef mycommand_command(args: list[str]) -> bool:
"""Description of what this command does."""
# Command implementation
print("Hello from mycommand!")
return True- Functions ending with
_commandare automatically discovered - The command name is derived from the function name (removing
_commandsuffix) - Private functions (starting with
_) have the underscore removed Commandinstances can use any name, but follow similar naming conventions
Commands receive a list of string arguments and return a boolean indicating success:
def mycommand_command(args: list[str]) -> bool:
"""Command description for help text."""
if not args:
print("Usage: mycommand <arg1> <arg2>")
return False
# Process arguments
arg1 = args[0]
# ... command logic ...
return True # SuccessQualitybase provides several built-in commands in qualitybase/commands/:
help: Display available commandsversion: Show package version informationvarenv: Show or manage environment variables
Create src/mypackage/commands/greet.py:
from .base import Command
def greet_command(args: list[str]) -> bool:
"""Greet someone by name."""
if not args:
print("Usage: greet <name>")
return False
name = args[0]
print(f"Hello, {name}!")
return True
greet_command = Command(greet_command, "Greet someone by name")Then use it:
./service.py cli greet Alice
# Output: Hello, Alice!You can configure command discovery using .commands.json:
{
"packages": ["otherpackage"],
"directories": ["custom_commands"],
"commands": []
}The ENVFILE_PATH environment variable allows you to automatically specify the path to a .env file to load when starting services.
Usage:
# Absolute path
ENVFILE_PATH=/path/to/.env ./service.py dev install-dev
# Relative path (relative to project root)
ENVFILE_PATH=.env.local ./service.py quality lintBehavior:
- If the path is relative, it is resolved relative to the project root
- The
.envfile is automatically loaded before command execution - Uses
python-dotenvto parse the file (installed automatically if needed) - Works with
devandcliservices
Example:
# Create a .env.local file
echo "API_KEY=secret123" > .env.local
# Use this file
ENVFILE_PATH=.env.local ./service.py dev install-devThe ENSURE_VIRTUALENV environment variable allows you to automatically activate the .venv virtual environment if it exists, before executing commands.
Usage:
ENSURE_VIRTUALENV=1 ./service.py dev help
ENSURE_VIRTUALENV=1 ./service.py quality lintBehavior:
- Must be set to
1to be active - Automatically activates the
.venvvirtual environment at the project root - Modifies
sys.executable,PATH, andsys.pathto use the venv's Python - Only works if the
.venvdirectory exists - Compatible with Windows and Unix
Note: The ensure_virtualenv() function is also automatically called by the main service, but ENSURE_VIRTUALENV allows you to force activation even in contexts where it might not be automatic.
Example:
# Create a virtual environment
python -m venv .venv
# Use it automatically
ENSURE_VIRTUALENV=1 ./service.py quality allQualitybase uses a service-based architecture:
- Each service domain is organized in its own module directory
- Services are accessed through a unified entry point (
service.py) - Services can be invoked via
./service.py <service> <command>or directly via Python modules - The system ensures virtual environment setup and proper dependency management
- Services are designed to work consistently across different Python projects
See docs/ for project rules and guidelines.