pylintext.py
branchstable
changeset 9311 8833ead6f3e4
parent 8696 0bb18407c053
child 10663 54b8a1f249fb
equal deleted inserted replaced
9310:80c58c35145f 9311:8833ead6f3e4
     1 """https://pastebin.logilab.fr/show/860/"""
     1 """https://pastebin.logilab.fr/show/860/"""
     2 
     2 
     3 from logilab.astng import MANAGER, nodes, scoped_nodes
     3 from astroid import MANAGER, InferenceError, nodes, scoped_nodes
     4 from logilab.astng.builder import ASTNGBuilder
     4 from astroid.builder import AstroidBuilder
     5 
     5 
     6 def turn_function_to_class(node):
     6 def turn_function_to_class(node):
     7     """turn a Function node into a Class node (in-place)"""
     7     """turn a Function node into a Class node (in-place)"""
     8     node.__class__ = scoped_nodes.Class
     8     node.__class__ = scoped_nodes.Class
     9     node.bases = ()
     9     node.bases = ()
    19     # is kept). Only look at module level functions, should be enough.
    19     # is kept). Only look at module level functions, should be enough.
    20     for assnodes in module.locals.itervalues():
    20     for assnodes in module.locals.itervalues():
    21         for node in assnodes:
    21         for node in assnodes:
    22             if isinstance(node, scoped_nodes.Function) and node.decorators:
    22             if isinstance(node, scoped_nodes.Function) and node.decorators:
    23                 for decorator in node.decorators.nodes:
    23                 for decorator in node.decorators.nodes:
    24                     for infered in decorator.infer():
    24                     try:
    25                         if infered.name in ('objectify_predicate', 'objectify_selector'):
    25                         for infered in decorator.infer():
    26                             turn_function_to_class(node)
    26                             if infered.name in ('objectify_predicate', 'objectify_selector'):
    27                             break
    27                                 turn_function_to_class(node)
    28                     else:
    28                                 break
       
    29                         else:
       
    30                             continue
       
    31                         break
       
    32                     except InferenceError:
    29                         continue
    33                         continue
    30                     break
       
    31     # add yams base types into 'yams.buildobjs', astng doesn't grasp globals()
    34     # add yams base types into 'yams.buildobjs', astng doesn't grasp globals()
    32     # magic in there
    35     # magic in there
    33     if module.name == 'yams.buildobjs':
    36     if module.name == 'yams.buildobjs':
    34         from yams import BASE_TYPES
    37         from yams import BASE_TYPES
    35         for etype in BASE_TYPES:
    38         for etype in BASE_TYPES:
    36             module.locals[etype] = [scoped_nodes.Class(etype, None)]
    39             module.locals[etype] = [scoped_nodes.Class(etype, None)]
    37     # add data() to uiprops module
    40     # add data() to uiprops module
    38     if module.name.split('.')[-1] == 'uiprops':
    41     if module.name.split('.')[-1] == 'uiprops':
    39         fake = ASTNGBuilder(MANAGER).string_build('''
    42         fake = AstroidBuilder(MANAGER).string_build('''
    40 def data(string):
    43 def data(string):
    41   return u''
    44   return u''
    42 ''')
    45 ''')
    43         module.locals['data'] = fake.locals['data']
    46         module.locals['data'] = fake.locals['data']
    44 
    47 
    45 def register(linter):
    48 def register(linter):
    46     """called when loaded by pylint --load-plugins, nothing to do here"""
    49     """called when loaded by pylint --load-plugins, nothing to do here"""
    47     MANAGER.register_transformer(cubicweb_transform)
    50     MANAGER.register_transform(nodes.Module, cubicweb_transform)
    48 
    51