Coding style overhaul for Python files (PEP8 conformant). Removed buggy csstidy pytho...
[redakcja.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
12 class YUICompressorFilter(FilterBase):
13
14     def filter_common(self, content, type_, arguments):
15         command = '%s --type=%s %s' % (BINARY, type_, arguments)
16
17         if self.verbose:
18             command += ' --verbose'
19
20         p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
21         p.stdin.write(content)
22         p.stdin.close()
23
24         filtered_css = p.stdout.read()
25         p.stdout.close()
26
27         err = p.stderr.read()
28         p.stderr.close()
29
30         if p.wait() != 0:
31             if not err:
32                 err = 'Unable to apply YUI Compressor filter'
33
34             raise FilterError(err)
35
36         if self.verbose:
37             print err
38
39         return filtered_css
40
41     def filter_js(self, js):
42         return self.filter_common(js, 'js', JS_ARGUMENTS)
43
44     def filter_css(self, css):
45         return self.filter_common(css, 'css', CSS_ARGUMENTS)