Package logilab-common-0 :: Package 39 :: Package 0 :: Module astutils
[frames] | no frames]

Source Code for Module logilab-common-0.39.0.astutils

 1  """functions to manipulate ast tuples. 
 2   
 3  :copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
 4  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr 
 5  :license: General Public License version 2 - http://www.gnu.org/licenses 
 6  """ 
 7  __docformat__ = "restructuredtext en" 
 8  __author__ = u"Sylvain Thenault" 
 9   
10  from warnings import warn 
11  warn('this module has been moved into logilab.astng and will disappear from \ 
12  logilab.common in a future release', 
13       DeprecationWarning, stacklevel=1) 
14   
15  import symbol 
16  import token 
17  from types import TupleType 
18   
19 -def debuild(ast_tuple):
20 """ 21 reverse ast_tuple to string 22 """ 23 if type(ast_tuple[1]) is TupleType: 24 result = '' 25 for child in ast_tuple[1:]: 26 result = '%s%s' % (result, debuild(child)) 27 return result 28 else: 29 return ast_tuple[1]
30
31 -def clean(ast_tuple):
32 """ 33 reverse ast tuple to a list of tokens 34 merge sequences (token.NAME, token.DOT, token.NAME) 35 """ 36 result = [] 37 last = None 38 for couple in _clean(ast_tuple): 39 if couple[0] == token.NAME and last == token.DOT: 40 result[-1][1] += couple[1] 41 elif couple[0] == token.DOT and last == token.NAME: 42 result[-1][1] += couple[1] 43 else: 44 result.append(couple) 45 last = couple[0] 46 return result
47
48 -def _clean(ast_tuple):
49 """ transform the ast into as list of tokens (i.e. final elements) 50 """ 51 if type(ast_tuple[1]) is TupleType: 52 v = [] 53 for c in ast_tuple[1:]: 54 v += _clean(c) 55 return v 56 else: 57 return [list(ast_tuple[:2])]
58
59 -def cvrtr(tuple):
60 """debug method returning an ast string in a readable fashion""" 61 if type(tuple) is TupleType: 62 try: 63 try: 64 txt = 'token.'+token.tok_name[tuple[0]] 65 except: 66 txt = 'symbol.'+symbol.sym_name[tuple[0]] 67 except: 68 txt = 'Unknown token/symbol' 69 return [txt] + map(cvrtr, tuple[1:]) 70 else: 71 return tuple
72 73 __all__ = ('debuild', 'clean', 'cvrtr') 74