-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbc.py
165 lines (122 loc) · 5.96 KB
/
tbc.py
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
import PySimpleGUI as sg
import binascii
import pyperclip
import webbrowser
from json import (load as jsonload, dump as jsondump)
from os import path
binary_base = [1, 2, 4, 8, 16, 32, 64, 128]
def cypher(message):
cypher_words = []
for letter in message:
cypher_letter = format(ord(letter), 'b')
cypher_words.append(cypher_letter)
return ' '.join(cypher_words)
def decipher(message):
words = message.split(' ')
decipher_message = []
for word in words:
word = str(word)
sumatory = 0
for value, letter in enumerate(word[::-1]):
if int(letter) == 1:
sumatory += binary_base[value]
decipher_letter = chr(sumatory)
decipher_message.append(decipher_letter)
return "".join(decipher_message)
SETTINGS_FILE = path.join(path.dirname(__file__), r'settings_file.cfg')
DEFAULT_SETTINGS = {'theme': sg.theme()}
SETTINGS_KEYS_TO_ELEMENT_KEYS = {'theme': '-THEME-'}
def load_settings(settings_file, default_settings):
try:
with open(settings_file, 'r') as f:
settings = jsonload(f)
except Exception as e:
sg.popup_quick_message(f'exception {e}', 'No settings file found... will create one for you', keep_on_top=True, background_color='red', text_color='white')
settings = default_settings
save_settings(settings_file, settings, None)
return settings
def save_settings(settings_file, settings, values):
if values:
for key in SETTINGS_KEYS_TO_ELEMENT_KEYS:
try:
settings[key] = values[SETTINGS_KEYS_TO_ELEMENT_KEYS[key]]
except Exception as e:
print(f'Problem updating settings from window values. Key = {key}')
with open(settings_file, 'w') as f:
jsondump(settings, f)
sg.popup('Settings saved')
def create_settings_window(settings):
sg.theme(settings['theme'])
def TextLabel(text): return sg.Text(text+':', justification='r', size=(15,1))
layout = [ [sg.Text('Settings', font='Any 15')],
[TextLabel('Theme'),sg.Combo(sg.theme_list(), size=(20, 20), key='-THEME-')],
[sg.Button('Save'), sg.Button('Exit')] ]
window = sg.Window('Settings', layout, keep_on_top=True, finalize=True)
for key in SETTINGS_KEYS_TO_ELEMENT_KEYS:
try:
window[SETTINGS_KEYS_TO_ELEMENT_KEYS[key]].update(value=settings[key])
except Exception as e:
print(f'Problem updating PySimpleGUI window from settings. Key = {key}')
return window
def create_main_window(settings):
sg.theme(settings['theme'])
menu_def = [['&Menu', ['Refresh', '&Copy', '&Paste', 'Settings', 'E&xit']],
['&Help', '&About...']]
right_click_menu = ['Unused', ['&Copy', '&Paste','Settings', 'E&xit']]
layout = [[sg.Menu(menu_def)],
[sg.Text('Text to Binary Converter', size=(50,1), font=('Helvetica', 11)), sg.Button('', key='paypal', size=(12,1), font=('Helvetica', 9), button_color=(sg.theme_background_color(), sg.theme_background_color()),
image_filename='paypal.png', image_size=(80, 50), image_subsample=2, border_width=0),
sg.Button('', key='bitcoin', size=(12,1), font=('Helvetica', 9), button_color=(sg.theme_background_color(), sg.theme_background_color()),
image_filename='bitcoin.png', image_size=(80, 60), image_subsample=2, border_width=0)],
[sg.Output(size=(76, 14), key='out', font=('Helvetica', 11))],
[sg.Text('Enter your text or binary here', size=[40, 1], font=('Helvetica', 9))],
[sg.Multiline(size=(76,6), key='-key-', font=('Helvetica', 11))],
[sg.Button('To binary', font=('Helvetica', 9), size=(8, 1)),
sg.Button('To text', font=('Helvetica', 9), size=(8, 1)),
sg.Button('Refresh', font=('Helvetica', 9), size=(8, 1))]]
return sg.Window('TBC',
layout=layout,
right_click_menu=right_click_menu)
def main():
window, settings = None, load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
while True:
if window is None:
window = create_main_window(settings)
event, value = window.Read()
message = value['-key-'].rstrip()
if event in (None, 'Exit'):
break
elif event == 'To binary':
cypher_message = cypher(message)
print(cypher_message+"\n")
elif event == 'To text':
try:
decipher_message = decipher(message)
print(decipher_message+"\n")
except ValueError:
continue
elif event == 'Refresh':
text = ''
window.Element('out').Update(str(text))
window.Element('-key-').Update(str(text))
elif event == 'Copy':
cypher_message = cypher(message)
pyperclip.copy(str(cypher_message))
pyperclip.paste()
elif event == 'Paste':
text = pyperclip.paste()
window.Element('-key-').Update(str(text))
elif event == 'Settings':
event, values = create_settings_window(settings).read(close=True)
if event == 'Save':
window.close()
window = None
save_settings(SETTINGS_FILE, settings, values)
elif event == 'About...':
sg.popup('About:', 'Created by A. Petek', 'TBC', 'Version 1.1',)
elif event == 'paypal':
webbrowser.open_new_tab("https://www.paypal.com/donate/?cmd=_s-xclick&hosted_button_id=PFB6A6HLAQHC2&source=url")
elif event == 'bitcoin':
webbrowser.open_new_tab("https://commerce.coinbase.com/checkout/149a6235-ec7e-4d3b-a1ae-b08c4f08b4f6")
window.Close()
main()