Fixed admin template bug: load before extends.
[wolnelektury.git] / apps / compress / utils.py
1 import os
2 import re
3 import tempfile
4
5 from django.conf import settings as django_settings
6 from django.utils.http import urlquote
7 from django.dispatch import dispatcher
8
9 from compress.conf import settings
10 from compress.signals import css_filtered, js_filtered
11
12 def get_filter(compressor_class):
13     """
14     Convert a string version of a function name to the callable object.
15     """
16
17     if not hasattr(compressor_class, '__bases__'):
18
19         try:
20             compressor_class = compressor_class.encode('ascii')
21             mod_name, class_name = get_mod_func(compressor_class)
22             if class_name != '':
23                 compressor_class = getattr(__import__(mod_name, {}, {}, ['']), class_name)
24         except (ImportError, AttributeError):
25             raise Exception('Failed to import filter %s' % compressor_class)
26
27     return compressor_class
28
29 def get_mod_func(callback):
30     """
31     Converts 'django.views.news.stories.story_detail' to
32     ('django.views.news.stories', 'story_detail')
33     """
34
35     try:
36         dot = callback.rindex('.')
37     except ValueError:
38         return callback, ''
39     return callback[:dot], callback[dot+1:]
40
41 def needs_update(output_file, source_files):
42     """
43     Scan the source files for changes and returns True if the output_file needs to be updated.
44     """
45
46     mtime = max_mtime(source_files)
47     version = get_version(mtime)
48
49     compressed_file_full = media_root(get_output_filename(output_file, version))
50
51     if not os.path.exists(compressed_file_full):
52         return True, version
53
54     # Check if the output file is outdated
55     return (os.stat(compressed_file_full).st_mtime < mtime), mtime
56
57 def media_root(filename):
58     """
59     Return the full path to ``filename``. ``filename`` is a relative path name in MEDIA_ROOT
60     """
61     return os.path.join(django_settings.STATIC_ROOT, filename)
62
63 def media_url(url):
64     return django_settings.STATIC_URL + urlquote(url)
65
66 def concat(filenames, separator=''):
67     """
68     Concatenate the files from the list of the ``filenames``, ouput separated with ``separator``.
69     """
70     r = ''
71
72     for filename in filenames:
73         fd = open(media_root(filename), 'rb')
74         r += fd.read()
75         r += separator
76         fd.close()
77
78     return r
79
80 def max_mtime(files):
81     return int(max([os.stat(media_root(f)).st_mtime for f in files]))
82
83 def save_file(filename, contents):
84     fd = open(media_root(filename), 'wb+')
85     fd.write(contents)
86     fd.close()
87
88 def get_output_filename(filename, version):
89     if settings.COMPRESS_VERSION and version is not None:
90         return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, get_version(version))
91     else:
92         return filename.replace(settings.COMPRESS_VERSION_PLACEHOLDER, settings.COMPRESS_VERSION_DEFAULT)
93
94 def get_version(version):
95     try:
96         return str(int(version))
97     except ValueError:
98         return str(version)
99
100 def remove_files(path, filename, verbosity=0):    
101     regex = re.compile(r'^%s$' % (os.path.basename(get_output_filename(settings.COMPRESS_VERSION_PLACEHOLDER.join([re.escape(part) for part in filename.split(settings.COMPRESS_VERSION_PLACEHOLDER)]), r'\d+'))))
102
103     for f in os.listdir(path):
104         if regex.match(f):
105             if verbosity >= 1:
106                 print "Removing outdated file %s" % f
107
108             os.unlink(os.path.join(path, f))
109
110 def filter_common(obj, verbosity, filters, attr, separator, signal):
111     output = concat(obj['source_filenames'], separator)
112     filename = get_output_filename(obj['output_filename'], get_version(max_mtime(obj['source_filenames'])))
113
114     if settings.COMPRESS_VERSION:
115         remove_files(os.path.dirname(media_root(filename)), obj['output_filename'], verbosity)
116
117     if verbosity >= 1:
118         print "Saving %s" % filename
119
120     for f in filters:
121         output = getattr(get_filter(f)(verbose=(verbosity >= 2)), attr)(output)
122
123     save_file(filename, output)
124     signal.send(None)
125
126 def filter_css(css, verbosity=0):
127     return filter_common(css, verbosity, filters=settings.COMPRESS_CSS_FILTERS, attr='filter_css', separator='', signal=css_filtered)
128
129 def filter_js(js, verbosity=0):
130     return filter_common(js, verbosity, filters=settings.COMPRESS_JS_FILTERS, attr='filter_js', separator=';', signal=js_filtered)