Detect
provide.foundation.file.operations.detect
¶
Simple functional API for file operation detection.
This module provides a minimal, user-friendly API for detecting file operations from filesystem events. It hides the complexity of the underlying detector system while providing all necessary functionality.
Examples:
>>> from provide.foundation.file.operations import detect, Event
>>>
>>> # Single operation detection
>>> events = [Event(...), Event(...)]
>>> operation = detect(events)
>>> if operation:
... print(f"{operation.type}: {operation.path}")
>>>
>>> # Multiple operations detection
>>> operations = detect_all(events)
>>> for op in operations:
... print(f"{op.type}: {op.path}")
Classes¶
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
¶
detect(
events: FileEvent | list[FileEvent],
config: DetectorConfig | None = None,
) -> FileOperation | list[FileOperation] | None
Detect file operations from event(s).
This is the primary API for operation detection. It automatically determines whether to return a single operation or a list based on the input type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
FileEvent | list[FileEvent]
|
Single event or list of events to analyze |
required |
config
|
DetectorConfig | None
|
Optional detector configuration (uses defaults if not provided) |
None
|
Returns:
| Type | Description |
|---|---|
FileOperation | list[FileOperation] | None
|
|
FileOperation | list[FileOperation] | None
|
|
Examples:
>>> # Single event
>>> operation = detect(event)
>>> if operation:
... print(f"Found: {operation.operation_type}")
>>>
>>> # Multiple events
>>> operations = detect(event_list)
>>> print(f"Found {len(operations)} operations")
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_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.