forked from vpfautz/pdfindex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfindex.py
executable file
·301 lines (242 loc) · 7.38 KB
/
pdfindex.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python2
#coding: UTF-8
import subprocess
import os
import zlib
import argparse
import json
import re
# import textract
import hashlib
import sys
from time import time
from copy import deepcopy
INDEX_PATH = os.path.expanduser("~/.pdfindex")
"""
Index is zlib of json:
hash is sha256
files[filename]:
hash
modified: modified time
hashs[hash]:
txt from pdf file
"""
# Parse a pdf file and returns containing text.
def pdf_to_text(fname):
# t = textract.process(fname)
# TODO faster alternative?
r = subprocess.Popen(["pdftotext", fname, "-"], stdout=subprocess.PIPE)
t = r.communicate()[0]
for c,u in zip("aouAOU", ["ä","ö","ü","Ä","Ö","Ü"]):
t = t.replace("%s\xcc\x88" % c, u)
t = t.replace("%sĚ" % c, u)
t = t.replace("Ă", "ß")
return t
def hash_file(fname):
d = open(fname, "rb").read()
return hashlib.sha256(d).hexdigest()
def stderr(s):
print >> sys.stderr, s
# Recurse given rootdir and adds new files to index.
def dir_to_index(index, rootdir, instant_save=False):
cwd = os.path.abspath(rootdir)
file_list = []
for root, subdirs, files in os.walk(cwd):
for fname in files:
if not fname.endswith(".pdf"):
continue
fname = os.path.join(cwd, root, fname)
if need_update(index, fname):
file_list.append(fname)
last_save = time()
# TODO do this threaded?
for i,fname in enumerate(file_list):
# stderr("[%s/%s] %s" % (i+1, len(file_list), os.path.relpath(fname,rootdir)))
add_file_to_index(index, fname)
if instant_save and time() > last_save + 10:
save_index(INDEX_PATH, index)
last_save = time()
return index
# Returns true, iff the file needs to be updated in index.
def need_update(index, fname):
if not fname.endswith(".pdf"):
return False
fname = os.path.abspath(fname).decode("utf8")
if not os.path.isfile(fname):
return False
if not fname in index["files"]:
return True
if os.path.getmtime(fname) != index["files"][fname]["modified"]:
return True
return False
# Checks if the given file (fname) needs to be updated in the index
# and, if needed, do this.
def add_file_to_index(index, fname):
if not fname.endswith(".pdf"):
return
fname = os.path.abspath(fname)
if not os.path.isfile(fname):
if fname in index["files"]:
del index["files"][fname]
return
if fname in index["files"] \
and os.path.getmtime(fname) == index["files"][fname]["modified"]\
and index["files"][fname]["hash"] in index["hashs"]:
return
h = hash_file(fname)
modified = os.path.getmtime(fname)
if not h in index["hashs"]:
index["hashs"][h] = pdf_to_text(fname)
index["files"][fname] = {
"hash": h,
"modified": modified
}
# saves the index to file
def save_index(fname, index):
if index == orig_data:
# stderr("index already up-to-date :)")
return
d = zlib.compress(json.dumps(index))
if os.path.exists(fname):
os.rename(fname, "%s_bak" % fname)
open(fname, "wb").write(d)
# stderr("index file saved")
orig_data = None
# loads the index from given file
def load_index(fname, parse_files=False):
global orig_data
if not os.path.isfile(fname):
return {"files": {}, "hashs": {}}
d = open(fname, "rb").read()
index = json.loads(zlib.decompress(d))
orig_data = deepcopy(index)
if parse_files:
for fname in index["files"].keys():
add_file_to_index(index, fname)
return index
class Color:
RED = '\033[91m'
RED2 = '\033[1;91m'
GREEN = '\033[92m'
GREEN2 = '\033[1;92m'
YELLOW = '\033[93m'
YELLOW2 = '\033[1;93m'
BLUE = '\033[94m'
BLUE2 = '\033[1;94m'
PINK = '\033[95m'
PINK2 = '\033[1;95m'
CYAN = '\033[96m'
CYAN2 = '\033[1;96m'
GRAY = '\033[1;30m'
WHITE = '\033[1;37m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'
NORMAL = '\033[0m'
def clr(s, c):
return "%s%s%s" % (c, s, Color.ENDC)
# Highlight the string pattern in s.
def highlight(s, pattern, c=Color.YELLOW2):
return s.replace(pattern, clr(pattern, c))
def highlight_match(match):
txt, pattern = match
d = highlight(txt, pattern)
d = d.replace(" \"u", "ü")
d = d.replace(" \"o", "ö")
d = d.replace(" \"a", "ä")
d = d.replace("\"u", "ü")
d = d.replace("\"o", "ö")
d = d.replace("\"a", "ä")
return d
# Iff txt is a unicode, it will encode it as UTF8.
def enc(txt):
if isinstance(txt, unicode):
return txt.encode("utf8")
else:
return txt
# Search query in pdfs located in directory path.
def search(index, query, path, filenames_only=False):
rootdir = os.path.abspath(path)
for fname, file in index["files"].items():
if not fname.startswith(rootdir):
continue
if file["hash"] not in index["hashs"]:
print "\nERROR Index file corrupt! (missing hash)"
print fname
print file
exit(1)
txt = index["hashs"][file["hash"]]
txt = enc(txt)
query2 = query.replace("ü", " \"u")
query2 = query2.replace("ö", " \"o")
query2 = query2.replace("ä", " \"a")
query3 = query.replace("ü", "\"u")
query3 = query3.replace("ö", "\"o")
query3 = query3.replace("ä", "\"a")
# TODO escape query
matches = re.findall("^(.*(%s|%s|%s).*)$" % (query, query2, query3),
txt, re.IGNORECASE | re.MULTILINE)
if len(matches) == 0:
continue
if filenames_only:
if rootdir == fname:
print path.encode("utf8")
else:
print enc(os.path.relpath(fname, rootdir))
else:
print ""
print clr(enc(os.path.relpath(fname, rootdir)), Color.WHITE)
print "\n".join(map(highlight_match, matches))
if __name__ == '__main__':
# TODO add option to disable regex
parser = argparse.ArgumentParser(description='Indexed search in files.')
parser.add_argument('-l', "--files-with-matches", const=True, default=False,
dest="filenames_only", action='store_const',
help='Only print filenames that contain matches')
parser.add_argument('--no-parse', const=False, default=True, dest="parse_files",
action='store_const', help='Query only index, don\'t indize any file')
parser.add_argument('query', nargs="?", default=None, help='The string to search for')
parser.add_argument('directory', nargs='?', help='The directory to search in')
group = parser.add_mutually_exclusive_group()
group.add_argument('--cached-files', const=True, default=False,
dest="cached_files", action="store_const", help="List all cached files")
group.add_argument('--cached-hashs', const=True, default=False,
dest="cached_hashs", action="store_const", help="List all cached hashs")
parser.add_argument('--clear-cache', const=True, default=False, dest="clear_cache",
action='store_const', help='Clear cache')
args = parser.parse_args()
if args.clear_cache:
os.remove(INDEX_PATH)
exit()
if args.cached_files:
index = load_index(INDEX_PATH)
for f in index["files"]:
print f.encode("utf8")
exit()
if args.cached_hashs:
index = load_index(INDEX_PATH)
for h in index["hashs"]:
print h
exit()
if args.query is None:
parser.print_usage()
print "%s: error: too few arguments" % os.path.split(sys.argv[0])[1]
exit(2)
query = args.query
path = os.getcwd() if args.directory is None else args.directory
if not os.path.exists(path):
print "cannot access '%s': No such file or directory" % path
exit()
index = load_index(INDEX_PATH, args.parse_files)
if args.parse_files:
if os.path.isdir(path):
try:
dir_to_index(index, path, True)
save_index(INDEX_PATH, index)
except KeyboardInterrupt:
save_index(INDEX_PATH, index)
exit()
elif os.path.isfile(path):
add_file_to_index(index, path)
save_index(INDEX_PATH, index)
search(index, query, path, args.filenames_only)