Permalink
Browse files

Basic initial version of the Flask webassets integration.

  • Loading branch information...
0 parents commit c6377e6dd2ed68aefb676a3e18b70b64372932ab @miracle2k committed Aug 6, 2010
Showing with 171 additions and 0 deletions.
  1. +9 −0 .gitignore
  2. +27 −0 LICENSE
  3. +1 −0 MANIFEST.in
  4. +16 −0 TODO
  5. +1 −0 example/.gitignore
  6. +21 −0 example/app.py
  7. +3 −0 example/static/style1.css
  8. +3 −0 example/static/style2.css
  9. +9 −0 example/templates/index.html
  10. +42 −0 setup.py
  11. +1 −0 src/flaskext/__init__.py
  12. +38 −0 src/flaskext/assets.py
@@ -0,0 +1,9 @@
+*.pyc
+
+/BRANCH_TODO
+/flask-assets.wpr
+
+# distutils stuff
+/build/
+/dist/
+/src/Flask_Assets.egg-info/
27 LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) 2010, Michael Elsdörfer <http://elsdoerfer.name>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials
+ provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1 @@
+include LICENSE CHANGES *.py
16 TODO
@@ -0,0 +1,16 @@
+Flask-Script integration.
+
+We could potentially integrate better with Flask modules if we'd allow
+referencing static files from bundles the way you can reference templates:
+"module/file" would select "module/static/file".
+Some work is required in webassets to enable this. Possibly the right
+thing would be to implement general support for custom filesystems (like
+S3), and build this upon it.
+
+Maybe add support for automatic bundle-loading from assets.py files,
+Django-like. However, since we have no single, global environment, we'd
+prefer to collect all "Bundle" instances from a module, rather than
+attempting something like g.assets_env.register().
+
+Using loaders is currently somewhat verbose, it'd be more micro-framework-like
+if we could say "assets_env.load('yaml', ...)".
@@ -0,0 +1 @@
+/static/cached.css
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+import sys
+from os import path
+sys.path.insert(0, path.join(path.dirname(__file__), '../src'))
+
+from flask import Flask, render_template, url_for
+from flaskext.assets import Environment, Bundle
+
+app = Flask(__name__)
+
+assets = Environment(app, './static', '/static/')
+assets.register('main',
+ 'style1.css', 'style2.css',
+ output='cached.css', filters='cssmin')
+
+@app.route('/')
+def index():
+ return render_template('index.html')
+
+
+app.run(debug=True)
@@ -0,0 +1,3 @@
+h1 {
+ color: red;
+}
@@ -0,0 +1,3 @@
+h2 {
+ color: blue;
+}
@@ -0,0 +1,9 @@
+<html><head>
+{% assets "main" %}
+<link rel="stylesheet" href="{{ ASSET_URL }}" type="text/css" />
+{% endassets %}
+</head>
+<body>
+<h1>This should be red.</h1>
+<h2>This should be blue.</h2>
+</body>
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+# coding: utf8
+"""
+Flask-Assets
+-------------
+
+Integrates the ``webassets`` library with Flask, adding support for
+merging, minifying and compiling CSS and Javascript files.
+"""
+
+from setuptools import setup, find_packages
+
+
+setup(
+ name='Flask-Assets',
+ version='1.0',
+ url='github.com/miracle2k/flask-assets',
+ license='BSD',
+ author='Michael Elsdörfer',
+ author_email='michael@elsdoerfer.com',
+ description='Asset management for Flask, to compress and merge ' \
+ 'CSS and Javascript files.',
+ long_description=__doc__,
+ packages = find_packages('src'),
+ package_dir = {'': 'src'},
+ namespace_packages=['flaskext'],
+ zip_safe=False,
+ platforms='any',
+ install_requires=[
+ 'Flask',
+ 'webassets',
+ ],
+ classifiers=[
+ 'Environment :: Web Environment',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: BSD License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
+ 'Topic :: Software Development :: Libraries :: Python Modules'
+ ]
+)
@@ -0,0 +1 @@
+__import__('pkg_resources').declare_namespace(__name__)
@@ -0,0 +1,38 @@
+from webassets import Environment as RawEnvironment, Bundle
+from webassets.env import ConfigStorage
+
+
+__all__ = ('Environment', 'Bundle',)
+
+
+class FlaskConfigStorage(ConfigStorage):
+
+ _mapping = [
+ 'debug', 'cache', 'updater', 'auto_create', 'expire', 'directory', 'url',]
+
+ def _transform_key(self, key):
+ if key.lower() in self._mapping:
+ return "ASSETS_%s" % key.upper()
+ else:
+ return key.upper()
+
+ def __getitem__(self, key):
+ return self.env.app.config[self._transform_key(key)]
+
+ def __setitem__(self, key, value):
+ self.env.app.config[self._transform_key(key)] = value
+
+ def __delitem__(self, key):
+ del self.env.app.config[self._transform_key(key)]
+
+
+class Environment(RawEnvironment):
+
+ config_storage_class = FlaskConfigStorage
+
+ def __init__(self, app, directory, url):
+ self.app = app
+ super(Environment, self).__init__(directory, url)
+
+ self.app.jinja_env.add_extension('webassets.ext.jinja2.AssetsExtension')
+ self.app.jinja_env.assets_environment = self

0 comments on commit c6377e6

Please sign in to comment.