-
Notifications
You must be signed in to change notification settings - Fork 8
/
geekbooksearch2.py
executable file
·61 lines (52 loc) · 1.86 KB
/
geekbooksearch2.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
#!/usr/bin/env python
"""search for @future
grep 'date' ~/Dropbox/geekbook/notes/ eng* -n -A 15
"""
import os
import sys
import re
from engine.colors import bcolors
from engine.conf import PATH_TO_MD
if __name__ == '__main__':
#print (bcolors.OKGREEN + '// geekbook search for <for> in <in> //' + bcolors.ENDC)
action = 'search' # sys.argv[1] # search
curr = os.getcwd()
os.chdir(PATH_TO_MD)
v = False
# if action == 'search':
if 1:
################################################################################
# for
if ' '.join(sys.argv[:]).find('for') == -1:
raise Exception('Missing for in your query')
if v:
print (bcolors.OKGREEN + '> action:' + action + bcolors.ENDC)
match = re.search('for (?P<for>.*)', ' '.join(sys.argv[:]))
f = match.group('for').split('in')[0]
if v:
print (bcolors.OKGREEN + '\_ for:' + f + bcolors.ENDC)
################################################################################
match = re.search('in (?P<in>.*)', ' '.join(sys.argv[:]))
if match:
searchin = match.group('in').strip().split(' n ')[0]
else:
searchin = '*'
# -n forr grep
match = re.search('n (?P<n>\d+)', ' '.join(sys.argv[:]))
if match:
n = match.group('n').strip()
else:
n = '0'
if v:
print (bcolors.OKGREEN + '\_ n:' + n + bcolors.ENDC)
if v:
print (bcolors.OKGREEN + '\_ in: ' + searchin[:200] + bcolors.ENDC)
cmd = "grep --color=auto -i '" + f + "' " + searchin + " -n "
if n:
cmd += "-A " + n
if v:
print (bcolors.OKGREEN + '> ' + cmd[:200] + bcolors.ENDC)
os.system(cmd + ' 2>/dev/null ')
else:
print 'action is missing'
os.chdir(curr)