-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
153 lines (118 loc) · 4.24 KB
/
main.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
from __future__ import annotations
import re
from abc import abstractmethod, ABC
from enum import Enum
from typing import List, Optional, Literal, Tuple, Union
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
import ulauncher.api.shared.event as events
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
class DemoExtension(Extension):
def __init__(self):
super().__init__()
self.subscribe(events.KeywordQueryEvent, KeywordQueryEventListener())
class Number(ABC):
@classmethod
def parse(cls, payload: str, encoding: Encoding) -> Union[Number, ExtensionResultItem]:
if len(payload) == 0:
return ExtensionResultItem(
icon='images/icon.png',
name='No input',
description=f"Please input a {encoding} number",
on_enter=DoNothingAction(),
)
try:
value = encoding.decode(payload)
return Number(value)
except ValueError:
msg = "Failed to convert number"
description = f"Value {payload} is not a {encoding} number."
return ExtensionResultItem(
icon='images/icon.png',
name=msg,
description=description,
on_enter=DoNothingAction(),
on_alt_enter=DoNothingAction(),
)
def __init__(self, value: int):
self.value = value
def result_item(self, encoding: Encoding) -> ExtensionResultItem:
payload = encoding.encode(self.value)
return ExtensionResultItem(
icon=encoding.icon,
name=payload,
description=encoding.__str__().capitalize() + '; Copy to clipboard.',
on_enter=CopyToClipboardAction(payload),
on_alt_enter=CopyToClipboardAction(payload),
)
class Encoding:
@abstractmethod
def base(self) -> int:
pass
@property
def icon(self) -> str:
return 'images/icon.png'
@abstractmethod
def __str__(self):
pass
@abstractmethod
def encode(self, value: int) -> str:
pass
def decode(self, value: str) -> int:
return int(value, self.base())
class Hexadecimal(Encoding):
def base(self) -> int:
return 16
@property
def icon(self) -> str:
return 'images/hex.png'
def __str__(self):
return "hexadecimal"
def encode(self, value: int) -> str:
return hex(value)[2:]
class Decimal(Encoding):
def base(self) -> int:
return 10
@property
def icon(self) -> str:
return 'images/dec.png'
def __str__(self):
return "decimal"
def encode(self, value: int) -> str:
return str(value)
class Binary(Encoding):
def base(self) -> int:
return 2
@property
def icon(self) -> str:
return 'images/bin.png'
def __str__(self):
return "binary"
def encode(self, value: int) -> str:
return bin(value)[2:]
class KeywordQueryEventListener(EventListener):
def on_event(self, event: events.KeywordQueryEvent, extension: Extension):
arg = event.get_argument() or ""
value = re.split(r"\s+", arg)[0]
kw = event.get_keyword()
if kw == extension.preferences["kw_hex"]:
num = Number.parse(value, Hexadecimal())
encodings = [Decimal(), Binary()]
elif kw == extension.preferences["kw_bin"]:
num = Number.parse(value, Binary())
encodings = [Decimal(), Hexadecimal()]
elif kw == extension.preferences["kw_dec"]:
num = Number.parse(value, Decimal())
encodings = [Hexadecimal(), Binary()]
else:
raise RuntimeError()
if isinstance(num, ExtensionResultItem):
items = [num]
else:
items = list(map(lambda enc: num.result_item(enc), encodings))
return RenderResultListAction(items)
if __name__ == '__main__':
DemoExtension().run()