-
Notifications
You must be signed in to change notification settings - Fork 2
/
wsgi.py
executable file
·59 lines (45 loc) · 1.63 KB
/
wsgi.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
#!/usr/bin/env python
import html
import flask
import markdown
import lexdb
import lexicon
LEX_FILENAME = 'lexicon.txt'
DB_FILENAME = 'lexicon.out.sqlite3'
application = flask.Flask(__name__)
@application.route('/api/search/oe/')
@application.route('/api/search/oe/<search_terms>')
def search_oe(search_terms="nawiht"):
with lexdb.LexDB(LEX_FILENAME, DB_FILENAME) as db:
search_terms = search_terms.split()
text = ""
for term in search_terms:
entries = db.lookup(term)
if len(entries) == 0:
text += f"<h2>Not found: {html.escape(term)}</h2>\n"
else:
text += format_entries(entries)
return text
@application.route('/api/search/reverse/')
@application.route('/api/search/reverse/<search_string>')
def search_reverse(search_string="nothing"):
with lexdb.LexDB(LEX_FILENAME, DB_FILENAME) as db:
entries = db.reverse_lookup(search_string)
if len(entries) == 0:
text = f"<h2>Not found: {html.escape(search_string)}</h2>\n"
else:
text = format_entries(entries)
return text
@application.route('/api/search/random/')
def random():
with lexdb.LexDB(LEX_FILENAME, DB_FILENAME) as db:
text = format_entries([db.random_lookup()])
return text
def format_entries(entries):
text = ""
for entry in entries:
types = "; ".join(lexicon.expand_word_type(word_type) for word_type in entry.word_types)
text += f"<h2 lang=\"ang\">{html.escape(entry.lemma)}</h2>\n"
text += f"<p><i>{html.escape(types)}</i></p>\n"
text += markdown.markdown(entry.text)
return text