diff options
| author | David Douard <david.douard@logilab.fr> | 2013-06-17 18:09:03 +0200 |
|---|---|---|
| committer | David Douard <david.douard@logilab.fr> | 2013-06-17 18:09:03 +0200 |
| commit | 5b40f8a5d5e6a36fd966eb473d0897f3e2f8c547 (patch) | |
| tree | 1597a5d9029ffa1d6ebe328e842f1dd9778290a0 /as_string.py | |
| parent | 712b9ea501fcea3dce78de05e83083bfa31a7510 (diff) | |
| download | astroid-git-5b40f8a5d5e6a36fd966eb473d0897f3e2f8c547.tar.gz | |
rename the project astroid
Diffstat (limited to 'as_string.py')
| -rw-r--r-- | as_string.py | 134 |
1 files changed, 67 insertions, 67 deletions
diff --git a/as_string.py b/as_string.py index c21144e5..360e9bbe 100644 --- a/as_string.py +++ b/as_string.py @@ -1,21 +1,21 @@ # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # -# This file is part of logilab-astng. +# This file is part of astroid. # -# logilab-astng is free software: you can redistribute it and/or modify it +# astroid 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.1 of the License, or (at your # option) any later version. # -# logilab-astng is distributed in the hope that it will be useful, but +# astroid 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 logilab-astng. If not, see <http://www.gnu.org/licenses/>. -"""This module renders ASTNG nodes as string: +# with astroid. If not, see <http://www.gnu.org/licenses/>. +"""This module renders Astroid nodes as string: * :func:`to_code` function return equivalent (hopefuly valid) python string @@ -29,7 +29,7 @@ INDENT = ' ' # 4 spaces ; keep indentation variable def dump(node, ids=False): - """print a nice astng tree representation. + """print a nice astroid tree representation. :param ids: if true, we also print the ids (usefull for debugging) """ @@ -41,7 +41,7 @@ def _repr_tree(node, result, indent='', _done=None, ids=False): """built a tree representation of a node as a list of lines""" if _done is None: _done = set() - if not hasattr(node, '_astng_fields'): # not a astng node + if not hasattr(node, '_astroid_fields'): # not a astroid node return if node in _done: result.append( indent + 'loop in tree: %s' % node ) @@ -52,7 +52,7 @@ def _repr_tree(node, result, indent='', _done=None, ids=False): node_str += ' . \t%x' % id(node) result.append( indent + node_str ) indent += INDENT - for field in node._astng_fields: + for field in node._astroid_fields: value = getattr(node, field) if isinstance(value, (list, tuple) ): result.append( indent + field + " = [" ) @@ -71,7 +71,7 @@ def _repr_tree(node, result, indent='', _done=None, ids=False): class AsStringVisitor(object): - """Visitor to render an ASTNG node as a valid python code string""" + """Visitor to render an Astroid node as a valid python code string""" def __call__(self, node): """Makes this visitor behave as a simple function""" @@ -86,52 +86,52 @@ class AsStringVisitor(object): ## visit_<node> methods ########################################### def visit_arguments(self, node): - """return an astng.Function node as string""" + """return an astroid.Function node as string""" return node.format_args() def visit_assattr(self, node): - """return an astng.AssAttr node as string""" + """return an astroid.AssAttr node as string""" return self.visit_getattr(node) def visit_assert(self, node): - """return an astng.Assert node as string""" + """return an astroid.Assert node as string""" if node.fail: return 'assert %s, %s' % (node.test.accept(self), node.fail.accept(self)) return 'assert %s' % node.test.accept(self) def visit_assname(self, node): - """return an astng.AssName node as string""" + """return an astroid.AssName node as string""" return node.name def visit_assign(self, node): - """return an astng.Assign node as string""" + """return an astroid.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 an astroid.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 an astroid.Backquote node as string""" return '`%s`' % node.value.accept(self) def visit_binop(self, node): - """return an astng.BinOp node as string""" + """return an astroid.BinOp node as string""" return '(%s) %s (%s)' % (node.left.accept(self), node.op, node.right.accept(self)) def visit_boolop(self, node): - """return an astng.BoolOp node as string""" + """return an astroid.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 an astroid.Break node as string""" return 'break' def visit_callfunc(self, node): - """return an astng.CallFunc node as string""" + """return an astroid.CallFunc node as string""" expr_str = node.func.accept(self) args = [arg.accept(self) for arg in node.args] if node.starargs: @@ -141,7 +141,7 @@ class AsStringVisitor(object): return '%s(%s)' % (expr_str, ', '.join(args)) def visit_class(self, node): - """return an astng.Class node as string""" + """return an astroid.Class node as string""" decorate = node.decorators and node.decorators.accept(self) or '' bases = ', '.join([n.accept(self) for n in node.bases]) bases = bases and '(%s)' % bases or '' @@ -150,54 +150,54 @@ class AsStringVisitor(object): self._stmt_list( node.body)) def visit_compare(self, node): - """return an astng.Compare node as string""" + """return an astroid.Compare node as string""" rhs_str = ' '.join(['%s %s' % (op, expr.accept(self)) for op, expr in node.ops]) return '%s %s' % (node.left.accept(self), rhs_str) def visit_comprehension(self, node): - """return an astng.Comprehension node as string""" + """return an astroid.Comprehension node as string""" ifs = ''.join([ ' if %s' % n.accept(self) for n in node.ifs]) return 'for %s in %s%s' % (node.target.accept(self), node.iter.accept(self), ifs ) def visit_const(self, node): - """return an astng.Const node as string""" + """return an astroid.Const node as string""" return repr(node.value) def visit_continue(self, node): - """return an astng.Continue node as string""" + """return an astroid.Continue node as string""" return 'continue' def visit_delete(self, node): # XXX check if correct - """return an astng.Delete node as string""" + """return an astroid.Delete node as string""" return 'del %s' % ', '.join([child.accept(self) for child in node.targets]) def visit_delattr(self, node): - """return an astng.DelAttr node as string""" + """return an astroid.DelAttr node as string""" return self.visit_getattr(node) def visit_delname(self, node): - """return an astng.DelName node as string""" + """return an astroid.DelName node as string""" return node.name def visit_decorators(self, node): - """return an astng.Decorators node as string""" + """return an astroid.Decorators node as string""" return '@%s\n' % '\n@'.join([item.accept(self) for item in node.nodes]) def visit_dict(self, node): - """return an astng.Dict node as string""" + """return an astroid.Dict node as string""" return '{%s}' % ', '.join(['%s: %s' % (key.accept(self), value.accept(self)) for key, value in node.items]) def visit_dictcomp(self, node): - """return an astng.DictComp node as string""" + """return an astroid.DictComp node as string""" return '{%s: %s %s}' % (node.key.accept(self), node.value.accept(self), ' '.join([n.accept(self) for n in node.generators])) def visit_discard(self, node): - """return an astng.Discard node as string""" + """return an astroid.Discard node as string""" return node.value.accept(self) def visit_emptynode(self, node): @@ -216,7 +216,7 @@ class AsStringVisitor(object): return '%s:\n%s' % (excs, self._stmt_list(node.body)) def visit_ellipsis(self, node): - """return an astng.Ellipsis node as string""" + """return an astroid.Ellipsis node as string""" return '...' def visit_empty(self, node): @@ -224,7 +224,7 @@ class AsStringVisitor(object): return '' def visit_exec(self, node): - """return an astng.Exec node as string""" + """return an astroid.Exec node as string""" if node.locals: return 'exec %s in %s, %s' % (node.expr.accept(self), node.locals.accept(self), @@ -235,11 +235,11 @@ class AsStringVisitor(object): return 'exec %s' % node.expr.accept(self) def visit_extslice(self, node): - """return an astng.ExtSlice node as string""" + """return an astroid.ExtSlice node as string""" return ','.join( [dim.accept(self) for dim in node.dims] ) def visit_for(self, node): - """return an astng.For node as string""" + """return an astroid.For node as string""" fors = 'for %s in %s:\n%s' % (node.target.accept(self), node.iter.accept(self), self._stmt_list( node.body)) @@ -248,78 +248,78 @@ class AsStringVisitor(object): return fors def visit_from(self, node): - """return an astng.From node as string""" + """return an astroid.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""" + """return an astroid.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 an astroid.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 an astroid.Getattr node as string""" return '%s.%s' % (node.expr.accept(self), node.attrname) def visit_global(self, node): - """return an astng.Global node as string""" + """return an astroid.Global node as string""" return 'global %s' % ', '.join(node.names) def visit_if(self, node): - """return an astng.If node as string""" + """return an astroid.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 an astroid.IfExp node as string""" return '%s if %s else %s' % (node.body.accept(self), node.test.accept(self), node.orelse.accept(self)) def visit_import(self, node): - """return an astng.Import node as string""" + """return an astroid.Import node as string""" return 'import %s' % _import_string(node.names) def visit_keyword(self, node): - """return an astng.Keyword node as string""" + """return an astroid.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 an astroid.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 an astroid.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 an astroid.ListComp node as string""" return '[%s %s]' % (node.elt.accept(self), ' '.join([n.accept(self) for n in node.generators])) def visit_module(self, node): - """return an astng.Module node as string""" + """return an astroid.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 an astroid.Name node as string""" return node.name def visit_pass(self, node): - """return an astng.Pass node as string""" + """return an astroid.Pass node as string""" return 'pass' def visit_print(self, node): - """return an astng.Print node as string""" + """return an astroid.Print node as string""" nodes = ', '.join([n.accept(self) for n in node.values]) if not node.nl: nodes = '%s,' % nodes @@ -328,7 +328,7 @@ class AsStringVisitor(object): return 'print %s' % nodes def visit_raise(self, node): - """return an astng.Raise node as string""" + """return an astroid.Raise node as string""" if node.exc: if node.inst: if node.tback: @@ -341,27 +341,27 @@ class AsStringVisitor(object): return 'raise' def visit_return(self, node): - """return an astng.Return node as string""" + """return an astroid.Return node as string""" if node.value: return 'return %s' % node.value.accept(self) else: return 'return' def visit_index(self, node): - """return a astng.Index node as string""" + """return a astroid.Index node as string""" return node.value.accept(self) def visit_set(self, node): - """return an astng.Set node as string""" + """return an astroid.Set node as string""" return '{%s}' % ', '.join([child.accept(self) for child in node.elts]) def visit_setcomp(self, node): - """return an astng.SetComp node as string""" + """return an astroid.SetComp node as string""" return '{%s %s}' % (node.elt.accept(self), ' '.join([n.accept(self) for n in node.generators])) def visit_slice(self, node): - """return a astng.Slice node as string""" + """return a astroid.Slice node as string""" lower = node.lower and node.lower.accept(self) or '' upper = node.upper and node.upper.accept(self) or '' step = node.step and node.step.accept(self) or '' @@ -370,11 +370,11 @@ class AsStringVisitor(object): return '%s:%s' % (lower, upper) def visit_subscript(self, node): - """return an astng.Subscript node as string""" + """return an astroid.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""" + """return an astroid.TryExcept node as string""" trys = ['try:\n%s' % self._stmt_list( node.body)] for handler in node.handlers: trys.append(handler.accept(self)) @@ -383,16 +383,16 @@ class AsStringVisitor(object): return '\n'.join(trys) def visit_tryfinally(self, node): - """return an astng.TryFinally node as string""" + """return an astroid.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 an astroid.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""" + """return an astroid.UnaryOp node as string""" if node.op == 'not': operator = 'not ' else: @@ -400,7 +400,7 @@ class AsStringVisitor(object): return '%s%s' % (operator, node.operand.accept(self)) def visit_while(self, node): - """return an astng.While node as string""" + """return an astroid.While node as string""" whiles = 'while %s:\n%s' % (node.test.accept(self), self._stmt_list(node.body)) if node.orelse: @@ -408,7 +408,7 @@ class AsStringVisitor(object): return whiles def visit_with(self, node): # 'with' without 'as' is possible - """return an astng.With node as string""" + """return an astroid.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)) @@ -439,11 +439,11 @@ class AsStringVisitor3k(AsStringVisitor): return '%s:\n%s' % (excs, self._stmt_list(node.body)) def visit_nonlocal(self, node): - """return an astng.Nonlocal node as string""" + """return an astroid.Nonlocal node as string""" return 'nonlocal %s' % ', '.join(node.names) def visit_raise(self, node): - """return an astng.Raise node as string""" + """return an astroid.Raise node as string""" if node.exc: if node.cause: return 'raise %s from %s' % (node.exc.accept(self), |
