Login

YUI Loader as Django middleware

Author:
akaihola
Posted:
April 21, 2008
Language:
Python
Version:
.96
Score:
3 (after 3 ratings)

This server-side middleware implements some of the functionality in the Yahoo User Interface Loader component. YUI JavaScript and CSS modules requirements can be declared anywhere in the base, inherited or included templates, and the resulting, optimized <script> and <link rel="stylesheet"> tags are inserted at the specified position of the resulting page.

Requirements may be specified in multiple locations. This is useful when zero or more components are included in the HTML head section, and inherited and/or included templates require possibly overlapping sets of YUI components in the body across inherited and included templates. All tags are collected in the head section, and duplicate tags are automatically eliminated.

The middleware understands component dependencies and ensures that resources are loaded in the right order. It knows about built-in rollup files that ship with YUI. By automatically using rolled-up files, the number of HTTP requests is reduced.

The default syntax looks like HTML comments. Markup for the insertion point is replaced with <script> and <link> tags:

<!-- YUI_init -->

Component requirements are indicated, possibly in multiple locations, with the YUI_include markup. It is removed from the resulting page by the middleware. Example:

<!-- YUI_include fonts grids event dragdrop -->

Non-minified and compressed versions are requested, respectively, by:

<!-- YUI_version raw -->
<!-- YUI_version debug -->

Example:

<html><head>
<!-- YUI_init -->
<!-- YUI_include dom event -->
</head><body>
<!-- YUI_include element selector reset fonts base -->
</body></html>

Renders:

<html><head>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.1/build/reset-fonts/reset-fonts.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.1/build/base/base-min.css" />
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/element/element-beta-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/selector/selector-beta-min.js"></script>
</head><body>
</body></html>

The markup format can be customized with global Django settings. Example:

YUI_INCLUDE_PREFIX_RE = r'{!'
YUI_INCLUDE_SUFFIX_RE = r'!}'

would change markup to e.g. {! init !} and {! include dom event !}.

The base URL is customized with the YUI_INCLUDE_BASE setting, e.g.:

YUI_INCLUDE_BASE = 'http://localhost:8000/yui/build/'

To remove the XHTML trailing slash from the <link> tag, use:

YUI_INCLUDE_CSS_TAG = '<link rel="stylesheet" type="text/css" href="%s">'

See also the home page for this module.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
---- middleware.py

import re

from django.conf import settings

from module_info_2_5_1 import MODULE_INFO
from components import Components


DEFAULT_BASE = 'http://yui.yahooapis.com/2.5.1/build/'
DEFAULT_JS_TAG = '<script type="text/javascript" src="%s"></script>'
DEFAULT_CSS_TAG = '<link rel="stylesheet" type="text/css" href="%s" />'

YUI_BASE = getattr(settings, 'YUI_INCLUDE_BASE', DEFAULT_BASE)
PREFIX_RE = getattr(settings, 'YUI_INCLUDE_PREFIX_RE', '<!-- *YUI_')
SUFFIX_RE = getattr(settings, 'YUI_INCLUDE_SUFFIX_RE', ' *-->')
TAGS = {'js': getattr(settings, 'YUI_INCLUDE_JS_TAG', DEFAULT_JS_TAG),
        'css': getattr(settings, 'YUI_INCLUDE_CSS_TAG', DEFAULT_CSS_TAG)}
VERSIONS = {'raw': '', '': '', 'min': '-min', 'debug': '-debug'}

COMPONENTS = Components(MODULE_INFO)


class YUILoader:

    def __init__(self):
        self._components = set()
        self._rolled_up_components = {}
        self._rollup_counters = {}
        self.set_version('min')

    def set_version(self, version):
        self._version = VERSIONS[version]

    def add_component(self, new_component_name):
        if not self._has_component(new_component_name):
            self._add_requirements(new_component_name)
            self._count_in_rollups(new_component_name)
            rollup_name = self._get_satisfied_rollup(new_component_name)
            if rollup_name:
                self.add_component(rollup_name)
            else:
                self._components.add(new_component_name)
                self._roll_up_superseded(new_component_name)

    def render(self):
        return '\n'.join(self._render_component(component)
                         for component in self._sort_components())

    def _has_component(self, component_name):
        return component_name in self._components \
               or component_name in self._rolled_up_components

    def _get_satisfied_rollup(self, component_name):
        if self._version == '-min':
            for rollup_name in COMPONENTS.get_rollups(component_name):
                rollup_status = self._rollup_counters.get(rollup_name, set())
                if len(rollup_status) >= COMPONENTS[rollup_name].rollup:
                    return rollup_name

    def _count_in_rollups(self, component_name):
        for rollup_name in COMPONENTS.get_rollups(component_name):
            rolled_up = self._rollup_counters.setdefault(rollup_name, set())
            rolled_up.add(component_name)
        for superseded in COMPONENTS[component_name].supersedes:
            self._count_in_rollups(superseded)

    def _roll_up_superseded(self, component_name):
        for superseded in COMPONENTS[component_name].supersedes:
            self._rolled_up_components[superseded] = component_name
            if superseded in self._components:
                self._components.remove(superseded)

    def _add_requirements(self, component_name):
        for requirement in COMPONENTS[component_name].requires:
            self.add_component(requirement)

    def _render_component(self, component_name):
        component = COMPONENTS[component_name]
        path = component.path
        if component.type == 'js':
            if self._version != '-min' and path.endswith('-min.js'):
                path = path[:-7] + self._version + '.js'
        elif component.type == 'css':
            if self._version == '' and path.endswith('-min.css'):
                path = path[:-8] + '.css'
        return TAGS[component.type] % (YUI_BASE + path,)

    def _sort_components(self, component_names=None):
        if component_names is None:
            comps = self._components.copy()
        else:
            comps = component_names
        while comps:
            component_name = comps.pop()
            component = COMPONENTS[component_name]
            direct_deps = component.requires + component.after
            indirect_deps = [
                self._rolled_up_components[r] for r in direct_deps
                if r in self._rolled_up_components]
            all_deps = set(direct_deps).union(set(indirect_deps))
            deps_left = comps.intersection(all_deps)
            for r in self._sort_components(deps_left):
                yield r
                comps.remove(r)
            yield component_name


YUI_RE = re.compile(
    r'%s(include|version) +(.*?)%s' % (PREFIX_RE, SUFFIX_RE))
YUI_INIT_RE = re.compile(
    '%sinit%s' % (PREFIX_RE, SUFFIX_RE))

class YUIIncludeMiddleware(object):
    def process_response(self, request, response):
        components = set()
        node = YUILoader()
        def collect(match):
            cmd, data = match.groups()
            if cmd == 'include':
                components.update(data.split())
            elif cmd == 'version':
                node.set_version(data)
            else:
                return '<!-- UNKNOWN COMMAND YUI_%s -->' % cmd
            return ''
        content = YUI_RE.sub(collect, response.content)
        for component in components:
            node.add_component(component)
        content = YUI_INIT_RE.sub(node.render(), content, 1)
        response.content = YUI_INIT_RE.sub(
            '<!-- WARNING: MULTIPLE YUI_init STATEMENTS -->', content)
        return response


---- components.py

class ComponentAdapter:

    def __init__(self, components, name, data):
        self.components = components
        self.name = name
        self.data = data

    @property
    def supersedes(self):
        return self.data.get('supersedes', [])

    @property
    def requires(self):
        r = self.data.get('requires', [])

        if self.type != 'js' \
        or self.name == 'yahoo' \
        or 'yahoo' in self.supersedes \
        or 'yahoo' in r:
            return r
        else:
            return ['yahoo'] + r

    @property
    def after(self):
        if self.type == 'css':
            return self.data.get('after', [])
        elif self.type == 'js':
            return self.data.get('after', []) + \
                   self.components.get_css_components()

    @property
    def rollup(self):
        return self.data.get('rollup', 1)

    def __getattr__(self, attname):
        return self.data[attname]


class Components(dict):
    def __init__(self, components_dict):
        super(Components, self).__init__(components_dict)
        self.rollup_mapping = {}
        for component_name, data in components_dict.items():
            self.rollup_mapping.setdefault(component_name, set())
            if 'rollup' not in data:
                continue
            for rolled_up in data['supersedes']:
                self.rollup_mapping.setdefault(rolled_up, set()).add(
                    component_name)

    def __getitem__(self, component_name):
        data = super(Components, self).__getitem__(component_name)
        return ComponentAdapter(self, component_name, data)

    def get_rollups(self, component_name):
        return self.rollup_mapping[component_name]

    def get_all_rollups(self, component_name):
        rollups = self.get_rollups(component_name)
        for superseded in self[component_name].supersedes:
            rollups.update(self.get_all_rollups(superseded))
        return rollups

    def get_css_components(self):
        return [name for name, data in self.items()
                if data['type'] == 'css']


---- module_info_2_5_1.py

MODULE_INFO = {
    'animation': {'path': 'animation/animation-min.js',
                  'requires': ['dom', 'event'],
                  'type': 'js'},
    'autocomplete': {'optional': ['connection', 'animation'],
                     'path': 'autocomplete/autocomplete-min.js',
                     'requires': ['dom', 'event'],
                     'skinnable': True,
                     'type': 'js'},
    'base': {'after': ['reset', 'fonts', 'grids'],
             'path': 'base/base-min.css',
             'type': 'css'},
    'button': {'optional': ['menu'],
               'path': 'button/button-min.js',
               'requires': ['element'],
               'skinnable': True,
               'type': 'js'},
    'calendar': {'path': 'calendar/calendar-min.js',
                 'requires': ['event', 'dom'],
                 'skinnable': True,
                 'type': 'js'},
    'charts': {'path': 'charts/charts-experimental-min.js',
               'requires': ['element', 'json', 'datasource'],
               'type': 'js'},
    'colorpicker': {'optional': ['animation'],
                    'path': 'colorpicker/colorpicker-min.js',
                    'requires': ['slider', 'element'],
                    'skinnable': True,
                    'type': 'js'},
    'connection': {'path': 'connection/connection-min.js',
                   'requires': ['event'],
                   'type': 'js'},
    'container': {'optional': ['dragdrop', 'animation', 'connection'],
                  'path': 'container/container-min.js',
                  'requires': ['dom', 'event'],
                  'skinnable': True,
                  'supersedes': ['containercore'],
                  'type': 'js'},
    'containercore': {'path': 'container/container_core-min.js',
                      'pkg': 'container',
                      'requires': ['dom', 'event'],
                      'type': 'js'},
    'cookie': {'path': 'cookie/cookie-beta-min.js',
               'requires': ['yahoo'],
               'type': 'js'},
    'datasource': {'optional': ['connection'],
                   'path': 'datasource/datasource-beta-min.js',
                   'requires': ['event'],
                   'type': 'js'},
    'datatable': {'optional': ['calendar', 'dragdrop'],
                  'path': 'datatable/datatable-beta-min.js',
                  'requires': ['element', 'datasource'],
                  'skinnable': True,
                  'type': 'js'},
    'dom': {'path': 'dom/dom-min.js', 'requires': ['yahoo'], 'type': 'js'},
    'dragdrop': {'path': 'dragdrop/dragdrop-min.js',
                 'requires': ['dom', 'event'],
                 'type': 'js'},
    'editor': {'optional': ['animation', 'dragdrop'],
               'path': 'editor/editor-beta-min.js',
               'requires': ['menu', 'element', 'button'],
               'skinnable': True,
               'type': 'js'},
    'element': {'path': 'element/element-beta-min.js',
                'requires': ['dom', 'event'],
                'type': 'js'},
    'event': {'path': 'event/event-min.js',
              'requires': ['yahoo'],
              'type': 'js'},
    'fonts': {'path': 'fonts/fonts-min.css', 'type': 'css'},
    'get': {'path': 'get/get-min.js', 'requires': ['yahoo'], 'type': 'js'},
    'grids': {'optional': ['reset'],
              'path': 'grids/grids-min.css',
              'requires': ['fonts'],
              'type': 'css'},
    'history': {'path': 'history/history-min.js',
                'requires': ['event'],
                'type': 'js'},
    'imagecropper': {'path': 'imagecropper/imagecropper-beta-min.js',
                     'requires': ['dom',
                                  'event',
                                  'dragdrop',
                                  'element',
                                  'resize'],
                     'skinnable': True,
                     'type': 'js'},
    'imageloader': {'path': 'imageloader/imageloader-min.js',
                    'requires': ['event', 'dom'],
                    'type': 'js'},
    'json': {'path': 'json/json-min.js',
             'requires': ['yahoo'],
             'type': 'js'},
    'layout': {'optional': ['animation', 'dragdrop', 'resize', 'selector'],
               'path': 'layout/layout-beta-min.js',
               'requires': ['dom', 'event', 'element'],
               'skinnable': True,
               'type': 'js'},
    'logger': {'optional': ['dragdrop'],
               'path': 'logger/logger-min.js',
               'requires': ['event', 'dom'],
               'skinnable': True,
               'type': 'js'},
    'menu': {'path': 'menu/menu-min.js',
             'requires': ['containercore'],
             'skinnable': True,
             'type': 'js'},
    'profiler': {'path': 'profiler/profiler-beta-min.js',
                 'requires': ['yahoo'],
                 'type': 'js'},
    'profilerviewer': {'path': 'profilerviewer/profilerviewer-beta-min.js',



                       'requires': ['profiler', 'yuiloader', 'element'],
                       'skinnable': True,
                       'type': 'js'},
    'reset': {'path': 'reset/reset-min.css', 'type': 'css'},
    'reset-fonts': {'path': 'reset-fonts/reset-fonts.css',
                    'rollup': 2,
                    'supersedes': ['reset', 'fonts'],
                    'type': 'css'},
    'reset-fonts-grids': {'path': 'reset-fonts-grids/reset-fonts-grids.css',
                          'rollup': 4,
                          'supersedes': ['reset',
                                         'fonts',
                                         'grids',
                                         'reset-fonts'],
                          'type': 'css'},
    'resize': {'optional': ['animation'],
               'path': 'resize/resize-beta-min.js',
               'requires': ['dom', 'event', 'dragdrop', 'element'],
               'skinnable': True,
               'type': 'js'},
    'selector': {'path': 'selector/selector-beta-min.js',
                 'requires': ['yahoo', 'dom'],
                 'type': 'js'},
    'simpleeditor': {'optional': ['containercore',
                                  'menu',
                                  'button',
                                  'animation',
                                  'dragdrop'],
                     'path': 'editor/simpleeditor-beta-min.js',
                     'pkg': 'editor',
                     'requires': ['element'],
                     'skinnable': True,
                     'type': 'js'},
    'slider': {'optional': ['animation'],
               'path': 'slider/slider-min.js',
               'requires': ['dragdrop'],
               'type': 'js'},
    'tabview': {'optional': ['connection'],
                'path': 'tabview/tabview-min.js',
                'requires': ['element'],
                'skinnable': True,
                'type': 'js'},
    'treeview': {'path': 'treeview/treeview-min.js',
                 'requires': ['event'],
                 'skinnable': True,
                 'type': 'js'},
    'uploader': {'path': 'uploader/uploader-experimental.js',
                 'requires': ['yahoo', 'element'],
                 'type': 'js'},
    'utilities': {'path': 'utilities/utilities.js',
                  'rollup': 8,
                  'supersedes': ['yahoo',
                                 'event',
                                 'dragdrop',
                                 'animation',
                                 'dom',
                                 'connection',
                                 'element',
                                 'yahoo-dom-event',
                                 'get',
                                 'yuiloader',
                                 'yuiloader-dom-event'],
                  'type': 'js'},
    'yahoo': {'path': 'yahoo/yahoo-min.js', 'type': 'js'},
    'yahoo-dom-event': {'path': 'yahoo-dom-event/yahoo-dom-event.js',
                        'rollup': 3,
                        'supersedes': ['yahoo', 'event', 'dom'],
                        'type': 'js'},
    'yuiloader': {'path': 'yuiloader/yuiloader-beta-min.js',
                  'supersedes': ['yahoo', 'get'],
                  'type': 'js'},
    'yuiloader-dom-event': {'path': 'yuiloader-dom-event/yuiloader-dom-event.js',
                            'rollup': 5,
                            'supersedes': ['yahoo',
                                           'dom',
                                           'event',
                                           'get',
                                           'yuiloader',
                                           'yahoo-dom-event'],
                            'type': 'js'},
    'yuitest': {'path': 'yuitest/yuitest-min.js',
                'requires': ['logger'],
                'skinnable': True,
                'type': 'js'}}

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, 7 months ago

Comments

shadfc (on August 29, 2008):

This is really cool, but it doesnt seem to include any of the css dependencies on some of the controls such as autocomplete and calendar.

Hopefully someone can retool it to do this and updated it to YUI 2.5.2

#

Please login first before commenting.