X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/3199cbfa76c763b4082b5a8d8f971d74f67e27c0..0cae17bec6d31806615fae59a5b3945016285fbe:/apps/compress/filters/yui/__init__.py?ds=sidebyside diff --git a/apps/compress/filters/yui/__init__.py b/apps/compress/filters/yui/__init__.py new file mode 100644 index 000000000..1e2e711fd --- /dev/null +++ b/apps/compress/filters/yui/__init__.py @@ -0,0 +1,44 @@ +import subprocess + +from django.conf import settings + +from compress.filter_base import FilterBase, FilterError + +BINARY = getattr(settings, 'COMPRESS_YUI_BINARY', 'java -jar yuicompressor.jar') +CSS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_CSS_ARGUMENTS', '') +JS_ARGUMENTS = getattr(settings, 'COMPRESS_YUI_JS_ARGUMENTS', '') + +class YUICompressorFilter(FilterBase): + + def filter_common(self, content, type_, arguments): + command = '%s --type=%s %s' % (BINARY, type_, arguments) + + if self.verbose: + command += ' --verbose' + + p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) + p.stdin.write(content) + p.stdin.close() + + filtered_css = p.stdout.read() + p.stdout.close() + + err = p.stderr.read() + p.stderr.close() + + if p.wait() != 0: + if not err: + err = 'Unable to apply YUI Compressor filter' + + raise FilterError(err) + + if self.verbose: + print err + + return filtered_css + + def filter_js(self, js): + return self.filter_common(js, 'js', JS_ARGUMENTS) + + def filter_css(self, css): + return self.filter_common(css, 'css', CSS_ARGUMENTS) \ No newline at end of file