Skip to content

Commit

Permalink
Merge branch 'release/0.9.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed May 23, 2011
2 parents eb79a65 + 338f1be commit 9717eab
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion compressor/__init__.py
@@ -1,4 +1,4 @@
VERSION = (0, 9, 1, "f", 0) # following PEP 386
VERSION = (0, 9, 2, "f", 0) # following PEP 386
DEV_N = None


Expand Down
60 changes: 27 additions & 33 deletions compressor/filters/base.py
Expand Up @@ -33,15 +33,13 @@ class CompilerFilter(FilterBase):
filename = None
options = {}

def __init__(self, content, filter_type=None, verbose=0, command=None, filename=None, **kwargs):
super(CompilerFilter, self).__init__(content, filter_type, verbose)
def __init__(self, content, command=None, filename=None, *args, **kwargs):
super(CompilerFilter, self).__init__(content, *args, **kwargs)
if command:
self.command = command
self.options.update(kwargs)
if self.command is None:
raise FilterError("Required command attribute not set")
if filename:
self.filename = filename
raise FilterError("Required attribute 'command' not given")
self.filename = filename
self.stdout = subprocess.PIPE
self.stdin = subprocess.PIPE
self.stderr = subprocess.PIPE
Expand All @@ -51,41 +49,37 @@ def output(self, **kwargs):
outfile = None
try:
if "{infile}" in self.command:
if not self.filename:
infile = tempfile.NamedTemporaryFile(mode='w')
infile.write(self.content)
infile.flush()
self.options["infile"] = infile.name
else:
self.options["infile"] = self.filename
infile = tempfile.NamedTemporaryFile(mode='w')
infile.write(self.content)
infile.flush()
self.options["infile"] = self.filename or infile.name
if "{outfile}" in self.command:
ext = ".%s" % self.type and self.type or ""
outfile = tempfile.NamedTemporaryFile(mode='w', suffix=ext)
outfile = tempfile.NamedTemporaryFile(mode='rw', suffix=ext)
self.options["outfile"] = outfile.name
cmd = stringformat.FormattableString(self.command).format(**self.options)
proc = subprocess.Popen(cmd_split(cmd),
command = stringformat.FormattableString(self.command)
proc = subprocess.Popen(cmd_split(command.format(**self.options)),
stdout=self.stdout, stdin=self.stdin, stderr=self.stderr)
if infile is not None or self.filename is not None:
if infile is not None:
filtered, err = proc.communicate()
else:
filtered, err = proc.communicate(self.content)
except (IOError, OSError), e:
raise FilterError('Unable to apply %s (%r): %s' % (
self.__class__.__name__, self.command, e))
raise FilterError('Unable to apply %s (%r): %s' %
(self.__class__.__name__, self.command, e))
else:
if proc.wait() != 0:
if not err:
err = ('Unable to apply %s (%s)' %
(self.__class__.__name__, self.command))
raise FilterError(err)
if self.verbose:
self.logger.debug(err)
if outfile is not None:
filtered = outfile.read()
finally:
if infile:
if infile is not None:
infile.close()
if proc.wait() != 0:
if not err:
err = 'Unable to apply %s (%s)' % (
self.__class__.__name__, self.command)
raise FilterError(err)
if self.verbose:
self.logger.debug(err)
if outfile is not None:
try:
outfile_obj = open(outfile.name)
filtered = outfile_obj.read()
finally:
outfile_obj.close()
if outfile is not None:
outfile.close()
return filtered
7 changes: 6 additions & 1 deletion compressor/tests/tests.py
Expand Up @@ -496,7 +496,7 @@ def setUp(self):
self.this_dir = os.path.dirname(__file__)
self.filename = os.path.join(self.this_dir, 'media/css/one.css')
self.test_precompiler = os.path.join(self.this_dir, 'precompiler.py')
with open(self.filename, 'r') as f:
with open(self.filename) as f:
self.content = f.read()

def test_precompiler_infile_outfile(self):
Expand All @@ -514,6 +514,11 @@ def test_precompiler_stdin_stdout(self):
compiler = CompilerFilter(content=self.content, filename=None, command=command)
self.assertEqual(u"body { color:#990; }\n", compiler.output())

def test_precompiler_stdin_stdout_filename(self):
command = '%s %s' % (sys.executable, self.test_precompiler)
compiler = CompilerFilter(content=self.content, filename=self.filename, command=command)
self.assertEqual(u"body { color:#990; }\n", compiler.output())

def test_precompiler_infile_stdout(self):
command = '%s %s -f {infile}' % (sys.executable, self.test_precompiler)
compiler = CompilerFilter(content=self.content, filename=None, command=command)
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.txt
@@ -1,6 +1,11 @@
Changelog
=========

0.9.2
-----

- Fixed stdin handling of precompiler filter.

0.9.1
-----

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -50,7 +50,7 @@
# The short X.Y version.
version = '0.9'
# The full version, including alpha/beta/rc tags.
release = '0.9.1'
release = '0.9.2'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down

0 comments on commit 9717eab

Please sign in to comment.