Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
bytes_to_base.py
Go to the documentation of this file.
1r"""
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: image_to_image.py
14# CREATION DATE: 15-01-2026
15# LAST Modified: 4:12:11 15-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: The file containing the code for converting bytes to a base of the user's choice.
21# // AR
22# +==== END CatFeeder =================+
23"""
24
25import base64
26from typing import Optional, Dict, Callable
27
28from display_tty import Disp, initialise_logger
29
30from . import converters_constants as CONV_CONST
31
32from ..http_constants import DataTypes, CONVERSION_TARGETS
33
34from ...core import FinalClass
35from ...utils import CONST
36
37
38class BytesToBase(metaclass=FinalClass):
39 """Class used to convert images from one format to another using Pillow."""
40
41 disp: Disp = initialise_logger(__qualname__, CONST.DEBUG)
42
43 _instance: Optional["BytesToBase"] = None
44
45 def __new__(cls) -> "BytesToBase":
46 if cls._instance is None:
47 cls._instance = super(BytesToBase, cls).__new__(cls)
48 return cls._instance
49
50 def __init__(self) -> None:
51 self.disp.log_debug("Initialising...")
52 self._cls_reference: Dict[DataTypes, Callable[[bytes], str]] = {
53 DataTypes.BASE16: self.bytes_to_base16,
54 DataTypes.BASE32: self.bytes_to_base32,
55 DataTypes.BASE64: self.bytes_to_base64,
56 DataTypes.BASE85: self.bytes_to_base85,
57 }
58 self.disp.log_debug("Initialised.")
59
60 def __call__(self, data: bytes, source_format: DataTypes) -> CONV_CONST.ConversionResult:
61 return self.convert(data, source_format)
62
63 def _convert(self, data: bytes, from_type: DataTypes, to_type: DataTypes) -> CONV_CONST.ConversionResult:
64 """_summary_
65 Internal method to convert data from one type to another.
66 Args:
67 data (bytes): The data to convert.
68 from_type (str): The original data type.
69 to_type (str): The target data type.
70 Returns:
71 ConversionResult: The converted data in a contained dataclass.
72 """
73 func = self._cls_reference.get(to_type)
74 if from_type == DataTypes.BYTES and func:
75 return CONV_CONST.ConversionResult(
76 data=data,
77 converted=True,
78 from_type=from_type,
79 to_type=to_type,
80 result=func(data)
81 )
82 return CONV_CONST.ConversionResult(
83 data=data,
84 converted=False,
85 from_type=from_type,
86 to_type=to_type,
87 result=None
88 )
89
90 def convert(self, data: bytes, from_type: DataTypes) -> CONV_CONST.ConversionResult:
91 """_summary_
92 Convert data from one type to another.
93 Args:
94 data (bytes): The data to convert.
95 from_type (str): The original data type.
96 to_type (str): The target data type.
97 Returns:
98 bytes: The converted data.
99 """
100 target = CONVERSION_TARGETS.get(from_type)
101 resp = CONV_CONST.ConversionResult(
102 data=data,
103 converted=False,
104 from_type=from_type,
105 to_type=target or from_type,
106 result=None
107 )
108 if target:
109 return self._convert(data, from_type, target)
110 return resp
111
112 @staticmethod
113 def bytes_to_base85(data: bytes) -> str:
114 """_summary_
115 Convert bytes data to a base85 encoded string.
116 Args:
117 data (bytes): The bytes data to convert.
118 Returns:
119 str: The base85 encoded string.
120 """
121 return base64.b85encode(data).decode('utf-8')
122
123 @staticmethod
124 def bytes_to_base64(data: bytes) -> str:
125 """_summary_
126 Convert bytes data to a base64 encoded string.
127 Args:
128 data (bytes): The bytes data to convert.
129 Returns:
130 str: The base64 encoded string.
131 """
132 return base64.b64encode(data).decode('utf-8')
133
134 @staticmethod
135 def bytes_to_base32(data: bytes) -> str:
136 """_summary_
137 Convert bytes data to a base32 encoded string.
138 Args:
139 data (bytes): The bytes data to convert.
140 Returns:
141 str: The base32 encoded string.
142 """
143 return base64.b32encode(data).decode('utf-8')
144
145 @staticmethod
146 def bytes_to_base16(data: bytes) -> str:
147 """_summary_
148 Convert bytes data to a base16 encoded string.
149 Args:
150 data (bytes): The bytes data to convert.
151 Returns:
152 str: The base16 encoded string.
153 """
154 return base64.b16encode(data).decode('utf-8')
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)