mirror of
https://github.com/Sosokker/HomieCare.git
synced 2025-12-19 18:04:07 +01:00
Add util docstring and url validator
This commit is contained in:
parent
adb6ec6497
commit
a44378a115
@ -1,10 +1,17 @@
|
||||
"""
|
||||
This module contains utility functions that are used in the project.
|
||||
"""
|
||||
|
||||
import re
|
||||
import json
|
||||
|
||||
from typing import Never
|
||||
from pydantic import BaseModel
|
||||
from scheme import Camera
|
||||
|
||||
|
||||
def serialize_value(value) -> any:
|
||||
"""Serialize the given value to a JSON serializable value."""
|
||||
if isinstance(value, list):
|
||||
if all(isinstance(item, BaseModel) for item in value):
|
||||
return [item.dict() for item in value]
|
||||
@ -17,6 +24,7 @@ def serialize_value(value) -> any:
|
||||
|
||||
|
||||
def save_to_config(key, value) -> None:
|
||||
"""Save the given key-value pair to the config file."""
|
||||
try:
|
||||
with open('config.json', 'r') as file:
|
||||
config_data = json.load(file)
|
||||
@ -29,6 +37,7 @@ def save_to_config(key, value) -> None:
|
||||
|
||||
|
||||
def read_cameras_from_config(file_path) -> list[Camera] | list[Never]:
|
||||
"""Read the cameras data from the config file."""
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
config_data = json.load(file)
|
||||
@ -38,4 +47,15 @@ def read_cameras_from_config(file_path) -> list[Camera] | list[Never]:
|
||||
cameras_data = config_data.get('cameras', [])
|
||||
cameras = [Camera(**cam_data) for cam_data in cameras_data]
|
||||
|
||||
return cameras
|
||||
return cameras
|
||||
|
||||
|
||||
def video_url_validation(url: str) -> bool:
|
||||
"""Validate if the given URL is a rtsp/rtmp URL or not."""
|
||||
rtmp_pattern = r'^rtmp://.*$'
|
||||
rtsp_pattern = r'^rtsp://.*$'
|
||||
|
||||
if re.match(rtmp_pattern, url) or re.match(rtsp_pattern, url):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
Loading…
Reference in New Issue
Block a user