pylintext.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 28 Oct 2011 11:33:26 +0200
changeset 8037 a36bd56f33bb
parent 7941 018b5deca73e
child 8061 88ca47ceb9f2
permissions -rw-r--r--
[diet] move iprogress to its own cube. Closes #1916016
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
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
def turn_function_to_class(node):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
    """turn a Function node into a Class node (in-place)"""
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
    node.__class__ = scoped_nodes.Class
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
    node.bases = ()
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
    # 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
    10
    # function' by pylint
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
    for rnode in node.nodes_of_class(nodes.Return):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
        rnode.parent.body.remove(rnode)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
    # that seems to be enough :)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
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
def cubicweb_transform(module):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
    # handle objectify_selector decorator. Only look at module level functions,
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
    # should be enough
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
    for assnodes in module.locals.values():
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
        for node in assnodes:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
            if isinstance(node, scoped_nodes.Function) and node.decorators:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
                for decorator in node.decorators.nodes:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
                    for infered in decorator.infer():
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
                        if infered.name == 'objectify_selector':
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
                            turn_function_to_class(node)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
                            break
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
                    else:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
                        continue
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
                    break
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
    # 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
    31
    # magic in there
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
    if module.name == 'yams.buildobjs':
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
        from yams import BASE_TYPES
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
        for etype in BASE_TYPES:
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
            module.locals[etype] = [scoped_nodes.Class(etype, None)]
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
MANAGER.register_transformer(cubicweb_transform)
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
def register(linter):
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
    """called when loaded by pylint --load-plugins, nothing to do here"""
018b5deca73e pylint extension
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41