File: api.py

package info (click to toggle)
python-airspeed 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 208 kB
  • sloc: python: 2,110; makefile: 5; sh: 4
file content (43 lines) | stat: -rw-r--r-- 1,039 bytes parent folder | download | duplicates (2)
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
# encoding: utf-8

import os
try:
    from airspeed import CachingFileLoader
except ImportError:
    raise ImportError('You must install the airspeed package.')

from cachetools import LRUCache

__all__ = ['Airspeed']


class Airspeed(object):

    """The airspeed templating engine.

    Sample usage:

        >>> from cti.core import Engines
        >>> render = Engines()
        >>> render('airspeed:../tests/test.txt', dict())
        ('text/plain', 'Bingo!')

    """

    def __init__(self, cache=10, **kw):
        self.loaders = LRUCache(maxsize=cache)

    def __call__(self, data, template, mime_type="text/plain", **options):
        basepath = os.path.dirname(template)

        if basepath not in self.loaders:
            loader = CachingFileLoader(basepath)
            self.loaders[basepath] = loader

        else:
            loader = self.loaders[basepath]

        template = self.loaders[basepath].load_template(
            os.path.basename(template))

        return mime_type, template.merge(data, loader=loader)