diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2011-09-27 14:11:47 +0200 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2011-09-27 14:11:47 +0200 |
commit | 669fce23ef24fdf1877db8a2677a14ab67e75807 (patch) | |
tree | b21a57e4541fffaad99ab7691a1173a8fd925b81 /as_string.py | |
parent | e14e47255b491bb90fe3315eb2637b6dae7ead23 (diff) | |
download | astroid-669fce23ef24fdf1877db8a2677a14ab67e75807.tar.gz |
cosmetic changes: update/fix copyright and comments, d-t-w
Diffstat (limited to 'as_string.py')
-rw-r--r-- | as_string.py | 86 |
1 files changed, 37 insertions, 49 deletions
diff --git a/as_string.py b/as_string.py index 1e57f0e..0a42668 100644 --- a/as_string.py +++ b/as_string.py @@ -1,16 +1,4 @@ -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU Lesser 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 Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser 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. -# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. +# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # copyright 2003-2010 Sylvain Thenault, all rights reserved. # contact mailto:thenault@gmail.com @@ -56,13 +44,13 @@ class AsStringVisitor(object): def __call__(self, node): """Makes this visitor behave as a simple function""" return node.accept(self) - + def _stmt_list(self, stmts): """return a list of nodes to string""" stmts = '\n'.join([nstr for nstr in [n.accept(self) for n in stmts] if nstr]) return INDENT + stmts.replace('\n', '\n'+INDENT) - + ## visit_<node> methods ########################################### def visit_arguments(self, node): @@ -88,15 +76,15 @@ class AsStringVisitor(object): """return an astng.Assign node as string""" lhs = ' = '.join([n.accept(self) for n in node.targets]) return '%s = %s' % (lhs, node.value.accept(self)) - + def visit_augassign(self, node): """return an astng.AugAssign node as string""" return '%s %s %s' % (node.target.accept(self), node.op, node.value.accept(self)) - + def visit_backquote(self, node): """return an astng.Backquote node as string""" return '`%s`' % node.value.accept(self) - + def visit_binop(self, node): """return an astng.BinOp node as string""" return '(%s) %s (%s)' % (node.left.accept(self), node.op, node.right.accept(self)) @@ -105,11 +93,11 @@ class AsStringVisitor(object): """return an astng.BoolOp node as string""" return (' %s ' % node.op).join(['(%s)' % n.accept(self) for n in node.values]) - + def visit_break(self, node): """return an astng.Break node as string""" return 'break' - + def visit_callfunc(self, node): """return an astng.CallFunc node as string""" expr_str = node.func.accept(self) @@ -119,7 +107,7 @@ class AsStringVisitor(object): if node.kwargs: args.append( '**' + node.kwargs.accept(self)) return '%s(%s)' % (expr_str, ', '.join(args)) - + def visit_class(self, node): """return an astng.Class node as string""" decorate = node.decorators and node.decorators.accept(self) or '' @@ -128,7 +116,7 @@ class AsStringVisitor(object): docs = node.doc and '\n%s"""%s"""' % (INDENT, node.doc) or '' return '\n\n%sclass %s%s:%s\n%s\n' % (decorate, node.name, bases, docs, self._stmt_list( node.body)) - + def visit_compare(self, node): """return an astng.Compare node as string""" rhs_str = ' '.join(['%s %s' % (op, expr.accept(self)) @@ -144,14 +132,14 @@ class AsStringVisitor(object): def visit_const(self, node): """return an astng.Const node as string""" return repr(node.value) - + def visit_continue(self, node): """return an astng.Continue node as string""" return 'continue' - + def visit_delete(self, node): # XXX check if correct """return an astng.Delete node as string""" - return 'del %s' % ', '.join([child.accept(self) + return 'del %s' % ', '.join([child.accept(self) for child in node.targets]) def visit_delattr(self, node): @@ -168,7 +156,7 @@ class AsStringVisitor(object): def visit_dict(self, node): """return an astng.Dict node as string""" - return '{%s}' % ', '.join(['%s: %s' % (key.accept(self), + return '{%s}' % ', '.join(['%s: %s' % (key.accept(self), value.accept(self)) for key, value in node.items]) def visit_dictcomp(self, node): @@ -194,15 +182,15 @@ class AsStringVisitor(object): else: excs = 'except' return '%s:\n%s' % (excs, self._stmt_list(node.body)) - + def visit_ellipsis(self, node): """return an astng.Ellipsis node as string""" return '...' - + def visit_empty(self, node): """return an Empty node as string""" return '' - + def visit_exec(self, node): """return an astng.Exec node as string""" if node.locals: @@ -226,24 +214,24 @@ class AsStringVisitor(object): if node.orelse: fors = '%s\nelse:\n%s' % (fors, self._stmt_list(node.orelse)) return fors - + def visit_from(self, node): """return an astng.From node as string""" return 'from %s import %s' % ('.' * (node.level or 0) + node.modname, _import_string(node.names)) - + def visit_function(self, node): """return an astng.Function node as string""" decorate = node.decorators and node.decorators.accept(self) or '' docs = node.doc and '\n%s"""%s"""' % (INDENT, node.doc) or '' return '\n%sdef %s(%s):%s\n%s' % (decorate, node.name, node.args.accept(self), docs, self._stmt_list(node.body)) - + def visit_genexpr(self, node): """return an astng.GenExpr node as string""" return '(%s %s)' % (node.elt.accept(self), ' '.join([n.accept(self) for n in node.generators])) - + def visit_getattr(self, node): """return an astng.Getattr node as string""" return '%s.%s' % (node.expr.accept(self), node.attrname) @@ -251,14 +239,14 @@ class AsStringVisitor(object): def visit_global(self, node): """return an astng.Global node as string""" return 'global %s' % ', '.join(node.names) - + def visit_if(self, node): """return an astng.If node as string""" ifs = ['if %s:\n%s' % (node.test.accept(self), self._stmt_list(node.body))] if node.orelse:# XXX use elif ??? ifs.append('else:\n%s' % self._stmt_list(node.orelse)) return '\n'.join(ifs) - + def visit_ifexp(self, node): """return an astng.IfExp node as string""" return '%s if %s else %s' % (node.body.accept(self), @@ -267,19 +255,19 @@ class AsStringVisitor(object): def visit_import(self, node): """return an astng.Import node as string""" return 'import %s' % _import_string(node.names) - + def visit_keyword(self, node): """return an astng.Keyword node as string""" return '%s=%s' % (node.arg, node.value.accept(self)) - + def visit_lambda(self, node): """return an astng.Lambda node as string""" return 'lambda %s: %s' % (node.args.accept(self), node.body.accept(self)) - + def visit_list(self, node): """return an astng.List node as string""" return '[%s]' % ', '.join([child.accept(self) for child in node.elts]) - + def visit_listcomp(self, node): """return an astng.ListComp node as string""" return '[%s %s]' % (node.elt.accept(self), ' '.join([n.accept(self) @@ -289,15 +277,15 @@ class AsStringVisitor(object): """return an astng.Module node as string""" docs = node.doc and '"""%s"""\n\n' % node.doc or '' return docs + '\n'.join([n.accept(self) for n in node.body]) + '\n\n' - + def visit_name(self, node): """return an astng.Name node as string""" return node.name - + def visit_pass(self, node): """return an astng.Pass node as string""" return 'pass' - + def visit_print(self, node): """return an astng.Print node as string""" nodes = ', '.join([n.accept(self) for n in node.values]) @@ -352,7 +340,7 @@ class AsStringVisitor(object): def visit_subscript(self, node): """return an astng.Subscript node as string""" return '%s[%s]' % (node.value.accept(self), node.slice.accept(self)) - + def visit_tryexcept(self, node): """return an astng.TryExcept node as string""" trys = ['try:\n%s' % self._stmt_list( node.body)] @@ -361,16 +349,16 @@ class AsStringVisitor(object): if node.orelse: trys.append('else:\n%s' % self._stmt_list(node.orelse)) return '\n'.join(trys) - + def visit_tryfinally(self, node): """return an astng.TryFinally node as string""" return 'try:\n%s\nfinally:\n%s' % (self._stmt_list( node.body), self._stmt_list(node.finalbody)) - + def visit_tuple(self, node): """return an astng.Tuple node as string""" return '(%s)' % ', '.join([child.accept(self) for child in node.elts]) - + def visit_unaryop(self, node): """return an astng.UnaryOp node as string""" if node.op == 'not': @@ -378,7 +366,7 @@ class AsStringVisitor(object): else: operator = node.op return '%s%s' % (operator, node.operand.accept(self)) - + def visit_while(self, node): """return an astng.While node as string""" whiles = 'while %s:\n%s' % (node.test.accept(self), @@ -386,14 +374,14 @@ class AsStringVisitor(object): if node.orelse: whiles = '%s\nelse:\n%s' % (whiles, self._stmt_list(node.orelse)) return whiles - + def visit_with(self, node): # 'with' without 'as' is possible """return an astng.With node as string""" as_var = node.vars and " as (%s)" % (node.vars.accept(self)) or "" withs = 'with (%s)%s:\n%s' % (node.expr.accept(self), as_var, self._stmt_list( node.body)) return withs - + def visit_yield(self, node): """yield an ast.Yield node as string""" yi_val = node.value and (" " + node.value.accept(self)) or "" |