Decorators API¶
StreamFlow uses decorators to implement functionality that surrounds the main download operation.
Network connectivity¶
is_connected¶
Checks whether a network connection can be established.
network_required¶
Waits for a network connection before executing a function.
FFmpeg¶
ffmpeg_required¶
Ensures that FFmpeg is available before executing the decorated operation.
Timing¶
timed¶
Measures the execution time of the decorated function.
Why decorators?¶
Decorators keep cross-cutting concerns outside the main download implementation.
Instead of writing network and FFmpeg checks directly inside download_video(), the checks can be composed around the function:
@ffmpeg_required
@network_required
@timed
def download_video():
...
Class reference¶
Connected¶
src.decorators.connected
¶
Module providing network-related utilities.
This module including:
- is_connected(): Check internet connectivity.
- network_required: Decorator that ensures a network connection before executing a function.
is_connected(host='8.8.8.8', port=53, timeout=3)
¶
Check internet connectivity by attempting to connect to a public DNS server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Host to connect to. Defaults to "8.8.8.8" (Google DNS). |
'8.8.8.8'
|
port
|
int
|
Port to use for the connection. Defaults to 53. |
53
|
timeout
|
int
|
Connection timeout in seconds. Defaults to 3. |
3
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the connection succeeds, False otherwise. |
network_required(func)
¶
Decorator that ensures a network connection before executing a function.
If no network connection is available, the decorated function will wait and retry until a connection is restored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Function to decorate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
callable
|
Wrapped function that checks network connectivity before execution. |
FFmpeg¶
src.decorators.ffmpeg
¶
This module provides a decorator for verifying the presence of FFmpeg.
Functions:
| Name | Description |
|---|---|
ffmpeg_required |
Ensures FFmpeg is installed before executing the decorated function. |
ffmpeg_required(func)
¶
Decorator that checks if FFmpeg is installed before running a function.
If FFmpeg is not found, prints an error message and prevents the function from executing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Function to decorate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
callable
|
Wrapped function that verifies FFmpeg installation. |
Timed¶
src.decorators.timed
¶
This module provides a decorator for measuring the execution time of functions.
Functions:
| Name | Description |
|---|---|
timed |
Decorator that measures and displays the execution time of the decorated function. |
timed(func)
¶
Decorator that measures and displays the execution time of a function.
The execution time is shown in seconds if less than 60 seconds, otherwise in minutes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Function to decorate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
callable
|
Wrapped function that measures execution time. |