Login

Django mediagenerator folder bundler

Author:
hcliff
Posted:
May 2, 2012
Language:
Python
Version:
Not specified
Tags:
django wildcard django-mediagenerator mediagenerator
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. LinkShare Pixel Tracking template tag for Satchmo by maxk 4 years, 3 months ago
  2. Variable resolving URL template tag by UloPe 6 years, 1 month ago
  3. Search DjangoSnippets with Firefox by Natim 5 years, 7 months ago
  4. Template tag to clear cached template fragment by joao.coelho 5 years, 5 months ago
  5. Simple Plone Migration by msm-art 7 years, 3 months ago

Comments

Please login first before commenting.