import mimetypes
import os
from StringIO import StringIO
from flask import send_file, current_app

from gears.assets import build_asset
from gears.environment import Environment, DEFAULT_PUBLIC_ASSETS
from gears.exceptions import FileNotFound
from gears.finders import FileSystemFinder


class Gears(object):

    def __init__(self, app=None, defaults=True, assets_folder='assets',
                 compilers=None, compressors=None, public_assets=None,
                 extra_public_assets=None, cache=None):
        self.defaults = defaults
        self.assets_folder = assets_folder
        self.compilers = compilers
        self.compressors = compressors
        self.public_assets = public_assets
        self.extra_public_assets = extra_public_assets
        self.cache = None
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        app.extensions['gears'] = {}
        self.init_environment(app)
        self.init_asset_view(app)

    def init_environment(self, app):
        environment = Environment(
            root=self.get_static_folder(app),
            public_assets=self.get_public_assets(app),
            cache=self.get_cache(app),
        )
        if self.defaults:
            environment.register_defaults()
            environment.finders.register(self.get_default_finder(app))
        if self.compilers is not None:
            for extension, compiler in self.compilers.items():
                environment.compilers.register(extension, compiler)
        if self.compressors is not None:
            for mimetype, compressor in self.compressors.items():
                environment.compressors.register(mimetype, compressor)
        app.extensions['gears']['environment'] = environment

    def init_asset_view(self, app):
        app.extensions['gears']['static_view'] = app.view_functions['static']
        app.add_url_rule(app.static_url_path + '/<path:filename>',
                         endpoint='static', view_func=self.asset_view)

    def asset_view(self, filename):
        environment = current_app.extensions['gears']['environment']
        static_view = current_app.extensions['gears']['static_view']
        try:
            asset = build_asset(environment, filename)
        except FileNotFound:
            return static_view(filename)
        mimetype, encoding = mimetypes.guess_type(filename)
        return send_file(StringIO(asset), mimetype=mimetype, conditional=True)

    def get_environment(self, app):
        return app.extensions['gears']['environment']

    def get_default_finder(self, app):
        return FileSystemFinder(directories=(self.get_assets_folder(app),))

    def get_static_folder(self, app):
        return app.config.get('GEARS_ROOT', app.static_folder)

    def get_assets_folder(self, app):
        return os.path.join(app.root_path, self.assets_folder)

    def get_public_assets(self, app):
        if self.public_assets is not None:
            public_assets = tuple(self.public_assets)
        else:
            public_assets = DEFAULT_PUBLIC_ASSETS
        if self.extra_public_assets is not None:
            public_assets += tuple(self.extra_public_assets)
        return public_assets

    def get_cache(self, app):
        return self.cache

@@ -50,16 +50,46 @@
 tringIO%0A
+from functools import partial%0A
 from fla
@@ -120,16 +120,60 @@
 rent_app
+, url_for, request%0Afrom jinja2 import Markup
 %0A%0Afrom g
@@ -377,16 +377,125 @@
 ject):%0A%0A
+    css_template = '%3Clink rel=%22stylesheet%22 href=%22%7Burl%7D%22%3E'%0A    js_template = '%3Cscript src=%22%7Burl%7D%22%3E%3C/script%3E'%0A%0A
     def 
@@ -1151,24 +1151,258 @@
 _view(app)%0A%0A
+        @app.context_processor%0A        def gears_processor():%0A            return dict(%0A                css_tag=partial(self.html_tag, self.css_template),%0A                js_tag=partial(self.html_tag, self.js_template),%0A            )%0A%0A
     def init
@@ -2719,32 +2719,128 @@
 _view(filename)%0A
+        if request.args.get('body'):%0A            asset = asset.processed_source.encode('utf-8')%0A
         mimetype
@@ -2963,16 +2963,638 @@
 =True)%0A%0A
+    def html_tag(self, template, logical_path, debug=False):%0A        if debug or self.debug(current_app):%0A            environment = self.get_environment(current_app)%0A            asset = build_asset(environment, logical_path)%0A            urls = %5B%5D%0A            for requirement in asset.requirements:%0A                logical_path = requirement.attributes.logical_path%0A                url = url_for('static', filename=logical_path, body=1)%0A                urls.append(url)%0A        else:%0A            urls = (url_for('static', filename=logical_path),)%0A        return Markup('%5Cn'.join(template.format(url=url) for url in urls))%0A%0A
     def 
@@ -4357,32 +4357,32 @@
 che(self, app):%0A
-
         return s
@@ -4391,8 +4391,91 @@
 f.cache%0A
+%0A    def debug(self, app):%0A        return app.config.get('GEARS_DEBUG', app.debug)%0A

Add debug support