Created
March 12, 2012 21:22
-
-
Save garbados/2024760 to your computer and use it in GitHub Desktop.
"startproject"-like script for Flask, to make adding view files easier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys | |
class Setup: | |
def __init__(self, app_name): | |
self.app_name = app_name | |
self.cwd = os.path.join(os.getcwd(), app_name) | |
try: | |
print "Creating project folder..." | |
os.mkdir(app_name) | |
except OSError: | |
raise OSError("Project {app_name} already exists." | |
.format(app_name=self.app_name)) | |
def run(self): | |
run = """import flask | |
import {app_name}_views | |
app = flask.Flask(__name__) | |
app.register_blueprint(views.{app_name}) | |
if __name__ == '__main__': | |
app.run(debug=True) | |
""".format(app_name=self.app_name) | |
with open(os.path.join(self.cwd, 'run.py'),'w') as f: | |
f.truncate(0) | |
print "Creating run file..." | |
f.write(run) | |
def views(self): | |
views = """from flask import Blueprint, render_template, request | |
{app_name} = Blueprint('', __name__, template_folder='templates') | |
@{app_name}.route('/') | |
def index(): | |
print "Hello World!" | |
""".format(app_name=self.app_name) | |
file_name = "{app_name}_views.py".format(app_name = self.app_name) | |
with open(os.path.join(self.cwd, file_name),'w') as f: | |
f.truncate(0) | |
print "Creating views file..." | |
f.write(views) | |
def folders(self): | |
try: | |
print "Creating static folder..." | |
os.mkdir(os.path.join(self.cwd, 'static')) | |
except OSError: | |
print "Static folder already exists." | |
try: | |
print "Creating templates folder..." | |
os.mkdir(os.path.join(self.cwd, 'templates')) | |
except OSError: | |
print "Templates folder already exists." | |
def main(app_name): | |
app = Setup(app_name) | |
app.run() | |
app.views() | |
app.folders() | |
if __name__ == '__main__': | |
try: | |
main(sys.argv[1]) | |
except IndexError: | |
print "Must enter an app name." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment