# 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() """ classCreateProject (Object): DEF the __init__ (Self): self.base_dir = os.path.dirname (os.path.abspath with ( __FILE__ )) self.argument = " -H " IF len (the sys.argv) ==. 1 the else SYS .argv [. 1 ] Self. __help__ = "" " operation: \ n -p = project_name: created in the current path \ n -f = / full_path / project_name :.. created in the specified path " "" 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 " ! input can not be empty " Exit () Check = the re.compile ( ' \ W is + ' ) IF check.search (Project): Print " entered incorrectly (project name can not contain Chinese or special characters) Please re-enter! " 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 " file name already exists, please re-enter! " Exit () self.project_dir = os.path.join (self.base_dir, self.project_name) the try : Self. __set_up__ () the 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 " project path already exists, please re-enter! " Exit () the try : . Self __set_up__ () the 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()
Simple small program to automatically create projects tornado