python.video_converter_v3/app/class_media_file_stat.py

52 lines
No EOL
1.3 KiB
Python
Executable file

import logging
from typing import TYPE_CHECKING
import yaml
import os
if TYPE_CHECKING:
from app.class_media_file import Media
class Stat:
"""
Handles reading and writing video conversion statistics
"""
def __init__(self) -> None:
"""
Initialize Class with path for statistic file
"""
self.path = "app/cfg/statistic.yaml"
def save_stat(self, obj: "Media") -> None:
"""
Saves the statistic in YAML file
:param obj: Media object
:type obj: Media
"""
daten = self.read_stat()
try:
# Neuen Eintrag hinzufügen
daten["videos"].update(obj.to_dict_stat())
with open(self.path, "w", encoding="utf8") as file:
yaml.dump(daten, file, default_flow_style=False, indent=4, allow_unicode=True)
except Exception as e:
logging.error(f"Save Stat Failure: {e}")
def read_stat(self) -> Dict[str, any]:
"""
Read statistic from YAML file
:return: Dictionary width statistics
:rtype: Dict[str, any]
"""
# Bestehende Daten laden
if os.path.exists(self.path):
with open(self.path, "r", encoding="utf8") as file:
daten = yaml.safe_load(file) or {}
else:
daten = {}
return daten