forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocate.py
More file actions
56 lines (47 loc) · 1.75 KB
/
Copy pathlocate.py
File metadata and controls
56 lines (47 loc) · 1.75 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
# -*- coding: utf-8 -*-
"""Unix 'locate' wrapper extension. Note that it is up to you to ensure that the database is up to \
date"""
import os
import re
import subprocess
from shutil import which
from albertv0 import *
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Locate"
__version__ = "1.0"
__trigger__ = "'"
__author__ = "Manuel Schneider"
__dependencies__ = ['locate']
if which("locate") is None:
raise Exception("'locate' is not in $PATH.")
for iconName in ["system-search", "search", "text-x-generic"]:
iconPath = iconLookup(iconName)
if iconPath:
break
def handleQuery(query):
results = []
if query.isTriggered:
if len(query.string) > 2:
pattern = re.compile(query.string, re.IGNORECASE)
proc = subprocess.Popen(['locate', '-bi', query.string], stdout=subprocess.PIPE)
for line in proc.stdout:
path = line.decode().strip()
basename = os.path.basename(path)
results.append(
Item(
id=path,
icon=iconPath,
text=pattern.sub(lambda m: "<u>%s</u>" % m.group(0), basename),
subtext=path,
completion="%s%s" % (__trigger__, basename),
actions=[UrlAction("Open", "file://%s" % path)]))
else:
results.append(
Item(
id=__prettyname__,
icon=iconPath,
text="Update locate database",
subtext="Type at least three chars for a seach",
completion=query.rawString,
actions=[TermAction("Update database", ["sudo", "updatedb"])]))
return results