jackyzm

导航

自动化创建tornado项目

tornado目录结构:

index.py             入口文件

app               app目录

  |___  __init__.py      初始化脚本

  |___  templates        模板目录

  |          |___ index.html  模板

  |___  static          静态目录

  |___  configs.py       配置文件

    |___  urls.py        路由规则

  |___  home        视图目录

  |     |___ __init__.py  初始化文件

  |___  view.py        视图脚本

  

引用视频教程:http://study.163.com/course/courseLearn.htm?courseId=1003852044#/learn/video?lessonId=1004567012&courseId=1003852044

如下代码还有部分问题未解决,会抓紧调试

import os
os.system('pip3 install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com fabric')
os.system('pip3 install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com tornado')
from fabric.api import *
from fabric.colors import *

def index_py_s():
    content = """#coding:utf8
from app import app
if __name__ == "__main__":
    app()
"""

    return content

def server_py_s(port):
    content = """#coding:utf8
import tornado.web
import tornado.ioloop
import tornado.options
import tornado.httpserver

from tornado.options import options,define
from configs import configs
from urls import urls
define('port',default=%d,type=int)

class CustomAppLication(tornado.web.Application):
    def __init__(self,configs,urls):
        settings = configs
        handlers = urls
        super(CustomApplication,self).__init__(handlers=handlers,**settings)
def create_app():
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(CustomApplicantion(configs,urls))
    http_server.listen(options.port)
    tornado.ioloop.IOLloop.instance().start()
app = create_app
    """ % int(port)
    return content
def urls_py_s():
    content = """#coding:utf8
from home.views import IndexHandler as home_index

urls = [
    (r"/",home_index)]
    """
    return content

def configs_py_s():
    import uuid
    content = """#coding:utf8
import os
root = os.path.dirname(__file__)
configs = dict(
    template_path = os.path.join(root,'templates'),
    static_path = os.path.join(root,'static'),
    debug = True,
    xscrf_cookies = True,
    cookie_secret = '%s'
    )
    """%str(uuid.uuid4().hex)

    return content

def views_py_s():
    content = """#coding:utf8
import tornado.web

class IndexHandler(tornado.web.RequetHandler):
    def get(self):
        self.render('index.html',title = 'hello world !')
    """
    return content

def index_html_s():
    content = """
    <h1>{{ title }}<h1>
    """
    return content 


@task
def mk_web():
    dir_name = prompt(green('please input project name:'))
    local('mkdir %s' % dir_name)
    with lcd(dir_name):
        local('touch index.py')
        local("echo '%s' >> index.py" % index_py_s())
        local('mkdir app')
        with lcd ('app'):
            local('touch __init__.py')
            port = prompt(cyan('please input project port:'))
            local("echo '%s' >> __init__.py" % server_py_s(port))
            local('touch urls.py')
            local("echo '%s' >> urls.py" % urls_py_s())
            local('touch configs.py')
            local("echo '%s'>> configs.py" % configs_py_s())
            local('mkdir home')
            with lcd('home'):
                local('touch __init__.py')
                local('touch views.py')
                local("echo '%s' >> views.py" % views_py_s())
            local('mkdir templates')
            with lcd('templates'):
                local('touch index.html')
                local("echo '%s' >> index.html" % index_html_s())
            local('mkdir static')

        local('python3 index.py')
if __name__ == '__main__':
    execute(mk_web)

 

posted on 2018-06-01 09:37  jackyzm  阅读(355)  评论(0编辑  收藏  举报