root / logilab.pylintinstaller / logilab / common / astutils.py

Revision 202:d67e86292521, 2.6 kB (checked in by tziade@…, 9 months ago)

added logilab.pylintinstaller

Line 
1# Copyright (c) 2003 Sylvain Thenault (thenault@nerim.net)
2# Copyright (c) 2003 Logilab
3#
4# This program is free software; you can redistribute it and/or modify it under
5# the terms of the GNU General Public License as published by the Free Software
6# Foundation; either version 2 of the License, or (at your option) any later
7# version.
8#
9# This program is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along with
14# this program; if not, write to the Free Software Foundation, Inc.,
15# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16"""Some usefull functions to manipulate ast tuples
17"""
18
19from warnings import warn
20warn('this module has been moved into logilab.astng and will disappear from \
21logilab.common in a near release',
22     DeprecationWarning, stacklevel=1)
23
24__author__ = u"Sylvain Thenault"
25
26import symbol
27import token
28from types import TupleType
29
30def debuild(ast_tuple):
31    """
32    reverse ast_tuple to string
33    """
34    if type(ast_tuple[1]) is TupleType:
35        result = ''
36        for child in ast_tuple[1:]: 
37            result = '%s%s' % (result, debuild(child))
38        return result
39    else:
40        return ast_tuple[1]
41
42def clean(ast_tuple):
43    """
44    reverse ast tuple to a list of tokens
45    merge sequences (token.NAME, token.DOT, token.NAME)
46    """
47    result = []
48    last = None
49    for couple in _clean(ast_tuple):
50        if couple[0] == token.NAME and last == token.DOT:
51            result[-1][1] += couple[1]
52        elif couple[0] == token.DOT and last == token.NAME:
53            result[-1][1] += couple[1]
54        else:
55            result.append(couple)
56        last = couple[0]
57    return result
58
59def _clean(ast_tuple):
60    """ transform the ast into as list of tokens (i.e. final elements)
61    """
62    if type(ast_tuple[1]) is TupleType:
63        v = []
64        for c in ast_tuple[1:]:
65            v += _clean(c)
66        return v
67    else:
68        return [list(ast_tuple[:2])]
69   
70def cvrtr(tuple):
71    """debug method returning an ast string in a readable fashion"""
72    if type(tuple) is TupleType:
73        try:
74            try:
75                txt = 'token.'+token.tok_name[tuple[0]]
76            except:
77                txt = 'symbol.'+symbol.sym_name[tuple[0]]
78        except:
79            txt =  'Unknown token/symbol'
80        return [txt] + map(cvrtr, tuple[1:])
81    else:
82        return tuple
83
84__all__ = ('debuild', 'clean', 'cvrtr')
Note: See TracBrowser for help on using the browser.