# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import os
from django.apps import apps, AppConfig
from django.conf import settings
from django.core.files.storage import get_storage_class


APPS_INCLUDE_DIRS = []

class SassProcessorConfig(AppConfig):
    name = 'sass_processor'
    verbose_name = "Sass Processor"
    _sass_exts = ('.scss', '.sass')
    _storage = get_storage_class(import_path=settings.STATICFILES_STORAGE)()

    def ready(self):
        if getattr(settings, 'SASS_PROCESSOR_AUTO_INCLUDE', True):
            app_configs = apps.get_app_configs()
            for app_config in app_configs:
                static_dir = os.path.join(app_config.path, self._storage.base_url.strip(os.path.sep))
                if os.path.isdir(static_dir):
                    self.traverse_tree(static_dir)

    @classmethod
    def traverse_tree(cls, static_dir):
        """traverse the static folders an look for at least one file ending in .scss/.sass"""
        for root, dirs, files in os.walk(static_dir):
            for filename in files:
                basename, ext = os.path.splitext(filename)
                if basename.startswith('_') and ext in cls._sass_exts:
                    APPS_INCLUDE_DIRS.append(static_dir)
                    return

@@ -58,16 +58,26 @@
 terals%0A%0A
+import re%0A
 import o
@@ -339,44 +339,8 @@
 or%22%0A
-    _sass_exts = ('.scss', '.sass')%0A
     
@@ -416,41 +416,109 @@
 )()%0A
-%0A
     
-def ready(self):%0A        if 
+_auto_include = getattr(settings, 'SASS_PROCESSOR_AUTO_INCLUDE', True)%0A    _pattern = re.compile(
 geta
@@ -547,36 +547,114 @@
 OCESSOR_
-AUTO_
 INCLUDE
-', True)
+_FILE_PATTERN', r'%5E_.+%5C.(scss%7Csass)$'))%0A%0A    def ready(self):%0A        if self._auto_include
 :%0A      
@@ -1199,120 +1199,39 @@
     
-basename, ext = o
+if cl
 s.
+_
 pat
-h.splitext(filename)%0A                if basename.startswith('_') and ext in cls._sass_exts
+tern.match(filename)
 :%0A  

use regex pattern to look for possible include files