def render_style(self, style_path, force=False, **kwargs):
"""
Renders the CSS template at *style_path* using *kwargs* and returns the
path to the rendered result. If the given style has already been
rendered the existing cache path will be returned.
If *force* is ``True`` the stylesheet will be rendered even if it
already exists (cached).
This method also cleans up older versions of the same rendered template.
"""
cache_dir = self.settings['cache_dir']
if not isinstance(cache_dir, str):
cache_dir = cache_dir.decode('utf-8')
if not isinstance(style_path, str):
style_path = style_path.decode('utf-8')
mtime = os.stat(style_path).st_mtime
shortened_path = short_hash(style_path)
rendered_filename = 'rendered_%s_%s' % (shortened_path, int(mtime))
rendered_path = os.path.join(cache_dir, rendered_filename)
if not os.path.exists(rendered_path) or force:
style_css = self.render_string(
style_path,
**kwargs
)
# NOTE: Tornado templates are always rendered as bytes. That is why
# we're using 'wb' below...
with io.open(rendered_path, 'wb') as f:
f.write(smart_bytes(style_css))
# Remove older versions of the rendered template if present
for fname in os.listdir(cache_dir):
if fname == rendered_filename:
continue
elif shortened_path in fname:
# Older version present.
# Remove it (and it's minified counterpart).
os.remove(os.path.join(cache_dir, fname))
return rendered_path
评论列表
文章目录