Index
provide.foundation.file.operations.detectors
¶
File operation detection system with extensible registry.
This module provides a registry-based system for detecting file operation patterns from file system events. Built-in detectors are automatically registered with priorities, and custom detectors can be added via the registry API.
Architecture
- Protocol-based detector interface (DetectorFunc)
- Priority-ordered execution (0-100, higher = earlier)
- Extensible via register_detector()
- Thread-safe singleton registry
Example - Using built-in detectors
from provide.foundation.file.operations.detectors import OperationDetector detector = OperationDetector() operation = detector.detect_operation(events)
Example - Registering custom detector
from provide.foundation.file.operations.detectors import register_detector def detect_my_pattern(events): ... # Custom detection logic ... return FileOperation(...) if pattern_found else None register_detector( ... name="detect_custom", ... func=detect_my_pattern, ... priority=85, ... description="Detects custom pattern" ... )
Classes¶
AtomicOperationDetector
¶
Detects atomic file operations like safe writes and atomic saves.
Functions¶
detect_atomic_save
¶
Detect atomic save pattern (write to temp file, then rename).
Common pattern: create temp -> write temp -> rename temp to target
Source code in provide/foundation/file/operations/detectors/atomic.py
detect_safe_write
¶
Detect safe write pattern (backup original, write new, cleanup).
Common pattern: create backup -> modify original OR rename original to backup -> create new
Source code in provide/foundation/file/operations/detectors/atomic.py
BatchOperationDetector
¶
Detects batch operations and rename sequences.
Functions¶
detect_backup_create
¶
Detect backup creation pattern.
Source code in provide/foundation/file/operations/detectors/batch.py
detect_batch_update
¶
Detect batch update pattern (multiple related files updated together).
Source code in provide/foundation/file/operations/detectors/batch.py
detect_rename_sequence
¶
Detect rename sequence pattern.
Source code in provide/foundation/file/operations/detectors/batch.py
DetectorFunc
¶
Bases: Protocol
Protocol for file operation detector functions.
A detector function analyzes a list of file events and attempts to identify a specific file operation pattern (atomic save, batch update, etc.).
Returns None if the pattern is not detected, or a FileOperation with confidence score if the pattern matches.
Functions¶
__call__
¶
Detect file operation pattern from events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
list[FileEvent]
|
List of file events to analyze |
required |
Returns:
| Type | Description |
|---|---|
FileOperation | None
|
FileOperation if pattern detected, None otherwise |
Source code in provide/foundation/file/operations/detectors/protocol.py
OperationDetector
¶
OperationDetector(
config: DetectorConfig | None = None,
on_operation_complete: Any = None,
registry: Registry | None = None,
)
Detects and classifies file operations from events.
Initialize with optional configuration and callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
DetectorConfig | None
|
Detector configuration |
None
|
on_operation_complete
|
Any
|
Callback function(operation: FileOperation) called when an operation is detected. Used for streaming mode. |
None
|
registry
|
Registry | None
|
Optional registry for detectors (defaults to global) |
None
|
Source code in provide/foundation/file/operations/detectors/orchestrator.py
Functions¶
add_event
¶
Add event with auto-flush and callback support.
This is the recommended method for streaming detection with automatic temp file hiding and callback-based operation reporting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
FileEvent
|
File event to process |
required |
Behavior
- Hides temp files automatically (no callback until operation completes)
- Schedules auto-flush timer for pending operations
- Calls on_operation_complete(operation) when pattern detected
- Emits non-temp files immediately if no operation pattern found
Source code in provide/foundation/file/operations/detectors/orchestrator.py
detect
¶
Detect all operations from a list of events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
list[FileEvent]
|
List of file events to analyze |
required |
Returns:
| Type | Description |
|---|---|
list[FileOperation]
|
List of detected operations, ordered by start time |
Source code in provide/foundation/file/operations/detectors/orchestrator.py
detect_streaming
¶
Process events in streaming fashion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
FileEvent
|
Single file event |
required |
Returns:
| Type | Description |
|---|---|
FileOperation | None
|
Completed operation if detected, None otherwise |
Source code in provide/foundation/file/operations/detectors/orchestrator.py
flush
¶
Get any pending operations and clear buffer.
Source code in provide/foundation/file/operations/detectors/orchestrator.py
SimpleOperationDetector
¶
Detects simple, direct file operations.
Functions¶
detect_direct_modification
¶
Detect direct file modification (multiple events on same file).
Source code in provide/foundation/file/operations/detectors/simple.py
detect_same_file_delete_create_pattern
¶
detect_same_file_delete_create_pattern(
events: list[FileEvent], window_ms: int = 1000
) -> FileOperation | None
Detect delete followed by create of same file (replace pattern).
Source code in provide/foundation/file/operations/detectors/simple.py
detect_simple_operation
¶
Detect simple single-event operations.
Source code in provide/foundation/file/operations/detectors/simple.py
TempPatternDetector
¶
Detects patterns involving temporary files.
Functions¶
detect_delete_temp_pattern
¶
detect_delete_temp_pattern(
events: list[FileEvent], temp_window_ms: int = 1000
) -> FileOperation | None
Detect pattern: delete original -> create temp -> rename temp.
Source code in provide/foundation/file/operations/detectors/temp.py
detect_temp_create_delete_pattern
¶
detect_temp_create_delete_pattern(
events: list[FileEvent], temp_window_ms: int = 5000
) -> FileOperation | None
Detect pattern: create temp -> delete temp -> create real file.
Note: High complexity is intentional to handle all temp file patterns.
Source code in provide/foundation/file/operations/detectors/temp.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
detect_temp_modify_pattern
¶
detect_temp_modify_pattern(
events: list[FileEvent], temp_window_ms: int = 1000
) -> FileOperation | None
Detect pattern: create temp -> modify temp -> rename to final.
Source code in provide/foundation/file/operations/detectors/temp.py
detect_temp_rename_pattern
¶
detect_temp_rename_pattern(
events: list[FileEvent], temp_window_ms: int = 1000
) -> FileOperation | None
Detect temp file rename pattern: create temp -> rename to final.
Source code in provide/foundation/file/operations/detectors/temp.py
Functions¶
clear_detector_registry
¶
Clear all registered detectors (primarily for testing).
get_all_detectors
¶
Get all registered detectors sorted by priority (highest first).
Returns:
| Type | Description |
|---|---|
list[tuple[str, DetectorFunc, int]]
|
List of tuples: (name, detector_func, priority) |
Source code in provide/foundation/file/operations/detectors/registry.py
get_detector_registry
¶
Get the global detector registry singleton.
Returns:
| Type | Description |
|---|---|
Registry
|
Registry instance for file operation detectors |
Source code in provide/foundation/file/operations/detectors/registry.py
register_detector
¶
Register a file operation detector function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique detector name (e.g., "detect_atomic_save") |
required |
func
|
DetectorFunc
|
Detector function conforming to DetectorFunc protocol |
required |
priority
|
int
|
Execution priority (0-100, higher = earlier execution) |
required |
description
|
str
|
Human-readable description of what pattern is detected |
''
|
Raises:
| Type | Description |
|---|---|
AlreadyExistsError
|
If detector name already registered |
Example
def detect_custom_pattern(events): ... # Custom detection logic ... return FileOperation(...) if pattern_found else None
register_detector( ... name="detect_custom", ... func=detect_custom_pattern, ... priority=75, ... description="Detects custom file operation pattern" ... )