Skip to main content
Glama
CONSTANTS.md7.58 kB
# Constants Module Documentation ## Overview The `docker_mcp/constants.py` module provides centralized string constants to eliminate duplicate values throughout the codebase. This module was created during a comprehensive code cleanup to replace 100+ duplicate string literals across 17 files. ## Design Principles 1. **Single Source of Truth**: All frequently used strings are defined once 2. **Categorized Organization**: Constants grouped by functional area 3. **Clear Naming**: Descriptive names that indicate purpose and scope 4. **Type Safety**: All constants are immutable strings 5. **Import Efficiency**: Easy to import specific constants as needed ## Constant Categories ### SSH Configuration Options ```python SSH_NO_HOST_CHECK = "StrictHostKeyChecking=no" SSH_NO_KNOWN_HOSTS = "UserKnownHostsFile=/dev/null" SSH_ERROR_LOG_LEVEL = "LogLevel=ERROR" ``` **Usage**: Used in SSH command construction across Docker operations **Files**: `core/backup.py`, `core/transfer/base.py`, `services/host.py` ### Docker Labels ```python DOCKER_COMPOSE_PROJECT = "com.docker.compose.project" DOCKER_COMPOSE_SERVICE = "com.docker.compose.service" DOCKER_COMPOSE_CONFIG_FILES = "com.docker.compose.project.config_files" DOCKER_COMPOSE_WORKING_DIR = "com.docker.compose.project.working_dir" ``` **Usage**: Container inspection and stack management operations **Files**: `tools/containers.py`, `tools/stacks.py`, `core/cache_manager.py`, `services/stack.py` ### Common Field Names ```python HOST_ID = "host_id" CONTAINER_ID = "container_id" COMPOSE_PATH = "compose_path" APPDATA_PATH = "appdata_path" COMPOSE_FILE = "compose_file" ``` **Usage**: Response dictionaries and data structures **Files**: All service files, tool files, core modules ### Container Fields ```python TOTAL_CONTAINERS = "total_containers" PORT_MAPPINGS = "port_mappings" BIND_MOUNTS = "bind_mounts" COMPOSE_PROJECT = "compose_project" MEMORY_USAGE = "memory_usage" HEALTH_STATUS = "health_status" ``` **Usage**: Container data processing and statistics **Files**: `tools/containers.py`, `core/cache_manager.py` ### Migration & Transfer Fields ```python TRANSFER_TYPE = "transfer_type" NAMED_VOLUMES = "named_volumes" FILES_TRANSFERRED = "files_transferred" TRANSFER_RATE = "transfer_rate" DATA_ACCESSIBLE = "data_accessible" ``` **Usage**: Migration operations and data transfer tracking **Files**: `core/migration/`, transfer modules ### Error Messages ```python NOT_FOUND_SUFFIX = "' not found" INVALID_ACTION_PREFIX = "Invalid action '" SAFETY_BLOCK_PREFIX = "SAFETY BLOCK: " CONTAINER_PREFIX = "Container '" ``` **Usage**: Consistent error message formatting **Files**: Service and tool modules ### Security Fields (for filtering) ```python SECURITY_FIELDS = [ "password", "passwd", "pwd", "token", "access_token", "api_token", "key", "api_key", "private_key", "secret_key", "secret", "client_secret", "credential", "auth" ] ``` **Usage**: Filtering sensitive data from logs and responses ## Import Patterns ### Selective Imports (Recommended) ```python from ..constants import DOCKER_COMPOSE_PROJECT, HOST_ID, CONTAINER_ID ``` ### Category Imports ```python from ..constants import ( SSH_NO_HOST_CHECK, SSH_NO_KNOWN_HOSTS, SSH_ERROR_LOG_LEVEL ) ``` ### Full Module Import (for multiple constants) ```python from .. import constants # Usage labels.get(constants.DOCKER_COMPOSE_PROJECT) result[constants.HOST_ID] = host_id ``` ## Migration Examples ### Before (Duplicate Strings) ```python # tools/containers.py compose_project = labels.get("com.docker.compose.project", "") # tools/stacks.py project_name = labels.get("com.docker.compose.project") # services/container.py return {"host_id": host_id, "container_id": container_id} # services/host.py return {"host_id": host_id, "compose_path": path} ``` ### After (Centralized Constants) ```python # tools/containers.py from ..constants import DOCKER_COMPOSE_PROJECT compose_project = labels.get(DOCKER_COMPOSE_PROJECT, "") # tools/stacks.py from ..constants import DOCKER_COMPOSE_PROJECT project_name = labels.get(DOCKER_COMPOSE_PROJECT) # services/container.py from ..constants import HOST_ID, CONTAINER_ID return {HOST_ID: host_id, CONTAINER_ID: container_id} # services/host.py from ..constants import HOST_ID, COMPOSE_PATH return {HOST_ID: host_id, COMPOSE_PATH: path} ``` ## Reusable Patterns Created ### 1. **String Constant Elimination Pattern** ```python # Pattern: Replace duplicate string literals with constants # Before: Multiple files with "com.docker.compose.project" # After: Single DOCKER_COMPOSE_PROJECT constant # Implementation steps: # 1. Identify duplicate strings across files # 2. Create descriptive constant names # 3. Add to appropriate category in constants.py # 4. Replace all occurrences with constant imports # 5. Verify imports and functionality ``` ### 2. **Categorical Organization Pattern** ```python # Pattern: Group related constants by functional area # Categories: SSH, Docker Labels, Fields, Errors, Security # Benefits: # - Easy to find related constants # - Clear separation of concerns # - Logical import groupings # - Maintainable structure ``` ### 3. **Defensive Import Pattern** ```python # Pattern: Import only needed constants to minimize namespace pollution from ..constants import DOCKER_COMPOSE_PROJECT, CONTAINER_ID # Avoid: from ..constants import * # Benefits: Clear dependencies, smaller imports, better IDE support ``` ### 4. **Consistent Field Naming Pattern** ```python # Pattern: Use constants for all dictionary keys in responses response = { HOST_ID: host_id, CONTAINER_ID: container_id, "success": True, # Simple boolean, no constant needed "timestamp": datetime.now().isoformat() } # Benefits: Typo prevention, consistent naming, refactoring safety ``` ## Adding New Constants ### Guidelines 1. **Check for existing similar constants** before creating new ones 2. **Use descriptive names** that indicate purpose and scope 3. **Place in appropriate category** or create new category if needed 4. **Document usage** and affected files 5. **Update imports** in all affected modules ### Example Addition ```python # 1. Add to constants.py # New Docker command constant DOCKER_LOGS_COMMAND = "docker logs --tail={lines} {container_id}" # 2. Import in affected files from ..constants import DOCKER_LOGS_COMMAND # 3. Use in code cmd = DOCKER_LOGS_COMMAND.format(lines=100, container_id=container_id) ``` ## Benefits Achieved ### Code Quality - **Eliminated 100+ duplicate strings** across the codebase - **Single source of truth** for all common strings - **Typo prevention** through centralized definitions - **Easier refactoring** when constants need to change ### Maintainability - **Clear organization** by functional categories - **Easy to find** related constants - **Consistent naming** across the entire codebase - **Import efficiency** with selective imports ### Developer Experience - **IDE autocomplete** for all constants - **Clear dependencies** through explicit imports - **Type safety** for string literals - **Documentation** of all constant usage ## Impact Statistics - **17 files** updated with centralized constants - **35+ field references** converted to constants - **15+ Docker label** duplicates eliminated - **100+ total string literals** centralized - **Zero functionality** broken during migration - **Significant improvement** in code maintainability The constants module represents a comprehensive solution to string duplication that can be applied to any Python codebase with similar issues.

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jmagar/docker-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server