-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.py
222 lines (202 loc) · 5.97 KB
/
catalog.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
import logging
from dharma import parse, texts, common, document
class Query:
def __init__(self, query, field=""):
self.query = query
self.field = field
def __str__(self):
if isinstance(self.query, str):
ret = '"%s"' % self.query.replace('"', '""')
else:
ret = str(self.query)
if self.field:
ret = "%s:%s" % (self.field, ret)
return ret
class AND(Query):
def __init__(self, clauses):
self.clauses = clauses
def __str__(self):
return "(%s)" % " AND ".join(str(clause) for clause in self.clauses)
class OR(Query):
def __init__(self, clauses):
self.clauses = clauses
def __str__(self):
return "(%s)" % " OR ".join(str(clause) for clause in self.clauses)
def delete(name):
db = common.db("texts")
db.execute("delete from documents_index where name = ?", (name,))
db.execute("delete from documents where name = ?", (name,))
def insert(file):
db = common.db("texts")
doc = parse.process_file(file, mode="catalog")
for key in ("title", "authors", "editors", "summary"):
val = getattr(doc, key, None)
if val is None:
val = parse.Block(val)
val.finish()
setattr(doc, key, val)
db.execute("""insert or replace into documents(name, repo, title,
authors, editors, editors_ids, langs, summary, html_path, status)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", (doc.ident, doc.repository,
doc.title.render_logical() or None, doc.authors, doc.editors,
doc.editors_ids,
[lang.id for lang in doc.edition_langs], doc.summary.render_logical() or None,
file.html, file.status))
# No primary key on documents_index, so we cannot use "insert or replace"
db.execute("delete from documents_index where name = ?", (doc.ident,))
db.execute("""insert into documents_index(
name, ident, repo, title, author,
editor, editor_id, lang, summary)
values (?, ?, ?, ?, ?, ?, ?, ?, ?)""", (
doc.ident, doc.ident.lower(),
doc.repository.lower(), doc.title.searchable_text(),
common.normalize_text("|||".join(doc.authors)),
common.normalize_text("|||".join(doc.editors)),
common.normalize_text("|||".join(doc.editors_ids)),
"---".join(lang.id for lang in doc.edition_langs),
doc.summary.searchable_text()))
# Rebuild the full catalog with the data already present in the db.
def rebuild():
logging.info("rebuilding the catalog")
db = common.db("texts")
db.execute("delete from documents_index")
for repo, path, mtime, data, html in db.execute("""
select files.repo, path, mtime, data, html_path
from files join documents on files.name = documents.name
order by files.repo, files.name"""):
file = texts.File(repo, path)
file.html = html
setattr(file, "_mtime", mtime)
setattr(file, "_data", data)
insert(file)
logging.info("rebuilded the catalog")
class InvalidQuery(Exception):
pass
def tokenize_query(q):
toks = []
i = 0
while i < len(q):
c = q[i]
if c == ":":
toks.append(c)
i += 1
elif c == '"':
i += 1
tok = ""
while i < len(q) and q[i] != '"':
if q[i] == "\\":
i += 1
if i == len(q):
raise InvalidQuery
tok += q[i]
i += 1
if i == len(q):
raise InvalidQuery
toks.append(tok)
i += 1
elif c.isspace():
i += 1
else:
i += 1
tok = c
while i < len(q) and q[i] not in ": ":
if q[i] == "\\":
i += 1
if i == len(q):
raise InvalidQuery
tok += q[i]
i += 1
toks.append(tok)
return toks
search_fields = {
"ident", "repo", "title", "author",
"editor", "editor_id", "summary", "lang",
}
def parse_query(q):
toks = tokenize_query(q)
i = 0
clauses = []
while i < len(toks):
if toks[i] == ":":
raise InvalidQuery
elif i + 1 < len(toks) and toks[i + 1] == ":":
field = toks[i]
if i + 2 < len(toks) and toks[i + 2] != ":":
clauses.append(Query(toks[i + 2], field))
i += 3
else:
clauses.append(Query(toks[i]))
i += 1
return AND(clauses)
def patch_languages(q):
db = common.db("texts")
for clause in q.clauses:
if clause.field != "lang":
continue
text = clause.query
assert isinstance(text, str)
if len(text) <= 3:
(text,) = db.execute("select ifnull((select id from langs_by_code where code = ?), '')", (text,)).fetchone()
clause.query = text
continue
langs = [Query(lang) for (lang,) in db.execute("select id from langs_by_name where name match ?", (text,))]
if langs:
clause.query = OR(langs)
else:
clause.query = "" # prevent matching
def construct_query(q):
q = " ".join(document.normalize(t) for t in q.split()
if t not in ("AND", "OR", "NOT"))
if q:
q = parse_query(q)
patch_languages(q)
return str(q)
PER_PAGE = 50
@common.transaction("texts")
def search(q, sort, page):
db = common.db("texts")
q = construct_query(q)
if q:
(total,) = db.execute("""select count(*) from documents_index
where documents_index match ?""", (q,)).fetchone()
else:
(total,) = db.execute("select count(*) from documents_index").fetchone()
sql = """
select documents.name, documents.repo, documents.title,
documents.authors, documents.editors, json_group_array(distinct langs_list.name) as langs, documents.summary,
repos.title as repo_title,
html_path
from documents
join documents_index on documents.name = documents_index.name
join repos on documents.repo = repos.repo
join json_each(documents.langs)
left join langs_by_code on langs_by_code.code = json_each.value
left join langs_list on langs_list.id = langs_by_code.id
"""
if q:
sql += " where documents_index match ?"
match sort:
case "ident":
sort = "name"
case "title" | "repo":
pass
case _:
sort = "title"
match sort:
case "title":
collation = "html_icu"
case _:
collation = "icu"
sql += f"""
group by documents.name order by documents.{sort} collate {collation} nulls last
limit ? offset ?"""
offset = (page - 1) * PER_PAGE
limit = PER_PAGE
if q:
ret = db.execute(sql, (q, limit, offset)).fetchall()
else:
ret = db.execute(sql, (limit, offset)).fetchall()
(last_updated,) = db.execute("""
select cast(value as int)
from metadata where key = 'last_updated'""").fetchone()
return ret, total, PER_PAGE, last_updated