forked from compilelife/ulauncher-searchfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locator.py
40 lines (35 loc) · 1.09 KB
/
locator.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
import os
import subprocess
import logging
import sys
class Locator:
def __init__(self):
self.cmd = 'locate' if self.__check_has_locate() else None
self.limit = 5
self.opt = ''
def set_limit(self, limit):
print('set limit to '+str(limit))
self.limit = limit
def set_locate_opt(self, opt):
print('set locate opt to '+opt)
self.opt = opt
def __check_has_locate(self):
try:
subprocess.check_call(['which', 'locate'])
return True
except:
return False
def run(self, pattern):
if self.cmd == None:
raise RuntimeError('command locate not found or options config error')
else:
cmd = [self.cmd, '-l', str(self.limit)]
args = pattern.split(' ')
if args[0] == 'r':
cmd.extend(args[1:])
else:
cmd.append(self.opt)
cmd.extend(args)
print('----->'+str(cmd))
output = subprocess.check_output(cmd, encoding='utf-8')
return output.splitlines()