Subversion Repositories navi

Rev

Rev 283 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
#    This file is part of Tsim Apiak.
#
#    Tsim Apiak is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public Licence as published by
#    the Free Software Foundation, either version 3 of the Licence, or
#    (at your option) any later version.
#
#    In addition to this, you must also comply with clause 4 of the
#    Apache Licence, version 2.0, concerning attribution. Where there
#    is a contradiction between the two licences, the GPL          
#    takes preference.
#
#    Tsim Apiak 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 Tsim Apiak.  If not, see <http://www.gnu.org/licenses/>.


from tsimapiak import parse, parsenum, translate
import http.client as httplib
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web


class Index(tornado.web.RequestHandler):
    def get(self):
        self.render("templates/index.html")

class Number(tornado.web.RequestHandler):
    def get(self):
        self.render("templates/number.html", last="", numout=None)

    def post(self):
        try:
            num = self.get_argument("num").strip()
        except:
            self.redirect("/number")
        numout = parsenum.parse(num.replace(" ", ""))
        if numout == None:
            numoutt = -1
        else:
            numoutt = (numout["dec"], numout["oct"])
        self.render("templates/number.html", last=num, numout=numoutt)

class Restart(tornado.web.RequestHandler):
    def get(self):
        os.system("/usr/bin/restartnavi")

class Parse(tornado.web.RequestHandler):
    def get(self):
        self.render("templates/parse.html", last="", out=None)

    def post(self):
        try:
            word = self.get_argument("word")
        except:
            self.redirect("/parse")
        out = parse.parsesent(word)
        self.render("templates/parse.html", last=word, out=out)

class Translate(tornado.web.RequestHandler):
    def get(self):
        self.render("templates/translate.html", last="", out=None, lang="en")

    def post(self):
        try:
            word = self.get_argument("word")
            lang = self.get_argument("lang")
        except:
            self.redirect("/translate")
        out = translate.translatesent(word, lang)
        self.render("templates/translate.html", last=word, out=out, lang=lang)
class Errors(tornado.web.RequestHandler):
    def get_error_html(self, status_code, **kwargs):
        if status_code == 500:
            return "<html><title>%(code)d: %(message)s</title>" \
                "<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>" % {
                "code": status_code,
                "message": httplib.responses[status_code],
                }
        else:
            return "<html><title>%(code)d: %(message)s</title>" \
                "<body>%(code)d: %(message)s</body></html>" % {
                "code": status_code,
                "message": httplib.responses[status_code],
                }

settings = {
    "static_path": os.path.join(os.path.dirname(__file__), "static")
}

application = tornado.web.Application([
    ("/", Index),
    ("/number", Number),
    ("/restart", Restart),
    ("/parse", Parse),
    ("/translate", Translate),
    ("/(\\.well-known/.*)", tornado.web.StaticFileHandler, dict(path=settings["static_path"]))
], **settings)

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(1337)
    tornado.ioloop.IOLoop.instance().start()