Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
new StaticInline class
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe committed Apr 22, 2013
1 parent 31397e6 commit dd8f04f
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions tornado_utils/tornado_static.py
Expand Up @@ -16,6 +16,7 @@
import stat
import marshal
import warnings
from cStringIO import StringIO
from time import time
from tempfile import gettempdir
from base64 import encodestring
Expand Down Expand Up @@ -69,15 +70,17 @@ def save_name_conversion():
class StaticURL(tornado.web.UIModule):

def render(self, *static_urls, **options):
# the following 4 lines will have to be run for every request. Since
# it's just a basic lookup on a dict it's going to be uber fast.
basic_name = ''.join(static_urls)
already = _name_conversion.get(basic_name)
if already:
cdn_prefix = self.handler.get_cdn_prefix()
if cdn_prefix:
already = cdn_prefix + already
return already
return_inline = options.get('return_inline', False)
if not return_inline:
# the following 4 lines will have to be run for every request. Since
# it's just a basic lookup on a dict it's going to be uber fast.
basic_name = ''.join(static_urls)
already = _name_conversion.get(basic_name)
if already:
cdn_prefix = self.handler.get_cdn_prefix()
if cdn_prefix:
already = cdn_prefix + already
return already

new_name = self._combine_filename(static_urls)
# If you run multiple tornados (on different ports) it's possible
Expand All @@ -99,13 +102,18 @@ def render(self, *static_urls, **options):

n, ext = os.path.splitext(new_name)
new_name = "%s.%s%s" % (n, youngest, ext)

optimization_done = False
if os.path.isfile(new_name):
if not return_inline and os.path.isfile(new_name):
# conversion and preparation has already been done!
# No point doing it again, so just exit here
pass
else:
destination = file(new_name, 'w')
if return_inline:
destination = StringIO()
else:
destination = file(new_name, 'w')

if options.get('dont_optimize'):
do_optimize_static_content = False
else:
Expand All @@ -121,8 +129,7 @@ def render(self, *static_urls, **options):
.settings.get('YUI_LOCATION')

for full_path in full_paths:
f = open(full_path)
code = f.read()
code = open(full_path).read()
if full_path.endswith('.js'):
if len(full_paths) > 1:
destination.write('/* %s */\n' % os.path.basename(full_path))
Expand Down Expand Up @@ -169,10 +176,15 @@ def render(self, *static_urls, **options):
else:
# this just copies the file
pass

destination.write(code)
destination.write("\n")
if not return_inline:
destination.close()

if return_inline:
return destination.getvalue()

destination.close()
prefix = self.handler.settings.get('combined_static_url_prefix', '/combined/')
new_name = os.path.join(prefix, os.path.basename(new_name))
_name_conversion[basic_name] = new_name
Expand All @@ -187,7 +199,6 @@ def render(self, *static_urls, **options):
new_name = cdn_prefix + new_name
return new_name


def _combine_filename(self, names, max_length=60):
# expect the parameter 'names' be something like this:
# ['css/foo.css', 'css/jquery/datepicker.css']
Expand Down Expand Up @@ -276,6 +287,25 @@ def render(self, *static_urls, **options):
return template % dict(url=url)


class StaticInline(StaticURL):
"""given a list of static resources, return the whole HTML tag"""
def render(self, *static_urls, **options):
extension = static_urls[0].split('.')[-1]
if extension == 'css':
template = '<style type="text/css">%s</style>'
elif extension == 'js':
template = '<script type="text/javascript" '
if options.get('defer'):
template += 'defer '
elif options.get('async'):
template += 'async '
template += '>%s</script>'
else:
raise NotImplementedError
code = super(StaticInline, self).render(*static_urls, return_inline=True)
return template % code


def run_closure_compiler(code, jar_location, verbose=False): # pragma: no cover
if verbose:
t0 = time()
Expand Down

0 comments on commit dd8f04f

Please sign in to comment.