-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
228 lines (190 loc) · 7.88 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding: utf-8 -*-
"""Module for z search ulauncher extension.
This module is an extension for ulauncher that searches for directories using
the 'z' file and sorts the results based on 'frecency'.
"""
import logging
import fileinput
import re
import time
import os
import sys
from pathlib import Path
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import (
KeywordQueryEvent,
PreferencesEvent,
PreferencesUpdateEvent,
ItemEnterEvent,
)
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.item.ExtensionSmallResultItem import ExtensionSmallResultItem
from ulauncher.api.shared.action.ActionList import ActionList
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.OpenAction import OpenAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gio, Gtk
def frecency(rank, last_time):
"""Calculate 'frecency' from rank and last access time."""
delta_t = time.time() - last_time
multiplier = 0
if delta_t < 3600:
multiplier = 4
elif delta_t < 86400:
multiplier = 2
elif delta_t < 604800:
multiplier = 0.5
else:
multiplier = 0.25
return rank * multiplier
def update_z_file(file, path, rank, time):
"""Update path in .z file with new rank and time."""
logging.debug(
"Updating '%s' in '%s' with rank %f and time %d", path, file, rank, int(time)
)
line_replaced = False
for line in fileinput.input(file, inplace=1):
old_path, old_rank, old_time = line.rsplit("|", maxsplit=3)
if old_path == path:
line = f"{path}|{rank}|{int(time)}" + os.linesep
line_replaced = True
sys.stdout.write(line)
if not line_replaced:
logging.warning(
"Updating '%s' failed; '%s' not found in '%s'", file, path, file
)
class ZSearchExtension(Extension):
"""The z search extension."""
def __init__(self):
"""Initialize the base class and subscribe to events."""
super(ZSearchExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(PreferencesEvent, PreferencesLoadListener())
self.subscribe(PreferencesUpdateEvent, PreferencesChangeListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
self.z_file = ""
self.max_results = 0
self.update_z_file = False
def search(self, query):
"""Search for entries matching a query return a list of dicts.
Apart from the rank and time values contained in the z file, the
dicts contain calculated frecency as well.
"""
results = []
with open(self.z_file, "r") as z_file:
lines = z_file.readlines()
query = query.lower()
pattern = re.compile(query, re.IGNORECASE)
for line in lines:
if pattern.search(line):
path, rank, last_time = line.rstrip("\n").split("|")
rank = float(rank)
last_time = float(last_time)
results.append(
{
"path": path,
"rank": rank,
"time": last_time,
"frecency": frecency(rank, last_time),
}
)
sorted_results = sorted(results, key=lambda dic: dic["frecency"], reverse=True)
return sorted_results[: self.max_results]
class PreferencesLoadListener(EventListener):
"""This event listener is called when the extension is loaded."""
def on_event(self, event, extension):
"""Set extension member variables according to preferences."""
extension.preferences.update(event.preferences)
extension.z_file = os.path.expanduser(extension.preferences["z_file"])
extension.max_results = int(extension.preferences["max_results"])
extension.update_z_file = extension.preferences["update_z_file"] == "true"
class PreferencesChangeListener(EventListener):
"""This event listener is called when the extension properties are changed."""
def on_event(self, event, extension):
"""Update extension member variables when preferences change."""
if event.id == "z_file":
extension.z_file = os.path.expanduser(event.new_value)
elif event.id == "max_results":
extension.max_results = int(event.new_value)
elif event.id == "update_z_file":
extension.update_z_file = event.new_value == "true"
class KeywordQueryEventListener(EventListener):
"""This event listener is called when the extension keyword is typed."""
def __init__(self):
"""Initialize the base class and members."""
super(KeywordQueryEventListener, self).__init__()
self.folder_icon = self.get_folder_icon()
def on_event(self, event, extension):
"""Run search if query was entered and act on results."""
query = event.get_argument()
if not query:
return RenderResultListAction(
[
ExtensionResultItem(
icon="images/icon.png",
name="Type a part of a directory name",
on_enter=DoNothingAction(),
)
]
)
results = extension.search(query)
if not results:
return RenderResultListAction(
[
ExtensionResultItem(
icon="images/icon.png",
name="No results matching %s" % query,
on_enter=HideWindowAction(),
)
]
)
entries = []
for result in results:
actions = [OpenAction(result["path"])]
if extension.update_z_file:
actions.append(ExtensionCustomAction(result, keep_app_open=False))
entries.append(
ExtensionSmallResultItem(
icon=self.folder_icon,
name=self.get_display_path(result["path"]),
on_enter=ActionList(actions),
)
)
return RenderResultListAction(entries)
def get_display_path(self, path):
"""Strip /home/user from path if appropriate."""
path = Path(path)
home = Path.home()
if home in path.parents:
return "~/" + str(path.relative_to(home))
else:
return str(path)
def get_folder_icon(self):
"""Get a path to a reasonable folder icon.
Fall back to an included one if none is found.
"""
file = Gio.File.new_for_path("/")
folder_info = file.query_info("standard::icon", 0, Gio.Cancellable())
folder_icon = folder_info.get_icon().get_names()[0]
icon_theme = Gtk.IconTheme.get_default()
icon_folder = icon_theme.lookup_icon(folder_icon, 128, 0)
if icon_folder:
folder_icon = icon_folder.get_filename()
else:
folder_icon = "images/folder.png"
return folder_icon
class ItemEnterEventListener(EventListener):
"""This event listener is called when the user selects an entry."""
def on_event(self, event, extension):
"""Update the entry's rank and time in the z file."""
entry = event.get_data()
update_z_file(
extension.z_file, entry["path"], entry["rank"] + 1, float(time.time())
)
if __name__ == "__main__":
ZSearchExtension().run()