Package pyrobase :: Module templating
[hide private]
[frames] | no frames]

Source Code for Module pyrobase.templating

  1  # -*- coding: utf-8 -*- 
  2  # pylint: disable=too-few-public-methods 
  3  """ Templating Helpers. 
  4   
  5      Copyright (c) 2012 The PyroScope Project <pyroscope.project@gmail.com> 
  6  """ 
  7  # This program is free software; you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation; either version 2 of the License, or 
 10  # (at your option) any later version. 
 11  # 
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  # GNU General Public License for more details. 
 16  # 
 17  # You should have received a copy of the GNU General Public License along 
 18  # with this program; if not, write to the Free Software Foundation, Inc., 
 19  # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 20  from __future__ import with_statement 
 21   
 22  import os 
 23  from contextlib import closing 
 24   
 25   
26 -class InterpolationTemplate(object):
27 """ Simple string interpolation. 28 """ 29
30 - def __init__(self, fmt, mapping=None):
31 """ Create template ADT wrapper object. 32 """ 33 self.fmt = unicode(fmt) 34 self.mapping = mapping or (lambda _: _) 35 self.__engine__ = "interpolation" 36 self.__file__ = None 37 self.__text__ = ''
38 39
40 - def __repr__(self):
41 """ Returns interpolation string. 42 """ 43 return self.fmt
44 45
46 - def __str__(self):
47 """ Returns interpolation string. 48 """ 49 return self.fmt
50 51
52 - def substitute(self, **variables):
53 """ Return expanded template for given variable set. 54 """ 55 return self.fmt % self.mapping(variables)
56 57
58 -def preparse(template_text, lookup=None):
59 """ Do any special processing of a template, including recognizing the templating language 60 and resolving file: references, then return an appropriate wrapper object. 61 62 Currently Tempita and Python string interpolation are supported. 63 `lookup` is an optional callable that resolves any ambiguous template path. 64 """ 65 # First, try to resolve file: references to their contents 66 template_path = None 67 try: 68 is_file = template_text.startswith("file:") 69 except (AttributeError, TypeError): 70 pass # not a string 71 else: 72 if is_file: 73 template_path = template_text[5:] 74 if template_path.startswith('/'): 75 template_path = '/' + template_path.lstrip('/') 76 elif template_path.startswith('~'): 77 template_path = os.path.expanduser(template_path) 78 elif lookup: 79 template_path = lookup(template_path) 80 81 with closing(open(template_path, "r")) as handle: 82 template_text = handle.read().rstrip() 83 84 if hasattr(template_text, "__engine__"): 85 # Already preparsed 86 template = template_text 87 else: 88 if template_text.startswith("{{"): 89 import tempita # only on demand 90 91 template = tempita.Template(template_text, name=template_path) 92 template.__engine__ = "tempita" 93 else: 94 template = InterpolationTemplate(template_text) # pylint: disable=redefined-variable-type 95 96 template.__file__ = template_path 97 98 template.__text__ = template_text 99 return template
100