Permalink
Browse files

update settings to be more django 1.4ish

  • Loading branch information...
defrex committed Nov 30, 2012
1 parent 586ff1a commit eee60ff30a777039018fa41f495d4a83779278e3
Showing with 65 additions and 75 deletions.
  1. +5 −3 README.md
  2. +41 −51 javascript/js.py
  3. +8 −8 javascript/management/commands/compile_js.py
  4. +5 −4 javascript/settings.py
  5. +6 −9 javascript/templatetags/include_js.py
View
@@ -36,10 +36,12 @@ This will create 'compiled.js' (or the file at `COMPILED_JS_LOC`), and will rev
One setting is required: `JS_DIR`. It should be the full path to the directory where all the js files are. Here are all of the settings available though:
JS_DIR = '' # must be set
- USE_COMPILED_JS = not DEBUG
- COMPILED_JS_LOC = os.path.join(JS_DIR, 'compiled.js')
+ # This is stripped off the file's path and replaced with STATIC_URL
+ JS_STATIC_ROOT = settings.STATIC_ROOT
+ JS_USE_COMPILED = not DEBUG
+ JS_COMPILED_LOC = os.path.join(JS_DIR, 'compiled.js')
JS_COMPILATION_LEVEL = 'SIMPLE_OPTIMIZATIONS' # use 'ADVANCED_OPTIMIZATIONS' if your hardcore.
- INCLUDE_JS_RECURSIVELY = True # False is handy if there is some js you don't want compiled
+ JS_INCLUDE_RECURSIVELY = True # False is handy if there is some js you don't want compiled
See more about [Closure Compiler compilation levels](http://code.google.com/closure/compiler/docs/compilation_levels.html).
View
@@ -1,19 +1,20 @@
import os
-from os.path import join, isfile, abspath, isdir, dirname
+from os.path import join, abspath, isdir
import tempfile
from subprocess import Popen
from javascript import settings
+
class GoogleClosureMinifier(object):
-
+
def __init__(self, jar_loc):
self.jar_loc = jar_loc
-
+
def assign_values(self, js, output_location):
self.js = js
self.output_location = output_location
-
+
def minify(self):
tmp = tempfile.NamedTemporaryFile(delete=False)
tmp.write(str(self.js))
@@ -25,15 +26,16 @@ def minify(self):
'output_loc': self.output_location,
}
p = Popen(command, shell=True)
- retcode = p.wait()
+ p.wait()
os.remove(tmp.name)
+
class DummyMinifier(object):
-
+
def assign_values(self, js, output_location):
self.js = js
self.output_location = output_location
-
+
def minify(self):
with open(self.output_location, 'w+') as f:
f.write(self.js)
@@ -46,66 +48,67 @@ class JavaScriptCompiler(object):
concated_js = None
compiled_js = None
output_location = None
-
+
def __init__(self, directory, minifier=None, compiled_loc=None, v=False,
recurse=True):
if not isdir(directory):
- raise AttributeError, 'JavaScriptCompiler object must be passed a valid directory'
+ raise AttributeError('JavaScriptCompiler object must be passed a valid directory')
self.directory = abspath(directory)
self.output_location = compiled_loc
self.minifier = minifier
self.v = v
self.recurse = recurse
-
+
def make_file_list(self):
if self.file_list is not None:
return self.file_list
-
+
if self.v: print 'Making file list...'
-
+
files = list()
+
def search_dir(directory):
for item in os.listdir(directory):
if isdir(join(directory, item)) and self.recurse:
search_dir(join(directory, item))
elif item.endswith('.js'):
if not self.output_location == join(directory, item):
files.append(join(directory, item))
-
+
search_dir(self.directory)
self.file_list = files
-
+
def resolve_dependencies(self, search_depth=200):
- '''
+ '''
by pstein (mostly)
-
+
Resolves the dependencies from a list of javascript files.
-
+
Paths must be absolute. Returns a sorted list of files, in order of
inclusion. If any dependencies cannot be met they will be printed to the
command line and ignored.
-
+
The method used to detect circular dependencies is a simple cap on the
- number of iterations allowed; as such, complex dependencies between a
+ number of iterations allowed; as such, complex dependencies between a
large list of files may not resolve completely. If this happens, setting
the maximum iterations argument to a value higher than the default will
help.
'''
if self.file_list is None:
self.make_file_list()
-
+
if self.v: print 'Resolving file dependancies...'
-
+
js_files = self.file_list[:]
output = list()
js_added = 0
cur_depth = 0
-
+
#The algorithm is rather simple: run through the list of files, removing
#any file who's dependencies are fully met. We count and limit the number
#of runs through the list in order to prevent circular dependencies from
#sticking us into an infinite loop.
-
+
#keep going as long as we haven't resolved all the files and we haven't
#hit the run cap
while js_added < len(self.file_list) and cur_depth < search_depth:
@@ -118,7 +121,7 @@ def resolve_dependencies(self, search_depth=200):
#if the file has dependencies, see if they're resolved
if deps.startswith('//depends:'):
dep_files = deps[10:].rstrip('\n').split(',')
- found = False
+ found = False
for dep in [dep.strip() for dep in dep_files]:
found = False
for item in output:
@@ -136,56 +139,43 @@ def resolve_dependencies(self, search_depth=200):
output.append(f)
js_added += 1
break #break, since we modified the array we're iterating over
-
+
#increment the run count
cur_depth += 1
-
+
#if we hit the run limit, print the unresolved files
if cur_depth == search_depth:
print 'Unable to resolve dependencies for the following files:'
for f in js_files:
print ' %s' % f
-
+
self.file_list_resolved = True
self.file_list = output
-
+
def concat_files(self):
if self.file_list is None or not self.file_list_resolved:
self.resolve_dependencies()
-
+
if self.v: print 'Concating resolved files...'
-
+
concated_js = ''
for f in self.file_list:
concated_js += open(f, 'r').read() + '\n'
-
+
self.concated_js = concated_js
-
+
def compile_js(self):
if self.concated_js is None:
self.concat_files()
-
+
if self.v: print 'Minifing concated js...'
-
+
if self.minifier is None:
- raise AttributeError, 'cannot compile without a minifier'
+ raise AttributeError('cannot compile without a minifier')
if self.output_location is None:
- raise AttributeError, 'cannot compile without an output_location'
-
+ raise AttributeError('cannot compile without an output_location')
+
self.minifier.assign_values(self.concated_js, self.output_location)
self.minifier.minify()
-
- if self.v: print 'Compilation complete.'
-
-
-def compile():
- minifier = GoogleClosureMinifier(settings.CLOSURE_JAR)
- jsc = JavaScriptCompiler(settings.JS_DIR, minifier,
- compiled_loc=settings.COMPILED_JS_LOC, v=True)
- jsc.compile_js()
-
-if __name__ == "__main__": compile
-
-
-
+ if self.v: print 'Compilation complete.'
@@ -1,9 +1,9 @@
import os
-from django.core.management.base import BaseCommand, CommandError
+from django.core.management.base import BaseCommand
-from javascript.js import GoogleClosureMinifier, JavaScriptCompiler, DummyMinifier
+from javascript.js import GoogleClosureMinifier, JavaScriptCompiler
from javascript.models import JSVersion
from javascript import settings
@@ -12,19 +12,19 @@ class Command(BaseCommand):
def handle(self, *args, **options):
print 'Begining JS Compilation...'
-
+
cur_dir = os.path.dirname(__file__)
closure_jar = os.path.join(cur_dir, '../../closure-compiler.jar')
minifier = GoogleClosureMinifier(closure_jar)
#minifier = DummyMinifier()
-
+
jsc = JavaScriptCompiler(settings.JS_DIR, minifier,
- compiled_loc=settings.COMPILED_JS_LOC, v=True,
- recurse=settings.INCLUDE_JS_RECURSIVELY)
+ compiled_loc=settings.JS_COMPILED_LOC, v=True,
+ recurse=settings.JS_INCLUDE_RECURSIVELY)
jsc.compile_js()
-
+
js_version, c = JSVersion.objects.get_or_create(pk=1)
js_version.version += 1
js_version.save()
-
+
print 'JS at version %s.' % str(js_version.version)
View
@@ -2,11 +2,12 @@
import os
from django.conf import settings
-JS_DIR = getattr(settings, 'JS_DIR', os.path.join(settings.MEDIA_ROOT, 'js'))
-USE_COMPILED_JS = getattr(settings, 'USE_COMPILED_JS', (not settings.DEBUG))
-COMPILED_JS_LOC = getattr(settings, 'COMPILED_JS_LOC', os.path.join(JS_DIR, 'compiled.js'))
+JS_DIR = getattr(settings, 'JS_DIR', os.path.join(settings.STATIC_ROOT, 'js'))
+JS_STATIC_ROOT = getattr(settings, 'JS_STATIC_ROOT', settings.STATIC_ROOT)
+JS_USE_COMPILED = getattr(settings, 'USE_COMPILED_JS', (not settings.DEBUG))
+JS_COMPILED_LOC = getattr(settings, 'COMPILED_JS_LOC', os.path.join(JS_DIR, 'compiled.js'))
JS_COMPILATION_LEVEL = getattr(settings, 'JS_COMPILATION_LEVEL', 'SIMPLE_OPTIMIZATIONS')
-INCLUDE_JS_RECURSIVELY = getattr(settings, 'INCLUDE_JS_RECURSIVELY', True)
+JS_INCLUDE_RECURSIVELY = getattr(settings, 'INCLUDE_JS_RECURSIVELY', True)
MEDIA_ROOT = settings.MEDIA_ROOT
MEDIA_URL = settings.MEDIA_URL
@@ -1,5 +1,3 @@
-from uuid import uuid4
-from os.path import join
from django import template
@@ -12,18 +10,17 @@
@register.simple_tag
def include_js():
- if not settings.USE_COMPILED_JS:
+ if not settings.JS_USE_COMPILED:
jsc = JavaScriptCompiler(settings.JS_DIR,
- compiled_loc=settings.COMPILED_JS_LOC,
- recurse=settings.INCLUDE_JS_RECURSIVELY)
+ compiled_loc=settings.JS_COMPILED_LOC,
+ recurse=settings.JS_INCLUDE_RECURSIVELY)
jsc.resolve_dependencies()
- js_files = [f.split(settings.MEDIA_ROOT)[-1] for f in jsc.file_list]
+ js_files = [f.split(settings.JS_STATIC_ROOT)[-1] for f in jsc.file_list]
else:
- js_files = ['/'+settings.COMPILED_JS_LOC.split(settings.MEDIA_ROOT)[-1]]
+ js_files = ['/' + settings.JS_COMPILED_LOC.split(settings.JS_STATIC_ROOT)[-1]]
version = JSVersion.objects.get_or_create(pk=1)[0].version
resp = ''
for f in js_files:
resp += '<script src="%s?version=%s"></script>' % (
- settings.MEDIA_URL+f, str(version))
+ settings.STATIC_URL + f, str(version))
return resp
-

0 comments on commit eee60ff

Please sign in to comment.