Skip to content

Commit

Permalink
glad: Added a landing page after generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Jul 29, 2015
1 parent bf5b7d0 commit 04362f4
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.pyc
__pycache__
cache
temp
24 changes: 23 additions & 1 deletion gladweb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from flask import Flask, g
from flask.ext.autoindex import AutoIndexBlueprint
import werkzeug
from gladweb.metadata import Metadata
import gladweb.util
import logging
import sys
import os


class LevelFilter(logging.Filter):
Expand Down Expand Up @@ -63,6 +66,9 @@ def create_application(debug=False, verbose=False):
app.config.from_object('gladweb.config')
app.debug = debug

if not os.path.exists(app.config['TEMP']):
os.makedirs(app.config['TEMP'])

if verbose or (not app.debug and verbose is None):
setup_logging()

Expand All @@ -74,7 +80,23 @@ def before_request():
from gladweb.views.index import index
app.register_blueprint(index)

from gladweb.views.generated import generated
idx = AutoIndexBlueprint(generated, browse_root=app.config['TEMP'], add_url_rules=False)

@generated.route('/<root>/')
@generated.route('/<root>/<path:path>')
def autoindex(root, path='.'):
root = werkzeug.utils.secure_filename(root)
return idx.render_autoindex(
path,
browse_root=os.path.join(app.config['TEMP'], root),
template='autoindex.html',
template_context={'root': root}
)

app.register_blueprint(generated, url_prefix='/generated')

app.jinja_env.filters['pretty_date'] = gladweb.util.pretty_date
app.jinja_env.globals.update(glad_version=get_glad_version())

return app
return app
2 changes: 2 additions & 0 deletions gladweb/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# A cache, which will be used to store/retrieve various files.
CACHE = gladweb.cache.FileCache(os.path.join(base_path, 'cache'))

# Path to a folder which will be used to store generation results
TEMP = os.path.join(base_path, 'temp')

try:
from local_config import *
Expand Down
102 changes: 99 additions & 3 deletions gladweb/static/glad.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


@media (min-width: 550px) {
.header {
margin-top: 3rem;
Expand Down Expand Up @@ -50,4 +48,102 @@ footer {
border-top: 1px solid grey;
font-size: 80%;
text-transform: uppercase;
}
}

/* Auto Index */

.aidx a:link, .aidx a:visited {
text-decoration: none;
}

.aidx a:hover {
text-decoration: underline;
}

.breadcrumb {
padding: 0;
}

.breadcrumb a {
margin: 0;
font-size: 12px;
}

.breadcrumb img {
vertical-align: middle;
}

.breadcrumb .sep {
color: #bbb;
}

.breadcrumb h1 {
font-size: 12px;
font-weight: normal;
padding: 5px;
margin: 0;
border-top: 1px solid #ccc;
}

table {
border-collapse: collapse;
width: 100%;
background: #fff;
}

th a:link, th a:visited {
margin: 0 10px;
color: #333;
}

th img {
position: absolute;
margin-left: -6px;
}

tbody {
border: solid #ccc;
border-width: 1px 0;
}

hr {
margin: 0;
border: none;
}

td {
padding: 5px;
font-size: 11px;
}

td a {
margin-right: 40px;
font-size: 14px;
}

td.modified {
text-align: center;
}

td.size {
text-align: right;
}

.icon, .name {
border: none;
}

.icon {
width: 16px;
padding-right: 0;
}

.modified {
width: 130px;
}

.size {
width: 60px;
}


47 changes: 47 additions & 0 deletions gladweb/templates/autoindex.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% extends '__autoindex__/autoindex.html' %}
{% import 'macros.html' as macros with context %}


{% block meta %}
<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="{{ url_for('static', filename='normalize.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='multi-select.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='skeleton.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='glad.css') }}">
{% endblock %}


{% block header %}
<div class="container">
<header class="header">
<h3>Glad</h3><h5>Generated files. <small>These files are not permanent!</small></h5>
</header>
{% endblock %}


{% block table %}
<div class="aidx">
<table>
<thead>
{{ macros.thead() }}
{% if not curdir.is_root() %}
<tr>
<td class="breadcrumb" colspan="4">
<h1>{{ macros.breadcrumb(curdir) }}</h1>
</td>
</tr>
{% endif %}
</thead>
<tbody>
{% for ent in entries %}
{{ macros.entry(ent) }}
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}


{% block footer %}
</div>
{% endblock %}
70 changes: 70 additions & 0 deletions gladweb/templates/macros.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{% macro entry(ent) %}
<tr>
{% set icon = ent.guess_icon() %}
<td class="icon">
{% if icon %}
<img src="{{ icon }}" />
{% endif %}
</td>
<td class="name">
<a href="{{ url_for(endpoint, root=root, path=ent.path) }}">
{%- if ent.name == ".." -%}
Parent folder
{%- else -%}
{{ ent.name }}
{%- endif -%}
</a></td>
<td class="modified">
<time datetime="{{ ent.modified }}">{{ ent.modified }}</time>
</td>
<td class="size">
{% if ent.size %}
{{ ent.size|filesizeformat }}
{% else %}
-
{% endif %}
</td>
</tr>
{% endmacro %}

{% macro th(key, label, colspan=1) %}
<th class="{{ key }}" colspan="{{ colspan }}">
{%- if sort_by == key and order > 0 -%}
<a href="?sort_by={{ key }}&amp;order=desc">{{ label }}</a>
{%- else -%}
<a href="?sort_by={{ key }}">{{ label }}</a>
{%- endif -%}
{%- if sort_by == key -%}
{%- if order > 0 -%}
<img src="{{ url_for('__autoindex__.static', filename='asc.gif') }}" alt="ASC" />
{%- elif order < 0 -%}
<img src="{{ url_for('__autoindex__.static', filename='desc.gif') }}" alt="DESC" />
{%- endif -%}
{%- endif -%}
</th>
{% endmacro %}

{% macro thead() %}
<tr>
{{ th("name", "Name", 2) }}
{{ th("modified", "Last modified") }}
{{ th("size", "Size") }}
</tr>
{% endmacro %}

{% macro breadcrumb(ent) %}
{% set parent = ent.parent %}
{% if parent %}
{{ breadcrumb(parent) }}
<span class="sep">&raquo;</span>
{% endif %}
<a href="{{ url_for(endpoint, root=root, path=ent.path) }}">
{% set icon = ent.guess_icon() %}
{% if icon %}
<img src="{{ icon }}" />
{% endif %}
{% if not ent.is_root() %}
{{ ent.name }}
{% endif %}
</a>
{% endmacro %}
9 changes: 9 additions & 0 deletions gladweb/views/generated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from flask import Blueprint, abort


generated = Blueprint('generated', __name__)


# @generated.route('/')
# def landing():
# return abort(404)
29 changes: 15 additions & 14 deletions gladweb/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ def validate_form():
return language, specification, profile, apis_parsed, extensions, loader


def write_dir_to_zipfile(path, zipf):
def write_dir_to_zipfile(path, zipf, exclude=None):
if exclude is None:
exclude = []

for root, dirs, files in os.walk(path):
for file_ in files:
if file_ in exclude:
continue

zipf.write(
os.path.join(root, file_),
os.path.relpath(os.path.join(root, file_), path)
Expand All @@ -84,32 +90,27 @@ def glad_generate():

glad.lang.c.generator.KHRPLATFORM = g.cache.get_khrplatform()

directory = tempfile.mkdtemp()
directory = tempfile.mkdtemp(dir=current_app.config['TEMP'])
with generator_cls(directory, spec, apis, loader) as generator:
generator.generate(extensions)

try:
fobj = io.BytesIO()
zip_path = os.path.join(directory, 'glad.zip')
with open(zip_path, 'w') as fobj:
zipf = zipfile.ZipFile(fobj, mode='w')
write_dir_to_zipfile(directory, zipf)
write_dir_to_zipfile(directory, zipf, exclude=['glad.zip'])
zipf.close()
finally:
shutil.rmtree(directory)

fobj.seek(0, io.SEEK_SET)
return fobj
return url_for('generated.autoindex', root=os.path.split(directory)[1])


@index.route('/generate', methods=['POST'])
def generate():
try:
fobj = glad_generate()
url = glad_generate()
except Exception, e:
current_app.logger.exception(e)
current_app.logger.error(request.form)
flash(e.message, category='error')
return redirect(url_for('index.landing'))

return send_file(
fobj, mimetype='application/octet-stream',
as_attachment=True, attachment_filename='glad-generated.zip'
)
return redirect(url)
Empty file added temp/.gitkeep
Empty file.

0 comments on commit 04362f4

Please sign in to comment.