aboutsummaryrefslogtreecommitdiffstats
path: root/mako/ast.py
diff options
context:
space:
mode:
Diffstat (limited to 'mako/ast.py')
-rw-r--r--mako/ast.py36
1 files changed, 27 insertions, 9 deletions
diff --git a/mako/ast.py b/mako/ast.py
index 8d5b1d7..242b6ee 100644
--- a/mako/ast.py
+++ b/mako/ast.py
@@ -4,7 +4,8 @@
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
-"""utilities for analyzing expressions and blocks of Python code, as well as generating Python from AST nodes"""
+"""utilities for analyzing expressions and blocks of Python
+code, as well as generating Python from AST nodes"""
from mako import exceptions, pyparser, util
import re
@@ -22,9 +23,12 @@ class PythonCode(object):
# note that an identifier can be in both the undeclared and declared lists.
- # using AST to parse instead of using code.co_varnames, code.co_names has several advantages:
- # - we can locate an identifier as "undeclared" even if its declared later in the same block of code
- # - AST is less likely to break with version changes (for example, the behavior of co_names changed a little bit
+ # using AST to parse instead of using code.co_varnames,
+ # code.co_names has several advantages:
+ # - we can locate an identifier as "undeclared" even if
+ # its declared later in the same block of code
+ # - AST is less likely to break with version changes
+ # (for example, the behavior of co_names changed a little bit
# in python version 2.5)
if isinstance(code, basestring):
expr = pyparser.parse(code.lstrip(), "exec", **exception_kwargs)
@@ -65,7 +69,9 @@ class PythonFragment(PythonCode):
def __init__(self, code, **exception_kwargs):
m = re.match(r'^(\w+)(?:\s+(.*?))?:\s*(#|$)', code.strip(), re.S)
if not m:
- raise exceptions.CompileException("Fragment '%s' is not a partial control statement" % code, **exception_kwargs)
+ raise exceptions.CompileException(
+ "Fragment '%s' is not a partial control statement" %
+ code, **exception_kwargs)
if m.group(3):
code = code[:m.start(3)]
(keyword, expr) = m.group(1,2)
@@ -78,7 +84,9 @@ class PythonFragment(PythonCode):
elif keyword == 'except':
code = "try:pass\n" + code + "pass"
else:
- raise exceptions.CompileException("Unsupported control keyword: '%s'" % keyword, **exception_kwargs)
+ raise exceptions.CompileException(
+ "Unsupported control keyword: '%s'" %
+ keyword, **exception_kwargs)
super(PythonFragment, self).__init__(code, **exception_kwargs)
@@ -91,12 +99,17 @@ class FunctionDecl(object):
f = pyparser.ParseFunc(self, **exception_kwargs)
f.visit(expr)
if not hasattr(self, 'funcname'):
- raise exceptions.CompileException("Code '%s' is not a function declaration" % code, **exception_kwargs)
+ raise exceptions.CompileException(
+ "Code '%s' is not a function declaration" % code,
+ **exception_kwargs)
if not allow_kwargs and self.kwargs:
- raise exceptions.CompileException("'**%s' keyword argument not allowed here" % self.argnames[-1], **exception_kwargs)
+ raise exceptions.CompileException(
+ "'**%s' keyword argument not allowed here" %
+ self.argnames[-1], **exception_kwargs)
def get_argument_expressions(self, include_defaults=True):
"""return the argument declarations of this FunctionDecl as a printable list."""
+
namedecls = []
defaults = [d for d in self.defaults]
kwargs = self.kwargs
@@ -114,12 +127,17 @@ class FunctionDecl(object):
else:
default = len(defaults) and defaults.pop() or None
if include_defaults and default:
- namedecls.insert(0, "%s=%s" % (arg, pyparser.ExpressionGenerator(default).value()))
+ namedecls.insert(0, "%s=%s" %
+ (arg,
+ pyparser.ExpressionGenerator(default).value()
+ )
+ )
else:
namedecls.insert(0, arg)
return namedecls
class FunctionArgs(FunctionDecl):
"""the argument portion of a function declaration"""
+
def __init__(self, code, **kwargs):
super(FunctionArgs, self).__init__("def ANON(%s):pass" % code, **kwargs)