Index
provide.foundation.file.operations
¶
File operation detection and analysis.
This module provides intelligent detection and grouping of file system events into logical operations (e.g., atomic saves, batch updates, rename sequences).
Simple API (Recommended)¶
For most use cases, use the simple functional API:
>>> from provide.foundation.file.operations import detect, Event, Operation
>>>
>>> events = [Event(...), Event(...)]
>>> operation = detect(events)
>>> if operation:
... print(f"{operation.type}: {operation.path}")
Advanced API¶
For streaming detection or custom configuration:
>>> from provide.foundation.file.operations import create_detector, DetectorConfig
>>>
>>> config = DetectorConfig(time_window_ms=1000)
>>> detector = create_detector(config)
>>>
>>> for event in event_stream:
... if operation := detector.detect_streaming(event):
... handle_operation(operation)
Classes¶
DetectorConfig
¶
Configuration for operation detection.
Event
¶
FileEvent
¶
FileEventMetadata
¶
Rich metadata for a file event.
FileOperation
¶
Operation
¶
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
Functions¶
create_detector
¶
Create a new operation detector instance.
Use this when you need a persistent detector for streaming detection or want custom configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
DetectorConfig | None
|
Optional detector configuration |
None
|
Returns:
| Type | Description |
|---|---|
OperationDetector
|
New OperationDetector instance |
Examples:
>>> from provide.foundation.file.operations import create_detector, DetectorConfig
>>>
>>> # Custom configuration
>>> config = DetectorConfig(time_window_ms=1000, min_confidence=0.8)
>>> detector = create_detector(config)
>>>
>>> # Use for streaming
>>> for event in events:
... operation = detector.detect_streaming(event)
Source code in provide/foundation/file/operations/detect.py
detect_all
¶
Detect all operations from a list of events.
Explicit function for when you always want a list result, even for single events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
list[FileEvent]
|
List of events to analyze |
required |
config
|
DetectorConfig | None
|
Optional detector configuration |
None
|
Returns:
| Type | Description |
|---|---|
list[FileOperation]
|
List of detected operations (may be empty) |
Examples:
>>> operations = detect_all(events)
>>> for op in operations:
... print(f"{op.operation_type}: {op.primary_path}")
Source code in provide/foundation/file/operations/detect.py
detect_atomic_save
¶
Detect if events represent an atomic save operation.
Source code in provide/foundation/file/operations/utils.py
detect_streaming
¶
detect_streaming(
event: FileEvent,
detector: OperationDetector | None = None,
) -> FileOperation | None
Process a single event in streaming mode.
For real-time detection, use this with a persistent OperationDetector instance. Operations are returned when patterns are detected based on time windows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
FileEvent
|
Single file event to process |
required |
detector
|
OperationDetector | None
|
Optional persistent detector instance (required for stateful detection) |
None
|
Returns:
| Type | Description |
|---|---|
FileOperation | None
|
Completed operation if detected, None otherwise |
Examples:
>>> # Create persistent detector for streaming
>>> from provide.foundation.file.operations import OperationDetector
>>> detector = OperationDetector()
>>>
>>> # Feed events as they arrive
>>> for event in event_stream:
... operation = detect_streaming(event, detector)
... if operation:
... print(f"Operation detected: {operation.operation_type}")
>>>
>>> # Flush at end
>>> remaining = detector.flush()
Note
This is a lower-level API. For most use cases, the batch detect() function
is simpler and sufficient.
Source code in provide/foundation/file/operations/detect.py
extract_original_path
¶
Extract the original filename from a temp file path.
Source code in provide/foundation/file/operations/utils.py
group_related_events
¶
Group events that occur within a time window.
Source code in provide/foundation/file/operations/utils.py
is_temp_file
¶
Check if a path represents a temporary file.