from mediagenerator.generators.bundles.base import Filter
from subprocess import Popen, PIPE

class CoffeeScript(Filter):    
    def __init__(self, **kwargs):
        super(CoffeeScript, self).__init__(**kwargs)
        assert self.filetype == 'js', (
            'CoffeeScript only supports compilation to js. '
            'The parent filter expects "%s".' % self.filetype)
        self.input_filetype = 'coffee-script'

    def compile(self, input):
        try:
            # coffee
            # -s = Read from stdin for the source
            # -c = Compile
            # -p = print the compiled output to stdout
            cmd = Popen(['coffee', '-c', '-p', '-s', '--no-wrap'],
                        stdin=PIPE, stdout=PIPE, stderr=PIPE,
                        universal_newlines=True)
            output, error = cmd.communicate(input)
            assert cmd.wait() == 0, ('CoffeeScript command returned bad '
                                     'result:\n%s' % error)
            return output
        except Exception, e:
            raise ValueError("Failed to run CoffeeScript compiler for this "
                "file. Please confirm that the \"coffee\" application is "
                "on your path and that you can run it from your own command "
                "line.\n"
                "Error was: %s" % e)

    def should_use_default_filter(self, ext):
        if ext == 'coffee':
            return False
        return super(CoffeeScript, self).should_use_default_filter(ext)

    def get_output(self, variation):
        for input in self.get_input(variation):
            yield self.compile(input)

    def get_dev_output(self, name, variation):
        content = super(CoffeeScript, self).get_dev_output(name, variation)
        return self.compile(content)

@@ -1,12 +1,37 @@
+from hashlib import sha1%0A
 from mediage
@@ -76,16 +76,59 @@
  Filter%0A
+from mediagenerator.utils import find_file%0A
 from sub
@@ -153,16 +153,26 @@
 en, PIPE
+%0Aimport os
 %0A%0Aclass 
@@ -197,16 +197,41 @@
 r):    %0A
+    takes_input = False%0A%0A
     def 
@@ -256,16 +256,57 @@
 wargs):%0A
+        self.config(kwargs, module=None)%0A
         
@@ -531,50 +531,955 @@
 elf.
-input_filetype = 'coffee-script'%0A%0A    def 
+_compiled = None%0A        self._compiled_hash = None%0A        self._mtime = None%0A%0A    @classmethod%0A    def from_default(cls, name):%0A        return %7B'module': name%7D%0A%0A    def get_output(self, variation):%0A        self._regenerate(debug=False)%0A        yield self._compiled%0A%0A    def get_dev_output(self, name, variation):%0A        assert name == self.main_module%0A        self._regenerate(debug=True)%0A        return self._compiled%0A%0A    def get_dev_output_names(self, variation):%0A        self._regenerate(debug=True)%0A        yield self.module, self._compiled_hash%0A%0A    def _regenerate(debug=False):%0A        path = find_file(path)%0A        mtime = os.path.getmtime(path)%0A        if mtime == self._mtime:%0A            return%0A        fp = open(path, 'r')%0A        source = fp.read()%0A        fp.close()%0A        self._compiled = self._compile(source, debug=debug)%0A        self._compiled_hash = sha1(self._compiled).hexdigest()%0A        self._mtime = mtime%0A%0A    def _
 comp
@@ -1493,16 +1493,29 @@
 f, input
+, debug=False
 ):%0A     
@@ -2390,461 +2390,4 @@
  e)%0A
-%0A    def should_use_default_filter(self, ext):%0A        if ext == 'coffee':%0A            return False%0A        return super(CoffeeScript, self).should_use_default_filter(ext)%0A%0A    def get_output(self, variation):%0A        for input in self.get_input(variation):%0A            yield self.compile(input)%0A%0A    def get_dev_output(self, name, variation):%0A        content = super(CoffeeScript, self).get_dev_output(name, variation)%0A        return self.compile(content)%0A

enable caching for CoffeeScript filter