Skip to content

Instantly share code, notes, and snippets.

@sheenobu
Created June 14, 2012 18:46
Show Gist options
  • Save sheenobu/2932141 to your computer and use it in GitHub Desktop.
Save sheenobu/2932141 to your computer and use it in GitHub Desktop.
Flaskr - utility building modular flask applications. Prototype/v0.0.1
#!/usr/bin/env python
# simple command line application and utility library for building modular flask applications.
# Just drop me in your root, chmod +x ./testserver.py, and ./flaskr.py init
# then you can chmod +x ./testserver.py and ./testserver.py to run it.
# finally create new modules using ./flaskr new module-name and add
# the name to mods.py.
# v0.0.1
def build_flask(config):
from flask import Flask
app = Flask(__name__)
app.config.update(config)
modloader(app,config)
return app
class modloader(object):
mods = {}
def __init__(self,app,config):
self.app = app
self.config = config
self.app.modloader = self
if 'modules' in config:
for x in config['modules']:
self.find_and_add(x)
def find_and_add(self,name):
if len(name.split(" ")) == 1:
self.mods[name] = __import__(name,fromlist=['build']).build(self.app,self.config)
self.app.register_blueprint(self.mods[name])
else:
cfg = name.split(" ")
mtype = cfg[1]
name = cfg[0]
url = '/' if len(cfg) == 2 else cfg[2]
self.mods[name] = __import__(name,fromlist=['build']).build(self.app,self.config)
self.app.register_blueprint(self.mods[name],url_prefix=url)
def add(self,name,mod):
self.mods[name] = mod
def get(self,name):
if name in self.mods:
return self.mods[name]
else:
self.find_and_add(name)
return self.mods[name]
if __name__ == '__main__':
import sys
if len(sys.argv) == 1:
print "usage: flaskr <command> <args>"
print "\tinit - create a new project in the current directy"
print "\tnew <name> - create a new flask blueprint"
exit()
cmd = sys.argv[1]
if cmd == 'init':
from string import Template
test_file = Template("""#!/usr/bin/env python
from werkzeug.serving import run_simple
from flaskr import build_flask
config = {
"SECRET_KEY":"asdf",
"DEBUG":"true",
"modules":[
]
}
import mods
config["modules"] = [x for x in mods.MODULES('test')]
run_simple(
'localhost',
5000,
build_flask(config),
use_reloader=True,
use_debugger=True,
use_evalex=True)
""")
open("testserver.py","w").write(test_file.substitute())
mod_file = Template("""
def MODULES(environment):
if environment == 'test':
yield "testmodule"
yield "mod1"
""")
open("mods.py","w").write(mod_file.substitute())
if cmd == 'new':
name = sys.argv[2]
import os
os.mkdir(name)
os.mkdir(name + "/templates")
from string import Template
init_file = Template("""
from flask import Blueprint
def build(app,config):
${name} = Blueprint("${name}",__name__,template_folder ='templates')
from views import build_views
build_views(app,${name},config)
return ${name}
""")
view_file = Template("""
def build_views(app,${name},config):
@${name}.route("/${name}")
def ${name}_view():
return "Sample View"
return {}
""")
open(name + "/__init__.py","w").write(init_file.substitute(name = name))
open(name + "/views.py","w").write(view_file.substitute(name = name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment