Subversion Repositories navi

Rev

Rev 176 | Rev 239 | Go to most recent revision | 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/>.


import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.autoreload

import os
import re

from tsimapiak import parsenum
from tsimapiak import dbconnector
from tsimapiak import parse
from tsimapiak import translate

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 TestDB(tornado.web.RequestHandler):
    def get(self):
        lis = dbconnector.getnavilist()
        text = u"id | navi | infix | partofspeech<br />"
        text += u"<br />".join(u" | ".join(unicode(y) for y in x) for x in lis)
        self.write(text)

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="eng")
   
    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)

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

application = tornado.web.Application([
    ("/", Index),
    ("/number", Number),
    ("/restart", Restart),
    ("/testdb", TestDB),
    ("/parse", Parse),
    ("/translate", Translate)
], **settings)

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