/
coldbrew.py
96 lines (73 loc) · 3.22 KB
/
coldbrew.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from ..cache import get_cache_key, get_hexdigest, get_hashed_mtime
from .. import compile, get_string_from_path
from ..exceptions import ColdBrewCompileError
from django.conf import settings
from .. import settings as coldbrew_settings
from django.core.cache import cache
from django.template.base import Library, Node
import os
register = Library()
class InlineCoffeescriptNode(Node):
def __init__(self, nodelist):
self.nodelist = nodelist
def compile(self, source):
quiet, compile_result = compile(source)
if not quiet:
raise ColdBrewCompileError("Inline", compile_result)
else:
return compile_result
def render(self, context):
output = self.nodelist.render(context)
if coldbrew_settings.COFFEESCRIPT_USE_CACHE:
cache_key = get_cache_key(get_hexdigest(output))
cached = cache.get(cache_key, None)
if cached is not None:
return cached
output = self.compile(output)
cache.set(cache_key, output, coldbrew_settings.COFFEESCRIPT_CACHE_TIMEOUT)
return output
else:
return self.compile(output)
@register.tag(name="inlinecoffeescript")
def do_inlinecoffeescript(parser, token):
nodelist = parser.parse(("endinlinecoffeescript",))
parser.delete_first_token()
return InlineCoffeescriptNode(nodelist)
@register.simple_tag
def coffeescript(source_file_path):
filename = os.path.split(source_file_path)[-1]
if filename.endswith(".coffee"):
base_filename = filename[:-7]
else:
base_filename = filename
full_path = os.path.join(coldbrew_settings.COFFEESCRIPT_LOCATION, source_file_path)
hashed_mtime = get_hashed_mtime(full_path)
# TODO: Resolve #7 and fix this.
output_parent = settings.STATICFILES_DIRS[0]
output_directory = os.path.join(output_parent, coldbrew_settings.COFFEESCRIPT_OUTPUT_DIR)
output_path = os.path.join(output_directory, "%s-%s.js" % (base_filename, hashed_mtime))
# Now we know to which path we'll write; let's just make the URL.
url = "%s/%s-%s.js" % (settings.COFFEESCRIPT_OUTPUT_DIR,
base_filename,
hashed_mtime
)
# If the file already exists, we're not going to even bother reading the input again.
# Instead, just return the URL.
if os.path.exists(output_path):
return url
coffeescript_string = get_string_from_path(full_path)
quiet, compile_result = compile(coffeescript_string)
if not quiet:
raise ColdBrewCompileError(full_path, compile_result)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
compiled_file = open(output_path, "w+")
compiled_file.write(compile_result)
compiled_file.close()
# If DEBUG is on, we want to see if a staticfiles directory is at the beginning
# of our output_path. If it is, we know to use that path instead of STATIC_ROOT.
if settings.DEBUG:
for static_dir in settings.STATICFILES_DIRS:
if output_path.startswith(static_dir):
return output_path[len(static_dir):].replace(os.sep, '/').lstrip("/")
return url