From 9267bb6f2e39c9242604ee44482869b49dfb31bb Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 18 Apr 2011 11:24:57 +0200 Subject: [PATCH] Stop overwriting the CompileFilter's options attribute in the __init__ method. Added optional test for the CSSTidy filter. Fixes issue #33. --- compressor/filters/base.py | 5 +++-- compressor/tests/tests.py | 18 +++++++++++++++++- compressor/utils/__init__.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/compressor/filters/base.py b/compressor/filters/base.py index bfcc016..3c1a9ca 100644 --- a/compressor/filters/base.py +++ b/compressor/filters/base.py @@ -31,14 +31,15 @@ class CompilerFilter(FilterBase): external commands. """ command = None + options = {} - def __init__(self, content, filter_type=None, verbose=0, command=None): + def __init__(self, content, filter_type=None, verbose=0, command=None, **kwargs): super(CompilerFilter, self).__init__(content, filter_type, verbose) if command: self.command = command + self.options.update(kwargs) if self.command is None: raise FilterError("Required command attribute not set") - self.options = {} self.stdout = subprocess.PIPE self.stdin = subprocess.PIPE self.stderr = subprocess.PIPE diff --git a/compressor/tests/tests.py b/compressor/tests/tests.py index 5fdcbd3..9e9580d 100644 --- a/compressor/tests/tests.py +++ b/compressor/tests/tests.py @@ -19,7 +19,7 @@ from compressor.conf import settings from compressor.css import CssCompressor from compressor.js import JsCompressor from compressor.management.commands.compress import Command as CompressCommand - +from compressor.utils import find_command class CompressorTestCase(TestCase): @@ -419,3 +419,19 @@ class OfflineGenerationTestCase(TestCase): u'', ], result) settings.COMPRESS_OFFLINE_CONTEXT = self._old_offline_context + + +if find_command(settings.COMPRESS_CSSTIDY_BINARY): + + class CssTidyTestCase(TestCase): + + def test_tidy(self): + content = """ +/* Some comment */ +font,th,td,p{ + color: black; +} +""" + from compressor.filters.csstidy import CSSTidyFilter + self.assertEqual( + "font,th,td,p{color:#000;}", CSSTidyFilter(content).output()) diff --git a/compressor/utils/__init__.py b/compressor/utils/__init__.py index 2c13265..699e37d 100644 --- a/compressor/utils/__init__.py +++ b/compressor/utils/__init__.py @@ -57,3 +57,32 @@ def walk(root, topdown=True, onerror=None, followlinks=False): if os.path.islink(p): for link_dirpath, link_dirnames, link_filenames in walk(p): yield (link_dirpath, link_dirnames, link_filenames) + + +def find_command(cmd, paths=None, pathext=None): + """ + Searches the PATH for the given command and returns its path + """ + if paths is None: + paths = os.environ.get('PATH', []).split(os.pathsep) + if isinstance(paths, basestring): + paths = [paths] + # check if there are funny path extensions for executables, e.g. Windows + if pathext is None: + pathext = os.environ.get('PATHEXT', '.COM;.EXE;.BAT;.CMD') + pathext = [ext for ext in pathext.lower().split(os.pathsep)] + # don't use extensions if the command ends with one of them + if os.path.splitext(cmd)[1].lower() in pathext: + pathext = [''] + # check if we find the command on PATH + for path in paths: + # try without extension first + cmd_path = os.path.join(path, cmd) + for ext in pathext: + # then including the extension + cmd_path_ext = cmd_path + ext + if os.path.isfile(cmd_path_ext): + return cmd_path_ext + if os.path.isfile(cmd_path): + return cmd_path + return None