pylintext.py
author Julien Cristau <julien.cristau@logilab.fr>
Wed, 29 May 2013 11:41:39 +0200
branchstable
changeset 8993 82ffa4b68c9e
parent 8696 0bb18407c053
child 9311 8833ead6f3e4
permissions -rw-r--r--
[server] fix documentation of ZMQ options harder The syntax for zmq addresses is tcp://host:port, no zmqpickle involved.
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):
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8061
diff changeset
    18
    # handle objectify_predicate decorator (and its former name until bw compat
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8061
diff changeset
    19
    # is kept). Only look at module level functions, should be enough.
8696
0bb18407c053 [toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8216
diff changeset
    20
    for assnodes in module.locals.itervalues():
7941
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():
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8061
diff changeset
    25
                        if infered.name in ('objectify_predicate', 'objectify_selector'):
7941
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
8198
ce9c6ae03c2d [pylint] correctly detect uiprops module when it's not in a package
David Douard <david.douard@logilab.fr>
parents: 8061
diff changeset
    38
    if module.name.split('.')[-1] == 'uiprops':
8061
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