Commits

Jan-Jaap Driessen committed b3d1259

When compiling a resource, write a log record. This can be picked up in the development console when working on a running app or in the fanstatic-compile script by passing '-v' or '--verbose' for verbose output.

Comments (0)

Files changed (3)

fanstatic/compiler.py

 import argparse
 import fanstatic
 import os.path
+import time
+import logging
 import pkg_resources
 import setuptools.command.sdist
 import subprocess
 
 mtime = os.path.getmtime
 
+logger = logging.getLogger('fanstatic')
+
 
 class CompilerError(Exception):
     """A compiler or minifier returned an error.
         :param force: If True, always perform compilation. If False (default),\
         only perform compilation if ``should_process`` returns True.
         """
-
         source = self.source_path(resource)
         target = self.target_path(resource)
         if force or self.should_process(source, target):
+            start = time.time()
             self.process(source, target)
+            logger.info(
+                'Compiling %s in %0.3f seconds', resource, time.time() - start)
 
     def process(self, source, target):
         pass  # Override in subclass
         ' declared in the given package.')
     parser.add_argument(
         'package', help='Dotted name of the package to compile')
+    parser.add_argument(
+        '-v', '--verbose', dest='verbose',
+        action='store_true', help='Verbose output')
     options = parser.parse_args()
+    if options.verbose:
+        # setup logger to output to console
+        logging.basicConfig(level=logging.INFO)
     _compile_resources(options.package)
 
 

fanstatic/core.py

 
     def compile(self, force=False):
         if self.mode_parent:
-            self.mode_parent.compile(force)
+            self.mode_parent.compile(force=force)
         else:
-            self.compiler(self, force)
-            self.minifier(self, force)
+            self.compiler(self, force=force)
+            self.minifier(self, force=force)
 
     def render(self, library_url):
         return self.renderer('%s/%s' % (library_url, self.relpath))

tests/test_compiler.py

     return TestingRegistry(request)
 
 
+
+def test_logging_when_compiling(tmpdir, compilers, caplog):
+    class WhiteSpaceRemover(fanstatic.compiler.Compiler):
+        """A silly minifier, to showcase logging."""
+        name = 'whitespace'
+        source_extension = '.frop'
+
+        def process(self, source, target):
+            with open(target, 'wb') as output:
+                output.write(open(source, 'r').read().replace(' ', ''))
+
+    compilers.add_compiler(WhiteSpaceRemover())
+
+    lib = Library('lib', str(tmpdir))
+    tmpdir.join('a.frop').write(' foo bar baz ')
+    a = Resource(lib, 'a.js', compiler='whitespace')
+    assert len(caplog.records()) == 0
+    a.compile()
+    assert len(caplog.records()) == 1
+    assert "Compiling <Resource 'a.js' in library 'lib'> in" in caplog.text()
+    # The 'compiler' really worked!
+    assert tmpdir.join('a.js').read() == 'foobarbaz'
+
+
 def test_setting_compile_False_should_not_call_compiler_and_minifier(
     compilers):
     compilers.add_compiler(MockCompiler())