1e2e711fd406c267fd349d3721bd0ba560694090
[wolnelektury.git] / apps / compress / filters / yui / __init__.py
1 import subprocess
2
3 from django.conf import settings
4
5 from compress.filter_base import FilterBase, FilterError
6
7 BINARY = getattr(settings, 'COMPRESS_YUI_BINARY', 'java -jar yuicompressor.jar')
8 CSS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_CSS_ARGUMENTS', '')
9 JS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_JS_ARGUMENTS', '')
10
11 class YUICompressorFilter(FilterBase):
12
13     def filter_common(self, content, type_, arguments):
14         command = '%s --type=%s %s' % (BINARY, type_, arguments)
15
16         if self.verbose:
17             command += ' --verbose'
18
19         p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
20         p.stdin.write(content)
21         p.stdin.close()
22
23         filtered_css = p.stdout.read()
24         p.stdout.close()
25
26         err = p.stderr.read()
27         p.stderr.close()
28
29         if p.wait() != 0:
30             if not err:
31                 err = 'Unable to apply YUI Compressor filter'
32
33             raise FilterError(err)
34
35         if self.verbose:
36             print err
37
38         return filtered_css
39
40     def filter_js(self, js):
41         return self.filter_common(js, 'js', JS_ARGUMENTS)
42
43     def filter_css(self, css):
44         return self.filter_common(css, 'css', CSS_ARGUMENTS)