forked from brpaz/ulauncher-encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (98 loc) · 4.57 KB
/
main.py
File metadata and controls
116 lines (98 loc) · 4.57 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
"""
Ulauncher Encoder extension
"""
import logging
import base64
import urllib
import html
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
LOGGER = logging.getLogger(__name__)
class EncoderExtension(Extension):
""" Main Extension class """
def __init__(self):
LOGGER.info('Starting Encoder Extension')
super(EncoderExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
def decode(self, text):
""" Decode a string into multiple formats """
try:
decoded_base64 = base64.b64decode(text).decode()
except: # pylint: disable=bare-except
decoded_base64 = "Cannot decode input text as base64."
try:
decoded_url = urllib.parse.unquote_plus(text)
except: # pylint: disable=bare-except
decoded_url = "Cannot decode input text"
decoded_html = html.unescape(text)
try:
decoded_hex = bytes.fromhex(text).decode()
except ValueError:
decoded_hex = "Cannot decode input text as hex string"
return [
ExtensionResultItem(icon='images/icon.png',
name=decoded_hex,
description='HEX Decoded',
highlightable=False,
on_enter=CopyToClipboardAction(decoded_hex)),
ExtensionResultItem(icon='images/icon.png',
name=decoded_base64,
description='Base64 Decoded',
highlightable=False,
on_enter=CopyToClipboardAction(decoded_base64)),
ExtensionResultItem(icon='images/icon.png',
name=decoded_url,
description='URL Decoded',
highlightable=False,
on_enter=CopyToClipboardAction(decoded_url)),
ExtensionResultItem(icon='images/icon.png',
name=decoded_html,
description='HTML Decoded',
highlightable=False,
on_enter=CopyToClipboardAction(decoded_html))
]
def encode(self, text):
""" Encodes a string into multiple formats """
encoded_base64 = base64.b64encode(bytes(text, "utf-8")).decode("utf-8")
encoded_url = urllib.parse.quote_plus(text)
encoded_html = html.escape(text)
encoded_hex = text.encode().hex().upper()
return [
ExtensionResultItem(icon='images/icon.png',
name=encoded_hex,
description='HEX Encoded',
highlightable=False,
on_enter=CopyToClipboardAction(encoded_hex)),
ExtensionResultItem(icon='images/icon.png',
name=encoded_base64,
description='Base64 Encoded',
highlightable=False,
on_enter=CopyToClipboardAction(encoded_base64)),
ExtensionResultItem(icon='images/icon.png',
name=encoded_url,
description='URL Encoded',
highlightable=False,
on_enter=CopyToClipboardAction(encoded_url)),
ExtensionResultItem(icon='images/icon.png',
name=encoded_html,
description='HTML Encoded',
highlightable=False,
on_enter=CopyToClipboardAction(encoded_html))
]
class KeywordQueryEventListener(EventListener):
""" Handles input events """
def on_event(self, event, extension):
""" Handles event """
items = []
text = event.get_argument() or ""
if event.get_keyword() == "encode":
items = extension.encode(text)
else:
items = extension.decode(text)
return RenderResultListAction(items)
if __name__ == '__main__':
EncoderExtension().run()