d40e8eebba75eafd6ca1ee6baa9075cf4c56f76f
[wolnelektury.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 class CSSTidyFilter(FilterBase):
15     def filter_css(self, css):
16         tmp_file = tempfile.NamedTemporaryFile(mode='w+b')
17         tmp_file.write(css)
18         tmp_file.flush()
19
20         output_file = tempfile.NamedTemporaryFile(mode='w+b')
21         
22         command = '%s %s %s %s' % (BINARY, tmp_file.name, ARGUMENTS, output_file.name)
23         
24         command_output = os.popen(command).read()
25         
26         filtered_css = output_file.read()
27         output_file.close()
28         tmp_file.close()
29         
30         if self.verbose:
31             print command_output
32         
33         return filtered_css