-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
205 lines (171 loc) · 6.36 KB
/
main.py
File metadata and controls
205 lines (171 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# !/usr/bin/env python3
import sys
from rich import box
from rich.console import Console
from rich.prompt import Prompt
from rich.table import Table
from rich.traceback import install
from ascii_input import ASCII
from base64_input import Base64
from binary_input import Binary
from decimal_input import Decimal
from hex_input import Hexadecimal
from morse_code import MorseCode
from octal_input import Octal
from rotate_string import RotateString
from results import Results
__author__ = "a/k/a bWlrZXNwb24="
# Make the console object
c = Console()
install()
class Convert:
def __init__(self):
self.version = "0.5.5"
self.last_updated = "06-Apr-2026"
self.input_type_dict = {
1: "ASCII",
2: "Base64",
3: "Binary",
4: "Decimal",
5: "Hexadecimal",
6: "Octal",
7: "Rotate String"
}
def print_error_msg(self, e: str) -> str:
return c.print(f"\n[red] There was an error during processing: {e}\n")
def get_input(self, input_type: str) -> str:
input_string = Prompt.ask(f"""[bright_white]
[+] Enter the data you want to convert from [bold khaki3]{input_type}[bright_white] """)
return input_string
# def get_binary_data_format(self) -> str:
# choice: str = c.input("""[bright_white]
# Is the binary data separated with a space character every 8 bits (y/n)?
# >> """)
# return choice.lower().strip()
def get_cipher_shift_value(self) -> int:
shift_value: int = c.input("""[bright_white]
[+] Enter a numeric value for the shift you want to use >> """)
return shift_value
def no_valid_yn_option(self) -> None:
c.print("""[bold bright_red]
!!! You did not enter a valid choice (either "y" or "n"). Please try again.""")
def main(self) -> None:
menu = {
1: "From ASCII",
2: "From Base64",
3: "From Binary",
4: "From Decimal (Integer)",
5: "From Hexadecimal",
6: "From Octal (Integer)",
7: "Rotate String (Caesar Cipher)",
8: "Exit Program"
}
main_menu = Table(
title=f"[bold dodger_blue1]\nDATA CONVERTER & ENCODER, v.{self.version}",
box=box.HEAVY_HEAD,
show_header=False,
header_style="bold #2070b2",
show_lines=False,
pad_edge=True,
padding=(0,5,0,1),
caption=f"Last Updated: {self.last_updated}",
caption_justify="left",
expand=True)
main_menu.add_row(
"[khaki3][?] What type of encoding/decoding do you want to do?\n")
for key, value in menu.items():
main_menu.add_row(f"[bright_white] [{key}] {value}")
# Add blank line at end of options
main_menu.add_row()
c.print(main_menu)
input_option = c.input("""[bold khaki3]\nENTER CHOICE >> """).lower().strip()
if input_option == "1":
try:
input_type = self.input_type_dict[1]
input_string = self.get_input(input_type=input_type)
ASCII(
results={},
data_type=input_type).print_ascii_output(
input_string=input_string
)
except Exception as e:
self.print_error_msg(e)
return None
elif input_option == "2":
try:
input_type = self.input_type_dict[2]
input_string = self.get_input(input_type=input_type)
Base64(
results={},
data_type=input_type).print_base64_output(
input_string=input_string
)
except Exception as e:
self.print_error_msg(e)
return None
elif input_option == "3":
try:
input_type = self.input_type_dict[3]
input_string = self.get_input(input_type=input_type)
input_string = input_string.strip().replace(" ", "")
Binary(
results={},
data_type=input_type).print_binary_output(
input_string=input_string
)
except Exception as e:
self.print_error_msg(e)
return None
elif input_option == "4":
try:
input_type = self.input_type_dict[4]
input_string = self.get_input(input_type=input_type)
Decimal(
results={},
data_type=input_type).print_decimal_output(
input_string=input_string
)
except Exception as e:
self.print_error_msg(e)
return None
elif input_option == "5":
try:
input_type = self.input_type_dict[5]
input_string = self.get_input(input_type=input_type)
Hexadecimal(
results={},
data_type=input_type).print_hex_output(
input_string=input_string
)
except Exception as e:
self.print_error_msg(e)
return None
elif input_option == "6":
try:
input_type = self.input_type_dict[6]
input_string = self.get_input(input_type=input_type)
Octal(
results={},
data_type=input_type).print_octal_output(
input_string=input_string
)
except Exception as e:
self.print_error_msg(e)
return None
elif input_option == "7":
try:
input_type = self.input_type_dict[7]
input_string = self.get_input(input_type=input_type)
n = self.get_cipher_shift_value()
RotateString(input_string=input_string, n=n).rotate_string()
except Exception as e:
self.print_error_msg(e)
return None
elif input_option == "8":
sys.exit(0)
else:
c.print(f"""[bold bright_red]\n
[+] Invalid choice entered. Please enter a valid option.""")
self.main()
if __name__ == "__main__":
app = Convert().main()