from __future__ import with_statement

from webassets.filter import ExternalTool
from webassets.utils import working_directory


class Less(ExternalTool):
    """Converts `less <http://lesscss.org/>`_ markup to real CSS.

    This depends on the NodeJS implementation of less, installable via npm.
    To use the old Ruby-based version (implemented in the 1.x Ruby gem), see
    :class:`~.less_ruby.Less`.

    *Supported configuration options*:

    LESS_BIN (binary)
        Path to the less executable used to compile source files. By default,
        the filter will attempt to run ``lessc`` via the system path.

    LESS_LINE_NUMBERS (line_numbers)
        Outputs filename and line numbers. Can be either 'comments', which
        will output the debug info within comments, 'mediaquery' that will
        output the information within a fake media query which is compatible
        with the SASSPath to the less executable used to compile source files.

    LESS_RUN_IN_DEBUG (run_in_debug)
        By default, the filter will compile in debug mode. Since the less
        compiler is written in Javascript and capable of running in the
        browser, you can set this to ``False`` to have your original less
        source files served (see below).

    .. admonition:: Compiling less in the browser

        less is an interesting case because it is written in Javascript and
        capable of running in the browser. While for performance reason you
        should prebuild your stylesheets in production, while developing you
        may be interested in serving the original less files to the client,
        and have less compile them in the browser.

        To do so, you first need to make sure the less filter is not applied
        when :attr:`Environment.debug` is ``True``. You can do so via an
        option::

            env.config['less_run_in_debug'] = False

        Second, in order for the less to identify the  less source files as
        needing to be compiled, they have to be referenced with a
        ``rel="stylesheet/less"`` attribute. One way to do this is to use the
        :attr:`Bundle.extra` dictionary, which works well with the template
        tags that webassets provides for some template languages::

            less_bundle = Bundle(
                '**/*.less',
                filters='less',
                extra={'rel': 'stylesheet/less' if env.debug else 'stylesheet'}
            )

        Then, for example in a Jinja2 template, you would write::

            {% assets less_bundle %}
                <link rel="{{ EXTRA.rel }}" type="text/css" href="{{ ASSET_URL }}">
            {% endassets %}

        With this, the ``<link>`` tag will sport the correct ``rel`` value both
        in development and in production.

        Finally, you need to include the less compiler::

            if env.debug:
                js_bundle.contents += 'http://lesscss.googlecode.com/files/less-1.3.0.min.js'
    """

    name = 'less'
    options = {
        'less': ('binary', 'LESS_BIN'),
        'run_in_debug': 'LESS_RUN_IN_DEBUG',
        'line_numbers': 'LESS_LINE_NUMBERS',
    }
    max_debug_level = None

    def setup(self):
        super(Less, self).setup()
        if self.run_in_debug is False:
            # Disable running in debug mode for this instance.
            self.max_debug_level = False

    def input(self, in_, out, source_path, **kw):
        # Set working directory to the source file so that includes are found
        with working_directory(filename=source_path):
            cmd = [self.less or 'lessc', '-']
            if self.line_numbers:
                cmd.insert(-1, '--line-numbers=%s' % self.line_numbers)
            self.subprocess(cmd, out, in_)

@@ -3128,16 +3128,57 @@
 MBERS',%0A
+        'extra_args': 'LESS_EXTRA_ARGS',%0A
     %7D%0A  
@@ -3542,69 +3542,12 @@
     
-with working_directory(filename=source_path):%0A            cmd
+args
  = %5B
@@ -3570,19 +3570,10 @@
 ssc'
-, '-'%5D%0A    
+%5D%0A
     
@@ -3614,27 +3614,20 @@
     
-    cmd.insert(-1, 
+args.append(
 '--l
@@ -3655,32 +3655,180 @@
 f.line_numbers)%0A
+        if self.extra_args:%0A            args.extend(self.extra_args)%0A        args.append('-')%0A        with working_directory(filename=source_path):%0A
             self
@@ -3843,11 +3843,12 @@
 ess(
-cmd
+args
 , ou

Add option for extra arguments to lessc