2# +==== BEGIN CatFeeder =================+
5# ...............)..(.')
7# ...............\(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
13# FILE: image_to_image.py
14# CREATION DATE: 15-01-2026
15# LAST Modified: 4:12:11 15-01-2026
17# This is the backend server in charge of making the actual website work.
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE: The file containing the code for converting bytes to a base of the user's choice.
22# +==== END CatFeeder =================+
26from typing
import Optional, Dict, Callable
28from display_tty
import Disp, initialise_logger
30from .
import converters_constants
as CONV_CONST
32from ..http_constants
import DataTypes, CONVERSION_TARGETS
34from ...core
import FinalClass
35from ...utils
import CONST
39 """Class used to convert images from one format to another using Pillow."""
41 disp: Disp = initialise_logger(__qualname__, CONST.DEBUG)
43 _instance: Optional[
"BytesToBase"] =
None
51 self.
disp.log_debug(
"Initialising...")
58 self.
disp.log_debug(
"Initialised.")
60 def __call__(self, data: bytes, source_format: DataTypes) -> CONV_CONST.ConversionResult:
61 return self.
convert(data, source_format)
63 def _convert(self, data: bytes, from_type: DataTypes, to_type: DataTypes) -> CONV_CONST.ConversionResult:
65 Internal method to convert data from one type to another.
67 data (bytes): The data to convert.
68 from_type (str): The original data type.
69 to_type (str): The target data type.
71 ConversionResult: The converted data in a contained dataclass.
74 if from_type == DataTypes.BYTES
and func:
75 return CONV_CONST.ConversionResult(
82 return CONV_CONST.ConversionResult(
90 def convert(self, data: bytes, from_type: DataTypes) -> CONV_CONST.ConversionResult:
92 Convert data from one type to another.
94 data (bytes): The data to convert.
95 from_type (str): The original data type.
96 to_type (str): The target data type.
98 bytes: The converted data.
100 target = CONVERSION_TARGETS.get(from_type)
101 resp = CONV_CONST.ConversionResult(
105 to_type=target
or from_type,
109 return self.
_convert(data, from_type, target)
115 Convert bytes data to a base85 encoded string.
117 data (bytes): The bytes data to convert.
119 str: The base85 encoded string.
121 return base64.b85encode(data).decode(
'utf-8')
126 Convert bytes data to a base64 encoded string.
128 data (bytes): The bytes data to convert.
130 str: The base64 encoded string.
132 return base64.b64encode(data).decode(
'utf-8')
137 Convert bytes data to a base32 encoded string.
139 data (bytes): The bytes data to convert.
141 str: The base32 encoded string.
143 return base64.b32encode(data).decode(
'utf-8')
148 Convert bytes data to a base16 encoded string.
150 data (bytes): The bytes data to convert.
152 str: The base16 encoded string.
154 return base64.b16encode(data).decode(
'utf-8')
str bytes_to_base16(bytes data)
"BytesToBase" __new__(cls)
CONV_CONST.ConversionResult convert(self, bytes data, DataTypes from_type)
CONV_CONST.ConversionResult _convert(self, bytes data, DataTypes from_type, DataTypes to_type)
CONV_CONST.ConversionResult __call__(self, bytes data, DataTypes source_format)
str bytes_to_base32(bytes data)
str bytes_to_base64(bytes data)
str bytes_to_base85(bytes data)