-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexer.py
More file actions
62 lines (48 loc) · 1.72 KB
/
Copy pathindexer.py
File metadata and controls
62 lines (48 loc) · 1.72 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
57
58
59
60
61
62
import os
from pdf import pdf_from_file, get_checksum
def find_files(folder_path):
files = []
for f in os.scandir(folder_path):
if f.is_file():
files.append(f)
return files
class Index:
def __init__(self, entries={}):
self.entries = entries
def find(self, query):
query = query.lower()
matches = []
for e in self.entries.values():
if e.title and query in e.title.lower():
matches.append(e)
elif e.keywords and query in e.keywords.lower():
matches.append(e)
return matches
def index_documents(self, doc_path):
files = find_files(doc_path)
entries = {}
for fp in files:
docpath = os.path.join(doc_path, fp.name)
with open(docpath, "rb") as fh:
cs = get_checksum(fh)
# Don't re-read the file if we have already indexed it.
if self.entries.get(cs):
entries[cs] = self.entries[cs]
continue
pdf = pdf_from_file(fh, docpath, cs)
if pdf and pdf.slug:
e = Entry(pdf.title, pdf.subject, pdf.keywords, pdf.slug, docpath, pdf.checksum, os.stat(docpath).st_mtime)
entries[e.checksum] = e
self.entries = entries
class Entry:
def __init__(self, title, category, keywords, id, filepath, checksum, last_updated):
self.title = title
self.category = category
self.keywords = keywords
self.id = id
self.filepath = filepath
self.checksum = checksum
self.last_updated = last_updated
@property
def filename(self):
return os.path.basename(self.filepath)