Skip to content

Commit

Permalink
Add falcon framework to initpy.
Browse files Browse the repository at this point in the history
  • Loading branch information
sinwoobang committed Mar 19, 2015
1 parent a282072 commit 9cc50bb
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
51 changes: 50 additions & 1 deletion initpy/creator.py
Expand Up @@ -4,7 +4,7 @@
import inspect
import os

from initpy.templates import blank, flask, tornado_web
from initpy.templates import blank, falcon, flask, tornado_web
from initpy.exceptions import RootPathDoesNotExists


Expand Down Expand Up @@ -125,3 +125,52 @@ def create_project(self, name, module):

self.create_handlers(project_path, module)
self.create_requirements(project_path)


class FalconCreator(Creator):

def create_app(self, _path, module):
args = dict(module=module, module_title=module.title())

self.create_module(_path, "app",
falcon.app_init.safe_substitute(args))
app_path = os.path.join(_path, "app")

self.create_middleware(app_path)
self.create_models(app_path, module)
self.create_app_module(app_path, module)

def create_app_module(self, _path, name):
args = dict(module=name, module_title=name.title())

self.create_folder(_path, 'resources')
module_path = os.path.join(_path, 'resources')

self.create_file(module_path, "__init__.py",
falcon.resource_init.safe_substitute(args), False)
self.create_file(module_path, "{}.py".format(name),
falcon.resource_controller.safe_substitute(args))

def create_models(self, _path, name):
self.create_module(_path, "models")
models_path = os.path.join(_path, "models")
self.create_file(models_path, "__init__.py", blank.blank)
self.create_file(models_path, "{}.py".format(name), blank.blank)

def create_middleware(self, _path):
self.create_module(_path, "middleware")
middleware_path = os.path.join(_path, "middleware")
self.create_file(middleware_path, "__init__.py", blank.blank)

def create_requirements(self, _path):
self.create_folder(_path, "requirements")
self.create_file(os.path.join(_path, "requirements"), "dev.txt",
falcon.requirements)

def create_project(self, name, module):
self.create_folder(self.root_path, name)
project_path = os.path.join(self.root_path, name)

self.create_file(project_path, "manage.py", falcon.manager)
self.create_app(project_path, module)
self.create_requirements(project_path)
7 changes: 6 additions & 1 deletion initpy/run.py
Expand Up @@ -10,6 +10,7 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument('--flask', '-f', action='store_true')
parser.add_argument('--tornado-web', '-tw', action='store_true')
parser.add_argument('--falcon', '-fc', action='store_true')
parser.add_argument('name', metavar='name', type=str)
args = parser.parse_args()

Expand All @@ -20,7 +21,7 @@ def main():
parser.print_help()
return

if args.flask or args.tornado_web:
if args.flask or args.tornado_web or args.falcon:
end_message = "Complete!\nYou can install "
end_message += "\"pip install -r requirements/dev.txt\""

Expand All @@ -32,6 +33,10 @@ def main():
end_message += "\nYou can run \"python app.py\""
from initpy.creator import TornadoCreator
creator = TornadoCreator(getcwd())
elif args.falcon:
end_message += "\nYou can run \"python manage.py\""
from initpy.creator import FalconCreator
creator = FalconCreator(getcwd())

from initpy.prompt import color_input
module = color_input('Please input base module name [common]: ',
Expand Down
73 changes: 73 additions & 0 deletions initpy/templates/falcon.py
@@ -0,0 +1,73 @@
#!/usr/bin/python
# -*- coding:utf-8 -*-
from string import Template


app_init = Template("""
#!/usr/bin/python
# -*- coding:utf-8 -*-
import falcon
# from .middleware import *
from .resources import ${module_title}Resource
${module}Resource = ${module_title}Resource()
def create_app():
app = falcon.API(middleware=[])
app.add_route('/', ${module}Resource)
return app
""".strip())


manager = """
#!/usr/bin/python
# -*- coding: utf-8 -*-
from wsgiref import simple_server
from app import create_app
# Set up falcon api
app = application = create_app()
if __name__ == '__main__':
httpd = simple_server.make_server('127.0.0.1', 5000, app)
httpd.serve_forever()
""".strip()


resource_init = Template("""
#!/usr/bin/python
# -*- coding:utf-8 -*-
from .${module} import ${module_title}Resource
""".strip())


resource_controller = Template("""
#!/usr/bin/python
# -*- coding:utf-8 -*-
import falcon
class ${module_title}Resource(object):
def __init__(self):
pass
def on_get(self, req, resp):
resp.status = falcon.HTTP_200
resp.body = 'Server works!'
def on_post(self, req, resp):
resp.status = falcon.HTTP_200
resp.body = 'Server works!'
""".strip())


requirements = """
falcon
""".strip()

0 comments on commit 9cc50bb

Please sign in to comment.