Skip to content

Commit

Permalink
using templating for most html-based tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
bollwyvl committed Oct 20, 2012
1 parent d729f0f commit bad0462
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 262 deletions.
File renamed without changes.
74 changes: 74 additions & 0 deletions blockd3_tests/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
import os
from os.path import join as opj
from os.path import abspath as opa

from flask import (
Flask,
)
from flask import (
render_template,
)

def make_app(env="dev"):

DEBUG = True #= env == "dev"

url_root = {
"dev": "/",
"prod": "/dist/"
}[env]

app_home = os.path.dirname(__file__)

cfg = {
"dev": dict(
static_url_path="",
template_folder="..",
static_folder=opa(opj(app_home, ".."))
),
"prod": dict(
static_url_path="/dist",
template_folder="..",
static_folder=opa(opj(app_home, "..", "dist"))
)
}[env]

app = Flask(__name__, **cfg)
app.config['CSRF_ENABLED'] = DEBUG
app.config['SECRET_KEY'] = "totally-insecure"
app.config['DEBUG'] = DEBUG

@app.route(url_root)
def home():
return render_template("index.html",
env=env,
**assets())

@app.route(url_root + "frame/")
def frame():
return render_template("frame/index.html",
env=env,
**assets("../frame"))

return app

def assets(for_file="../"):
result = dict(scripts=[], styles=[])
for a_type in result.keys():
a_list = opj(
os.path.dirname(__file__),
for_file,
a_type + ".txt")
if os.path.exists(a_list):
result[a_type] = [
asset.strip()
for asset
in open(a_list).read().split("\n")
if asset.strip() and not asset.strip().startswith("#")
]
return result

if __name__ == "__main__":
app = make_app()
app.run()
File renamed without changes.
3 changes: 2 additions & 1 deletion tests/run.py → blockd3_tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import logging

from ghost import GhostTestCase, Ghost
from app import app
from app import make_app

app = make_app()

PORT = 5000

Expand Down
10 changes: 0 additions & 10 deletions css/frame.css

This file was deleted.

12 changes: 12 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
html, body {
background-color: #fff;
margin: 0;
padding:0;
overflow: hidden;
}

.blocklySvg {
height: 100%;
width: 100%;
}

body#editor{
overflow-y: scroll;
overflow-x: hidden;
Expand Down
120 changes: 57 additions & 63 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
"""

import os
import sys
from pprint import pprint
import json
import re

from fabric.api import task

from bs4 import BeautifulSoup

import sh

PROJECT_ROOT = str(sh.git("rev-parse", **{"show-toplevel": True})).strip()
Expand All @@ -39,7 +38,7 @@ def proj():
def flake():
print(". running PyFlakes on test equipment")
proj()
sh.pyflakes("setup.py", "fabfile.py", "tests/")
sh.pyflakes("setup.py", "fabfile.py", "blockd3_tests/")


@task
Expand All @@ -52,6 +51,8 @@ def build():
proj()
flake()
clean()
copy_assets()
html()
minify()
favicon()
sh.cd("dist")
Expand Down Expand Up @@ -135,11 +136,10 @@ def favicon():
print(".. cleaning up")
sh.rm(sh.glob("/tmp/favicon-*.png"))


@task
def minify():
def copy_assets():
proj()
print(". minifying ...")
print(". copying assets ...")
copy_patterns = {
"dist/lib/blockly": ["lib/blockly/blockly.css"],
"dist/lib/blockly/media": sh.glob("lib/blockly/media/*") or [],
Expand All @@ -157,54 +157,49 @@ def minify():
for c_file in copy_files:
print "... copying", c_file, dst
sh.cp("-r", c_file, dst)

html = ["index.html", "frame.html"]

sources = dict(js=[], css=[])

for html_file in sh.glob("*.html"):
print ".. analyzing %s" % html_file
anything = re.compile(".+")
html = open(html_file).read()
soup = BeautifulSoup(html)

for link in soup.find_all("link", rel="stylesheet", href=anything):
sources["css"] += [link["href"]]

for script in soup.find_all("script", src=anything):
sources["js"] += [script["src"]]

new_file = open(os.path.join("dist", html_file), "w")

def _html_replace(html, THING, new_thing):
tmpl = "<!--%s%s-->"
t_start = html.find(tmpl % ("###", THING))
t_end = html.find(tmpl % (THING, "###"))

if -1 in (t_start, t_end):
return html

return "%s%s%s" % (
html[0:t_start],
new_thing,
html[t_end:]
)

replacements = dict(
STYLES="""<link rel="stylesheet" href="./css/blockd3-min.css">""",
SCRIPTS="""<script type="text/javascript"
src="./js/blockd3-min.js"></script>""",
THEMES=asset_links("THEMES"),
EXAMPLES=asset_links("EXAMPLES"),
)
@task
def html():
proj()
print ". generating production html"
sh.mkdir("-p", "dist/frame")

blockd3_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(blockd3_path)

for thing, new_thing in replacements.items():
html = _html_replace(html, thing, new_thing)
from blockd3_tests.app import make_app
app = make_app("prod")

prod_files = {
"dist/index.html": "/dist/",
"dist/frame/index.html": "/dist/frame/"
}

for prod_file, url in prod_files.items():
open(prod_file, "w").write(
app.test_client().get(url).data
)

print ".. writing out production dist/%s" % html_file
new_file.write(html)
new_file.close()
@task
def minify():
proj()
print(". minifying ...")

sources = dict(js=[], css=[])

for user in ["./frame", "."]:
for min_id, asset_list in dict(js="scripts", css="styles").items():
asset_list = os.path.join(user, asset_list+".txt")
if os.path.exists(asset_list):
[
sources[min_id].append(asset)
for asset
in [x.strip() for x in open(asset_list).read().split("\n")]
if asset
and not asset.startswith("#")
and asset not in sources[min_id]
]

cfg = open("setup.cfg", "w")

cfg.writelines([
Expand Down Expand Up @@ -249,18 +244,17 @@ def asset_links(asset_type):
])

@task
def serve():
"""
Mostly deprecated, but useful for "full stack" debugging
"""
import SimpleHTTPServer
import SocketServer
def serve_dev():
serve("dev")

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)
@task
def serve_prod():
serve("prod")

def serve(env):
blockd3_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(blockd3_path)

print "serving at port", PORT
httpd.serve_forever()
from blockd3_tests.app import make_app
app = make_app(env)
app.run(port=[5000, 5001][env=="prod"])
Loading

0 comments on commit bad0462

Please sign in to comment.