Implementation Utilities
Relevant source files
The following files were used as context for generating this wiki page:
Implementation Utilities within the flext-plugin repository provide a set of domain-specific tools designed to manage the lifecycle, discovery, and validation of plugins. These utilities leverage Python 3.13+ patterns and follow enterprise standards to ensure type safety, secure execution, and consistent plugin behavior across the FLEXT platform.
The utility system is primarily composed of discovery mechanisms, metadata extraction logic, and rigorous validation rules. It serves as a bridge between raw filesystem assets and the high-level FlextPluginApi, allowing the platform to dynamically identify and integrate new functional modules while maintaining strict quality gates.
Sources: src/flext_plugin/utilities.py:1-20, README.md:10-15
Plugin Discovery and Extraction
The discovery utility scans specified directories for valid plugin files, filtering by supported extensions and extracting metadata to register them within the platform.
Discovery Logic
The discover_plugins method performs a recursive search (rglob) through a directory. It identifies potential plugins by matching file extensions against a whitelist and then proceeds to validate the file structure and extract metadata.
The diagram above illustrates the sequential steps taken to identify and process plugin files from the filesystem.
Sources: src/flext_plugin/utilities.py:64-98
Metadata Extraction
Metadata is extracted differently based on the file type. For Python files, the utility parses the source code to find version dunder constants (e.g., __version__) and module-level docstrings to populate descriptions.
| Field | Source/Logic | Default Value |
|---|---|---|
| Name | File stem (filename without extension) | N/A |
| Version | Extracted via regex __version__ = ["'](https://github.com/flext-sh/flext-plugin/blob/71500dd06dcf5c21d30a260631192aaf9028d1c8/%5B%5E%22'%5D%2B)["'] | 0.1.0-dev |
| Description | Module docstring (triple quotes) | "Plugin from [filename]" |
| Author | Metadata field | "Unknown" |
| Type | Hardcoded for discovered files | "extension" |
Sources: src/flext_plugin/utilities.py:108-149
Security and Validation Utilities
Validation utilities act as security gates to prevent the execution of malicious or malformed code within the FLEXT ecosystem.
File Validation
The validate_plugin_file method enforces size limits and scans for dangerous Python patterns.
- Size Constraint: Plugins exceeding 100MB are rejected to prevent resource exhaustion.
- Static Code Analysis: The utility checks for high-risk functions such as
exec(),eval(),__import__,subprocess, andos.system.
Sources: src/flext_plugin/utilities.py:161-192
Naming Conventions
Plugin names are validated against a strict regex pattern: ^[a-zA-Z][a-zA-Z0-9_-]*$. This ensures compatibility with internal registry systems and filesystem paths.
Sources: src/flext_plugin/utilities.py:204-215
Plugin Lifecycle and Execution Tracking
Utilities also facilitate the recording of operational metrics for plugin entities, which is critical for monitoring and debugging in production environments.
Execution Metrics
Plugin entities track performance and reliability through a standardized metadata structure. This is managed via the record_execution and record_error methods found in domain entities.
This diagram represents the flow of telemetry data from a plugin instance to its internal metadata storage.
Sources: tests/unit/test_domain_entities.py:59-95
Integration Patterns
Implementation utilities support advanced integration scenarios, such as managing plugins within Docker environments or configuring complex connectivity requirements.
Service Connectivity Utilities
The system provides helper functions to check the availability of external dependencies (e.g., PostgreSQL, Redis, LDAP) before plugin activation. This prevents "fail-loud" scenarios during platform startup.
def check_service_availability(host: str, port: int, timeout: float = 5.0) -> bool:
"""Check if a network service is available via socket connection."""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
result = sock.connect_ex((host, port))
sock.close()
return result == 0
except OSError:
return False
Sources: examples/03_docker_integration.py:23-33
Configuration Templates
Utilities define the structure for complex plugin configurations. These often include:
- Database: host, port, user, password, pool size.
- Retry Logic: max retries, delay, exponential backoff.
- Features: toggles for metrics, tracing, and health checks.
Sources: examples/02_plugin_configuration.py:23-45, examples/basic-plugin.md:237-268
Summary of Utility Constants
The following constants govern the behavior of the discovery and validation systems:
| Constant | Value | Description |
|---|---|---|
DEFAULT_WATCH_INTERVAL | 1.0 | Interval for hot-reload file watching. |
MAX_PLUGIN_SIZE_MB | 100 | Maximum allowed file size for a plugin. |
PLUGIN_FILE_EXTENSIONS | .py, .yaml, .yml, .json | Allowed file types for discovery. |
PLUGIN_MANIFEST_FILES | plugin.yaml, setup.py, etc. | Recognized manifest filenames. |
Sources: src/flext_plugin/utilities.py:38-60
Conclusion
The Implementation Utilities of flext-plugin provide a robust framework for handling plugins as first-class citizens. By combining filesystem discovery with proactive security scanning and detailed execution telemetry, these utilities ensure that plugins are not only easy to integrate but also safe and measurable within the broader FLEXT platform.