Subversion Repositories navi

Rev

Rev 240 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 szabot 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
176 muzer 3
#    This file is part of Tsim Apiak.
4
#
5
#    Tsim Apiak is free software: you can redistribute it and/or modify
6
#    it under the terms of the GNU General Public Licence as published by
7
#    the Free Software Foundation, either version 3 of the Licence, or
8
#    (at your option) any later version. 
9
# 
10
#    In addition to this, you must also comply with clause 4 of the
11
#    Apache Licence, version 2.0, concerning attribution. Where there
12
#    is a contradiction between the two licences, the GPL           
13
#    takes preference.
14
#
186 szabot 15
#    Tsim Apiak is distributed in the hope that it will be useful,
176 muzer 16
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
#    GNU General Public License for more details.
19
#
20
#    You should have received a copy of the GNU General Public License
21
#    along with Tsim Apiak.  If not, see <http://www.gnu.org/licenses/>.
2 szabot 22
 
176 muzer 23
 
246 szabot 24
from tsimapiak import parse, parsenum, translate
25
import httplib
26
import os
2 szabot 27
import tornado.httpserver
28
import tornado.ioloop
29
import tornado.web
30
 
31
 
32
class Index(tornado.web.RequestHandler):
33
    def get(self):
122 szabot 34
        self.render("templates/index.html")
2 szabot 35
 
36
class Number(tornado.web.RequestHandler):
37
    def get(self):
38
        self.render("templates/number.html", last="", numout=None)
39
 
40
    def post(self):
41
        try:
42
            num = self.get_argument("num").strip()
43
        except:
44
            self.redirect("/number")
246 szabot 45
        numout = parsenum.parse(num.replace(" ", ""))
2 szabot 46
        if numout == None:
103 szabot 47
            numoutt = -1
48
        else:
117 szabot 49
            numoutt = (numout["dec"], numout["oct"])
103 szabot 50
        self.render("templates/number.html", last=num, numout=numoutt)
2 szabot 51
 
52
class Restart(tornado.web.RequestHandler):
53
    def get(self):
54
        os.system("/usr/bin/restartnavi")
55
 
56 szabot 56
class Parse(tornado.web.RequestHandler):
49 szabot 57
    def get(self):
50 szabot 58
        self.render("templates/parse.html", last="", out=None)
246 szabot 59
 
49 szabot 60
    def post(self):
61
        try:
117 szabot 62
            word = self.get_argument("word")
49 szabot 63
        except:
64
            self.redirect("/parse")
117 szabot 65
        out = parse.parsesent(word)
50 szabot 66
        self.render("templates/parse.html", last=word, out=out)
49 szabot 67
 
146 szabot 68
class Translate(tornado.web.RequestHandler):
69
    def get(self):
152 szabot 70
        self.render("templates/translate.html", last="", out=None, lang="eng")
246 szabot 71
 
146 szabot 72
    def post(self):
73
        try:
74
            word = self.get_argument("word")
150 szabot 75
            lang = self.get_argument("lang")
146 szabot 76
        except:
77
            self.redirect("/translate")
150 szabot 78
        out = translate.translatesent(word, lang)
152 szabot 79
        self.render("templates/translate.html", last=word, out=out, lang=lang)
239 muzer 80
class Errors(tornado.web.RequestHandler):
81
    def get_error_html(self, status_code, **kwargs):
82
        if status_code == 500:
240 muzer 83
            return "<html><title>%(code)d: %(message)s</title>" \
84
                "<body>%(code)d: %(message)s<br /><br />Either we are currently working on the server, or you uncovered a bug. Please check back later on. If you still get this error, please report this bug to us in the forum thread or on IRC.</body></html>" % {
85
                "code": status_code,
86
                "message": httplib.responses[status_code],
87
                }
239 muzer 88
        else:
240 muzer 89
            return "<html><title>%(code)d: %(message)s</title>" \
90
                "<body>%(code)d: %(message)s</body></html>" % {
91
                "code": status_code,
92
                "message": httplib.responses[status_code],
93
                }
146 szabot 94
 
132 szabot 95
settings = {
96
    "static_path": os.path.join(os.path.dirname(__file__), "static")
97
}
98
 
2 szabot 99
application = tornado.web.Application([
100
    ("/", Index),
101
    ("/number", Number),
102
    ("/restart", Restart),
146 szabot 103
    ("/parse", Parse),
104
    ("/translate", Translate)
132 szabot 105
], **settings)
2 szabot 106
 
107
if __name__ == "__main__":
108
    http_server = tornado.httpserver.HTTPServer(application)
109
    http_server.listen(1337)
176 muzer 110
    tornado.ioloop.IOLoop.instance().start()