#!/usr/bin/python
# -*- coding:utf-8 -*-
# Powered By KK Studio

import tornado
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.netutil
import tornado.process
import tornado.locale
import platform
import db
from cache import RCache
from tornado.log import gen_log
from handler.page import Page404Handler
from config.settings import *
from handler import route
from ui_modules import UIModules
#from Template import TemplateLoader # For Jinja2


class App(tornado.web.Application):

    def __init__(self,handlers,conf,log):
        self.__version__ = conf['version']
        self.log = log
        settings = conf['app_settings']
        settings['default_handler_class'] = Page404Handler  # 404

        # Don't Support for Jinja2
        settings['ui_modules'] = UIModules
        tornado.web.Application.__init__(self, handlers, **settings)

        # Support for Jinja2
        #tpl_loader = TemplateLoader(settings['template_path'], False)
        #tornado.web.Application.__init__(self, handlers, template_loader=tpl_loader.Loader(), **settings)

        #每10秒执行一次
        #tornado.ioloop.PeriodicCallback(self.test, 1 * 10 * 1000).start()

        # Init Database
        self.db = db.DB(**conf['db'])

        #Init Redis
        R = RCache(**conf['redis'])
        self.redis = R.Connect()

        # Load Locale
        self.__load_locale(settings['default_lang'])


    #def test(self):
    #    self.log.info('Test')

    # Load Locale
    def __load_locale(self,default_lang):
        tornado.locale.load_translations('locale')
        tornado.locale.set_default_locale(default_lang)

class Torweb():

    def __init__(self,processes=4):
        self.__version__ = '1.0.4'
        self.host = config['host']
        self.port = config['port']
        self.urls = route
        self.config = config
        self.config['version'] = self.__version__
        self.log = gen_log
        if platform.system() == "Linux":  #根据操作系统类型来确定是否启用多线程
            self.processes = processes # 当processes>1时，PeriodicCallback定时任务会响相应的执行多次
        else:
            self.processes = 1
        self.log.info('Torweb %s' % self.__version__)  # 启动时打印版本号
        self.log.info('Listen Port: %s' % self.port)


    # 单进程模式
    def run(self):
        app = App(self.urls, self.config, self.log)
        app.listen(self.port)
        tornado.ioloop.IOLoop.current().start()


    # 多线程模式
    def run_multi(self):
        http_sockets = tornado.netutil.bind_sockets(self.port, self.host)
        tornado.process.fork_processes(num_processes=self.processes)
        http_server = tornado.httpserver.HTTPServer(request_callback=App(self.urls,self.config,self.log), xheaders=True)
        http_server.add_sockets(http_sockets)
        tornado.ioloop.IOLoop.instance().start()

@@ -208,16 +208,39 @@
 .locale%0A
+import tornado.options%0A
 import p
@@ -493,16 +493,302 @@
  Jinja2%0A
+from tornado.options import define, options%0A%0Adefine(%22host%22, default='0.0.0.0', help=%22Listen on the given IP%22, type=str)%0Adefine(%22port%22, default=8888, help=%22Run on the given port%22, type=int)%0A%0A# Call options.*** should be after the parse_command_line()%0Atornado.options.parse_command_line()
 %0A%0Aclass 
@@ -2046,22 +2046,20 @@
 t = 
-config%5B'
+options.
 host
-'%5D
 %0A   
@@ -2079,22 +2079,20 @@
 t = 
-config%5B'
+options.
 port
-'%5D
 %0A   

Improve runtime logic