-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearcher.py
48 lines (42 loc) · 1.44 KB
/
searcher.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
# -*- encoding: utf-8 -*-
# Programmed by: israelord <iferminm at gmail dot com>
from mako.template import Template
from mako.lookup import TemplateLookup
from querier import Querier
import cherrypy
from itertools import chain
import os
lookup = TemplateLookup(directories=['html'])
class Searcher:
def serve_template(self, template_name, **kwargs):
"""
Serves any existing template by name
"""
template = lookup.get_template(template_name)
return template.render(**kwargs)
def unpack_results(self, results):
return list(chain(*results))
@cherrypy.expose
def do_query(self, query):
"""
Renders the results page
"""
q = Querier()
results = None
try:
# extracts only the bindings from the result dictionary
bindings = [r['results']['bindings'] for r in q.query(str(query)) if r['results']['bindings'] != []]
results = self.unpack_results(bindings)
except:
# in case of any exception should render an error page
results = "ERROR"
return self.serve_template('results.txt', results=results)
@cherrypy.expose
def index(self):
"""
Renders the main search page
"""
return self.serve_template('index.txt')
if __name__ == '__main__':
cherrypy.config.update({'server.socket_port': 2112})
cherrypy.quickstart(Searcher(), '/', 'searcher.cfg')