Small fixes to main.py
This commit is contained in:
parent
094af5f25a
commit
f62e06b988
56
main.py
56
main.py
|
@ -16,18 +16,22 @@ from ulauncher.api.shared.action.RenderResultListAction import RenderResultListA
|
||||||
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
|
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
|
||||||
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
|
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
|
||||||
|
|
||||||
|
# Be compatible with both python 2 and 3
|
||||||
if sys.version_info[0] >= 3:
|
if sys.version_info[0] >= 3:
|
||||||
unichr = chr
|
unichr = chr
|
||||||
|
|
||||||
FILE_PATH = os.path.dirname(sys.argv[0])
|
FILE_PATH = os.path.dirname(sys.argv[0])
|
||||||
|
|
||||||
ICON_TEMPLATE = '''
|
ICON_TEMPLATE = """
|
||||||
<svg width="100" height="100">
|
<svg width="100" height="100">
|
||||||
<text x="50" y="50" dy=".35em" text-anchor="middle" font-family="{font}" font-size="80">{symbol}</text>
|
<text x="50" y="50" dy=".35em" text-anchor="middle" font-family="{font}" font-size="80">{symbol}</text>
|
||||||
</svg>
|
</svg>
|
||||||
'''
|
"""
|
||||||
|
|
||||||
|
|
||||||
class UnicodeChar:
|
class UnicodeChar:
|
||||||
|
""" Container class for unicode characters """
|
||||||
|
|
||||||
def __init__(self, name, block, code):
|
def __init__(self, name, block, code):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.block = block
|
self.block = block
|
||||||
|
@ -35,44 +39,35 @@ class UnicodeChar:
|
||||||
self.character = unichr(int(code, 16))
|
self.character = unichr(int(code, 16))
|
||||||
|
|
||||||
def get_search_name(self):
|
def get_search_name(self):
|
||||||
|
""" Called by `ulauncher.search.SortedList` to get the string that should be used in searches """
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class SearchableItem:
|
|
||||||
def __init__(self, name, item):
|
|
||||||
self._name = name
|
|
||||||
self._item = item
|
|
||||||
|
|
||||||
def get_search_name(self):
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
class UnicodeCharExtension(Extension):
|
class UnicodeCharExtension(Extension):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(UnicodeCharExtension, self).__init__()
|
super(UnicodeCharExtension, self).__init__()
|
||||||
|
self._load_character_table()
|
||||||
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
|
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
|
||||||
|
|
||||||
|
|
||||||
class KeywordQueryEventListener(EventListener):
|
|
||||||
def __init__(self):
|
|
||||||
self._load_character_table()
|
|
||||||
|
|
||||||
def _load_character_table(self):
|
def _load_character_table(self):
|
||||||
|
""" Read the data file and load to memory """
|
||||||
self.character_list = []
|
self.character_list = []
|
||||||
with open(join(FILE_PATH, "unicode_list.txt"), "r") as f:
|
with open(join(FILE_PATH, "unicode_list.txt"), "r") as f:
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
name, code, block = line.strip().split('\t')
|
name, code, block = line.strip().split("\t")
|
||||||
character = UnicodeChar(name, block, code)
|
character = UnicodeChar(name, block, code)
|
||||||
self.character_list.append(character)
|
self.character_list.append(character)
|
||||||
|
|
||||||
|
|
||||||
|
class KeywordQueryEventListener(EventListener):
|
||||||
def on_event(self, event, extension):
|
def on_event(self, event, extension):
|
||||||
items = []
|
items = []
|
||||||
arg = event.get_argument()
|
arg = event.get_argument()
|
||||||
if arg:
|
if arg:
|
||||||
m_ = SortedList(arg, min_score=60, limit=10)
|
result_list = SortedList(arg, min_score=60, limit=10)
|
||||||
m_.extend(self.character_list)
|
result_list.extend(extension.character_list)
|
||||||
for char in m_:
|
for char in result_list:
|
||||||
image_path = self._get_character_icon(char.code)
|
image_path = self._get_character_icon(char)
|
||||||
items.append(
|
items.append(
|
||||||
ExtensionResultItem(
|
ExtensionResultItem(
|
||||||
icon=image_path,
|
icon=image_path,
|
||||||
|
@ -84,25 +79,30 @@ class KeywordQueryEventListener(EventListener):
|
||||||
|
|
||||||
return RenderResultListAction(items)
|
return RenderResultListAction(items)
|
||||||
|
|
||||||
def _get_character_icon(self, code):
|
def _get_character_icon(self, char):
|
||||||
path = sys.argv[0] + "images/cache/icon_%s.svg" % code
|
path = sys.argv[0] + "images/cache/icon_%s.svg" % char.code
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
return path
|
return path
|
||||||
return create_character_icon(code)
|
return create_character_icon(char)
|
||||||
|
|
||||||
|
|
||||||
def is_icon_cached(code):
|
def is_icon_cached(code):
|
||||||
return os.path.isfile(os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code))
|
return os.path.isfile(os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code))
|
||||||
|
|
||||||
|
|
||||||
def create_character_icon(code, font='sans-serif'):
|
def create_character_icon(char, font="sans-serif"):
|
||||||
template = ICON_TEMPLATE
|
""" Create an SVG file containing the unicode glyph for char to be used
|
||||||
icon = template.replace("{symbol}", unichr(int(code, 16))).replace('{font}', font)
|
as a result icon.
|
||||||
|
|
||||||
|
Note: this could be avoided by providing a gtk.PixBuf without creating a file,
|
||||||
|
but ulauncher pickles the returned results, so it doesn't work currently.
|
||||||
|
"""
|
||||||
|
icon = ICON_TEMPLATE.replace("{symbol}", char.character).replace("{font}", font)
|
||||||
with codecs.open(
|
with codecs.open(
|
||||||
os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code), "w", "utf-8"
|
os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % char.code), "w", "utf-8"
|
||||||
) as target:
|
) as target:
|
||||||
target.write(icon)
|
target.write(icon)
|
||||||
return os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % code)
|
return os.path.join(FILE_PATH, "images/cache/icon_%s.svg" % char.code)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue