创建tornado小程序
# coding:utf8 import sys import os import re settings = """# -*- coding:utf-8 -*- import os base_path = os.path.dirname(__file__) setting = { "debug": True, "static_path": os.path.join(base_path, "static"), "template_path": os.path.join(base_path, "templates") } """ views = """# -*- coding:utf-8 -*- import tornado.web from tornado import gen from concurrent.futures import ThreadPoolExecutor from tornado.concurrent import run_on_executor # create your views hear class DemoHandler(tornado.web.RequestHandler): executor = ThreadPoolExecutor(32) @gen.coroutine def get(self, *args, **kwargs): result = yield self.demo() self.write(result) self.finish() @run_on_executor def demo(self): return "hello world!" """ urls = """# -*- coding:utf-8 -*- from tornado.web import Application from views import * from settings import setting from tornado.routing import ReversibleRuleRouter from tornado.web import url class Applications(Application, ReversibleRuleRouter): def __init__(self): handlers = [ url(r'/', DemoHandler, name="demo"), ] super(Applications, self).__init__(handlers=handlers, **setting) """ runserver = """# -*- coding:utf-8 -*- from tornado.options import define, options from tornado.ioloop import IOLoop from urls import Applications define("port", default=8888, type=int) define("address", default="127.0.0.1", type=str) if __name__ == '__main__': options.parse_command_line() app = Applications() app.listen(port=options.port, address=options.address) IOLoop.current().start() """ class CreateProject(object): def __init__(self): self.base_dir = os.path.dirname(os.path.abspath(__file__)) self.argument = "-h" if len(sys.argv) == 1 else sys.argv[1] self.__help__ = """操作:\n -p=project_name: 在当前路径创建。\n -f=/full_path/project_name: 在指定路径创建。""" self.project_name = "" self.project_dir = "" self.all_dir = [] self.settings = "settings.py" self.urls = "urls.py" self.views = "views.py" self.statics = "statics" self.templates = "templates" def __create_file__(self, filename, content): file_path = os.path.join(self.project_dir, filename) write_file = open(file_path, 'w') write_file.write(content) write_file.close() def __set_up__(self): os.makedirs(self.project_dir) os.makedirs(os.path.join(self.project_dir, self.templates)) os.makedirs(os.path.join(self.project_dir, self.statics)) self.__create_file__('views.py', views) self.__create_file__('settings.py', settings) self.__create_file__('urls.py', urls) self.__create_file__('runserver.py', runserver) def __project_name__(self): project = self.argument.split("=")[1] if not project: print "输入不能为空!" exit() check = re.compile('\W+') if check.search(project): print "输入有误(项目名称不能包含中文或特殊字符)请重新输入!" exit() self.project_name = project for root, dir, file in os.walk(self.base_dir): self.all_dir = dir break if self.project_name in self.all_dir: print "文件名已存在,请重新输入!" exit() self.project_dir = os.path.join(self.base_dir, self.project_name) try: self.__set_up__() except Exception as e: print e exit() def __full_path__(self): self.project_dir = self.argument.split('=')[1] if os.path.exists(self.project_dir): print "项目路径已存在,请重新输入!" exit() try: self.__set_up__() except Exception as e: print e exit() def main(self): if len(self.argument.split('=')) == 2: if '-p' in self.argument: self.__project_name__() elif '-f' in self.argument: self.__full_path__() else: print self.__help__ if __name__ == "__main__": CreateProject().main()
简单的自动创建tornado项目的小程序
本文来自博客园,作者:游走De提莫,转载请注明原文链接:https://www.cnblogs.com/Gaimo/p/12517842.html