pylintext.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 15 Nov 2011 22:22:18 +0100
changeset 8083 3db2f3317d15
parent 8061 88ca47ceb9f2
child 8190 2a3c1b787688
child 8198 ce9c6ae03c2d
permissions -rw-r--r--
[users management view] return None if no entity related, not an empty tuple. Closes #2089085
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
"""https://pastebin.logilab.fr/show/860/"""
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
from logilab.astng import MANAGER, nodes, scoped_nodes
8061
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
     4
from logilab.astng.builder import ASTNGBuilder
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
def turn_function_to_class(node):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
    """turn a Function node into a Class node (in-place)"""
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
    node.__class__ = scoped_nodes.Class
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
    node.bases = ()
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
    # remove return nodes so that we don't get warned about 'return outside
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
    # function' by pylint
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
    for rnode in node.nodes_of_class(nodes.Return):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
        rnode.parent.body.remove(rnode)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
    # that seems to be enough :)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
def cubicweb_transform(module):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
    # handle objectify_selector decorator. Only look at module level functions,
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
    # should be enough
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
    for assnodes in module.locals.values():
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
        for node in assnodes:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
            if isinstance(node, scoped_nodes.Function) and node.decorators:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
                for decorator in node.decorators.nodes:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
                    for infered in decorator.infer():
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
                        if infered.name == 'objectify_selector':
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
                            turn_function_to_class(node)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
                            break
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
                    else:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
                        continue
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
                    break
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
    # add yams base types into 'yams.buildobjs', astng doesn't grasp globals()
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
    # magic in there
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
    if module.name == 'yams.buildobjs':
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
        from yams import BASE_TYPES
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
        for etype in BASE_TYPES:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
            module.locals[etype] = [scoped_nodes.Class(etype, None)]
8061
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    37
    # add data() to uiprops module
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    38
    if module.name.endswith('.uiprops'):
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    39
        fake = ASTNGBuilder(MANAGER).string_build('''
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    40
def data(string):
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    41
  return u''
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    42
''')
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    43
        module.locals['data'] = fake.locals['data']
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
def register(linter):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
    """called when loaded by pylint --load-plugins, nothing to do here"""
8061
88ca47ceb9f2 [pylint] enhance plugin: define 'data' function in uiprops module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7941
diff changeset
    47
    MANAGER.register_transformer(cubicweb_transform)
7941
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48