from StringIO import StringIO
from mock import patch

from django.core.management import call_command
from django.test import SimpleTestCase
from nose.plugins.attrib import attr

B3_BASE = 'style/base.html'

BLOCK_JS = ' block js '
BLOCK_CSS = ' block stylesheets '
ENDBLOCK = ' endblock '

COMPRESS_JS = ' compress js '
COMPRESS_CSS = ' compress stylesheets '
ENDCOMPRESS = ' endcompress '

DISALLOWED_TAGS = [
    ('{% if', 'You cannot use "if" tags in a compress block'),
    ('^</script>$', 'You cannot use inline JS in a compress block'),
]

IGNORED_FILES = [
    'http://opensource.org/licenses/mit-license.html',
    'More Info : http://www.quirksmode.org/css/box.html',
]


class TestDjangoCompressOffline(SimpleTestCase):

    def _assert_valid_import(self, line, filename):
        if not line.strip():
            return
        for tag in DISALLOWED_TAGS:
            self.assertNotRegexpMatches(tag[0], line.strip(), tag[1])

    @attr("slow")
    def test_compress_offline(self):
        call_command('collectstatic', verbosity=0, interactive=False)
        call_command('fix_less_imports_collectstatic', verbosity=0, interactive=False)
        with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
            call_command('compress', force=True)

            mock_stdout.seek(0)
            filenames = mock_stdout.readlines()

        in_compress_block = False

        # Filter lines that are not html and strip whitespace
        filenames = filter(lambda name: name.endswith('.html'), map(lambda name: name.strip(), filenames))

        for filename in filenames:
            with open(filename, 'r') as f:
                for line in f.readlines():
                    has_start_tag = BLOCK_JS in line or BLOCK_CSS in line
                    has_start_tag = has_start_tag or COMPRESS_JS in line or COMPRESS_CSS in line
                    has_end_tag = ENDBLOCK in line or ENDCOMPRESS in line

                    if has_start_tag and not has_end_tag:
                        in_compress_block = True
                        continue

                    if has_end_tag:
                        in_compress_block = False

                    if in_compress_block:
                        self._assert_valid_import(line, filename)

@@ -1,51 +1,46 @@
-from StringIO import StringIO%0Afrom mock
+import os%0Afrom nose.plugins.attrib
  import 
 patc
@@ -35,21 +35,20 @@
  import 
-p
 at
-ch
+tr
 %0A%0Afrom d
@@ -57,43 +57,35 @@
 ngo.
-core.management import call_command
+test import SimpleTestCase%0A
 %0Afro
@@ -89,36 +89,36 @@
 from django.
-test
+conf
  import 
 SimpleTestCa
@@ -109,170 +109,118 @@
 ort 
-SimpleTestCase%0Afrom nose.plugins.attrib import attr%0A%0AB3_BASE = 'style/base.html'%0A%0ABLOCK_JS = ' block js '%0ABLOCK_CSS = ' block stylesheets '%0AENDBLOCK = ' endblock 
+settings%0Afrom django.template.loaders.app_directories import get_app_template_dirs%0A%0AB3_BASE = 'style/base.html
 '%0A%0AC
@@ -278,18 +278,10 @@
 ess 
+c
 s
-tylesheet
 s '%0A
@@ -831,16 +831,8 @@
 hes(
-tag%5B0%5D, 
 line
@@ -849,10 +849,45 @@
 tag%5B
-1%5D
+0%5D, '%7B%7D: %7B%7D'.format(tag%5B1%5D, filename)
 )%0A%0A 
@@ -952,69 +952,34 @@
     
-call_command('collectstatic', verbosity=0, interactive=
+in_compress_block = 
 False
-)
+%0A
 %0A   
@@ -987,241 +987,351 @@
     
-call_command('fix_less_imports_collectstatic', verbosity=0, interactive=False)%0A        with patch('sys.stdout', new_callable=StringIO) as mock_stdout:%0A            call_command('compress', force=True)%0A%0A            mock_stdout.seek(0)%0A
+template_dir_list = %5B%5D%0A        for template_dir in get_app_template_dirs('templates'):%0A            if settings.BASE_DIR in template_dir:%0A                template_dir_list.append(template_dir)%0A%0A        template_list = %5B%5D%0A        for template_dir in template_dir_list:%0A            for base_dir, dirnames, filenames in os.walk(template_dir):%0A    
     
@@ -1334,32 +1334,36 @@
             
+for 
 filename
 s = mock_std
@@ -1354,70 +1354,97 @@
 name
-s = mock_stdout.readlines()%0A%0A        in_compress_block = False
+ in filenames:%0A                    template_list.append(os.path.join(base_dir, filename))
 %0A%0A  
@@ -1598,25 +1598,29 @@
 trip(), 
-filenames
+template_list
 ))%0A%0A    
@@ -1693,16 +1693,58 @@
 ) as f:%0A
+                in_compress_block = False%0A
         
@@ -1817,99 +1817,8 @@
 ag =
- BLOCK_JS in line or BLOCK_CSS in line%0A                    has_start_tag = has_start_tag or
  COM
@@ -1895,28 +1895,8 @@
 ag =
- ENDBLOCK in line or
  END

Fix and speed up compress command