Login

Django mediagenerator folder bundler

Author:
hcliff
Posted:
May 2, 2012
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

This code will put an entire folder into your media bundle - instead of having to write out every file in a given folder:

It assumes your static root is a absolute directory

Usage

MEDIA_BUNDLES = ( ('init.js', 'coffeescript/init.coffee', ), bundle_builder('libraries.js', 'javascript/vendor'), bundle_builder('main.js', 'coffeescript', exclude=['init.js',]), bundle_builder('templates.js', 'eco'), )

Notes You may wish to use cache_utils or similar to avoid crawling the filesystem every time the site loads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import os

def bundle_builder(bundle_name, folder, **kwargs):

    class file_map(object):
        
        def __init__(self, root):
            self.root = os.path.relpath(root, STATIC_ROOT)

        def __call__(self, file_name, **kwargs):
            if file_name not in kwargs.get('exclude', []):
                return "%s/%s" % (self.root, file_name) 

    file_list = [bundle_name,]
    for root, dirs, files in os.walk(os.path.join(STATIC_ROOT, folder)):
       file_list.extend(map(file_map(root), files))

    return tuple(file_list)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.