Commits

powersurge360  committed ba3ad08

Moved around some code and made things a bit more modular

  • Participants
  • Parent commits 8216576

Comments (0)

Files changed (4)

File management/commands/compilescss.py

 import os
-import glob
 
 from django.core.management.base import BaseCommand
 from django.conf import settings
 
-import scss
-
-import djangosassy
-DJANGOSASSY_PATH = os.path.dirname(djangosassy.__file__)
-
-scss.LOAD_PATHS = scss.LOAD_PATHS+","+os.path.join(DJANGOSASSY_PATH, 'scss')
+import djangosassy.utils
 
 class Command(BaseCommand):
     '''A command that will pull down any files in blog/scss and compile it to
     '''
     args = "<app_label app_label>..."
     help = "Compile the scss for a given app."
-    compiler = scss.Scss()
     can_import_settings = True
 
     def handle(self, *args, **kwargs):
 
         for app in apps:
             module = __import__(app)
-            directories = {}
-            directories['app_root'] = os.path.realpath(os.path.dirname(getattr(module, "__file__")))
-            directories['scss_dir'] = os.path.join(directories['app_root'], 'scss')
-            directories['static_dir'] = os.path.join(directories['app_root'], 'static')
-            directories['css_dir'] = os.path.join(directories['static_dir'], 'styles')
+
+            directories = djangosassy.utils.build_paths(
+                os.path.realpath(
+                    os.path.dirname(getattr(module, "__file__"))
+                )
+            )
 
             # Try to create the directories.
-            self._create_directories(directories)
+            djangosassy.utils.create_directories(directories)
 
             # Scss works out of the working directory.
             os.chdir(directories['scss_dir'])
 
-            self._compile_styles(directories['scss_dir'], directories['css_dir'])
-
-    def _compile_styles(self, scss_dir, css_dir):
-        for file_name in glob.glob(os.path.join(scss_dir, '*.scss')):
-            # Build a CSS file based on the scss file
-            css_file_name = '.'.join(file_name.split('.')[:-1]) + '.css'
-            css_file_name = css_file_name.split(os.sep)[-1]
-
-            # Open both files, compile from one and write compiled css to the
-            # other. Note the escaped newline is not PEP8, but the recommended
-            # parens did not work here.
-            with open(file_name) as scss_file, \
-                 open(os.path.join(css_dir, css_file_name), 'w') as css_file:
-                css_file.write(self.compiler.compile(scss_file.read()))
-
-    def _create_directories(self, directory_dict):
-        for directory in directory_dict.values():
-            try:
-                os.mkdir(directory)
-            except OSError:
-                pass
+            djangosassy.utils.compile_styles(
+                directories['scss_dir'],
+                directories['css_dir']
+            )

File templatetags/__init__.py

Empty file added.

File templatetags/compilescss.py

+import os
+
+from django import template
+
+import djangosassy.utils
+
+register = template.Library()
+
+@register.tag
+def compile_scss(parser, token):
+    app, scss_file = token.split_contents()[1:]
+
+    return CompiledScssNode(app, scss_file)
+
+class CompiledScssNode(template.Node):
+    def __init__(self, app, scss_file):
+        self.app = app[1:-1]
+        self.scss_file = scss_file[1:-1]
+
+    def render(self, context):
+        module = __import__(self.app)
+        
+        directories = djangosassy.utils.build_paths(
+            os.path.realpath(
+                os.path.dirname(getattr(module, "__file__"))
+            )
+        )
+
+        djangosassy.utils.create_directories(directories)
+
+        os.chdir(directories['scss_dir'])
+
+        css_file = djangosassy.utils.compile_style(
+            self.scss_file,
+            directories['css_dir']
+        )
+
+        return css_file
+import os
+import glob
+import time
+
+import scss
+
+import djangosassy
+DJANGOSASSY_PATH = os.path.dirname(djangosassy.__file__)
+
+scss.LOAD_PATHS = scss.LOAD_PATHS+","+os.path.join(DJANGOSASSY_PATH, 'scss')
+
+compiler = scss.Scss()
+
+def compile_styles(scss_dir, css_dir):
+    '''Compiles a number of scss files into css files given the scss directory
+    and the css directory. Will only compile in the root, other files are
+    considered to be 'library' files.
+    '''
+    for file_name in glob.glob(os.path.join(scss_dir, '*.scss')):
+        # If the file has not been modified since last compiled, skip.
+
+        compile_style(file_name, css_dir)
+
+def compile_style(file_name, css_dir):
+    '''Compiles a style for a given scss file and sticks it in the given
+    css_dir with the same name up to the prefix.
+
+    Will only compile if the scss file has been modified later than the
+    css_file.
+    '''
+    # Build a CSS file based on the scss file
+    css_file_name = '.'.join(file_name.split('.')[:-1]) + '.css'
+    css_file_name = css_file_name.split(os.sep)[-1]
+    css_path = os.path.join(css_dir, css_file_name)
+
+    try:
+        if os.stat(css_path).st_mtime > os.stat(file_name).st_mtime:
+            return css_file_name
+    except OSError:
+        # File does not yet exist.
+        pass
+
+    # Open both files, compile from one and write compiled css to the
+    # other. Note the escaped newline is not PEP8, but the recommended
+    # parens did not work here.
+    with open(file_name) as scss_file, \
+         open(css_path, 'w') as css_file:
+        css_file.write(compiler.compile(scss_file.read()))
+
+    return css_file_name
+    
+
+def create_directories(directory_dict):
+    '''Creates all the directories necessary, passes on failures (assumes
+    directory exists.
+    '''
+    for directory in directory_dict.values():
+        try:
+            os.mkdir(directory)
+        except OSError:
+            pass
+
+def build_paths(module_path):
+    '''Builds a path dictionary for the other functions to use.'''
+    directories = {}
+    directories['app_root'] = module_path
+    directories['scss_dir'] = os.path.join(directories['app_root'], 'scss')
+    directories['static_dir'] = os.path.join(directories['app_root'], 'static')
+    directories['css_dir'] = os.path.join(directories['static_dir'], 'styles')
+
+    return directories