# coding=utf-8
import logging
import platform
import subprocess

from path import path

from engineer.conf import settings


__author__ = 'Tyler Butler <tyler@tylerbutler.com>'

logger = logging.getLogger(__name__)


#noinspection PyShadowingBuiltins
# Helper function to preprocess LESS files on demand
def preprocess_less(file):
    input_file = path(settings.OUTPUT_CACHE_DIR / settings.ENGINEER.STATIC_DIR.basename() / file)
    css_file = path("%s.css" % str(input_file)[:-5])
    is_cached = input_file in settings.LESS_CACHE
    exists = css_file.exists()
    if is_cached and not exists:
        with open(css_file, mode='wb') as the_file:
            the_file.write(settings.LESS_CACHE[input_file])
        logger.info('Found cached output for LESS file %s. Skipping recompilation.' % file)
    elif not is_cached or not css_file.exists():
        cmd = str.format(str(settings.LESS_PREPROCESSOR), infile=input_file, outfile=css_file).split()
        try:
            subprocess.check_output(cmd)
        except subprocess.CalledProcessError as e:
            logger.critical("Error pre-processing LESS file %s." % file)
            logger.critical(e.output)
            exit(1355)
        except WindowsError as e:
            logger.critical("Unexpected error pre-processing LESS file %s." % file)
            logger.critical(e.strerror)
            exit(1355)
        except Exception as e:
            logger.critical("Unexpected error pre-processing LESS file %s." % file)
            logger.critical(e.message)
            if platform.system() != 'Windows':
                logger.critical("Are you sure lessc is on your path?")
            exit(1355)
        with open(css_file, mode='rb') as the_file:
            contents = the_file.read()
        settings.LESS_CACHE[input_file] = contents
        logger.info("Preprocessed LESS file %s ==> %s." % (file, css_file.name))
    return ""

@@ -214,644 +214,43 @@
 )%0A%0A%0A
-#noinspection PyShadowingBuiltins%0A# Helper function to preprocess LESS files on demand%0Adef preprocess_less(file):%0A    input_file = path(settings.OUTPUT_CACHE_DIR / settings.ENGINEER.STATIC_DIR.basename() / file)%0A    css_file = path(%22%25s.css%22 %25 str(input_file)%5B:-5%5D)%0A    is_cached = input_file in settings.LESS_CACHE%0A    exists = css_file.exists()%0A    if is_cached and not exists:%0A        with open(css_file, mode='wb') as the_file:%0A            the_file.write(settings.LESS_CACHE%5Binp
+def convert_less(infile, o
 ut
-_
 file
-%5D)%0A        logger.info('Found cached output for LESS file %25s. Skipping recompilation.' %25 file)%0A    elif not is_cached or not css_file.exists():%0A    
+):%0A
     
@@ -308,20 +308,16 @@
 nfile=in
-put_
 file, ou
@@ -322,20 +322,19 @@
 outfile=
-css_
+out
 file).sp
@@ -347,21 +347,13 @@
     
-   
- 
 try:%0A
-    
     
@@ -385,20 +385,16 @@
 ut(cmd)%0A
-    
     exce
@@ -432,36 +432,32 @@
 r as e:%0A        
- 
-   
 logger.critical(
@@ -501,36 +501,32 @@
 %25 file)%0A        
-    
 logger.critical(
@@ -535,20 +535,16 @@
 output)%0A
-    
         
@@ -546,36 +546,32 @@
      exit(1355)%0A
-    
     except Windo
@@ -580,28 +580,24 @@
 Error as e:%0A
-    
         logg
@@ -664,36 +664,32 @@
 %25 file)%0A        
-  
-  
 logger.critical(
@@ -700,36 +700,32 @@
 rerror)%0A        
-    
 exit(1355)%0A     
@@ -715,28 +715,24 @@
  exit(1355)%0A
-    
     except E
@@ -746,36 +746,32 @@
 n as e:%0A        
-    
 logger.critical(
@@ -826,36 +826,32 @@
 %25 file)%0A        
-    
 logger.critical(
@@ -869,20 +869,16 @@
         
-  
-  
 if platf
@@ -904,20 +904,16 @@
 ndows':%0A
-    
         
@@ -975,28 +975,24 @@
 ?%22)%0A        
-    
 exit(1355)%0A 
@@ -986,24 +986,682 @@
  exit(1355)%0A
+%0A%0A# Helper function to preprocess LESS files on demand%0Adef preprocess_less(less_file):%0A    input_file = path(settings.OUTPUT_CACHE_DIR / settings.ENGINEER.STATIC_DIR.basename() / less_file)%0A    css_file = path(%22%25s.css%22 %25 str(input_file)%5B:-5%5D)%0A    is_cached = input_file in settings.LESS_CACHE%0A    exists = css_file.exists()%0A    if is_cached and not exists:%0A        with open(css_file, mode='wb') as the_file:%0A            the_file.write(settings.LESS_CACHE%5Binput_file%5D)%0A        logger.info('Found cached output for LESS file %25s. Skipping recompilation.' %25 less_file)%0A    elif not is_cached or not css_file.exists():%0A        convert_less(input_file, css_file)%0A
         with
@@ -1849,16 +1849,21 @@
 %25s.%22 %25 (
+less_
 file, cs

Refactor less processor to standalone function.