Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
converter.py
Go to the documentation of this file.
1"""
2# +==== BEGIN CatFeeder =================+
3# LOGO:
4# ..............(..../\\
5# ...............)..(.')
6# ..............(../..)
7# ...............\\(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
10# animals/cats
11# /STOP
12# PROJECT: CatFeeder
13# FILE: converter.py
14# CREATION DATE: 14-01-2026
15# LAST Modified: 23:43:25 16-01-2026
16# DESCRIPTION:
17# This is the backend server in charge of making the actual website work.
18# /STOP
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE: This is a python class that aims to convert received data types into other when the type is either not universally supported by browsers or to big to store. These are on demand functions, this means that they don't execute unless the endpoint calls them.
21# // AR
22# +==== END CatFeeder =================+
23"""
24from typing import Optional
25from display_tty import Disp, initialise_logger
26
27
28from .http_constants import DataTypes, MEDIA_TYPES
29from .converters import (
30 CONV_CONST,
31 BytesToBase, ImageToImage,
32 VideoToVideo, AudioToAudio,
33 DocumentToDocument, ArchiveToArchive,
34 FontToFont,
35)
36
37from ..utils import CONST
38from ..core import FinalClass
39
40
41class Converter(metaclass=FinalClass):
42 """_summary_
43 This class contains methods to convert data types into other formats.
44 """
45
46 disp: Disp = initialise_logger(__qualname__, CONST.DEBUG)
47
48 _instance: Optional["Converter"] = None
49
50 def __new__(cls) -> "Converter":
51 if cls._instance is None:
52 cls._instance = super(Converter, cls).__new__(cls)
53 return cls._instance
54
55 def __init__(self) -> None:
56 self.disp.log_debug("Initialising...")
57 self.disp.log_debug("Initialising ImageToImage...")
58 self.iti: ImageToImage = ImageToImage()
59 self.disp.log_debug("Initialising BytesToBase...")
60 self.btb: BytesToBase = BytesToBase()
61 self.disp.log_debug("Initialising VideoToVideo...")
62 self.vtv: VideoToVideo = VideoToVideo()
63 self.disp.log_debug("Initialising AudioToAudio...")
64 self.ata: AudioToAudio = AudioToAudio()
65 self.disp.log_debug("Initialising DocumentToDocument...")
66 self.dtd: DocumentToDocument = DocumentToDocument()
67 self.disp.log_debug("Initialising ArchiveToArchive...")
68 self.artoar: ArchiveToArchive = ArchiveToArchive()
69 self.disp.log_debug("Initialising FontToFont...")
70 self.ftf: FontToFont = FontToFont()
71 self.disp.log_debug("Setting up conversion references...")
72 self.disp.log_debug("Initialised.")
73
74 def __call__(self, data: bytes, from_type: DataTypes) -> CONV_CONST.ConversionResult:
75 return self.convert(data, from_type)
76
77 def _no_conversion_needed(self, data: bytes, from_type: DataTypes) -> CONV_CONST.ConversionResult:
78 """_summary_
79 Handle cases where no conversion is needed.
80 Args:
81 data (bytes): The original data.
82 from_type (DataTypes): The original data type.
83 Returns:
84 CONV_CONST.ConversionResult: The result indicating no conversion was performed.
85 """
86 return CONV_CONST.ConversionResult(
87 data=data,
88 converted=False,
89 from_type=from_type,
90 to_type=from_type,
91 result=data
92 )
93
94 def convert(self, data: bytes, from_type: DataTypes) -> CONV_CONST.ConversionResult:
95 """_summary_
96 Convert data from one type to another.
97 Args:
98 data (bytes): The data to convert.
99 from_type (str): The original data type.
100 to_type (str): The target data type.
101 Returns:
102 bytes: The converted data.
103 """
104 if not MEDIA_TYPES.needs_conversion(from_type):
105 return self._no_conversion_needed(data, from_type)
106 if not MEDIA_TYPES.get_conversion_target(from_type):
107 return self._no_conversion_needed(data, from_type)
108 if MEDIA_TYPES.is_base_type(from_type):
109 return self.btb(data, from_type)
110 if MEDIA_TYPES.is_image(from_type):
111 return self.iti(data, from_type)
112 if MEDIA_TYPES.is_video(from_type):
113 return self.vtv(data, from_type)
114 if MEDIA_TYPES.is_audio(from_type):
115 return self.ata(data, from_type)
116 if MEDIA_TYPES.is_document(from_type):
117 return self.dtd(data, from_type)
118 if MEDIA_TYPES.is_archive(from_type):
119 return self.artoar(data, from_type)
120 if MEDIA_TYPES.is_font(from_type):
121 return self.ftf(data, from_type)
122 if MEDIA_TYPES.is_binary(from_type):
123 return self._no_conversion_needed(data, from_type)
124 return self._no_conversion_needed(data, from_type)
CONV_CONST.ConversionResult convert(self, bytes data, DataTypes from_type)
Definition converter.py:94
CONV_CONST.ConversionResult _no_conversion_needed(self, bytes data, DataTypes from_type)
Definition converter.py:77
CONV_CONST.ConversionResult __call__(self, bytes data, DataTypes from_type)
Definition converter.py:74