comparison genshi/template/ast24.py @ 902:09cc3627654c experimental-inline

Sync `experimental/inline` branch with [source:trunk@1126].
author cmlenz
date Fri, 23 Apr 2010 21:08:26 +0000
parents 1837f39efd6f
children
comparison
equal deleted inserted replaced
830:de82830f8816 902:09cc3627654c
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 # 2 #
3 # Copyright (C) 2008 Edgewall Software 3 # Copyright (C) 2008-2009 Edgewall Software
4 # All rights reserved. 4 # All rights reserved.
5 # 5 #
6 # This software is licensed as described in the file COPYING, which 6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms 7 # you should have received as part of this distribution. The terms
8 # are also available at http://genshi.edgewall.org/wiki/License. 8 # are also available at http://genshi.edgewall.org/wiki/License.
25 def _new(cls, *args, **kwargs): 25 def _new(cls, *args, **kwargs):
26 ret = cls() 26 ret = cls()
27 if ret._fields: 27 if ret._fields:
28 for attr, value in zip(ret._fields, args): 28 for attr, value in zip(ret._fields, args):
29 if attr in kwargs: 29 if attr in kwargs:
30 raise ValueError, 'Field set both in args and kwargs' 30 raise ValueError('Field set both in args and kwargs')
31 setattr(ret, attr, value) 31 setattr(ret, attr, value)
32 for attr in kwargs: 32 for attr in kwargs:
33 if (getattr(ret, '_fields', None) and attr in ret._fields) \ 33 if (getattr(ret, '_fields', None) and attr in ret._fields) \
34 or (getattr(ret, '_attributes', None) and 34 or (getattr(ret, '_attributes', None) and
35 attr in ret._attributes): 35 attr in ret._attributes):
99 elif isinstance(t, tuple): 99 elif isinstance(t, tuple):
100 elts = [_tup(x) for x in t] 100 elts = [_tup(x) for x in t]
101 return self._new(_ast.Tuple, elts, _ast.Store()) 101 return self._new(_ast.Tuple, elts, _ast.Store())
102 else: 102 else:
103 raise NotImplemented 103 raise NotImplemented
104 104
105 args = [] 105 args = []
106 for arg in tab: 106 for arg in tab:
107 if isinstance(arg, str): 107 if isinstance(arg, str):
108 args.append(self._new(_ast.Name, arg, _ast.Param())) 108 args.append(self._new(_ast.Name, arg, _ast.Param()))
109 elif isinstance(arg, tuple): 109 elif isinstance(arg, tuple):
110 args.append(_tup(arg)) 110 args.append(_tup(arg))
111 else: 111 else:
112 assert False, node.__class__ 112 assert False, node.__class__
113 113
114 defaults = map(self.visit, node.defaults) 114 defaults = [self.visit(d) for d in node.defaults]
115 return self._new(_ast.arguments, args, vararg, kwarg, defaults) 115 return self._new(_ast.arguments, args, vararg, kwarg, defaults)
116 116
117 117
118 def visit_Function(self, node): 118 def visit_Function(self, node):
119 if getattr(node, 'decorators', ()): 119 if getattr(node, 'decorators', ()):
376 return self._new(_ast.Repr, self.visit(node.expr)) 376 return self._new(_ast.Repr, self.visit(node.expr))
377 377
378 def visit_Const(self, node): 378 def visit_Const(self, node):
379 if node.value is None: # appears in slices 379 if node.value is None: # appears in slices
380 return None 380 return None
381 elif isinstance(node.value, (str, unicode,)): 381 elif isinstance(node.value, basestring):
382 return self._new(_ast.Str, node.value) 382 return self._new(_ast.Str, node.value)
383 else: 383 else:
384 return self._new(_ast.Num, node.value) 384 return self._new(_ast.Num, node.value)
385 385
386 def visit_Name(self, node): 386 def visit_Name(self, node):
469 469
470 self.out_flags = node.flags 470 self.out_flags = node.flags
471 return self._new(_ast.Subscript, self.visit(node.expr), slice, ctx) 471 return self._new(_ast.Subscript, self.visit(node.expr), slice, ctx)
472 472
473 def visit_Sliceobj(self, node): 473 def visit_Sliceobj(self, node):
474 a = node.nodes + [None]*(3 - len(node.nodes)) 474 a = [self.visit(n) for n in node.nodes + [None]*(3 - len(node.nodes))]
475 a = map(self.visit, a)
476 return self._new(_ast.Slice, a[0], a[1], a[2]) 475 return self._new(_ast.Slice, a[0], a[1], a[2])
477 476
478 def visit_Ellipsis(self, node): 477 def visit_Ellipsis(self, node):
479 return self._new(_ast.Ellipsis) 478 return self._new(_ast.Ellipsis)
480 479
495 def _keep(n): 494 def _keep(n):
496 if isinstance(n, _ast.Expr) and n.value is None: 495 if isinstance(n, _ast.Expr) and n.value is None:
497 return False 496 return False
498 else: 497 else:
499 return True 498 return True
500 statements = [_check_del(self.visit(n)) for n in node.nodes] 499 return [s for s in [_check_del(self.visit(n)) for n in node.nodes]
501 return filter(_keep, statements) 500 if _keep(s)]
502 501
503 502
504 def parse(source, mode): 503 def parse(source, mode):
505 node = compiler.parse(source, mode) 504 node = compiler.parse(source, mode)
506 return ASTUpgrader().visit(node) 505 return ASTUpgrader().visit(node)
Copyright (C) 2012-2017 Edgewall Software