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
|
"""PyPlate : a simple Python-based templating program
PyPlate parses a file and replaces directives (in double square brackets [[ ... ]])
by various means using a given dictionary of variables. Arbitrary Python code
can be run inside many of the directives, making this system highly flexible.
Usage:
# Load and parse template file
template = pyplate.Template("output") (filename or string)
# Execute it with a dictionary of variables
template.execute_file(output_stream, locals())
PyPlate defines the following directives:
[[...]] evaluate the arbitrary Python expression and insert the
result into the output
[[# ... #]] comment.
[[exec ...]] execute arbitrary Python code in the sandbox namespace
[[if ...]] conditional expressions with usual Python semantics
[[elif ...]]
[[else]]
[[end]]
[[for ... in ...]] for-loop with usual Python semantics
[[end]]
[[def ...(...)]] define a "function" out of other templating elements
[[end]]
[[call ...]] call a templating function (not a regular Python function)
"""
#
# Copyright (C) 2002 Michael Droettboom
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
from __future__ import nested_scopes
import sys, string, re, cStringIO
re_directive = re.compile("\[\[(.*)\]\]")
re_for_loop = re.compile("for (.*) in (.*)")
re_if = re.compile("if (.*)")
re_elif = re.compile("elif (.*)")
re_def = re.compile("def (.*?)\((.*)\)")
re_call = re.compile("call (.*?)\((.*)\)")
re_exec = re.compile("exec (.*)")
re_comment = re.compile("#(.*)#")
############################################################
# Template parser
class ParserException(Exception):
def __init__(self, lineno, s):
Exception.__init__(self, "line %d: %s" % (lineno, s))
class Template:
def __init__(self, filename=None):
if filename != None:
try:
self.parse_file(filename)
except:
self.parse_string(filename)
def parse_file(self, filename):
file = open(filename, 'r')
self.parse(file)
file.close()
def parse_string(self, template):
file = cStringIO.StringIO(template)
self.parse(file)
file.close()
def parse(self, file):
self.file = file
self.line = self.file.read()
self.lineno = 0
self.functions = {}
self.tree = TopLevelTemplateNode(self)
def parser_get(self):
if self.line == '':
return None
return self.line
def parser_eat(self, chars):
self.lineno = self.lineno + self.line[:chars].count("\n")
self.line = self.line[chars:]
def parser_exception(self, s):
raise ParserException(self.lineno, s)
def execute_file(self, filename, data):
file = open(filename, 'w')
self.execute(file, data)
file.close()
def execute_string(self, data):
s = cStringIO.StringIO()
self.execute(s, data)
return s.getvalue()
def execute_stdout(self, data):
self.execute(sys.stdout, data)
def execute(self, stream=sys.stdout, data={}):
self.tree.execute(stream, data)
def __repr__(self):
return repr(self.tree)
############################################################
# NODES
class TemplateNode:
def __init__(self, parent, s):
self.parent = parent
self.s = s
self.node_list = []
while 1:
new_node = TemplateNodeFactory(parent)
if self.add_node(new_node):
break
def add_node(self, node):
if node == 'end':
return 1
elif node != None:
self.node_list.append(node)
else:
raise self.parent.parser_exception(
"[[%s]] does not have a matching [[end]]" % self.s)
def execute(self, stream, data):
for node in self.node_list:
node.execute(stream, data)
def __repr__(self):
r = "<" + self.__class__.__name__ + " "
for i in self.node_list:
r = r + repr(i)
r = r + ">"
return r
class TopLevelTemplateNode(TemplateNode):
def __init__(self, parent):
TemplateNode.__init__(self, parent, '')
def add_node(self, node):
if node != None:
self.node_list.append(node)
else:
return 1
class ForTemplateNode(TemplateNode):
def __init__(self, parent, s):
TemplateNode.__init__(self, parent, s)
match = re_for_loop.match(s)
if match == None:
raise self.parent.parser_exception(
"[[%s]] is not a valid for-loop expression" % self.s)
else:
self.vars_temp = match.group(1).split(",")
self.vars = []
for v in self.vars_temp:
self.vars.append(v.strip())
#print self.vars
self.expression = match.group(2)
def execute(self, stream, data):
remember_vars = {}
for var in self.vars:
if data.has_key(var):
remember_vars[var] = data[var]
for list in eval(self.expression, globals(), data):
if is_sequence(list):
for index, value in enumerate(list):
data[self.vars[index]] = value
else:
data[self.vars[0]] = list
TemplateNode.execute(self, stream, data)
for key, value in remember_vars.items():
data[key] = value
class IfTemplateNode(TemplateNode):
def __init__(self, parent, s):
self.else_node = None
TemplateNode.__init__(self, parent, s)
match = re_if.match(s)
if match == None:
raise self.parent.parser_exception(
"[[%s]] is not a valid if expression" % self.s)
else:
self.expression = match.group(1)
def add_node(self, node):
if node == 'end':
return 1
elif isinstance(node, ElseTemplateNode):
self.else_node = node
return 1
elif isinstance(node, ElifTemplateNode):
self.else_node = node
return 1
elif node != None:
self.node_list.append(node)
else:
raise self.parent.parser_exception(
"[[%s]] does not have a matching [[end]]" % self.s)
def execute(self, stream, data):
if eval(self.expression, globals(), data):
TemplateNode.execute(self, stream, data)
elif self.else_node != None:
self.else_node.execute(stream, data)
class ElifTemplateNode(IfTemplateNode):
def __init__(self, parent, s):
self.else_node = None
TemplateNode.__init__(self, parent, s)
match = re_elif.match(s)
if match == None:
self.parent.parser_exception(
"[[%s]] is not a valid elif expression" % self.s)
else:
self.expression = match.group(1)
class ElseTemplateNode(TemplateNode):
pass
class FunctionTemplateNode(TemplateNode):
def __init__(self, parent, s):
TemplateNode.__init__(self, parent, s)
match = re_def.match(s)
if match == None:
self.parent.parser_exception(
"[[%s]] is not a valid function definition" % self.s)
self.function_name = match.group(1)
self.vars_temp = match.group(2).split(",")
self.vars = []
for v in self.vars_temp:
self.vars.append(v.strip())
#print self.vars
self.parent.functions[self.function_name] = self
def execute(self, stream, data):
pass
def call(self, args, stream, data):
remember_vars = {}
for index, var in enumerate(self.vars):
if data.has_key(var):
remember_vars[var] = data[var]
data[var] = args[index]
TemplateNode.execute(self, stream, data)
for key, value in remember_vars.items():
data[key] = value
class LeafTemplateNode(TemplateNode):
def __init__(self, parent, s):
self.parent = parent
self.s = s
def execute(self, stream, data):
stream.write(self.s)
def __repr__(self):
return "<" + self.__class__.__name__ + ">"
class CommentTemplateNode(LeafTemplateNode):
def execute(self, stream, data):
pass
class ExpressionTemplateNode(LeafTemplateNode):
def execute(self, stream, data):
stream.write(str(eval(self.s, globals(), data)))
class ExecTemplateNode(LeafTemplateNode):
def __init__(self, parent, s):
LeafTemplateNode.__init__(self, parent, s)
match = re_exec.match(s)
if match == None:
self.parent.parser_exception(
"[[%s]] is not a valid statement" % self.s)
self.s = match.group(1)
def execute(self, stream, data):
exec(self.s, globals(), data)
pass
class CallTemplateNode(LeafTemplateNode):
def __init__(self, parent, s):
LeafTemplateNode.__init__(self, parent, s)
match = re_call.match(s)
if match == None:
self.parent.parser_exception(
"[[%s]] is not a valid function call" % self.s)
self.function_name = match.group(1)
self.vars = "(" + match.group(2).strip() + ",)"
def execute(self, stream, data):
self.parent.functions[self.function_name].call(
eval(self.vars, globals(), data), stream, data)
############################################################
# Node factory
template_factory_type_map = {
'if' : IfTemplateNode,
'for' : ForTemplateNode,
'elif' : ElifTemplateNode,
'else' : ElseTemplateNode,
'def' : FunctionTemplateNode,
'call' : CallTemplateNode,
'exec' : ExecTemplateNode }
template_factory_types = template_factory_type_map.keys()
def TemplateNodeFactory(parent):
src = parent.parser_get()
if src == None:
return None
match = re_directive.search(src)
if match == None:
parent.parser_eat(len(src))
return LeafTemplateNode(parent, src)
elif src == '' or match.start() != 0:
parent.parser_eat(match.start())
return LeafTemplateNode(parent, src[:match.start()])
else:
directive = match.group()[2:-2].strip()
parent.parser_eat(match.end())
if directive == 'end':
return 'end'
elif re_comment.match(directive):
return CommentTemplateNode(parent, directive)
else:
for i in template_factory_types:
if directive[0:len(i)] == i:
return template_factory_type_map[i](parent, directive)
return ExpressionTemplateNode(parent, directive)
def is_sequence(object):
try:
test = object[0:0]
except:
return False
else:
return True
|