summaryrefslogtreecommitdiff
path: root/mako/pyparser.py
diff options
context:
space:
mode:
Diffstat (limited to 'mako/pyparser.py')
-rw-r--r--mako/pyparser.py77
1 files changed, 43 insertions, 34 deletions
diff --git a/mako/pyparser.py b/mako/pyparser.py
index 15d0da6..c171897 100644
--- a/mako/pyparser.py
+++ b/mako/pyparser.py
@@ -10,46 +10,52 @@ Parsing to AST is done via _ast on Python > 2.5, otherwise the compiler
module is used.
"""
-from mako import exceptions, util, compat
-from mako.compat import arg_stringname
import operator
+import _ast
+
+from mako import _ast_util
+from mako import compat
+from mako import exceptions
+from mako import util
+from mako.compat import arg_stringname
+
if compat.py3k:
# words that cannot be assigned to (notably
# smaller than the total keys in __builtins__)
- reserved = set(['True', 'False', 'None', 'print'])
+ reserved = set(["True", "False", "None", "print"])
# the "id" attribute on a function node
- arg_id = operator.attrgetter('arg')
+ arg_id = operator.attrgetter("arg")
else:
# words that cannot be assigned to (notably
# smaller than the total keys in __builtins__)
- reserved = set(['True', 'False', 'None'])
+ reserved = set(["True", "False", "None"])
# the "id" attribute on a function node
- arg_id = operator.attrgetter('id')
+ arg_id = operator.attrgetter("id")
-import _ast
util.restore__ast(_ast)
-from mako import _ast_util
-def parse(code, mode='exec', **exception_kwargs):
+def parse(code, mode="exec", **exception_kwargs):
"""Parse an expression into AST"""
try:
- return _ast_util.parse(code, '<unknown>', mode)
+ return _ast_util.parse(code, "<unknown>", mode)
except Exception:
raise exceptions.SyntaxException(
- "(%s) %s (%r)" % (
+ "(%s) %s (%r)"
+ % (
compat.exception_as().__class__.__name__,
compat.exception_as(),
- code[0:50]
- ), **exception_kwargs)
+ code[0:50],
+ ),
+ **exception_kwargs
+ )
class FindIdentifiers(_ast_util.NodeVisitor):
-
def __init__(self, listener, **exception_kwargs):
self.in_function = False
self.in_assign_targets = False
@@ -119,9 +125,9 @@ class FindIdentifiers(_ast_util.NodeVisitor):
self.in_function = True
local_ident_stack = self.local_ident_stack
- self.local_ident_stack = local_ident_stack.union([
- arg_id(arg) for arg in self._expand_tuples(node.args.args)
- ])
+ self.local_ident_stack = local_ident_stack.union(
+ [arg_id(arg) for arg in self._expand_tuples(node.args.args)]
+ )
if islambda:
self.visit(node.body)
else:
@@ -146,9 +152,11 @@ class FindIdentifiers(_ast_util.NodeVisitor):
# this is eqiuvalent to visit_AssName in
# compiler
self._add_declared(node.id)
- elif node.id not in reserved and node.id \
- not in self.listener.declared_identifiers and node.id \
- not in self.local_ident_stack:
+ elif (
+ node.id not in reserved
+ and node.id not in self.listener.declared_identifiers
+ and node.id not in self.local_ident_stack
+ ):
self.listener.undeclared_identifiers.add(node.id)
def visit_Import(self, node):
@@ -156,24 +164,25 @@ class FindIdentifiers(_ast_util.NodeVisitor):
if name.asname is not None:
self._add_declared(name.asname)
else:
- self._add_declared(name.name.split('.')[0])
+ self._add_declared(name.name.split(".")[0])
def visit_ImportFrom(self, node):
for name in node.names:
if name.asname is not None:
self._add_declared(name.asname)
else:
- if name.name == '*':
+ if name.name == "*":
raise exceptions.CompileException(
"'import *' is not supported, since all identifier "
"names must be explicitly declared. Please use the "
"form 'from <modulename> import <name1>, <name2>, "
- "...' instead.", **self.exception_kwargs)
+ "...' instead.",
+ **self.exception_kwargs
+ )
self._add_declared(name.name)
class FindTuple(_ast_util.NodeVisitor):
-
def __init__(self, listener, code_factory, **exception_kwargs):
self.listener = listener
self.exception_kwargs = exception_kwargs
@@ -184,16 +193,17 @@ class FindTuple(_ast_util.NodeVisitor):
p = self.code_factory(n, **self.exception_kwargs)
self.listener.codeargs.append(p)
self.listener.args.append(ExpressionGenerator(n).value())
- self.listener.declared_identifiers = \
- self.listener.declared_identifiers.union(
- p.declared_identifiers)
- self.listener.undeclared_identifiers = \
- self.listener.undeclared_identifiers.union(
- p.undeclared_identifiers)
+ ldi = self.listener.declared_identifiers
+ self.listener.declared_identifiers = ldi.union(
+ p.declared_identifiers
+ )
+ lui = self.listener.undeclared_identifiers
+ self.listener.undeclared_identifiers = lui.union(
+ p.undeclared_identifiers
+ )
class ParseFunc(_ast_util.NodeVisitor):
-
def __init__(self, listener, **exception_kwargs):
self.listener = listener
self.exception_kwargs = exception_kwargs
@@ -224,10 +234,9 @@ class ParseFunc(_ast_util.NodeVisitor):
class ExpressionGenerator(object):
-
def __init__(self, astnode):
- self.generator = _ast_util.SourceGenerator(' ' * 4)
+ self.generator = _ast_util.SourceGenerator(" " * 4)
self.generator.visit(astnode)
def value(self):
- return ''.join(self.generator.result)
+ return "".join(self.generator.result)