de5dd09b03be34473cb9dcdd5813100f63bf7881
[redakcja.git] / apps / compress / filters / csstidy / __init__.py
1 import os
2 import warnings
3 import tempfile
4
5 from django.conf import settings
6
7 from compress.filter_base import FilterBase
8
9 BINARY = getattr(settings, 'CSSTIDY_BINARY', 'csstidy')
10 ARGUMENTS = getattr(settings, 'CSSTIDY_ARGUMENTS', '--template=highest')
11
12 warnings.simplefilter('ignore', RuntimeWarning)
13
14
15 class CSSTidyFilter(FilterBase):
16
17     def filter_css(self, css):
18         tmp_file = tempfile.NamedTemporaryFile(mode='w+b')
19         tmp_file.write(css)
20         tmp_file.flush()
21
22         output_file = tempfile.NamedTemporaryFile(mode='w+b')
23
24         command = '%s %s %s %s' % (BINARY, tmp_file.name, ARGUMENTS, output_file.name)
25
26         command_output = os.popen(command).read()
27
28         filtered_css = output_file.read()
29         output_file.close()
30         tmp_file.close()
31
32         if self.verbose:
33             print command_output
34
35         return filtered_css