~george-edison55/audataserver-proxy/trunk

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
#! /usr/bin/python

'''
Proxy server for providing local searches to the Unity lens
'''

# Copyright (c) 2011, Nathan Osman <nathan.osman@gmail.com>
# Copyright (c) 2011, Stefano Palazzo <stefano.palazzo@gmail.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# On Debian systems, the complete text of the GNU General
# Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".


from audataserver import threaded_aulibrary
import cherrypy
import json


class DataServerApp (object):
    
    @cherrypy.expose
    def index(self):
        return "[Instructions and configuration go here.]"
    
    @cherrypy.expose
    def similar(self, title):
        cherrypy.response.headers["Content-Type"] = "application/json"
        return json.dumps(cherrypy.engine.data_manager.search(title)).encode()


class DataManager (cherrypy.process.plugins.SimplePlugin):
    
    data_dump = '/opt/se-data-dumps/askubuntu.ddi'
    
    def start(self):
        print '\33[33mSpawning data manager thread...\33[m'
        self.data_manager = threaded_aulibrary(self.data_dump)
    
    def stop(self):
        print '\33[33mShutting down data manager thread...\33[m'
        self.data_manager.shutdown()
        print '\33[32mData manager thread terminated.\33[m'
    
    def search(self, search_terms):
        return self.data_manager.search(search_terms)


cherrypy.engine.data_manager = DataManager(cherrypy.engine)
cherrypy.quickstart(DataServerApp(), config={'global': {
    'tools.encode.encoding': 'utf-8',
    'server.socket_host': '0.0.0.0',
    'server.socket_port': 19115,
    'engine.data_manager.on': True,
}})